source: rtems-source-builder/source-builder/sb/cvs.py @ e02eaa6

4.11
Last change on this file since e02eaa6 was f88fcf3, checked in by Chris Johns <chrisj@…>, on 03/07/16 at 00:56:02

sb: Update code base to support Python3 and Python2.

Fix Windows support to allow MSYS2 Python to be used.

Updates #2619.

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