| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 13d399f commit a79cf67
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -210,11 +210,13 @@ def __hash__(self): | |||
| 210 | 210 | # Description property | |
| 211 | 211 | def _get_description(self): | |
| 212 | 212 | filename = join(self.git_dir, 'description') | |
| 213 | - return open(filename, 'rb').read().rstrip().decode(defenc) | ||
| 213 | + with open(filename, 'rb') as fp: | ||
| 214 | + return fp.read().rstrip().decode(defenc) | ||
| 214 | 215 | ||
| 215 | 216 | def _set_description(self, descr): | |
| 216 | 217 | filename = join(self.git_dir, 'description') | |
| 217 | - open(filename, 'wb').write((descr + '\n').encode(defenc)) | ||
| 218 | + with open(filename, 'wb') as fp: | ||
| 219 | + fp.write((descr + '\n').encode(defenc)) | ||
| 218 | 220 | ||
| 219 | 221 | description = property(_get_description, _set_description, | |
| 220 | 222 | doc="the project's description") | |
@@ -548,11 +550,8 @@ def _get_alternates(self): | |||
| 548 | 550 | alternates_path = join(self.git_dir, 'objects', 'info', 'alternates') | |
| 549 | 551 | ||
| 550 | 552 | if os.path.exists(alternates_path): | |
| 551 | - try: | ||
| 552 | - f = open(alternates_path, 'rb') | ||
| 553 | + with open(alternates_path, 'rb') as f: | ||
| 553 | 554 | alts = f.read().decode(defenc) | |
| 554 | - finally: | ||
| 555 | - f.close() | ||
| 556 | 555 | return alts.strip().splitlines() | |
| 557 | 556 | else: | |
| 558 | 557 | return list() | |
@@ -573,13 +572,8 @@ def _set_alternates(self, alts): | |||
| 573 | 572 | if isfile(alternates_path): | |
| 574 | 573 | os.remove(alternates_path) | |
| 575 | 574 | else: | |
| 576 | - try: | ||
| 577 | - f = open(alternates_path, 'wb') | ||
| 575 | + with open(alternates_path, 'wb') as f: | ||
| 578 | 576 | f.write("\n".join(alts).encode(defenc)) | |
| 579 | - finally: | ||
| 580 | - f.close() | ||
| 581 | - # END file handling | ||
| 582 | - # END alts handling | ||
| 583 | 577 | ||
| 584 | 578 | alternates = property(_get_alternates, _set_alternates, | |
| 585 | 579 | doc="Retrieve a list of alternates paths or set a list paths to be used as alternates") | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,8 +25,8 @@ | |||
| 25 | 25 | ||
| 26 | 26 | ||
| 27 | 27 | def touch(filename): | |
| 28 | - fp = open(filename, "ab") | ||
| 29 | - fp.close() | ||
| 28 | + with open(filename, "ab"): | ||
| 29 | + pass | ||
| 30 | 30 | return filename | |
| 31 | 31 | ||
| 32 | 32 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,8 +11,7 @@ | |||
| 11 | 11 | from git import ( | |
| 12 | 12 | GitConfigParser | |
| 13 | 13 | ) | |
| 14 | - from git.compat import ( | ||
| 15 | - string_types) | ||
| 14 | + from git.compat import string_types | ||
| 16 | 15 | from git.config import cp | |
| 17 | 16 | from git.test.lib import ( | |
| 18 | 17 | TestCase, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,18 +4,14 @@ | |||
| 4 | 4 | # | |
| 5 | 5 | # This module is part of GitPython and is released under | |
| 6 | 6 | # the BSD License: http://www.opensource.org/licenses/bsd-license.php | |
| 7 | + import glob | ||
| 8 | + from io import BytesIO | ||
| 9 | + import itertools | ||
| 10 | + import os | ||
| 7 | 11 | import pickle | |
| 12 | + import sys | ||
| 13 | + import tempfile | ||
| 8 | 14 | ||
| 9 | - from git.test.lib import ( | ||
| 10 | - patch, | ||
| 11 | - TestBase, | ||
| 12 | - with_rw_repo, | ||
| 13 | - fixture, | ||
| 14 | - assert_false, | ||
| 15 | - assert_equal, | ||
| 16 | - assert_true, | ||
| 17 | - raises | ||
| 18 | - ) | ||
| 19 | 15 | from git import ( | |
| 20 | 16 | InvalidGitRepositoryError, | |
| 21 | 17 | Repo, | |
@@ -33,23 +29,28 @@ | |||
| 33 | 29 | BadName, | |
| 34 | 30 | GitCommandError | |
| 35 | 31 | ) | |
| 36 | - from git.repo.fun import touch | ||
| 37 | - from git.util import join_path_native, rmtree | ||
| 32 | + from git.compat import string_types | ||
| 38 | 33 | from git.exc import ( | |
| 39 | 34 | BadObject, | |
| 40 | 35 | ) | |
| 41 | - from gitdb.util import bin_to_hex | ||
| 42 | - from git.compat import string_types | ||
| 36 | + from git.repo.fun import touch | ||
| 37 | + from git.test.lib import ( | ||
| 38 | + patch, | ||
| 39 | + TestBase, | ||
| 40 | + with_rw_repo, | ||
| 41 | + fixture, | ||
| 42 | + assert_false, | ||
| 43 | + assert_equal, | ||
| 44 | + assert_true, | ||
| 45 | + raises | ||
| 46 | + ) | ||
| 43 | 47 | from git.test.lib import with_rw_directory | |
| 44 | - | ||
| 45 | - import os | ||
| 46 | - import sys | ||
| 47 | - import tempfile | ||
| 48 | - import itertools | ||
| 49 | - from io import BytesIO | ||
| 50 | - | ||
| 48 | + from git.util import join_path_native, rmtree, rmfile | ||
| 49 | + from gitdb.util import bin_to_hex | ||
| 51 | 50 | from nose import SkipTest | |
| 52 | 51 | ||
| 52 | + import os.path as osp | ||
| 53 | + | ||
| 53 | 54 | ||
| 54 | 55 | def iter_flatten(lol): | |
| 55 | 56 | for items in lol: | |
@@ -61,9 +62,23 @@ def flatten(lol): | |||
| 61 | 62 | return list(iter_flatten(lol)) | |
| 62 | 63 | ||
| 63 | 64 | ||
| 65 | + _tc_lock_fpaths = osp.join(osp.dirname(__file__), '../../.git/*.lock') | ||
| 66 | + | ||
| 67 | + | ||
| 68 | + def _rm_lock_files(): | ||
| 69 | + for lfp in glob.glob(_tc_lock_fpaths): | ||
| 70 | + rmfile(lfp) | ||
| 71 | + | ||
| 72 | + | ||
| 64 | 73 | class TestRepo(TestBase): | |
| 65 | 74 | ||
| 75 | + def setUp(self): | ||
| 76 | + _rm_lock_files() | ||
| 77 | + | ||
| 66 | 78 | def tearDown(self): | |
| 79 | + for lfp in glob.glob(_tc_lock_fpaths): | ||
| 80 | + if osp.isfile(lfp): | ||
| 81 | + raise AssertionError('Previous TC left hanging git-lock file: %s', lfp) | ||
| 67 | 82 | import gc | |
| 68 | 83 | gc.collect() | |
| 69 | 84 | ||
@@ -309,10 +324,9 @@ def test_tag(self): | |||
| 309 | 324 | ||
| 310 | 325 | def test_archive(self): | |
| 311 | 326 | tmpfile = tempfile.mktemp(suffix='archive-test') | |
| 312 | - stream = open(tmpfile, 'wb') | ||
| 313 | - self.rorepo.archive(stream, '0.1.6', path='doc') | ||
| 314 | - assert stream.tell() | ||
| 315 | - stream.close() | ||
| 327 | + with open(tmpfile, 'wb') as stream: | ||
| 328 | + self.rorepo.archive(stream, '0.1.6', path='doc') | ||
| 329 | + assert stream.tell() | ||
| 316 | 330 | os.remove(tmpfile) | |
| 317 | 331 | ||
| 318 | 332 | @patch.object(Git, '_call_process') | |
@@ -401,9 +415,8 @@ def test_untracked_files(self, rwrepo): | |||
| 401 | 415 | ||
| 402 | 416 | num_recently_untracked = 0 | |
| 403 | 417 | for fpath in files: | |
| 404 | - fd = open(fpath, "wb") | ||
| 405 | - fd.close() | ||
| 406 | - # END for each filename | ||
| 418 | + with open(fpath, "wb"): | ||
| 419 | + pass | ||
| 407 | 420 | untracked_files = rwrepo.untracked_files | |
| 408 | 421 | num_recently_untracked = len(untracked_files) | |
| 409 | 422 | ||
@@ -426,19 +439,16 @@ def test_config_reader(self): | |||
| 426 | 439 | def test_config_writer(self): | |
| 427 | 440 | for config_level in self.rorepo.config_level: | |
| 428 | 441 | try: | |
| 429 | - writer = self.rorepo.config_writer(config_level) | ||
| 430 | - assert not writer.read_only | ||
| 431 | - writer.release() | ||
| 442 | + with self.rorepo.config_writer(config_level) as writer: | ||
| 443 | + self.assertFalse(writer.read_only) | ||
| 432 | 444 | except IOError: | |
| 433 | 445 | # its okay not to get a writer for some configuration files if we | |
| 434 | 446 | # have no permissions | |
| 435 | 447 | pass | |
| 436 | - # END for each config level | ||
| 437 | 448 | ||
| 438 | 449 | def test_config_level_paths(self): | |
| 439 | 450 | for config_level in self.rorepo.config_level: | |
| 440 | 451 | assert self.rorepo._get_config_path(config_level) | |
| 441 | - # end for each config level | ||
| 442 | 452 | ||
| 443 | 453 | def test_creation_deletion(self): | |
| 444 | 454 | # just a very quick test to assure it generally works. There are | |
@@ -448,8 +458,8 @@ def test_creation_deletion(self): | |||
| 448 | 458 | ||
| 449 | 459 | tag = self.rorepo.create_tag("new_tag", "HEAD~2") | |
| 450 | 460 | self.rorepo.delete_tag(tag) | |
| 451 | - writer = self.rorepo.config_writer() | ||
| 452 | - writer.release() | ||
| 461 | + with self.rorepo.config_writer(): | ||
| 462 | + pass | ||
| 453 | 463 | remote = self.rorepo.create_remote("new_remote", "git@server:repo.git") | |
| 454 | 464 | self.rorepo.delete_remote(remote) | |
| 455 | 465 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -574,7 +574,10 @@ def _obtain_lock_or_raise(self): | |||
| 574 | 574 | (self._file_path, lock_file)) | |
| 575 | 575 | ||
| 576 | 576 | try: | |
| 577 | - fd = os.open(lock_file, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0) | ||
| 577 | + flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL | ||
| 578 | + if is_win: | ||
| 579 | + flags |= getattr(os, 'O_SHORT_LIVED') | ||
| 580 | + fd = os.open(lock_file, flags, 0) | ||
| 578 | 581 | os.close(fd) | |
| 579 | 582 | except OSError as e: | |
| 580 | 583 | raise IOError(str(e)) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments