1#!/usr/bin/env python
2
3# Copyright (c) 2015 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7import os
8import sys
9
10version = 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)
14  sys.exit(1)
15
16systrace_dir = os.path.abspath(
17    os.path.join(os.path.dirname(__file__), 'catapult', 'systrace'))
18sys.path.insert(0, systrace_dir)
19
20def 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
45
46if __name__ == '__main__':
47  RemoveAllStalePycFiles(os.path.dirname(__file__))
48  from systrace import run_systrace
49  sys.exit(run_systrace.main())
50