Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Zhen Wang | 46b43bf | 2015-08-28 09:54:29 -0700 | [diff] [blame] | 3 | # Copyright (c) 2015 The Chromium Authors. All rights reserved. |
Jamie Gennis | 4b56a2b | 2012-04-28 01:06:56 -0700 | [diff] [blame] | 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 6 | |
Zhen Wang | 46b43bf | 2015-08-28 09:54:29 -0700 | [diff] [blame] | 7 | import os |
Brent Austin | c0555cc | 2015-05-14 14:05:59 -0700 | [diff] [blame] | 8 | import sys |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 9 | |
Zhen Wang | 46b43bf | 2015-08-28 09:54:29 -0700 | [diff] [blame] | 10 | version = sys.version_info[:2] |
| 11 | if version != (2, 7): |
| 12 | sys.stderr.write('Systrace does not support Python %d.%d. ' |
| 13 | 'Please use Python 2.7.\n' % version) |
Brent Austin | c0555cc | 2015-05-14 14:05:59 -0700 | [diff] [blame] | 14 | sys.exit(1) |
| 15 | |
Zhen Wang | 46b43bf | 2015-08-28 09:54:29 -0700 | [diff] [blame] | 16 | systrace_dir = os.path.abspath( |
Chris Craik | cef7893 | 2016-03-28 13:54:49 -0700 | [diff] [blame] | 17 | os.path.join(os.path.dirname(__file__), 'catapult', 'systrace')) |
Zhen Wang | 46b43bf | 2015-08-28 09:54:29 -0700 | [diff] [blame] | 18 | sys.path.insert(0, systrace_dir) |
Chris Craik | cef7893 | 2016-03-28 13:54:49 -0700 | [diff] [blame] | 19 | |
John Reck | b1cc17c | 2016-08-23 13:28:34 -0700 | [diff] [blame] | 20 | def RemoveAllStalePycFiles(base_dir): |
| 21 | """Scan directories for old .pyc files without a .py file and delete them.""" |
| 22 | for dirname, _, filenames in os.walk(base_dir): |
| 23 | if '.git' in dirname: |
| 24 | continue |
| 25 | for filename in filenames: |
| 26 | root, ext = os.path.splitext(filename) |
| 27 | if ext != '.pyc': |
| 28 | continue |
| 29 | |
| 30 | pyc_path = os.path.join(dirname, filename) |
| 31 | py_path = os.path.join(dirname, root + '.py') |
| 32 | |
| 33 | try: |
| 34 | if not os.path.exists(py_path): |
| 35 | os.remove(pyc_path) |
| 36 | except OSError: |
| 37 | # Wrap OS calls in try/except in case another process touched this file. |
| 38 | pass |
| 39 | |
| 40 | try: |
| 41 | os.removedirs(dirname) |
| 42 | except OSError: |
| 43 | # Wrap OS calls in try/except in case another process touched this dir. |
| 44 | pass |
Brent Austin | 6975e31 | 2015-05-14 13:27:45 -0700 | [diff] [blame] | 45 | |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 46 | if __name__ == '__main__': |
John Reck | b1cc17c | 2016-08-23 13:28:34 -0700 | [diff] [blame] | 47 | RemoveAllStalePycFiles(os.path.dirname(__file__)) |
| 48 | from systrace import run_systrace |
Chris Craik | 47f0f1e | 2016-08-19 14:42:29 -0700 | [diff] [blame] | 49 | sys.exit(run_systrace.main()) |