summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--editor.py5
-rw-r--r--git_config.py15
-rw-r--r--git_refs.py13
-rwxr-xr-xproject.py17
-rwxr-xr-xrepo5
-rw-r--r--subcmds/manifest.py7
-rw-r--r--subcmds/sync.py25
7 files changed, 21 insertions, 66 deletions
diff --git a/editor.py b/editor.py
index b761eb57..19b96c37 100644
--- a/editor.py
+++ b/editor.py
@@ -106,11 +106,8 @@ least one of these before using this command.""", file=sys.stderr)
106 raise EditorError('editor failed with exit status %d: %s %s' 106 raise EditorError('editor failed with exit status %d: %s %s'
107 % (rc, editor, path)) 107 % (rc, editor, path))
108 108
109 fd2 = open(path) 109 with open(path) as fd2:
110 try:
111 return fd2.read() 110 return fd2.read()
112 finally:
113 fd2.close()
114 finally: 111 finally:
115 if fd: 112 if fd:
116 os.close(fd) 113 os.close(fd)
diff --git a/git_config.py b/git_config.py
index 1ea9c43e..680de90f 100644
--- a/git_config.py
+++ b/git_config.py
@@ -276,22 +276,16 @@ class GitConfig(object):
276 return None 276 return None
277 try: 277 try:
278 Trace(': parsing %s', self.file) 278 Trace(': parsing %s', self.file)
279 fd = open(self._json) 279 with open(self._json) as fd:
280 try:
281 return json.load(fd) 280 return json.load(fd)
282 finally:
283 fd.close()
284 except (IOError, ValueError): 281 except (IOError, ValueError):
285 platform_utils.remove(self._json) 282 platform_utils.remove(self._json)
286 return None 283 return None
287 284
288 def _SaveJson(self, cache): 285 def _SaveJson(self, cache):
289 try: 286 try:
290 fd = open(self._json, 'w') 287 with open(self._json, 'w') as fd:
291 try:
292 json.dump(cache, fd, indent=2) 288 json.dump(cache, fd, indent=2)
293 finally:
294 fd.close()
295 except (IOError, TypeError): 289 except (IOError, TypeError):
296 if os.path.exists(self._json): 290 if os.path.exists(self._json):
297 platform_utils.remove(self._json) 291 platform_utils.remove(self._json)
@@ -773,15 +767,12 @@ class Branch(object):
773 self._Set('merge', self.merge) 767 self._Set('merge', self.merge)
774 768
775 else: 769 else:
776 fd = open(self._config.file, 'a') 770 with open(self._config.file, 'a') as fd:
777 try:
778 fd.write('[branch "%s"]\n' % self.name) 771 fd.write('[branch "%s"]\n' % self.name)
779 if self.remote: 772 if self.remote:
780 fd.write('\tremote = %s\n' % self.remote.name) 773 fd.write('\tremote = %s\n' % self.remote.name)
781 if self.merge: 774 if self.merge:
782 fd.write('\tmerge = %s\n' % self.merge) 775 fd.write('\tmerge = %s\n' % self.merge)
783 finally:
784 fd.close()
785 776
786 def _Set(self, key, value): 777 def _Set(self, key, value):
787 key = 'branch.%s.%s' % (self.name, key) 778 key = 'branch.%s.%s' % (self.name, key)
diff --git a/git_refs.py b/git_refs.py
index 98ed1e2f..debd4cbf 100644
--- a/git_refs.py
+++ b/git_refs.py
@@ -141,18 +141,11 @@ class GitRefs(object):
141 141
142 def _ReadLoose1(self, path, name): 142 def _ReadLoose1(self, path, name):
143 try: 143 try:
144 fd = open(path) 144 with open(path) as fd:
145 except IOError:
146 return
147
148 try:
149 try:
150 mtime = os.path.getmtime(path) 145 mtime = os.path.getmtime(path)
151 ref_id = fd.readline() 146 ref_id = fd.readline()
152 except (IOError, OSError): 147 except (IOError, OSError):
153 return 148 return
154 finally:
155 fd.close()
156 149
157 try: 150 try:
158 ref_id = ref_id.decode() 151 ref_id = ref_id.decode()
diff --git a/project.py b/project.py
index a2a3adc8..6a48c23a 100755
--- a/project.py
+++ b/project.py
@@ -58,11 +58,8 @@ else:
58def _lwrite(path, content): 58def _lwrite(path, content):
59 lock = '%s.lock' % path 59 lock = '%s.lock' % path
60 60
61 fd = open(lock, 'w') 61 with open(lock, 'w') as fd:
62 try:
63 fd.write(content) 62 fd.write(content)
64 finally:
65 fd.close()
66 63
67 try: 64 try:
68 platform_utils.rename(lock, path) 65 platform_utils.rename(lock, path)
@@ -1393,12 +1390,9 @@ class Project(object):
1393 if is_new: 1390 if is_new:
1394 alt = os.path.join(self.gitdir, 'objects/info/alternates') 1391 alt = os.path.join(self.gitdir, 'objects/info/alternates')
1395 try: 1392 try:
1396 fd = open(alt) 1393 with open(alt) as fd:
1397 try:
1398 # This works for both absolute and relative alternate directories. 1394 # This works for both absolute and relative alternate directories.
1399 alt_dir = os.path.join(self.objdir, 'objects', fd.readline().rstrip()) 1395 alt_dir = os.path.join(self.objdir, 'objects', fd.readline().rstrip())
1400 finally:
1401 fd.close()
1402 except IOError: 1396 except IOError:
1403 alt_dir = None 1397 alt_dir = None
1404 else: 1398 else:
@@ -2893,14 +2887,11 @@ class Project(object):
2893 else: 2887 else:
2894 path = os.path.join(self._project.worktree, '.git', HEAD) 2888 path = os.path.join(self._project.worktree, '.git', HEAD)
2895 try: 2889 try:
2896 fd = open(path) 2890 with open(path) as fd:
2891 line = fd.readline()
2897 except IOError as e: 2892 except IOError as e:
2898 raise NoManifestException(path, str(e)) 2893 raise NoManifestException(path, str(e))
2899 try: 2894 try:
2900 line = fd.readline()
2901 finally:
2902 fd.close()
2903 try:
2904 line = line.decode() 2895 line = line.decode()
2905 except AttributeError: 2896 except AttributeError:
2906 pass 2897 pass
diff --git a/repo b/repo
index 2f634d9e..90cdb60d 100755
--- a/repo
+++ b/repo
@@ -513,9 +513,8 @@ def SetupGnuPG(quiet):
513 sys.exit(1) 513 sys.exit(1)
514 print() 514 print()
515 515
516 fd = open(os.path.join(home_dot_repo, 'keyring-version'), 'w') 516 with open(os.path.join(home_dot_repo, 'keyring-version'), 'w') as fd:
517 fd.write('.'.join(map(str, KEYRING_VERSION)) + '\n') 517 fd.write('.'.join(map(str, KEYRING_VERSION)) + '\n')
518 fd.close()
519 return True 518 return True
520 519
521 520
diff --git a/subcmds/manifest.py b/subcmds/manifest.py
index 768f072e..9c1b3f0c 100644
--- a/subcmds/manifest.py
+++ b/subcmds/manifest.py
@@ -40,10 +40,9 @@ in a Git repository for use during future 'repo init' invocations.
40 helptext = self._helpDescription + '\n' 40 helptext = self._helpDescription + '\n'
41 r = os.path.dirname(__file__) 41 r = os.path.dirname(__file__)
42 r = os.path.dirname(r) 42 r = os.path.dirname(r)
43 fd = open(os.path.join(r, 'docs', 'manifest-format.md')) 43 with open(os.path.join(r, 'docs', 'manifest-format.md')) as fd:
44 for line in fd: 44 for line in fd:
45 helptext += line 45 helptext += line
46 fd.close()
47 return helptext 46 return helptext
48 47
49 def _Options(self, p): 48 def _Options(self, p):
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)