Bump version to 0.9.1.
[python/dscho.git] / Demo / pdist / rcslib.py
blob5e79247cb3670fba707b2601c61f2d62ff531b4a
1 """RCS interface module.
3 Defines the class RCS, which represents a directory with rcs version
4 files and (possibly) corresponding work files.
6 """
9 import fnmatch
10 import os
11 import regsub
12 import string
13 import tempfile
16 class RCS:
18 """RCS interface class (local filesystem version).
20 An instance of this class represents a directory with rcs version
21 files and (possible) corresponding work files.
23 Methods provide access to most rcs operations such as
24 checkin/checkout, access to the rcs metadata (revisions, logs,
25 branches etc.) as well as some filesystem operations such as
26 listing all rcs version files.
28 XXX BUGS / PROBLEMS
30 - The instance always represents the current directory so it's not
31 very useful to have more than one instance around simultaneously
33 """
35 # Characters allowed in work file names
36 okchars = string.letters + string.digits + '-_=+.'
38 def __init__(self):
39 """Constructor."""
40 pass
42 def __del__(self):
43 """Destructor."""
44 pass
46 # --- Informational methods about a single file/revision ---
48 def log(self, name_rev, otherflags = ''):
49 """Return the full log text for NAME_REV as a string.
51 Optional OTHERFLAGS are passed to rlog.
53 """
54 f = self._open(name_rev, 'rlog ' + otherflags)
55 data = f.read()
56 status = self._closepipe(f)
57 if status:
58 data = data + "%s: %s" % status
59 elif data[-1] == '\n':
60 data = data[:-1]
61 return data
63 def head(self, name_rev):
64 """Return the head revision for NAME_REV"""
65 dict = self.info(name_rev)
66 return dict['head']
68 def info(self, name_rev):
69 """Return a dictionary of info (from rlog -h) for NAME_REV
71 The dictionary's keys are the keywords that rlog prints
72 (e.g. 'head' and its values are the corresponding data
73 (e.g. '1.3').
75 XXX symbolic names and locks are not returned
77 """
78 f = self._open(name_rev, 'rlog -h')
79 dict = {}
80 while 1:
81 line = f.readline()
82 if not line: break
83 if line[0] == '\t':
84 # XXX could be a lock or symbolic name
85 # Anything else?
86 continue
87 i = string.find(line, ':')
88 if i > 0:
89 key, value = line[:i], string.strip(line[i+1:])
90 dict[key] = value
91 status = self._closepipe(f)
92 if status:
93 raise IOError, status
94 return dict
96 # --- Methods that change files ---
98 def lock(self, name_rev):
99 """Set an rcs lock on NAME_REV."""
100 name, rev = self.checkfile(name_rev)
101 cmd = "rcs -l%s %s" % (rev, name)
102 return self._system(cmd)
104 def unlock(self, name_rev):
105 """Clear an rcs lock on NAME_REV."""
106 name, rev = self.checkfile(name_rev)
107 cmd = "rcs -u%s %s" % (rev, name)
108 return self._system(cmd)
110 def checkout(self, name_rev, withlock=0, otherflags=""):
111 """Check out NAME_REV to its work file.
113 If optional WITHLOCK is set, check out locked, else unlocked.
115 The optional OTHERFLAGS is passed to co without
116 interpretation.
118 Any output from co goes to directly to stdout.
121 name, rev = self.checkfile(name_rev)
122 if withlock: lockflag = "-l"
123 else: lockflag = "-u"
124 cmd = 'co %s%s %s %s' % (lockflag, rev, otherflags, name)
125 return self._system(cmd)
127 def checkin(self, name_rev, message=None, otherflags=""):
128 """Check in NAME_REV from its work file.
130 The optional MESSAGE argument becomes the checkin message
131 (default "<none>" if None); or the file description if this is
132 a new file.
134 The optional OTHERFLAGS argument is passed to ci without
135 interpretation.
137 Any output from ci goes to directly to stdout.
140 name, rev = self._unmangle(name_rev)
141 new = not self.isvalid(name)
142 if not message: message = "<none>"
143 if message and message[-1] != '\n':
144 message = message + '\n'
145 lockflag = "-u"
146 textfile = None
147 try:
148 if new:
149 textfile = tempfile.mktemp()
150 f = open(textfile, 'w')
151 f.write(message)
152 f.close()
153 cmd = 'ci %s%s -t%s %s %s' % \
154 (lockflag, rev, textfile, otherflags, name)
155 else:
156 message = regsub.gsub('\([\\"$`]\)', '\\\\\\1', message)
157 cmd = 'ci %s%s -m"%s" %s %s' % \
158 (lockflag, rev, message, otherflags, name)
159 return self._system(cmd)
160 finally:
161 if textfile: self._remove(textfile)
163 # --- Exported support methods ---
165 def listfiles(self, pat = None):
166 """Return a list of all version files matching optional PATTERN."""
167 files = os.listdir(os.curdir)
168 files = filter(self._isrcs, files)
169 if os.path.isdir('RCS'):
170 files2 = os.listdir('RCS')
171 files2 = filter(self._isrcs, files2)
172 files = files + files2
173 files = map(self.realname, files)
174 return self._filter(files, pat)
176 def isvalid(self, name):
177 """Test whether NAME has a version file associated."""
178 namev = self.rcsname(name)
179 return (os.path.isfile(namev) or
180 os.path.isfile(os.path.join('RCS', namev)))
182 def rcsname(self, name):
183 """Return the pathname of the version file for NAME.
185 The argument can be a work file name or a version file name.
186 If the version file does not exist, the name of the version
187 file that would be created by "ci" is returned.
190 if self._isrcs(name): namev = name
191 else: namev = name + ',v'
192 if os.path.isfile(namev): return namev
193 namev = os.path.join('RCS', os.path.basename(namev))
194 if os.path.isfile(namev): return namev
195 if os.path.isdir('RCS'):
196 return os.path.join('RCS', namev)
197 else:
198 return namev
200 def realname(self, namev):
201 """Return the pathname of the work file for NAME.
203 The argument can be a work file name or a version file name.
204 If the work file does not exist, the name of the work file
205 that would be created by "co" is returned.
208 if self._isrcs(namev): name = namev[:-2]
209 else: name = namev
210 if os.path.isfile(name): return name
211 name = os.path.basename(name)
212 return name
214 def islocked(self, name_rev):
215 """Test whether FILE (which must have a version file) is locked.
217 XXX This does not tell you which revision number is locked and
218 ignores any revision you may pass in (by virtue of using rlog
219 -L -R).
222 f = self._open(name_rev, 'rlog -L -R')
223 line = f.readline()
224 status = self._closepipe(f)
225 if status:
226 raise IOError, status
227 if not line: return None
228 if line[-1] == '\n':
229 line = line[:-1]
230 return self.realname(name_rev) == self.realname(line)
232 def checkfile(self, name_rev):
233 """Normalize NAME_REV into a (NAME, REV) tuple.
235 Raise an exception if there is no corresponding version file.
238 name, rev = self._unmangle(name_rev)
239 if not self.isvalid(name):
240 raise os.error, 'not an rcs file %s' % `name`
241 return name, rev
243 # --- Internal methods ---
245 def _open(self, name_rev, cmd = 'co -p', rflag = '-r'):
246 """INTERNAL: open a read pipe to NAME_REV using optional COMMAND.
248 Optional FLAG is used to indicate the revision (default -r).
250 Default COMMAND is "co -p".
252 Return a file object connected by a pipe to the command's
253 output.
256 name, rev = self.checkfile(name_rev)
257 namev = self.rcsname(name)
258 if rev:
259 cmd = cmd + ' ' + rflag + rev
260 return os.popen("%s %s" % (cmd, `namev`))
262 def _unmangle(self, name_rev):
263 """INTERNAL: Normalize NAME_REV argument to (NAME, REV) tuple.
265 Raise an exception if NAME contains invalid characters.
267 A NAME_REV argument is either NAME string (implying REV='') or
268 a tuple of the form (NAME, REV).
271 if type(name_rev) == type(''):
272 name_rev = name, rev = name_rev, ''
273 else:
274 name, rev = name_rev
275 for c in rev:
276 if c not in self.okchars:
277 raise ValueError, "bad char in rev"
278 return name_rev
280 def _closepipe(self, f):
281 """INTERNAL: Close PIPE and print its exit status if nonzero."""
282 sts = f.close()
283 if not sts: return None
284 detail, reason = divmod(sts, 256)
285 if reason == 0: return 'exit', detail # Exit status
286 signal = reason&0x7F
287 if signal == 0x7F:
288 code = 'stopped'
289 signal = detail
290 else:
291 code = 'killed'
292 if reason&0x80:
293 code = code + '(coredump)'
294 return code, signal
296 def _system(self, cmd):
297 """INTERNAL: run COMMAND in a subshell.
299 Standard input for the command is taken from /dev/null.
301 Raise IOError when the exit status is not zero.
303 Return whatever the calling method should return; normally
304 None.
306 A derived class may override this method and redefine it to
307 capture stdout/stderr of the command and return it.
310 cmd = cmd + " </dev/null"
311 sts = os.system(cmd)
312 if sts: raise IOError, "command exit status %d" % sts
314 def _filter(self, files, pat = None):
315 """INTERNAL: Return a sorted copy of the given list of FILES.
317 If a second PATTERN argument is given, only files matching it
318 are kept. No check for valid filenames is made.
321 if pat:
322 def keep(name, pat = pat):
323 return fnmatch.fnmatch(name, pat)
324 files = filter(keep, files)
325 else:
326 files = files[:]
327 files.sort()
328 return files
330 def _remove(self, fn):
331 """INTERNAL: remove FILE without complaints."""
332 try:
333 os.unlink(fn)
334 except os.error:
335 pass
337 def _isrcs(self, name):
338 """INTERNAL: Test whether NAME ends in ',v'."""
339 return name[-2:] == ',v'