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