diff options
| author | Mike Frysinger <vapier@google.com> | 2019-11-11 05:40:22 -0500 |
|---|---|---|
| committer | Mike Frysinger <vapier@google.com> | 2019-11-12 03:44:39 +0000 |
| commit | 3164d40e2247d42537aef8e80fa7e048e14bec9f (patch) | |
| tree | 650cc33e3d5c4b39c3cc652e93495e47a170931b /subcmds/sync.py | |
| parent | f4545126197781beb03bb0fd47e7f24ce5af6ca8 (diff) | |
| download | git-repo-3164d40e2247d42537aef8e80fa7e048e14bec9f.tar.gz | |
use open context managers in more places
Use open() as a context manager to simplify the close logic and make
the code easier to read & understand. This is also more Pythonic.
Change-Id: I579d03cca86f99b2c6c6a1f557f6e5704e2515a7
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/244734
Reviewed-by: David Pursehouse <dpursehouse@collab.net>
Tested-by: Mike Frysinger <vapier@google.com>
Diffstat (limited to 'subcmds/sync.py')
| -rw-r--r-- | subcmds/sync.py | 25 |
1 files changed, 5 insertions, 20 deletions
diff --git a/subcmds/sync.py b/subcmds/sync.py index f77a785b..9b4a6147 100644 --- a/subcmds/sync.py +++ b/subcmds/sync.py | |||
| @@ -692,11 +692,8 @@ later is required to fix a server side protocol bug. | |||
| 692 | old_project_paths = [] | 692 | old_project_paths = [] |
| 693 | 693 | ||
| 694 | if os.path.exists(file_path): | 694 | if os.path.exists(file_path): |
| 695 | fd = open(file_path, 'r') | 695 | with open(file_path, 'r') as fd: |
| 696 | try: | ||
| 697 | old_project_paths = fd.read().split('\n') | 696 | old_project_paths = fd.read().split('\n') |
| 698 | finally: | ||
| 699 | fd.close() | ||
| 700 | # In reversed order, so subfolders are deleted before parent folder. | 697 | # In reversed order, so subfolders are deleted before parent folder. |
| 701 | for path in sorted(old_project_paths, reverse=True): | 698 | for path in sorted(old_project_paths, reverse=True): |
| 702 | if not path: | 699 | if not path: |
| @@ -731,12 +728,9 @@ later is required to fix a server side protocol bug. | |||
| 731 | return 1 | 728 | return 1 |
| 732 | 729 | ||
| 733 | new_project_paths.sort() | 730 | new_project_paths.sort() |
| 734 | fd = open(file_path, 'w') | 731 | with open(file_path, 'w') as fd: |
| 735 | try: | ||
| 736 | fd.write('\n'.join(new_project_paths)) | 732 | fd.write('\n'.join(new_project_paths)) |
| 737 | fd.write('\n') | 733 | fd.write('\n') |
| 738 | finally: | ||
| 739 | fd.close() | ||
| 740 | return 0 | 734 | return 0 |
| 741 | 735 | ||
| 742 | def _SmartSyncSetup(self, opt, smart_sync_manifest_path): | 736 | def _SmartSyncSetup(self, opt, smart_sync_manifest_path): |
| @@ -809,11 +803,8 @@ later is required to fix a server side protocol bug. | |||
| 809 | if success: | 803 | if success: |
| 810 | manifest_name = os.path.basename(smart_sync_manifest_path) | 804 | manifest_name = os.path.basename(smart_sync_manifest_path) |
| 811 | try: | 805 | try: |
| 812 | f = open(smart_sync_manifest_path, 'w') | 806 | with open(smart_sync_manifest_path, 'w') as f: |
| 813 | try: | ||
| 814 | f.write(manifest_str) | 807 | f.write(manifest_str) |
| 815 | finally: | ||
| 816 | f.close() | ||
| 817 | except IOError as e: | 808 | except IOError as e: |
| 818 | print('error: cannot write manifest to %s:\n%s' | 809 | print('error: cannot write manifest to %s:\n%s' |
| 819 | % (smart_sync_manifest_path, e), | 810 | % (smart_sync_manifest_path, e), |
| @@ -1102,11 +1093,8 @@ class _FetchTimes(object): | |||
| 1102 | def _Load(self): | 1093 | def _Load(self): |
| 1103 | if self._times is None: | 1094 | if self._times is None: |
| 1104 | try: | 1095 | try: |
| 1105 | f = open(self._path) | 1096 | with open(self._path) as f: |
| 1106 | try: | ||
| 1107 | self._times = json.load(f) | 1097 | self._times = json.load(f) |
| 1108 | finally: | ||
| 1109 | f.close() | ||
| 1110 | except (IOError, ValueError): | 1098 | except (IOError, ValueError): |
| 1111 | try: | 1099 | try: |
| 1112 | platform_utils.remove(self._path) | 1100 | platform_utils.remove(self._path) |
| @@ -1126,11 +1114,8 @@ class _FetchTimes(object): | |||
| 1126 | del self._times[name] | 1114 | del self._times[name] |
| 1127 | 1115 | ||
| 1128 | try: | 1116 | try: |
| 1129 | f = open(self._path, 'w') | 1117 | with open(self._path, 'w') as f: |
| 1130 | try: | ||
| 1131 | json.dump(self._times, f, indent=2) | 1118 | json.dump(self._times, f, indent=2) |
| 1132 | finally: | ||
| 1133 | f.close() | ||
| 1134 | except (IOError, TypeError): | 1119 | except (IOError, TypeError): |
| 1135 | try: | 1120 | try: |
| 1136 | platform_utils.remove(self._path) | 1121 | platform_utils.remove(self._path) |
