42
42
)
43
43
44
44
45
- execute_kwargs = set (('istream' , 'with_keep_cwd' , ' with_extended_output' ,
45
+ execute_kwargs = set (('istream' , 'with_extended_output' ,
46
46
'with_exceptions' , 'as_process' , 'stdout_as_string' ,
47
47
'output_stream' , 'with_stdout' , 'kill_after_timeout' ,
48
48
'universal_newlines' , 'shell' ))
49
49
50
- log = logging .getLogger ('git.cmd' )
50
+ log = logging .getLogger (__name__ )
51
51
log .addHandler (logging .NullHandler ())
52
52
53
53
__all__ = ('Git' ,)
59
59
# Documentation
60
60
## @{
61
61
62
- def handle_process_output (process , stdout_handler , stderr_handler , finalizer , decode_streams = True ):
62
+ def handle_process_output (process , stdout_handler , stderr_handler ,
63
+ finalizer = None , decode_streams = True ):
63
64
"""Registers for notifications to lean that process output is ready to read, and dispatches lines to
64
65
the respective line handlers.
65
66
This function returns once the finalizer returns
@@ -108,10 +109,13 @@ def pump_stream(cmdline, name, stream, is_decode, handler):
108
109
t .start ()
109
110
threads .append (t )
110
111
112
+ ## FIXME: Why Join?? Will block if `stdin` needs feeding...
113
+ #
111
114
for t in threads :
112
115
t .join ()
113
116
114
- return finalizer (process )
117
+ if finalizer :
118
+ return finalizer (process )
115
119
116
120
117
121
def dashify (string ):
@@ -186,14 +190,18 @@ def __setstate__(self, d):
186
190
# Override this value using `Git.USE_SHELL = True`
187
191
USE_SHELL = False
188
192
189
- class AutoInterrupt (object ):
193
+ @classmethod
194
+ def polish_url (cls , url ):
195
+ return url .replace ("\\ \\ " , "\\ " ).replace ("\\ " , "/" )
190
196
197
+ class AutoInterrupt (object ):
191
198
"""Kill/Interrupt the stored process instance once this instance goes out of scope. It is
192
199
used to prevent processes piling up in case iterators stop reading.
193
200
Besides all attributes are wired through to the contained process object.
194
201
195
202
The wait method was overridden to perform automatic status code checking
196
203
and possibly raise."""
204
+
197
205
__slots__ = ("proc" , "args" )
198
206
199
207
def __init__ (self , proc , args ):
@@ -239,7 +247,7 @@ def __del__(self):
239
247
def __getattr__ (self , attr ):
240
248
return getattr (self .proc , attr )
241
249
242
- def wait (self , stderr = b'' ):
250
+ def wait (self , stderr = b'' ): # TODO: Bad choice to mimic `proc.wait()` but with different args.
243
251
"""Wait for the process and return its status code.
244
252
245
253
:param stderr: Previously read value of stderr, in case stderr is already closed.
@@ -418,7 +426,6 @@ def version_info(self):
418
426
419
427
def execute (self , command ,
420
428
istream = None ,
421
- with_keep_cwd = False ,
422
429
with_extended_output = False ,
423
430
with_exceptions = True ,
424
431
as_process = False ,
@@ -441,11 +448,6 @@ def execute(self, command,
441
448
:param istream:
442
449
Standard input filehandle passed to subprocess.Popen.
443
450
444
- :param with_keep_cwd:
445
- Whether to use the current working directory from os.getcwd().
446
- The cmd otherwise uses its own working_dir that it has been initialized
447
- with if possible.
448
-
449
451
:param with_extended_output:
450
452
Whether to return a (status, stdout, stderr) tuple.
451
453
@@ -518,10 +520,7 @@ def execute(self, command,
518
520
log .info (' ' .join (command ))
519
521
520
522
# Allow the user to have the command executed in their working dir.
521
- if with_keep_cwd or self ._working_dir is None :
522
- cwd = os .getcwd ()
523
- else :
524
- cwd = self ._working_dir
523
+ cwd = self ._working_dir or os .getcwd ()
525
524
526
525
# Start the process
527
526
env = os .environ .copy ()
@@ -544,6 +543,9 @@ def execute(self, command,
544
543
cmd_not_found_exception = OSError
545
544
# end handle
546
545
546
+ stdout_sink = (PIPE
547
+ if with_stdout
548
+ else getattr (subprocess , 'DEVNULL' , open (os .devnull , 'wb' )))
547
549
log .debug ("Popen(%s, cwd=%s, universal_newlines=%s, shell=%s)" ,
548
550
command , cwd , universal_newlines , shell )
549
551
try :
@@ -553,9 +555,9 @@ def execute(self, command,
553
555
bufsize = - 1 ,
554
556
stdin = istream ,
555
557
stderr = PIPE ,
556
- stdout = PIPE if with_stdout else open ( os . devnull , 'wb' ) ,
558
+ stdout = stdout_sink ,
557
559
shell = shell is not None and shell or self .USE_SHELL ,
558
- close_fds = ( is_posix ) , # unsupported on windows
560
+ close_fds = is_posix , # unsupported on windows
559
561
universal_newlines = universal_newlines ,
560
562
creationflags = PROC_CREATIONFLAGS ,
561
563
** subprocess_kwargs
@@ -647,10 +649,7 @@ def as_text(stdout_value):
647
649
# END handle debug printing
648
650
649
651
if with_exceptions and status != 0 :
650
- if with_extended_output :
651
- raise GitCommandError (command , status , stderr_value , stdout_value )
652
- else :
653
- raise GitCommandError (command , status , stderr_value )
652
+ raise GitCommandError (command , status , stderr_value , stdout_value )
654
653
655
654
if isinstance (stdout_value , bytes ) and stdout_as_string : # could also be output_stream
656
655
stdout_value = safe_decode (stdout_value )
0 commit comments