blob: 71da650098f61a8ce0cfc0b2eccd8f6846dc4522 [file] [log] [blame]
Jamie Gennis92791472012-03-05 17:33:58 -08001#!/usr/bin/env python
2
Zhen Wang46b43bf2015-08-28 09:54:29 -07003# Copyright (c) 2015 The Chromium Authors. All rights reserved.
Jamie Gennis4b56a2b2012-04-28 01:06:56 -07004# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
Jamie Gennis92791472012-03-05 17:33:58 -08006
Zhen Wang46b43bf2015-08-28 09:54:29 -07007import os
Brent Austinc0555cc2015-05-14 14:05:59 -07008import sys
Jamie Gennis92791472012-03-05 17:33:58 -08009
Zhen Wang46b43bf2015-08-28 09:54:29 -070010version = sys.version_info[:2]
11if version != (2, 7):
12 sys.stderr.write('Systrace does not support Python %d.%d. '
13 'Please use Python 2.7.\n' % version)
Brent Austinc0555cc2015-05-14 14:05:59 -070014 sys.exit(1)
15
Zhen Wang46b43bf2015-08-28 09:54:29 -070016systrace_dir = os.path.abspath(
Chris Craikcef78932016-03-28 13:54:49 -070017 os.path.join(os.path.dirname(__file__), 'catapult', 'systrace'))
Zhen Wang46b43bf2015-08-28 09:54:29 -070018sys.path.insert(0, systrace_dir)
Chris Craikcef78932016-03-28 13:54:49 -070019
John Reckb1cc17c2016-08-23 13:28:34 -070020def 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 Austin6975e312015-05-14 13:27:45 -070045
Jamie Gennis92791472012-03-05 17:33:58 -080046if __name__ == '__main__':
John Reckb1cc17c2016-08-23 13:28:34 -070047 RemoveAllStalePycFiles(os.path.dirname(__file__))
48 from systrace import run_systrace
Chris Craik47f0f1e2016-08-19 14:42:29 -070049 sys.exit(run_systrace.main())