source: rtems-source-builder/source-builder/sb/git.py @ 69e5938

4.104.114.95
Last change on this file since 69e5938 was d7e4900, checked in by Chris Johns <chrisj@…>, on 03/03/13 at 02:24:19

Add support for GIT.

The git module allows basic access to git. Hosts are now required
to provide git support.

The defaults module now returns options as a list split on '='.

  • 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 defaults
27import error
28import execute
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, _defaults):
46        self.path = _path
47        self.opts = _opts
48        self.default = _defaults
49        self.git = _opts.expand('%{__git}', _defaults)
50
51    def git_version(self):
52        ec, output = self._run(['--version'], True)
53        gvs = output.split()
54        if len(gvs) < 3:
55            raise error.general('invalid version string from git: %s' % (output))
56        vs = gvs[2].split('.')
57        if len(vs) != 4:
58            raise error.general('invalid version number from git: %s' % (gvs[2]))
59        return (int(vs[0]), int(vs[1]), int(vs[2]), int(vs[3]))
60
61    def status(self):
62        _status = {}
63        ec, output = self._run(['status'])
64        if ec == 0:
65            state = 'none'
66            for l in output.split('\n'):
67                if l.startswith('# On branch '):
68                    _status['branch'] = l[len('# On branch '):]
69                elif l.startswith('# Changes to be committed:'):
70                    state = 'staged'
71                elif l.startswith('# Changes not staged for commit:'):
72                    state = 'unstaged'
73                elif l.startswith('# Untracked files:'):
74                    state = 'untracked'
75                elif state != 'none' and l[0] == '#':
76                    if l.strip() != '#' and not l.startswith('#   ('):
77                        if state not in _status:
78                            _status[state] = []
79                        l = l[1:]
80                        if ':' in l:
81                            l = l.split(':')[1]
82                        _status[state] += [l.strip()]
83        return _status
84
85    def clean(self):
86        _status = self.status()
87        return len(_status) == 1 and 'branch' in _status
88
89    def valid(self):
90        ec, output = self._run(['status'])
91        return ec == 0
92
93    def remotes(self):
94        _remotes = {}
95        ec, output = self._run(['config', '--list'])
96        if ec == 0:
97            for l in output.split('\n'):
98                if l.startswith('remote'):
99                    ls = l.split('=')
100                    if len(ls) >= 2:
101                        rs = ls[0].split('.')
102                        if len(rs) == 3:
103                            r_name = rs[1]
104                            r_type = rs[2]
105                            if r_name not in _remotes:
106                                _remotes[r_name] = {}
107                            if r_type not in _remotes[r_name]:
108                                _remotes[r_name][r_type] = []
109                            _remotes[r_name][r_type] = '='.join(ls[1:])
110        return _remotes
111
112    def head(self):
113        hash = ''
114        ec, output = self._run(['log', '-n', '1'])
115        if ec == 0:
116            l1 = output.split('\n')[0]
117            if l1.startswith('commit '):
118                hash = l1[len('commit '):]
119        return hash
120
121if __name__ == '__main__':
122    import sys
123    _opts, _defaults = defaults.load(sys.argv)
124    g = repo('.', _opts, _defaults)
125    print g.git_version()
126    print g.valid()
127    print g.status()
128    print g.clean()
129    print g.remotes()
130    print g.head()
Note: See TracBrowser for help on using the repository browser.