[tor-commits] [stem/master] Cleaning up orphaned *.pyc files

atagar at torproject.org atagar at torproject.org
Sat Oct 13 18:35:44 UTC 2012


commit 5cd457bebedb6e1582ce7d0d7782df80e51a7291
Author: Damian Johnson <atagar at torproject.org>
Date:   Fri Oct 12 08:49:56 2012 -0700

    Cleaning up orphaned *.pyc files
    
    A common gotcha when deleting or renaming files is that the python bytecode can
    still be imported and used without the source file. In other words if you
    delete 'foo.py' but forget to also delete 'foo.pyc' then your 'import foo'
    statements will still work.
    
    This has often bitten us because the tests still pass, so the problem goes
    undiscovered until someone new checks out the repository.
    
    Having stem's test runner check for orphaned pyc files and remove them prior to
    running its tests. Idea suggested by Ravi.
    
    https://trac.torproject.org/7103
---
 run_tests.py             |   26 ++++++++++++++++++++++++++
 test/check_whitespace.py |   12 +++++++-----
 2 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index b6ee64a..0ed5612 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -235,6 +235,25 @@ def load_user_configuration(test_config):
     print "Unable to start tor, '%s' does not exists." % tor_config
     sys.exit(1)
 
+def _clean_orphaned_pyc():
+  test.output.print_noline("  checking for orphaned .pyc files... ", *test.runner.STATUS_ATTR)
+  
+  orphaned_pyc = []
+  
+  for base_dir in ('stem', 'test', 'run_tests.py'):
+    for pyc_path in test.check_whitespace._get_files_with_suffix(base_dir, ".pyc"):
+      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_line("    removing %s" % pyc_file, *test.runner.ERROR_ATTR)
+      os.remove(pyc_file)
+
 if __name__ == '__main__':
   try:
     stem.prereq.check_requriements()
@@ -279,6 +298,13 @@ if __name__ == '__main__':
   logging_buffer = log.LogBuffer(CONFIG["argument.log"])
   stem_logger.addHandler(logging_buffer)
   
+  test.output.print_divider("INITIALISING", True)
+  
+  test.output.print_line("Performing startup activities...", *test.runner.STATUS_ATTR)
+  _clean_orphaned_pyc()
+  
+  print
+  
   if CONFIG["argument.unit"]:
     test.output.print_divider("UNIT TESTS", True)
     error_tracker.set_category("UNIT TEST")
diff --git a/test/check_whitespace.py b/test/check_whitespace.py
index 687a8a3..d30f99a 100644
--- a/test/check_whitespace.py
+++ b/test/check_whitespace.py
@@ -38,7 +38,7 @@ def get_issues(base_path = DEFAULT_TARGET):
   
   issues = {}
   
-  for file_path in _get_python_files(base_path):
+  for file_path in _get_files_with_suffix(base_path):
     with open(file_path) as f: file_contents = f.read()
     lines, file_issues, prev_indent = file_contents.split("\n"), [], 0
     has_with_import, given_with_warning = False, False
@@ -96,18 +96,20 @@ def get_issues(base_path = DEFAULT_TARGET):
   
   return issues
 
-def _get_python_files(base_path):
+def _get_files_with_suffix(base_path, suffix = ".py"):
   """
-  Iterates over all of the python files within a directory.
+  Iterates over files in a given directory, providing filenames with a certain
+  suffix.
   
   :param str base_path: directory to be iterated over
+  :param str suffix: filename suffix to look for
   
-  :returns: iterator that yields the absolute path for python source code
+  :returns: iterator that yields the absolute path for files with the given suffix
   """
   
   for root, _, files in os.walk(base_path):
     for filename in files:
-      if filename.endswith(".py"):
+      if filename.endswith(suffix):
         yield os.path.join(root, filename)
 
 if __name__ == '__main__':





More information about the tor-commits mailing list