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

Last change on this file was 650c6f9, checked in by Chris Johns <chrisj@…>, on 08/25/20 at 11:21:50

sb: Use shebang env python

Closes #4037

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