source: rtems-source-builder/source-builder/sb/cvs.py @ 200f0a3

4.104.114.95
Last change on this file since 200f0a3 was 200f0a3, checked in by Chris Johns <chrisj@…>, on 04/16/13 at 07:39:48

Add a CVS module to manage CVS.

  • Property mode set to 100644
File size: 4.7 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 cvs command.
22#
23
24import os
25
26import error
27import execute
28import options
29import path
30
31class repo:
32    """An object to manage a cvs repo."""
33
34    def _cvs_exit_code(self, ec, output):
35        if ec:
36            print output
37            raise error.general('cvs command failed (%s): %d' % (self.cvs, ec))
38
39    def _parse_args(self, url):
40        if not url.startswith('cvs://'):
41            raise error.general('invalid cvs url: %s' % (url))
42        opts = { 'cvsroot': ':%s' % (us[0][6:]),
43                 'module':  '' }
44        for o in us:
45            os = o.split('=')
46            if len(os) == 1:
47                opts[os[0]] = True
48            else:
49                opts[os[0]] = os[1:]
50        return opts
51
52    def _run(self, args, check = False):
53        e = execute.capture_execution()
54        if path.exists(self.path):
55            cwd = self.path
56        else:
57            cwd = None
58        exit_code, proc, output = e.spawn([self.cvs, '-q'] + args, cwd = cwd)
59        if check:
60            self._cvs_exit_code(exit_code, output)
61        return exit_code, output
62
63    def __init__(self, _path, opts, macros = None):
64        self.path = _path
65        self.opts = opts
66        if macros is None:
67            self.macros = opts.defaults
68        else:
69            self.macros = macros
70        self.cvs = self.macros.expand('%{__cvs}')
71
72    def cvs_version(self):
73        ec, output = self._run(['--version'], True)
74        lines = output.split('\n')
75        if len(lines) < 12:
76            raise error.general('invalid version string from cvs: %s' % (output))
77        cvs = lines[0].split(' ')
78        if len(cvs) != 6:
79            raise error.general('invalid version number from cvs: %s' % (lines[0]))
80        vs = cvs[4].split('.')
81        if len(vs) < 3:
82            raise error.general('invalid version number from cvs: %s' % (cvs[4]))
83        return (int(vs[0]), int(vs[1]), int(vs[2]))
84
85    def checkout(self, root, path, module = ''):
86        ec, output = self._run(['-d', root, 'co', '-N', '-d', path, module], check = True)
87
88    def update(self):
89        ec, output = self._run(['up'])
90
91    def reset(self):
92        ec, output = self._run(['up', '-C'], check = True)
93
94    def branch(self):
95        ec, output = self._run(['branch'])
96        if ec == 0:
97            for b in output.split('\n'):
98                if b[0] == '*':
99                    return b[2:]
100        return None
101
102    def status(self):
103        keys = { 'U': 'modified',
104                 'P': 'modified',
105                 'M': 'modified',
106                 'R': 'removed',
107                 'C': 'conflict',
108                 'A': 'added',
109                 '?': 'untracked' }
110        _status = {}
111        if path.exists(self.path):
112            ec, output = self._run(['-n', 'up'])
113            if ec == 0:
114                state = 'none'
115                for l in output.split('\n'):
116                    if len(l) > 2 and l[0] in keys:
117                        if keys[l[0]] not in _status:
118                            _status[keys[l[0]]] = []
119                        _status[keys[l[0]]] += [l[2:]]
120        return _status
121
122    def clean(self):
123        _status = self.status()
124        return len(_status) == 0
125
126    def valid(self):
127        if path.exists(self.path):
128            ec, output = self._run(['-n', 'up', '-l'])
129            if ec == 0:
130                if not output.startswith('cvs status: No CVSROOT specified'):
131                    return True
132        return False
133
134if __name__ == '__main__':
135    import sys
136    opts = options.load(sys.argv, defaults = 'defaults.mc')
137    ldir = 'cvs-test-rm-me'
138    c = repo(ldir, opts)
139    if not path.exists(ldir):
140        c.checkout(':pserver:anoncvs@sourceware.org:/cvs/src', ldir, 'newlib')
141    print c.cvs_version()
142    print c.valid()
143    print c.status()
144    c.reset()
145    print c.clean()
Note: See TracBrowser for help on using the repository browser.