source: rtems-source-builder/source-builder/sb/git.py @ cb12e48

4.104.114.95
Last change on this file since cb12e48 was cb12e48, checked in by Chris Johns <chrisj@…>, on Apr 9, 2013 at 3:51:43 AM

Refactor defaults, macros and options.

To support building snapshots and pre-release source the defaults
has been refactored. The defaults have been moved to a stand alone
file and a macros.py module added. This modile abstracts the
old default dictionary turning it into a class. The macros
class can load macros from a file therefore the defaults have
been moved to a stand alone file.

The use of defaults has been removed from the project. The only
case where it is used in the options where the defaults are read
from a file. Macros are used everywhere now.

The defaults.py has been moved to the option.py and the separate
options and defaults values has been moved to a new pattern. When
constructing an object that needs macros and options if the macros
passed in is None the defaults from the options are used. This makes
it clear when the defaults are being used or when a modified set of
macros is being used.

The macros class support maps. The default is 'global' and where all
the defaults reside and where configuratiion file changes end up.
Maps allow macros to be read from a file and override the values
being maintained in the 'global' map. Reading a macro first checks
the map and if not present checks the 'global' map.

The addition of maps to the macros provides the base to support
snapshots and pre-release testing with standard configurations.
This functionality needs to be added. It works by letting to
specify a snapshot with:

source0: none, override, 'my-dist.tar.bz2'

and it will be used rather the value from the standard configuration.
With a build set you need to also specify the package these macros
are for. The maps provide this.

  • Property mode set to 100644
File size: 4.5 KB
Line 
1#
2# RTEMS Tools Project (http://www.rtems.org/)
3# Copyright 2010-2013 Chris Johns (chrisj@rtems.org)
4# All rights reserved.
5#
6# This file is part of the RTEMS Tools package in 'rtems-tools'.
7#
8# Permission to use, copy, modify, and/or distribute this software for any
9# purpose with or without fee is hereby granted, provided that the above
10# copyright notice and this permission notice appear in all copies.
11#
12# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20#
21# Provide some basic access to the git command.
22#
23
24import os
25
26import error
27import execute
28import options
29import path
30
31class repo:
32    """An object to manage a git repo."""
33
34    def _git_exit_code(self, ec):
35        if ec:
36            raise error.general('git command failed (%s): %d' % (self.git, ec))
37
38    def _run(self, args, check = False):
39        e = execute.capture_execution()
40        exit_code, proc, output = e.spawn([self.git] + args)
41        if check:
42            self._git_exit_code(exit_code)
43        return exit_code, output
44
45    def __init__(self, _path, opts, macros = None):
46        self.path = _path
47        self.opts = opts
48        if macros is None:
49            self.macros = opts.defaults
50        else:
51            self.macros = macros
52        self.git = self.macros.expand('%{__git}')
53
54    def git_version(self):
55        ec, output = self._run(['--version'], True)
56        gvs = output.split()
57        if len(gvs) < 3:
58            raise error.general('invalid version string from git: %s' % (output))
59        vs = gvs[2].split('.')
60        if len(vs) != 4:
61            raise error.general('invalid version number from git: %s' % (gvs[2]))
62        return (int(vs[0]), int(vs[1]), int(vs[2]), int(vs[3]))
63
64    def status(self):
65        _status = {}
66        ec, output = self._run(['status'])
67        if ec == 0:
68            state = 'none'
69            for l in output.split('\n'):
70                if l.startswith('# On branch '):
71                    _status['branch'] = l[len('# On branch '):]
72                elif l.startswith('# Changes to be committed:'):
73                    state = 'staged'
74                elif l.startswith('# Changes not staged for commit:'):
75                    state = 'unstaged'
76                elif l.startswith('# Untracked files:'):
77                    state = 'untracked'
78                elif state != 'none' and l[0] == '#':
79                    if l.strip() != '#' and not l.startswith('#   ('):
80                        if state not in _status:
81                            _status[state] = []
82                        l = l[1:]
83                        if ':' in l:
84                            l = l.split(':')[1]
85                        _status[state] += [l.strip()]
86        return _status
87
88    def clean(self):
89        _status = self.status()
90        return len(_status) == 1 and 'branch' in _status
91
92    def valid(self):
93        ec, output = self._run(['status'])
94        return ec == 0
95
96    def remotes(self):
97        _remotes = {}
98        ec, output = self._run(['config', '--list'])
99        if ec == 0:
100            for l in output.split('\n'):
101                if l.startswith('remote'):
102                    ls = l.split('=')
103                    if len(ls) >= 2:
104                        rs = ls[0].split('.')
105                        if len(rs) == 3:
106                            r_name = rs[1]
107                            r_type = rs[2]
108                            if r_name not in _remotes:
109                                _remotes[r_name] = {}
110                            if r_type not in _remotes[r_name]:
111                                _remotes[r_name][r_type] = []
112                            _remotes[r_name][r_type] = '='.join(ls[1:])
113        return _remotes
114
115    def head(self):
116        hash = ''
117        ec, output = self._run(['log', '-n', '1'])
118        if ec == 0:
119            l1 = output.split('\n')[0]
120            if l1.startswith('commit '):
121                hash = l1[len('commit '):]
122        return hash
123
124if __name__ == '__main__':
125    import sys
126    opts = options.load(sys.argv)
127    g = repo('.', opts)
128    print g.git_version()
129    print g.valid()
130    print g.status()
131    print g.clean()
132    print g.remotes()
133    print g.head()
Note: See TracBrowser for help on using the repository browser.