[tor-commits] [stem/master] Add cwd argument to stem.util.system.call()

atagar at torproject.org atagar at torproject.org
Wed May 24 20:35:37 UTC 2017


commit dfcdc18526ee416302ea7fed780478a3fd08e0ae
Author: Damian Johnson <atagar at torproject.org>
Date:   Wed May 24 13:26:14 2017 -0700

    Add cwd argument to stem.util.system.call()
    
    While investigating #22366 I ran into rare failures with relative paths. On
    reflection this is likely due to a background thread calling chdir. We
    shouldn't change global state in the background.
    
    The subprocess accepts a cwd argument so taking advantage of that.
---
 docs/change_log.rst        |  1 +
 stem/util/system.py        |  6 +++---
 test/integ/installation.py | 35 +++++++++++++++--------------------
 3 files changed, 19 insertions(+), 23 deletions(-)

diff --git a/docs/change_log.rst b/docs/change_log.rst
index c9b96af..a097f5d 100644
--- a/docs/change_log.rst
+++ b/docs/change_log.rst
@@ -66,6 +66,7 @@ The following are only available within Stem's `git repository
  * **Utilities**
 
   * Added timeout argument to :func:`~stem.util.system.call`
+  * Added cwd argument to :func:`~stem.util.system.call`
   * Added :class:`~stem.util.test_tools.TimedTestRunner` and :func:`~stem.util.test_tools.test_runtimes`
 
  * **Interpreter**
diff --git a/stem/util/system.py b/stem/util/system.py
index 7b31516..9f85304 100644
--- a/stem/util/system.py
+++ b/stem/util/system.py
@@ -1036,7 +1036,7 @@ def files_with_suffix(base_path, suffix):
           yield os.path.join(root, filename)
 
 
-def call(command, default = UNDEFINED, ignore_exit_status = False, timeout = None, env = None):
+def call(command, default = UNDEFINED, ignore_exit_status = False, timeout = None, cwd = None, env = None):
   """
   call(command, default = UNDEFINED, ignore_exit_status = False)
 
@@ -1052,7 +1052,7 @@ def call(command, default = UNDEFINED, ignore_exit_status = False, timeout = Non
      Added env argument.
 
   .. versionchanged:: 1.6.0
-     Added timeout argument.
+     Added timeout and cwd arguments.
 
   :param str,list command: command to be issued
   :param object default: response if the query fails
@@ -1082,7 +1082,7 @@ def call(command, default = UNDEFINED, ignore_exit_status = False, timeout = Non
   try:
     is_shell_command = command_list[0] in SHELL_COMMANDS
 
-    process = subprocess.Popen(command_list, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = is_shell_command, env = env)
+    process = subprocess.Popen(command_list, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = is_shell_command, cwd = cwd, env = env)
 
     if timeout:
       while process.poll() is None:
diff --git a/test/integ/installation.py b/test/integ/installation.py
index 80a405d..dd4eb88 100644
--- a/test/integ/installation.py
+++ b/test/integ/installation.py
@@ -33,31 +33,26 @@ def setup():
 
   def _setup():
     global INSTALL_FAILURE, INSTALL_PATH, SDIST_FAILURE
-    original_cwd = os.getcwd()
 
     try:
-      try:
-        os.chdir(test.STEM_BASE)
-        stem.util.system.call('%s setup.py install --prefix %s' % (PYTHON_EXE, BASE_INSTALL_PATH), timeout = 60)
-        stem.util.system.call('%s setup.py clean --all' % PYTHON_EXE, timeout = 60)  # tidy up the build directory
-        site_packages_paths = glob.glob('%s/lib*/*/site-packages' % BASE_INSTALL_PATH)
+      stem.util.system.call('%s setup.py install --prefix %s' % (PYTHON_EXE, BASE_INSTALL_PATH), timeout = 60, cwd = test.STEM_BASE)
+      stem.util.system.call('%s setup.py clean --all' % PYTHON_EXE, timeout = 60, cwd = test.STEM_BASE)  # tidy up the build directory
+      site_packages_paths = glob.glob('%s/lib*/*/site-packages' % BASE_INSTALL_PATH)
+
+      if len(site_packages_paths) != 1:
+        raise AssertionError('We should only have a single site-packages directory, but instead had: %s' % site_packages_paths)
 
-        if len(site_packages_paths) != 1:
-          raise AssertionError('We should only have a single site-packages directory, but instead had: %s' % site_packages_paths)
+      INSTALL_PATH = site_packages_paths[0]
+    except Exception as exc:
+      INSTALL_FAILURE = AssertionError("Unable to install with 'python setup.py install': %s" % exc)
 
-        INSTALL_PATH = site_packages_paths[0]
+    if not os.path.exists(DIST_PATH):
+      try:
+        stem.util.system.call('%s setup.py sdist' % PYTHON_EXE, timeout = 60, cwd = test.STEM_BASE)
       except Exception as exc:
-        INSTALL_FAILURE = AssertionError("Unable to install with 'python setup.py install': %s" % exc)
-
-      if not os.path.exists(DIST_PATH):
-        try:
-          stem.util.system.call('%s setup.py sdist' % PYTHON_EXE, timeout = 60)
-        except Exception as exc:
-          SDIST_FAILURE = exc
-      else:
-        SDIST_FAILURE = AssertionError("%s already exists, maybe you manually ran 'python setup.py sdist'?" % DIST_PATH)
-    finally:
-      os.chdir(original_cwd)
+        SDIST_FAILURE = exc
+    else:
+      SDIST_FAILURE = AssertionError("%s already exists, maybe you manually ran 'python setup.py sdist'?" % DIST_PATH)
 
   if SETUP_THREAD is None:
     SETUP_THREAD = threading.Thread(target = _setup)





More information about the tor-commits mailing list