Add external command invocations to debugging output.
[update-copyright.git] / update_copyright / vcs / utils.py
index f7f0b407d9130d125bf69373e274f7fee7b2e28c..024568eae143851d294b4a47f65eb1e90638d04f 100644 (file)
@@ -23,6 +23,7 @@ import os.path as _os_path
 import subprocess as _subprocess
 import sys as _sys
 
+from .. import LOG as LOG
 from ..utils import ENCODING as _ENCODING
 
 
@@ -31,7 +32,7 @@ _POSIX = not _MSWINDOWS
 
 
 def invoke(args, stdin=None, stdout=_subprocess.PIPE, stderr=_subprocess.PIPE,
-           expect=(0,), unicode_output=False, encoding=None):
+           cwd=None, expect=(0,), unicode_output=False, encoding=None):
     """Invoke an external program and return the results
 
     ``expect`` should be a tuple of allowed exit codes.
@@ -39,15 +40,18 @@ def invoke(args, stdin=None, stdout=_subprocess.PIPE, stderr=_subprocess.PIPE,
     When ``unicode_output`` is ``True``, convert stdout and stdin
     strings to unicode before returing them.
     """
+    LOG.debug('{}$ {}'.format(cwd, args))
     try :
         if _POSIX:
             q = _subprocess.Popen(args, stdin=_subprocess.PIPE,
-                                  stdout=stdout, stderr=stderr)
+                                  stdout=stdout, stderr=stderr,
+                                  close_fds=True, cwd=cwd)
         else:
             assert _MSWINDOWS == True, 'invalid platform'
             # win32 don't have os.execvp() so run the command in a shell
             q = _subprocess.Popen(args, stdin=_subprocess.PIPE,
-                                  stdout=stdout, stderr=stderr, shell=True)
+                                  stdout=stdout, stderr=stderr, shell=True,
+                                  cwd=cwd)
     except OSError, e:
         raise ValueError([args, e])
     stdout,stderr = q.communicate(input=stdin)
@@ -80,7 +84,7 @@ def splitpath(path):
     while True:
         dirname,basename = _os_path.split(path)
         elements.insert(0,basename)
-        if dirname in ['', '.']:
+        if dirname in ['/', '', '.']:
             break
         path = dirname
     return tuple(elements)