Mercurial > hg
comparison hgext/largefiles/overrides.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 | e5c91dc909f7 |
children | 7356e6b1f5b8 |
comparison
equal
deleted
inserted
replaced
30141:c01acee367ec | 30142:3dcaf1c4e90d |
---|---|
881 if not repo: | 881 if not repo: |
882 return result | 882 return result |
883 | 883 |
884 # If largefiles is required for this repo, permanently enable it locally | 884 # If largefiles is required for this repo, permanently enable it locally |
885 if 'largefiles' in repo.requirements: | 885 if 'largefiles' in repo.requirements: |
886 fp = repo.vfs('hgrc', 'a', text=True) | 886 with repo.vfs('hgrc', 'a', text=True) as fp: |
887 try: | |
888 fp.write('\n[extensions]\nlargefiles=\n') | 887 fp.write('\n[extensions]\nlargefiles=\n') |
889 finally: | |
890 fp.close() | |
891 | 888 |
892 # Caching is implicitly limited to 'rev' option, since the dest repo was | 889 # Caching is implicitly limited to 'rev' option, since the dest repo was |
893 # truncated at that point. The user may expect a download count with | 890 # truncated at that point. The user may expect a download count with |
894 # this option, so attempt whether or not this is a largefile repo. | 891 # this option, so attempt whether or not this is a largefile repo. |
895 if opts.get('all_largefiles'): | 892 if opts.get('all_largefiles'): |
1337 return False | 1334 return False |
1338 return origvisitdirfn(lf) | 1335 return origvisitdirfn(lf) |
1339 m.visitdir = lfvisitdirfn | 1336 m.visitdir = lfvisitdirfn |
1340 | 1337 |
1341 for f in ctx.walk(m): | 1338 for f in ctx.walk(m): |
1342 fp = cmdutil.makefileobj(repo, opts.get('output'), ctx.node(), | 1339 with cmdutil.makefileobj(repo, opts.get('output'), ctx.node(), |
1343 pathname=f) | 1340 pathname=f) as fp: |
1344 lf = lfutil.splitstandin(f) | 1341 lf = lfutil.splitstandin(f) |
1345 if lf is None or origmatchfn(f): | 1342 if lf is None or origmatchfn(f): |
1346 # duplicating unreachable code from commands.cat | 1343 # duplicating unreachable code from commands.cat |
1347 data = ctx[f].data() | 1344 data = ctx[f].data() |
1348 if opts.get('decode'): | 1345 if opts.get('decode'): |
1349 data = repo.wwritedata(f, data) | 1346 data = repo.wwritedata(f, data) |
1350 fp.write(data) | 1347 fp.write(data) |
1351 else: | 1348 else: |
1352 hash = lfutil.readstandin(repo, lf, ctx.rev()) | 1349 hash = lfutil.readstandin(repo, lf, ctx.rev()) |
1353 if not lfutil.inusercache(repo.ui, hash): | 1350 if not lfutil.inusercache(repo.ui, hash): |
1354 store = storefactory.openstore(repo) | 1351 store = storefactory.openstore(repo) |
1355 success, missing = store.get([(lf, hash)]) | 1352 success, missing = store.get([(lf, hash)]) |
1356 if len(success) != 1: | 1353 if len(success) != 1: |
1357 raise error.Abort( | 1354 raise error.Abort( |
1358 _('largefile %s is not in cache and could not be ' | 1355 _('largefile %s is not in cache and could not be ' |
1359 'downloaded') % lf) | 1356 'downloaded') % lf) |
1360 path = lfutil.usercachepath(repo.ui, hash) | 1357 path = lfutil.usercachepath(repo.ui, hash) |
1361 fpin = open(path, "rb") | 1358 with open(path, "rb") as fpin: |
1362 for chunk in util.filechunkiter(fpin, 128 * 1024): | 1359 for chunk in util.filechunkiter(fpin, 128 * 1024): |
1363 fp.write(chunk) | 1360 fp.write(chunk) |
1364 fpin.close() | |
1365 fp.close() | |
1366 err = 0 | 1361 err = 0 |
1367 return err | 1362 return err |
1368 | 1363 |
1369 def mergeupdate(orig, repo, node, branchmerge, force, | 1364 def mergeupdate(orig, repo, node, branchmerge, force, |
1370 *args, **kwargs): | 1365 *args, **kwargs): |