[tor-commits] [stem/master] Moving clean_orphaned_pyc() to test utils

atagar at torproject.org atagar at torproject.org
Sun Apr 14 04:33:47 UTC 2013


commit 6076f4cf8375565acdde8dfbfffe2be4a2327480
Author: Damian Johnson <atagar at torproject.org>
Date:   Fri Apr 12 09:13:30 2013 -0700

    Moving clean_orphaned_pyc() to test utils
    
    Moving the functional bit of clean_orphaned_pyc() to the test utilities (the
    module specifically will *not* be used for generating output).
---
 run_tests.py |   42 +++++++++++-------------------------------
 test/util.py |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 31 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index 05f409c..cbb1941 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -46,36 +46,6 @@ base = os.path.sep.join(__file__.split(os.path.sep)[:-1]).lstrip("./")
 SOURCE_BASE_PATHS = [os.path.join(base, path) for path in ('stem', 'test', 'run_tests.py')]
 
 
-def _clean_orphaned_pyc():
-  test.output.print_noline("  checking for orphaned .pyc files... ", *test.runner.STATUS_ATTR)
-
-  orphaned_pyc = []
-
-  for base_dir in SOURCE_BASE_PATHS:
-    for pyc_path in test.static_checks._get_files_with_suffix(base_dir, ".pyc"):
-      # If we're running python 3 then the *.pyc files are no longer bundled
-      # with the *.py. Rather, they're in a __pycache__ directory.
-      #
-      # At the moment there's no point in checking for orphaned bytecode with
-      # python 3 because it's an exported copy of the python 2 codebase, so
-      # skipping.
-
-      if "__pycache__" in pyc_path:
-        continue
-
-      if not os.path.exists(pyc_path[:-1]):
-        orphaned_pyc.append(pyc_path)
-
-  if not orphaned_pyc:
-    # no orphaned files, nothing to do
-    test.output.print_line("done", *test.runner.STATUS_ATTR)
-  else:
-    print
-    for pyc_file in orphaned_pyc:
-      test.output.print_error("    removing %s" % pyc_file)
-      os.remove(pyc_file)
-
-
 def _python3_setup(python3_destination, clean):
   # Python 2.7.3 added some nice capabilities to 2to3, like '--output-dir'...
   #
@@ -321,7 +291,17 @@ if __name__ == '__main__':
   test.output.print_divider("INITIALISING", True)
 
   test.output.print_line("Performing startup activities...", *test.runner.STATUS_ATTR)
-  _clean_orphaned_pyc()
+  test.output.print_noline("  checking for orphaned .pyc files... ", *test.runner.STATUS_ATTR)
+
+  orphaned_pyc = test.util.clean_orphaned_pyc(SOURCE_BASE_PATHS)
+
+  if not orphaned_pyc:
+    # no orphaned files, nothing to do
+    test.output.print_line("done", *test.runner.STATUS_ATTR)
+  else:
+    print
+    for pyc_file in orphaned_pyc:
+      test.output.print_error("    removed %s" % pyc_file)
 
   print
 
diff --git a/test/util.py b/test/util.py
index 8ce61bf..bd5dd37 100644
--- a/test/util.py
+++ b/test/util.py
@@ -5,10 +5,16 @@ Helper functions for our test framework.
 
   get_unit_tests - provides our unit tests
   get_integ_tests - provides our integration tests
+
+  clean_orphaned_pyc - removes any *.pyc without a corresponding *.py
 """
 
+import os
+
 import stem.util.conf
 
+import test.static_checks
+
 CONFIG = stem.util.conf.config_dict("test", {
   "test.unit_tests": "",
   "test.integ_tests": "",
@@ -60,3 +66,43 @@ def _get_tests(modules, prefix):
         module = getattr(module, subcomponent)
 
       yield module
+
+
+def clean_orphaned_pyc(paths):
+  """
+  Deletes any file with a *.pyc extention without a corresponding *.py. This
+  helps to address a common gotcha when deleting python files...
+
+  * You delete module 'foo.py' and run the tests to ensure that you haven't
+    broken anything. They pass, however there *are* still some 'import foo'
+    statements that still work because the bytecode (foo.pyc) is still around.
+
+  * You push your change.
+
+  * Another developer clones our repository and is confused because we have a
+    bunch of ImportErrors.
+
+  :param list paths: paths to search for orphaned pyc files
+
+  :returns: list of files that we deleted
+  """
+
+  orphaned_pyc = []
+
+  for base_dir in paths:
+    for pyc_path in test.static_checks._get_files_with_suffix(base_dir, ".pyc"):
+      # If we're running python 3 then the *.pyc files are no longer bundled
+      # with the *.py. Rather, they're in a __pycache__ directory.
+      #
+      # At the moment there's no point in checking for orphaned bytecode with
+      # python 3 because it's an exported copy of the python 2 codebase, so
+      # skipping.
+
+      if "__pycache__" in pyc_path:
+        continue
+
+      if not os.path.exists(pyc_path[:-1]):
+        orphaned_pyc.append(pyc_path)
+        os.remove(pyc_path)
+
+  return orphaned_pyc





More information about the tor-commits mailing list