| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -50,7 +50,8 @@ | |||
| 50 | 50 | # built documents. | |
| 51 | 51 | # | |
| 52 | 52 | # The short X.Y version. | |
| 53 | - VERSION = open(os.path.join(os.path.dirname(__file__),"..", "..", 'VERSION')).readline().strip() | ||
| 53 | + with open(os.path.join(os.path.dirname(__file__),"..", "..", 'VERSION')) as fd: | ||
| 54 | + VERSION = fd.readline().strip() | ||
| 54 | 55 | version = VERSION | |
| 55 | 56 | # The full version, including alpha/beta/rc tags. | |
| 56 | 57 | release = VERSION | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -854,6 +854,8 @@ def remove(self, module=True, force=False, configuration=True, dry_run=False): | |||
| 854 | 854 | self._clear_cache() | |
| 855 | 855 | wtd = mod.working_tree_dir | |
| 856 | 856 | del(mod) # release file-handles (windows) | |
| 857 | + import gc | ||
| 858 | + gc.collect() | ||
| 857 | 859 | rmtree(wtd) | |
| 858 | 860 | # END delete tree if possible | |
| 859 | 861 | # END handle force | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -134,9 +134,8 @@ def _get_ref_info(cls, repo, ref_path): | |||
| 134 | 134 | point to, or None""" | |
| 135 | 135 | tokens = None | |
| 136 | 136 | try: | |
| 137 | - fp = open(join(repo.git_dir, ref_path), 'rt') | ||
| 138 | - value = fp.read().rstrip() | ||
| 139 | - fp.close() | ||
| 137 | + with open(join(repo.git_dir, ref_path), 'rt') as fp: | ||
| 138 | + value = fp.read().rstrip() | ||
| 140 | 139 | # Don't only split on spaces, but on whitespace, which allows to parse lines like | |
| 141 | 140 | # 60b64ef992065e2600bfef6187a97f92398a9144 branch 'master' of git-server:/path/to/repo | |
| 142 | 141 | tokens = value.split() | |
@@ -313,13 +312,17 @@ def set_reference(self, ref, logmsg=None): | |||
| 313 | 312 | ||
| 314 | 313 | lfd = LockedFD(fpath) | |
| 315 | 314 | fd = lfd.open(write=True, stream=True) | |
| 316 | - fd.write(write_value.encode('ascii') + b'\n') | ||
| 317 | - lfd.commit() | ||
| 318 | - | ||
| 315 | + ok = True | ||
| 316 | + try: | ||
| 317 | + fd.write(write_value.encode('ascii') + b'\n') | ||
| 318 | + lfd.commit() | ||
| 319 | + ok = True | ||
| 320 | + finally: | ||
| 321 | + if not ok: | ||
| 322 | + lfd.rollback() | ||
| 319 | 323 | # Adjust the reflog | |
| 320 | 324 | if logmsg is not None: | |
| 321 | 325 | self.log_append(oldbinsha, logmsg) | |
| 322 | - # END handle reflog | ||
| 323 | 326 | ||
| 324 | 327 | return self | |
| 325 | 328 | ||
@@ -422,40 +425,36 @@ def delete(cls, repo, path): | |||
| 422 | 425 | # check packed refs | |
| 423 | 426 | pack_file_path = cls._get_packed_refs_path(repo) | |
| 424 | 427 | try: | |
| 425 | - reader = open(pack_file_path, 'rb') | ||
| 426 | - except (OSError, IOError): | ||
| 427 | - pass # it didnt exist at all | ||
| 428 | - else: | ||
| 429 | - new_lines = list() | ||
| 430 | - made_change = False | ||
| 431 | - dropped_last_line = False | ||
| 432 | - for line in reader: | ||
| 433 | - # keep line if it is a comment or if the ref to delete is not | ||
| 434 | - # in the line | ||
| 435 | - # If we deleted the last line and this one is a tag-reference object, | ||
| 436 | - # we drop it as well | ||
| 437 | - line = line.decode(defenc) | ||
| 438 | - if (line.startswith('#') or full_ref_path not in line) and \ | ||
| 439 | - (not dropped_last_line or dropped_last_line and not line.startswith('^')): | ||
| 440 | - new_lines.append(line) | ||
| 441 | - dropped_last_line = False | ||
| 442 | - continue | ||
| 443 | - # END skip comments and lines without our path | ||
| 444 | - | ||
| 445 | - # drop this line | ||
| 446 | - made_change = True | ||
| 447 | - dropped_last_line = True | ||
| 448 | - # END for each line in packed refs | ||
| 449 | - reader.close() | ||
| 428 | + with open(pack_file_path, 'rb') as reader: | ||
| 429 | + new_lines = list() | ||
| 430 | + made_change = False | ||
| 431 | + dropped_last_line = False | ||
| 432 | + for line in reader: | ||
| 433 | + # keep line if it is a comment or if the ref to delete is not | ||
| 434 | + # in the line | ||
| 435 | + # If we deleted the last line and this one is a tag-reference object, | ||
| 436 | + # we drop it as well | ||
| 437 | + line = line.decode(defenc) | ||
| 438 | + if (line.startswith('#') or full_ref_path not in line) and \ | ||
| 439 | + (not dropped_last_line or dropped_last_line and not line.startswith('^')): | ||
| 440 | + new_lines.append(line) | ||
| 441 | + dropped_last_line = False | ||
| 442 | + continue | ||
| 443 | + # END skip comments and lines without our path | ||
| 444 | + | ||
| 445 | + # drop this line | ||
| 446 | + made_change = True | ||
| 447 | + dropped_last_line = True | ||
| 450 | 448 | ||
| 451 | 449 | # write the new lines | |
| 452 | 450 | if made_change: | |
| 453 | 451 | # write-binary is required, otherwise windows will | |
| 454 | 452 | # open the file in text mode and change LF to CRLF ! | |
| 455 | - open(pack_file_path, 'wb').writelines(l.encode(defenc) for l in new_lines) | ||
| 456 | - # END write out file | ||
| 457 | - # END open exception handling | ||
| 458 | - # END handle deletion | ||
| 453 | + with open(pack_file_path, 'wb') as fd: | ||
| 454 | + fd.writelines(l.encode(defenc) for l in new_lines) | ||
| 455 | + | ||
| 456 | + except (OSError, IOError): | ||
| 457 | + pass # it didnt exist at all | ||
| 459 | 458 | ||
| 460 | 459 | # delete the reflog | |
| 461 | 460 | reflog_path = RefLog.path(cls(repo, full_ref_path)) | |
@@ -484,7 +483,8 @@ def _create(cls, repo, path, resolve, reference, force, logmsg=None): | |||
| 484 | 483 | target_data = target.path | |
| 485 | 484 | if not resolve: | |
| 486 | 485 | target_data = "ref: " + target_data | |
| 487 | - existing_data = open(abs_ref_path, 'rb').read().decode(defenc).strip() | ||
| 486 | + with open(abs_ref_path, 'rb') as fd: | ||
| 487 | + existing_data = fd.read().decode(defenc).strip() | ||
| 488 | 488 | if existing_data != target_data: | |
| 489 | 489 | raise OSError("Reference at %r does already exist, pointing to %r, requested was %r" % | |
| 490 | 490 | (full_ref_path, existing_data, target_data)) | |
@@ -549,7 +549,11 @@ def rename(self, new_path, force=False): | |||
| 549 | 549 | if isfile(new_abs_path): | |
| 550 | 550 | if not force: | |
| 551 | 551 | # if they point to the same file, its not an error | |
| 552 | - if open(new_abs_path, 'rb').read().strip() != open(cur_abs_path, 'rb').read().strip(): | ||
| 552 | + with open(new_abs_path, 'rb') as fd1: | ||
| 553 | + f1 = fd1.read().strip() | ||
| 554 | + with open(cur_abs_path, 'rb') as fd2: | ||
| 555 | + f2 = fd2.read().strip() | ||
| 556 | + if f1 != f2: | ||
| 553 | 557 | raise OSError("File at path %r already exists" % new_abs_path) | |
| 554 | 558 | # else: we could remove ourselves and use the otherone, but | |
| 555 | 559 | # but clarity we just continue as usual | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -638,9 +638,8 @@ def _get_fetch_info_from_stderr(self, proc, progress): | |||
| 638 | 638 | finalize_process(proc, stderr=stderr_text) | |
| 639 | 639 | ||
| 640 | 640 | # read head information | |
| 641 | - fp = open(join(self.repo.git_dir, 'FETCH_HEAD'), 'rb') | ||
| 642 | - fetch_head_info = [l.decode(defenc) for l in fp.readlines()] | ||
| 643 | - fp.close() | ||
| 641 | + with open(join(self.repo.git_dir, 'FETCH_HEAD'), 'rb') as fp: | ||
| 642 | + fetch_head_info = [l.decode(defenc) for l in fp.readlines()] | ||
| 644 | 643 | ||
| 645 | 644 | l_fil = len(fetch_info_lines) | |
| 646 | 645 | l_fhi = len(fetch_head_info) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,6 @@ | |||
| 1 | 1 | import sys | |
| 2 | 2 | ||
| 3 | - for line in open(sys.argv[1]).readlines(): | ||
| 4 | - sys.stdout.write(line) | ||
| 5 | - sys.stderr.write(line) | ||
| 3 | + with open(sys.argv[1]) as fd: | ||
| 4 | + for line in fd.readlines(): | ||
| 5 | + sys.stdout.write(line) | ||
| 6 | + sys.stderr.write(line) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -39,7 +39,8 @@ def fixture_path(name): | |||
| 39 | 39 | ||
| 40 | 40 | ||
| 41 | 41 | def fixture(name): | |
| 42 | - return open(fixture_path(name), 'rb').read() | ||
| 42 | + with open(fixture_path(name), 'rb') as fd: | ||
| 43 | + return fd.read() | ||
| 43 | 44 | ||
| 44 | 45 | ||
| 45 | 46 | def absolute_project_path(): | |
@@ -373,7 +374,6 @@ def _make_file(self, rela_path, data, repo=None): | |||
| 373 | 374 | """ | |
| 374 | 375 | repo = repo or self.rorepo | |
| 375 | 376 | abs_path = os.path.join(repo.working_tree_dir, rela_path) | |
| 376 | - fp = open(abs_path, "w") | ||
| 377 | - fp.write(data) | ||
| 378 | - fp.close() | ||
| 377 | + with open(abs_path, "w") as fp: | ||
| 378 | + fp.write(data) | ||
| 379 | 379 | return abs_path | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -77,13 +77,11 @@ def test_base_object(self): | |||
| 77 | 77 | assert data | |
| 78 | 78 | ||
| 79 | 79 | tmpfilename = tempfile.mktemp(suffix='test-stream') | |
| 80 | - tmpfile = open(tmpfilename, 'wb+') | ||
| 81 | - assert item == item.stream_data(tmpfile) | ||
| 82 | - tmpfile.seek(0) | ||
| 83 | - assert tmpfile.read() == data | ||
| 84 | - tmpfile.close() | ||
| 80 | + with open(tmpfilename, 'wb+') as tmpfile: | ||
| 81 | + assert item == item.stream_data(tmpfile) | ||
| 82 | + tmpfile.seek(0) | ||
| 83 | + assert tmpfile.read() == data | ||
| 85 | 84 | os.remove(tmpfilename) | |
| 86 | - # END stream to file directly | ||
| 87 | 85 | # END for each object type to create | |
| 88 | 86 | ||
| 89 | 87 | # each has a unique sha | |
@@ -133,7 +131,8 @@ def test_add_unicode(self, rw_repo): | |||
| 133 | 131 | from nose import SkipTest | |
| 134 | 132 | raise SkipTest("Environment doesn't support unicode filenames") | |
| 135 | 133 | ||
| 136 | - open(file_path, "wb").write(b'something') | ||
| 134 | + with open(file_path, "wb") as fp: | ||
| 135 | + fp.write(b'something') | ||
| 137 | 136 | ||
| 138 | 137 | if is_win: | |
| 139 | 138 | # on windows, there is no way this works, see images on | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -313,14 +313,16 @@ def test_serialization_unicode_support(self): | |||
| 313 | 313 | ||
| 314 | 314 | def test_invalid_commit(self): | |
| 315 | 315 | cmt = self.rorepo.commit() | |
| 316 | - cmt._deserialize(open(fixture_path('commit_invalid_data'), 'rb')) | ||
| 316 | + with open(fixture_path('commit_invalid_data'), 'rb') as fd: | ||
| 317 | + cmt._deserialize(fd) | ||
| 317 | 318 | ||
| 318 | 319 | self.assertEqual(cmt.author.name, u'E.Azer Ko�o�o�oculu', cmt.author.name) | |
| 319 | 320 | self.assertEqual(cmt.author.email, 'azer@kodfabrik.com', cmt.author.email) | |
| 320 | 321 | ||
| 321 | 322 | def test_gpgsig(self): | |
| 322 | 323 | cmt = self.rorepo.commit() | |
| 323 | - cmt._deserialize(open(fixture_path('commit_with_gpgsig'), 'rb')) | ||
| 324 | + with open(fixture_path('commit_with_gpgsig'), 'rb') as fd: | ||
| 325 | + cmt._deserialize(fd) | ||
| 324 | 326 | ||
| 325 | 327 | fixture_sig = """-----BEGIN PGP SIGNATURE----- | |
| 326 | 328 | Version: GnuPG v1.4.11 (GNU/Linux) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -53,7 +53,8 @@ def test_init_repo_object(self, rw_dir): | |||
| 53 | 53 | # ![5-test_init_repo_object] | |
| 54 | 54 | ||
| 55 | 55 | # [6-test_init_repo_object] | |
| 56 | - repo.archive(open(join(rw_dir, 'repo.tar'), 'wb')) | ||
| 56 | + with open(join(rw_dir, 'repo.tar'), 'wb') as fp: | ||
| 57 | + repo.archive(fp) | ||
| 57 | 58 | # ![6-test_init_repo_object] | |
| 58 | 59 | ||
| 59 | 60 | # repository paths | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -93,10 +93,9 @@ def test_it_executes_git_to_shell_and_returns_result(self): | |||
| 93 | 93 | ||
| 94 | 94 | def test_it_accepts_stdin(self): | |
| 95 | 95 | filename = fixture_path("cat_file_blob") | |
| 96 | - fh = open(filename, 'r') | ||
| 97 | - assert_equal("70c379b63ffa0795fdbfbc128e5a2818397b7ef8", | ||
| 98 | - self.git.hash_object(istream=fh, stdin=True)) | ||
| 99 | - fh.close() | ||
| 96 | + with open(filename, 'r') as fh: | ||
| 97 | + assert_equal("70c379b63ffa0795fdbfbc128e5a2818397b7ef8", | ||
| 98 | + self.git.hash_object(istream=fh, stdin=True)) | ||
| 100 | 99 | ||
| 101 | 100 | @patch.object(Git, 'execute') | |
| 102 | 101 | def test_it_ignores_false_kwargs(self, git): | |
@@ -200,10 +199,9 @@ def test_environment(self, rw_dir): | |||
| 200 | 199 | self.assertEqual(self.git.environment(), {}) | |
| 201 | 200 | ||
| 202 | 201 | path = os.path.join(rw_dir, 'failing-script.sh') | |
| 203 | - stream = open(path, 'wt') | ||
| 204 | - stream.write("#!/usr/bin/env sh\n" + | ||
| 205 | - "echo FOO\n") | ||
| 206 | - stream.close() | ||
| 202 | + with open(path, 'wt') as stream: | ||
| 203 | + stream.write("#!/usr/bin/env sh\n" | ||
| 204 | + "echo FOO\n") | ||
| 207 | 205 | os.chmod(path, 0o777) | |
| 208 | 206 | ||
| 209 | 207 | rw_repo = Repo.init(os.path.join(rw_dir, 'repo')) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments