1 """RCS interface module.
3 Defines the class RCS, which represents a directory with rcs version
4 files and (possibly) corresponding work files.
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.
30 - The instance always represents the current directory so it's not
31 very useful to have more than one instance around simultaneously
35 # Characters allowed in work file names
36 okchars
= string
.letters
+ string
.digits
+ '-_=+.'
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.
54 f
= self
._open
(name_rev
, 'rlog ' + otherflags
)
56 status
= self
._closepipe
(f
)
58 data
= data
+ "%s: %s" % status
59 elif data
[-1] == '\n':
63 def head(self
, name_rev
):
64 """Return the head revision for NAME_REV"""
65 dict = self
.info(name_rev
)
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
75 XXX symbolic names and locks are not returned
78 f
= self
._open
(name_rev
, 'rlog -h')
84 # XXX could be a lock or symbolic name
87 i
= string
.find(line
, ':')
89 key
, value
= line
[:i
], string
.strip(line
[i
+1:])
91 status
= self
._closepipe
(f
)
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
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
134 The optional OTHERFLAGS argument is passed to ci without
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'
149 textfile
= tempfile
.mktemp()
150 f
= open(textfile
, 'w')
153 cmd
= 'ci %s%s -t%s %s %s' % \
154 (lockflag
, rev
, textfile
, otherflags
, name
)
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
)
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
)
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]
210 if os
.path
.isfile(name
): return name
211 name
= os
.path
.basename(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
222 f
= self
._open
(name_rev
, 'rlog -L -R')
224 status
= self
._closepipe
(f
)
226 raise IOError, status
227 if not line
: return None
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`
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
256 name
, rev
= self
.checkfile(name_rev
)
257 namev
= self
.rcsname(name
)
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
, ''
276 if c
not in self
.okchars
:
277 raise ValueError, "bad char in rev"
280 def _closepipe(self
, f
):
281 """INTERNAL: Close PIPE and print its exit status if nonzero."""
283 if not sts
: return None
284 detail
, reason
= divmod(sts
, 256)
285 if reason
== 0: return 'exit', detail
# Exit status
293 code
= code
+ '(coredump)'
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
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"
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.
322 def keep(name
, pat
= pat
):
323 return fnmatch
.fnmatch(name
, pat
)
324 files
= filter(keep
, files
)
330 def _remove(self
, fn
):
331 """INTERNAL: remove FILE without complaints."""
337 def _isrcs(self
, name
):
338 """INTERNAL: Test whether NAME ends in ',v'."""
339 return name
[-2:] == ',v'