comparison mercurial/util.py @ 4859:41ad4105dde9

Add symlink method to util.opener. Differences from os.symlink: - the symlink name is relative to the opener base directory - if a file with that name already exists, it's removed - if necessary, parent directories are created - if the system (OS or filesystem) doesn't support symlinks, a regular file is created. Its contents are the symlink target.
author Alexis S. L. Carvalho <alexis@cecm.usp.br>
date Wed, 11 Jul 2007 17:40:41 -0300
parents 89defeae88f3
children 0875082d5471
comparison
equal deleted inserted replaced
4858:89defeae88f3 4859:41ad4105dde9
1258 """ 1258 """
1259 def __init__(self, base, audit=True): 1259 def __init__(self, base, audit=True):
1260 self.base = base 1260 self.base = base
1261 self.audit = audit 1261 self.audit = audit
1262 1262
1263 def __getattr__(self, name):
1264 if name == '_can_symlink':
1265 self._can_symlink = checklink(self.base)
1266 return self._can_symlink
1267 raise AttributeError(name)
1268
1263 def __call__(self, path, mode="r", text=False, atomictemp=False): 1269 def __call__(self, path, mode="r", text=False, atomictemp=False):
1264 if self.audit: 1270 if self.audit:
1265 audit_path(path) 1271 audit_path(path)
1266 f = os.path.join(self.base, path) 1272 f = os.path.join(self.base, path)
1267 1273
1279 if atomictemp: 1285 if atomictemp:
1280 return atomictempfile(f, mode) 1286 return atomictempfile(f, mode)
1281 if nlink > 1: 1287 if nlink > 1:
1282 rename(mktempcopy(f), f) 1288 rename(mktempcopy(f), f)
1283 return posixfile(f, mode) 1289 return posixfile(f, mode)
1290
1291 def symlink(self, src, dst):
1292 if self.audit:
1293 audit_path(dst)
1294 linkname = os.path.join(self.base, dst)
1295 try:
1296 os.unlink(linkname)
1297 except OSError:
1298 pass
1299
1300 dirname = os.path.dirname(linkname)
1301 if not os.path.exists(dirname):
1302 os.makedirs(dirname)
1303
1304 if self._can_symlink:
1305 os.symlink(src, linkname)
1306 else:
1307 f = self(self, dst, "w")
1308 f.write(src)
1309 f.close()
1284 1310
1285 class chunkbuffer(object): 1311 class chunkbuffer(object):
1286 """Allow arbitrary sized chunks of data to be efficiently read from an 1312 """Allow arbitrary sized chunks of data to be efficiently read from an
1287 iterator over chunks of arbitrary size.""" 1313 iterator over chunks of arbitrary size."""
1288 1314