comparison mercurial/util.py @ 1090:1bca39b85615

Move opener to utils - move the opener code down to util - add docstring - change commands.py users to simply use file instead
author mpm@selenic.com
date Sat, 27 Aug 2005 14:31:41 -0700
parents ce96e316278a
children 2cf5c8a4eae5
comparison
equal deleted inserted replaced
1089:142b5d5ec9cc 1090:1bca39b85615
230 elif os.path.isfile(srcname): 230 elif os.path.isfile(srcname):
231 copyfile(srcname, dstname) 231 copyfile(srcname, dstname)
232 else: 232 else:
233 pass 233 pass
234 234
235 def opener(base):
236 """
237 return a function that opens files relative to base
238
239 this function is used to hide the details of COW semantics and
240 remote file access from higher level code.
241
242 todo: separate remote file access into a separate function
243 """
244 p = base
245 def o(path, mode="r"):
246 if p.startswith("http://"):
247 f = os.path.join(p, urllib.quote(path))
248 return httprangereader.httprangereader(f)
249
250 f = os.path.join(p, path)
251
252 mode += "b" # for that other OS
253
254 if mode[0] != "r":
255 try:
256 s = os.stat(f)
257 except OSError:
258 d = os.path.dirname(f)
259 if not os.path.isdir(d):
260 os.makedirs(d)
261 else:
262 if s.st_nlink > 1:
263 file(f + ".tmp", "wb").write(file(f, "rb").read())
264 rename(f+".tmp", f)
265
266 return file(f, mode)
267
268 return o
269
235 def _makelock_file(info, pathname): 270 def _makelock_file(info, pathname):
236 ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL) 271 ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
237 os.write(ld, info) 272 os.write(ld, info)
238 os.close(ld) 273 os.close(ld)
239 274