Mercurial > hg
comparison hgext/largefiles/lfutil.py @ 30142:3dcaf1c4e90d
largefiles: use context for file closing
Make the code slightly smaller and safer (and more deeply indented).
author | Mads Kiilerich <madski@unity3d.com> |
---|---|
date | Sat, 08 Oct 2016 00:59:41 +0200 |
parents | ce4ac5d19cb8 |
children | 736f92c44656 |
comparison
equal
deleted
inserted
replaced
30141:c01acee367ec | 30142:3dcaf1c4e90d |
---|---|
52 util.makedirs(os.path.dirname(dest)) | 52 util.makedirs(os.path.dirname(dest)) |
53 try: | 53 try: |
54 util.oslink(src, dest) | 54 util.oslink(src, dest) |
55 except OSError: | 55 except OSError: |
56 # if hardlinks fail, fallback on atomic copy | 56 # if hardlinks fail, fallback on atomic copy |
57 dst = util.atomictempfile(dest) | 57 with open(src, 'rb') as srcf: |
58 for chunk in util.filechunkiter(open(src, 'rb')): | 58 with util.atomictempfile(dest) as dstf: |
59 dst.write(chunk) | 59 for chunk in util.filechunkiter(srcf): |
60 dst.close() | 60 dstf.write(chunk) |
61 os.chmod(dest, os.stat(src).st_mode) | 61 os.chmod(dest, os.stat(src).st_mode) |
62 | 62 |
63 def usercachepath(ui, hash): | 63 def usercachepath(ui, hash): |
64 '''Return the correct location in the "global" largefiles cache for a file | 64 '''Return the correct location in the "global" largefiles cache for a file |
65 with the given hash. | 65 with the given hash. |
262 def copytostoreabsolute(repo, file, hash): | 262 def copytostoreabsolute(repo, file, hash): |
263 if inusercache(repo.ui, hash): | 263 if inusercache(repo.ui, hash): |
264 link(usercachepath(repo.ui, hash), storepath(repo, hash)) | 264 link(usercachepath(repo.ui, hash), storepath(repo, hash)) |
265 else: | 265 else: |
266 util.makedirs(os.path.dirname(storepath(repo, hash))) | 266 util.makedirs(os.path.dirname(storepath(repo, hash))) |
267 dst = util.atomictempfile(storepath(repo, hash), | 267 with open(file, 'rb') as srcf: |
268 createmode=repo.store.createmode) | 268 with util.atomictempfile(storepath(repo, hash), |
269 for chunk in util.filechunkiter(open(file, 'rb')): | 269 createmode=repo.store.createmode) as dstf: |
270 dst.write(chunk) | 270 for chunk in util.filechunkiter(srcf): |
271 dst.close() | 271 dstf.write(chunk) |
272 linktousercache(repo, hash) | 272 linktousercache(repo, hash) |
273 | 273 |
274 def linktousercache(repo, hash): | 274 def linktousercache(repo, hash): |
275 '''Link / copy the largefile with the specified hash from the store | 275 '''Link / copy the largefile with the specified hash from the store |
276 to the cache.''' | 276 to the cache.''' |
368 | 368 |
369 def hashfile(file): | 369 def hashfile(file): |
370 if not os.path.exists(file): | 370 if not os.path.exists(file): |
371 return '' | 371 return '' |
372 hasher = hashlib.sha1('') | 372 hasher = hashlib.sha1('') |
373 fd = open(file, 'rb') | 373 with open(file, 'rb') as fd: |
374 for data in util.filechunkiter(fd, 128 * 1024): | 374 for data in util.filechunkiter(fd, 128 * 1024): |
375 hasher.update(data) | 375 hasher.update(data) |
376 fd.close() | |
377 return hasher.hexdigest() | 376 return hasher.hexdigest() |
378 | 377 |
379 def getexecutable(filename): | 378 def getexecutable(filename): |
380 mode = os.stat(filename).st_mode | 379 mode = os.stat(filename).st_mode |
381 return ((mode & stat.S_IXUSR) and | 380 return ((mode & stat.S_IXUSR) and |