Skip to content

Commit f495e94

Browse files
committed
src, #519: collect all is_<platform>() calls
1 parent 29eb301 commit f495e94

File tree

14 files changed

+66
-47
lines changed

14 files changed

+66
-47
lines changed

git/cmd.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
# just to satisfy flake8 on py3
4141
unicode,
4242
safe_decode,
43+
is_posix,
44+
is_win,
4345
)
4446

4547
execute_kwargs = ('istream', 'with_keep_cwd', 'with_extended_output',
@@ -50,9 +52,9 @@
5052
log = logging.getLogger('git.cmd')
5153
log.addHandler(logging.NullHandler())
5254

53-
__all__ = ('Git', )
55+
__all__ = ('Git',)
5456

55-
if sys.platform != 'win32':
57+
if is_win():
5658
WindowsError = OSError
5759

5860
if PY3:
@@ -236,7 +238,7 @@ def dict_to_slots_and__excluded_are_none(self, d, excluded=()):
236238
## CREATE_NEW_PROCESS_GROUP is needed to allow killing it afterwards,
237239
# seehttps://docs.python.org/3/library/subprocess.html#subprocess.Popen.send_signal
238240
PROC_CREATIONFLAGS = (CREATE_NO_WINDOW | subprocess.CREATE_NEW_PROCESS_GROUP
239-
if sys.platform == 'win32'
241+
if is_win()
240242
else 0)
241243

242244

@@ -628,7 +630,7 @@ def execute(self, command,
628630
env["LC_ALL"] = "C"
629631
env.update(self._environment)
630632

631-
if sys.platform == 'win32':
633+
if is_win():
632634
cmd_not_found_exception = WindowsError
633635
if kill_after_timeout:
634636
raise GitCommandError('"kill_after_timeout" feature is not supported on Windows.')
@@ -648,7 +650,7 @@ def execute(self, command,
648650
stderr=PIPE,
649651
stdout=PIPE if with_stdout else open(os.devnull, 'wb'),
650652
shell=self.USE_SHELL,
651-
close_fds=(os.name == 'posix'), # unsupported on windows
653+
close_fds=(is_posix()), # unsupported on windows
652654
universal_newlines=universal_newlines,
653655
creationflags=PROC_CREATIONFLAGS,
654656
**subprocess_kwargs
@@ -688,7 +690,7 @@ def _kill_process(pid):
688690

689691
if kill_after_timeout:
690692
kill_check = threading.Event()
691-
watchdog = threading.Timer(kill_after_timeout, _kill_process, args=(proc.pid, ))
693+
watchdog = threading.Timer(kill_after_timeout, _kill_process, args=(proc.pid,))
692694

693695
# Wait for the process to return
694696
status = 0
@@ -932,7 +934,7 @@ def make_call():
932934
return call
933935
# END utility to recreate call after changes
934936

935-
if sys.platform == 'win32':
937+
if is_win():
936938
try:
937939
try:
938940
return self.execute(make_call(), **_kwargs)

git/compat.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""utilities to help provide compatibility with python 3"""
88
# flake8: noqa
99

10+
import os
1011
import sys
1112

1213
from gitdb.utils.compat import (
@@ -79,3 +80,16 @@ def __new__(cls, name, nbases, d):
7980
# end metaclass
8081
return metaclass(meta.__name__ + 'Helper', None, {})
8182
# end handle py2
83+
84+
85+
def is_win():
86+
return os.name == 'nt'
87+
88+
89+
def is_posix():
90+
return os.name == 'posix'
91+
92+
93+
def is_darwin():
94+
return os.name == 'darwin'
95+

git/index/base.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
string_types,
4747
force_bytes,
4848
defenc,
49-
mviter
49+
mviter,
50+
is_win
5051
)
5152

5253
from git.util import (
@@ -136,7 +137,7 @@ def _set_cache_(self, attr):
136137
# which happens during read-tree.
137138
# In this case, we will just read the memory in directly.
138139
# Its insanely bad ... I am disappointed !
139-
allow_mmap = (os.name != 'nt' or sys.version_info[1] > 5)
140+
allow_mmap = (is_win() or sys.version_info[1] > 5)
140141
stream = file_contents_ro(fd, stream=True, allow_mmap=allow_mmap)
141142

142143
try:
@@ -1059,7 +1060,7 @@ def handle_stderr(proc, iter_checked_out_files):
10591060
# END for each possible ending
10601061
# END for each line
10611062
if unknown_lines:
1062-
raise GitCommandError(("git-checkout-index", ), 128, stderr)
1063+
raise GitCommandError(("git-checkout-index",), 128, stderr)
10631064
if failed_files:
10641065
valid_files = list(set(iter_checked_out_files) - set(failed_files))
10651066
raise CheckoutError(

git/index/fun.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
from git.compat import (
4444
defenc,
4545
force_text,
46-
force_bytes
46+
force_bytes,
47+
is_posix,
4748
)
4849

4950
S_IFGITLINK = S_IFLNK | S_IFDIR # a submodule
@@ -75,7 +76,7 @@ def run_commit_hook(name, index):
7576
stdout=subprocess.PIPE,
7677
stderr=subprocess.PIPE,
7778
cwd=index.repo.working_dir,
78-
close_fds=(os.name == 'posix'),
79+
close_fds=(is_posix()),
7980
universal_newlines=True,
8081
creationflags=PROC_CREATIONFLAGS,)
8182
stdout, stderr = cmd.communicate()

git/index/util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import struct
33
import tempfile
44
import os
5+
from git.compat import is_win
56

67
__all__ = ('TemporaryFileSwap', 'post_clear_cache', 'default_index', 'git_working_dir')
78

@@ -29,7 +30,7 @@ def __init__(self, file_path):
2930

3031
def __del__(self):
3132
if os.path.isfile(self.tmp_file_path):
32-
if os.name == 'nt' and os.path.exists(self.file_path):
33+
if is_win and os.path.exists(self.file_path):
3334
os.remove(self.file_path)
3435
os.rename(self.tmp_file_path, self.file_path)
3536
# END temp file exists

git/remote.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
# Module implementing a remote object allowing easy access to git remotes
88
import re
9-
import os
109

1110
from .config import (
1211
SectionConstraint,
@@ -32,7 +31,7 @@
3231
)
3332
from git.cmd import handle_process_output
3433
from gitdb.util import join
35-
from git.compat import (defenc, force_text)
34+
from git.compat import (defenc, force_text, is_win)
3635
import logging
3736

3837
log = logging.getLogger('git.remote')
@@ -113,7 +112,7 @@ def __init__(self, flags, local_ref, remote_ref_string, remote, old_commit=None,
113112
self._remote = remote
114113
self._old_commit_sha = old_commit
115114
self.summary = summary
116-
115+
117116
@property
118117
def old_commit(self):
119118
return self._old_commit_sha and self._remote.repo.commit(self._old_commit_sha) or None
@@ -377,7 +376,7 @@ def __init__(self, repo, name):
377376
self.repo = repo
378377
self.name = name
379378

380-
if os.name == 'nt':
379+
if is_win():
381380
# some oddity: on windows, python 2.5, it for some reason does not realize
382381
# that it has the config_writer property, but instead calls __getattr__
383382
# which will not yield the expected results. 'pinging' the members
@@ -635,7 +634,7 @@ def _get_fetch_info_from_stderr(self, proc, progress):
635634
# end
636635
if progress.error_lines():
637636
stderr_text = '\n'.join(progress.error_lines())
638-
637+
639638
finalize_process(proc, stderr=stderr_text)
640639

641640
# read head information
@@ -657,7 +656,7 @@ def _get_fetch_info_from_stderr(self, proc, progress):
657656
fetch_info_lines = fetch_info_lines[:l_fhi]
658657
# end truncate correct list
659658
# end sanity check + sanitization
660-
659+
661660
output.extend(FetchInfo._from_line(self.repo, err_line, fetch_line)
662661
for err_line, fetch_line in zip(fetch_info_lines, fetch_head_info))
663662
return output
@@ -769,17 +768,17 @@ def push(self, refspec=None, progress=None, **kwargs):
769768
:param refspec: see 'fetch' method
770769
:param progress:
771770
Can take one of many value types:
772-
771+
773772
* None to discard progress information
774773
* A function (callable) that is called with the progress infomation.
775-
774+
776775
Signature: ``progress(op_code, cur_count, max_count=None, message='')``.
777-
776+
778777
`Click here <http://goo.gl/NPa7st>`_ for a description of all arguments
779778
given to the function.
780779
* An instance of a class derived from ``git.RemoteProgress`` that
781780
overrides the ``update()`` function.
782-
781+
783782
:note: No further progress information is returned after push returns.
784783
:param kwargs: Additional arguments to be passed to git-push
785784
:return:

git/repo/base.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
PY3,
5757
safe_decode,
5858
range,
59+
is_win,
5960
)
6061

6162
import os
@@ -71,7 +72,7 @@
7172
BlameEntry = namedtuple('BlameEntry', ['commit', 'linenos', 'orig_path', 'orig_linenos'])
7273

7374

74-
__all__ = ('Repo', )
75+
__all__ = ('Repo',)
7576

7677

7778
def _expand_path(p):
@@ -369,7 +370,7 @@ def delete_remote(self, remote):
369370
def _get_config_path(self, config_level):
370371
# we do not support an absolute path of the gitconfig on windows ,
371372
# use the global config instead
372-
if sys.platform == "win32" and config_level == "system":
373+
if is_win() and config_level == "system":
373374
config_level = "global"
374375

375376
if config_level == "system":
@@ -883,7 +884,7 @@ def _clone(cls, git, url, path, odb_default_type, progress, **kwargs):
883884
prev_cwd = None
884885
prev_path = None
885886
odbt = kwargs.pop('odbt', odb_default_type)
886-
if os.name == 'nt':
887+
if is_win():
887888
if '~' in path:
888889
raise OSError("Git cannot handle the ~ character in path %r correctly" % path)
889890

git/test/lib/helper.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import logging
1414

1515
from git import Repo, Remote, GitCommandError, Git
16-
from git.compat import string_types
16+
from git.compat import string_types, is_win
1717

1818
osp = os.path.dirname
1919

@@ -73,7 +73,7 @@ def _mktemp(*args):
7373
prefixing /private/ will lead to incorrect paths on OSX."""
7474
tdir = tempfile.mktemp(*args)
7575
# See :note: above to learn why this is comented out.
76-
# if sys.platform == 'darwin':
76+
# if is_darwin():
7777
# tdir = '/private' + tdir
7878
return tdir
7979

@@ -83,7 +83,7 @@ def _rmtree_onerror(osremove, fullpath, exec_info):
8383
Handle the case on windows that read-only files cannot be deleted by
8484
os.remove by setting it to mode 777, then retry deletion.
8585
"""
86-
if os.name != 'nt' or osremove is not os.remove:
86+
if is_win() or osremove is not os.remove:
8787
raise
8888

8989
os.chmod(fullpath, 0o777)
@@ -221,7 +221,7 @@ def remote_repo_creator(self):
221221
if gd is not None:
222222
gd.proc.terminate()
223223
log.warning('git-ls-remote failed due to: %s(%s)', type(e), e)
224-
if os.name == 'nt':
224+
if is_win():
225225
msg = "git-daemon needs to run this test, but windows does not have one. "
226226
msg += 'Otherwise, run: git-daemon "%s"' % temp_dir
227227
raise AssertionError(msg)

git/test/test_base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
)
2525
from git.objects.util import get_object_type_by_name
2626
from gitdb.util import hex_to_bin
27+
from git.compat import is_win
2728

2829

2930
class TestBase(TestBase):
@@ -117,7 +118,7 @@ def test_with_rw_remote_and_rw_repo(self, rw_repo, rw_remote_repo):
117118
assert rw_remote_repo.config_reader("repository").getboolean("core", "bare")
118119
assert os.path.isdir(os.path.join(rw_repo.working_tree_dir, 'lib'))
119120

120-
@skipIf(sys.version_info < (3, ) and os.name == 'nt',
121+
@skipIf(sys.version_info < (3,) and is_win(),
121122
"Unicode woes, see https://github.com/gitpython-developers/GitPython/pull/519")
122123
@with_rw_repo('0.1.6')
123124
def test_add_unicode(self, rw_repo):
@@ -134,7 +135,7 @@ def test_add_unicode(self, rw_repo):
134135

135136
open(file_path, "wb").write(b'something')
136137

137-
if os.name == 'nt':
138+
if is_win():
138139
# on windows, there is no way this works, see images on
139140
# https://github.com/gitpython-developers/GitPython/issues/147#issuecomment-68881897
140141
# Therefore, it must be added using the python implementation

git/test/test_git.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
)
2727
from gitdb.test.lib import with_rw_directory
2828

29-
from git.compat import PY3
29+
from git.compat import PY3, is_darwin
3030

3131
try:
3232
from unittest import mock
@@ -214,7 +214,7 @@ def test_environment(self, rw_dir):
214214
try:
215215
remote.fetch()
216216
except GitCommandError as err:
217-
if sys.version_info[0] < 3 and sys.platform == 'darwin':
217+
if sys.version_info[0] < 3 and is_darwin():
218218
assert 'ssh-origin' in str(err)
219219
assert err.status == 128
220220
else:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy