From 29c4a4869abce2b71a5dd91b855bbfa8ac4b2742 Mon Sep 17 00:00:00 2001 From: John Reck Date: Tue, 23 Aug 2016 13:28:34 -0700 Subject: Prune old pyc files Bug: 31038810 Change-Id: I800eaeae0b7c233d7dcabdbd2479bfb0132eb4ae --- systrace.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/systrace.py b/systrace.py index d96c67a0..71da6500 100755 --- a/systrace.py +++ b/systrace.py @@ -17,7 +17,33 @@ systrace_dir = os.path.abspath( os.path.join(os.path.dirname(__file__), 'catapult', 'systrace')) sys.path.insert(0, systrace_dir) -from systrace import run_systrace +def RemoveAllStalePycFiles(base_dir): + """Scan directories for old .pyc files without a .py file and delete them.""" + for dirname, _, filenames in os.walk(base_dir): + if '.git' in dirname: + continue + for filename in filenames: + root, ext = os.path.splitext(filename) + if ext != '.pyc': + continue + + pyc_path = os.path.join(dirname, filename) + py_path = os.path.join(dirname, root + '.py') + + try: + if not os.path.exists(py_path): + os.remove(pyc_path) + except OSError: + # Wrap OS calls in try/except in case another process touched this file. + pass + + try: + os.removedirs(dirname) + except OSError: + # Wrap OS calls in try/except in case another process touched this dir. + pass if __name__ == '__main__': + RemoveAllStalePycFiles(os.path.dirname(__file__)) + from systrace import run_systrace sys.exit(run_systrace.main()) -- cgit v1.2.3