[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 |
---|
| 28 | import options |
---|
| 29 | import path |
---|
| 30 | |
---|
| 31 | class repo: |
---|
| 32 | """An object to manage a cvs repo.""" |
---|
| 33 | |
---|
[4ce931b] | 34 | def _cvs_exit_code(self, cmd, ec, output): |
---|
[200f0a3] | 35 | if ec: |
---|
| 36 | print output |
---|
[4ce931b] | 37 | raise error.general('cvs command failed (%s): %d' % (cmd, ec)) |
---|
[200f0a3] | 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 | |
---|
[4ce931b] | 52 | def _run(self, args, check = False, cwd = None): |
---|
[200f0a3] | 53 | e = execute.capture_execution() |
---|
| 54 | if path.exists(self.path): |
---|
| 55 | cwd = self.path |
---|
[4ce931b] | 56 | cmd = [self.cvs, '-q'] + args |
---|
| 57 | exit_code, proc, output = e.spawn(cmd, cwd = cwd) |
---|
[200f0a3] | 58 | if check: |
---|
[4ce931b] | 59 | self._cvs_exit_code(cmd, exit_code, output) |
---|
[200f0a3] | 60 | return exit_code, output |
---|
| 61 | |
---|
[4ce931b] | 62 | def __init__(self, _path, opts, macros = None, prefix = None): |
---|
[200f0a3] | 63 | self.path = _path |
---|
| 64 | self.opts = opts |
---|
[4ce931b] | 65 | self.prefix = prefix |
---|
[200f0a3] | 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 | |
---|
[4ce931b] | 85 | def checkout(self, root, module = None, tag = None, date = None): |
---|
| 86 | cmd = ['-d', root, 'co', '-N'] |
---|
| 87 | if tag: |
---|
| 88 | cmd += ['-r', tag] |
---|
| 89 | if date: |
---|
| 90 | cmd += ['-D', date] |
---|
| 91 | if module: |
---|
| 92 | cmd += [module] |
---|
| 93 | ec, output = self._run(cmd, check = True) |
---|
[200f0a3] | 94 | |
---|
| 95 | def update(self): |
---|
| 96 | ec, output = self._run(['up']) |
---|
| 97 | |
---|
| 98 | def reset(self): |
---|
| 99 | ec, output = self._run(['up', '-C'], check = True) |
---|
| 100 | |
---|
| 101 | def branch(self): |
---|
| 102 | ec, output = self._run(['branch']) |
---|
| 103 | if ec == 0: |
---|
| 104 | for b in output.split('\n'): |
---|
| 105 | if b[0] == '*': |
---|
| 106 | return b[2:] |
---|
| 107 | return None |
---|
| 108 | |
---|
| 109 | def status(self): |
---|
| 110 | keys = { 'U': 'modified', |
---|
| 111 | 'P': 'modified', |
---|
| 112 | 'M': 'modified', |
---|
| 113 | 'R': 'removed', |
---|
| 114 | 'C': 'conflict', |
---|
| 115 | 'A': 'added', |
---|
| 116 | '?': 'untracked' } |
---|
| 117 | _status = {} |
---|
| 118 | if path.exists(self.path): |
---|
| 119 | ec, output = self._run(['-n', 'up']) |
---|
| 120 | if ec == 0: |
---|
| 121 | state = 'none' |
---|
| 122 | for l in output.split('\n'): |
---|
| 123 | if len(l) > 2 and l[0] in keys: |
---|
| 124 | if keys[l[0]] not in _status: |
---|
| 125 | _status[keys[l[0]]] = [] |
---|
| 126 | _status[keys[l[0]]] += [l[2:]] |
---|
| 127 | return _status |
---|
| 128 | |
---|
| 129 | def clean(self): |
---|
| 130 | _status = self.status() |
---|
| 131 | return len(_status) == 0 |
---|
| 132 | |
---|
| 133 | def valid(self): |
---|
| 134 | if path.exists(self.path): |
---|
| 135 | ec, output = self._run(['-n', 'up', '-l']) |
---|
| 136 | if ec == 0: |
---|
| 137 | if not output.startswith('cvs status: No CVSROOT specified'): |
---|
| 138 | return True |
---|
| 139 | return False |
---|
| 140 | |
---|
| 141 | if __name__ == '__main__': |
---|
| 142 | import sys |
---|
| 143 | opts = options.load(sys.argv, defaults = 'defaults.mc') |
---|
| 144 | ldir = 'cvs-test-rm-me' |
---|
| 145 | c = repo(ldir, opts) |
---|
| 146 | if not path.exists(ldir): |
---|
[4ce931b] | 147 | path.mkdir(ldir) |
---|
| 148 | c.checkout(':pserver:anoncvs@sourceware.org:/cvs/src', module = 'newlib') |
---|
[200f0a3] | 149 | print c.cvs_version() |
---|
| 150 | print c.valid() |
---|
| 151 | print c.status() |
---|
| 152 | c.reset() |
---|
| 153 | print c.clean() |
---|