[200f0a3] | 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 | |
---|
| 24 | import os |
---|
| 25 | |
---|
| 26 | import error |
---|
| 27 | import execute |
---|
[5142bec] | 28 | import log |
---|
[200f0a3] | 29 | import options |
---|
| 30 | import path |
---|
| 31 | |
---|
| 32 | class repo: |
---|
| 33 | """An object to manage a cvs repo.""" |
---|
| 34 | |
---|
[f077b2b] | 35 | def __init__(self, _path, opts, macros = None, prefix = None): |
---|
| 36 | self.path = _path |
---|
| 37 | self.opts = opts |
---|
| 38 | self.prefix = prefix |
---|
| 39 | if macros is None: |
---|
| 40 | self.macros = opts.defaults |
---|
| 41 | else: |
---|
| 42 | self.macros = macros |
---|
| 43 | self.cvs = self.macros.expand('%{__cvs}') |
---|
| 44 | |
---|
[4ce931b] | 45 | def _cvs_exit_code(self, cmd, ec, output): |
---|
[200f0a3] | 46 | if ec: |
---|
[5142bec] | 47 | log.output(output) |
---|
[4ce931b] | 48 | raise error.general('cvs command failed (%s): %d' % (cmd, ec)) |
---|
[200f0a3] | 49 | |
---|
| 50 | def _parse_args(self, url): |
---|
| 51 | if not url.startswith('cvs://'): |
---|
| 52 | raise error.general('invalid cvs url: %s' % (url)) |
---|
| 53 | opts = { 'cvsroot': ':%s' % (us[0][6:]), |
---|
| 54 | 'module': '' } |
---|
| 55 | for o in us: |
---|
| 56 | os = o.split('=') |
---|
| 57 | if len(os) == 1: |
---|
| 58 | opts[os[0]] = True |
---|
| 59 | else: |
---|
| 60 | opts[os[0]] = os[1:] |
---|
| 61 | return opts |
---|
| 62 | |
---|
[4ce931b] | 63 | def _run(self, args, check = False, cwd = None): |
---|
[200f0a3] | 64 | e = execute.capture_execution() |
---|
[5f44fcd] | 65 | if cwd is None: |
---|
[f077b2b] | 66 | _path = path.join(self.path, self.prefix) |
---|
| 67 | if not path.exists(_path): |
---|
| 68 | raise error.general('cvs path needs to exist: %s' % (_path)) |
---|
| 69 | cwd = _path |
---|
[4ce931b] | 70 | cmd = [self.cvs, '-q'] + args |
---|
[5142bec] | 71 | log.output('cmd: (%s) %s' % (str(cwd), ' '.join(cmd))) |
---|
[4ce931b] | 72 | exit_code, proc, output = e.spawn(cmd, cwd = cwd) |
---|
[5142bec] | 73 | log.trace(output) |
---|
[200f0a3] | 74 | if check: |
---|
[4ce931b] | 75 | self._cvs_exit_code(cmd, exit_code, output) |
---|
[200f0a3] | 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 | |
---|
[4ce931b] | 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) |
---|
[200f0a3] | 100 | |
---|
| 101 | def update(self): |
---|
[f077b2b] | 102 | ec, output = self._run(['up'], check = True) |
---|
[200f0a3] | 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 | |
---|
| 147 | if __name__ == '__main__': |
---|
| 148 | import sys |
---|
| 149 | opts = options.load(sys.argv, defaults = 'defaults.mc') |
---|
| 150 | ldir = 'cvs-test-rm-me' |
---|
| 151 | c = repo(ldir, opts) |
---|
| 152 | if not path.exists(ldir): |
---|
[4ce931b] | 153 | path.mkdir(ldir) |
---|
| 154 | c.checkout(':pserver:anoncvs@sourceware.org:/cvs/src', module = 'newlib') |
---|
[200f0a3] | 155 | print c.cvs_version() |
---|
| 156 | print c.valid() |
---|
| 157 | print c.status() |
---|
| 158 | c.reset() |
---|
| 159 | print c.clean() |
---|