systrace.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python
  2. # Copyright (c) 2015 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. import os
  6. import sys
  7. version = sys.version_info[:2]
  8. if version != (2, 7):
  9. sys.stderr.write('Systrace does not support Python %d.%d. '
  10. 'Please use Python 2.7.\n' % version)
  11. sys.exit(1)
  12. systrace_dir = os.path.abspath(
  13. os.path.join(os.path.dirname(__file__), 'catapult', 'systrace'))
  14. sys.path.insert(0, systrace_dir)
  15. def RemoveAllStalePycFiles(base_dir):
  16. """Scan directories for old .pyc files without a .py file and delete them."""
  17. for dirname, _, filenames in os.walk(base_dir):
  18. if '.git' in dirname:
  19. continue
  20. for filename in filenames:
  21. root, ext = os.path.splitext(filename)
  22. if ext != '.pyc':
  23. continue
  24. pyc_path = os.path.join(dirname, filename)
  25. py_path = os.path.join(dirname, root + '.py')
  26. try:
  27. if not os.path.exists(py_path):
  28. os.remove(pyc_path)
  29. except OSError:
  30. # Wrap OS calls in try/except in case another process touched this file.
  31. pass
  32. try:
  33. os.removedirs(dirname)
  34. except OSError:
  35. # Wrap OS calls in try/except in case another process touched this dir.
  36. pass
  37. if __name__ == '__main__':
  38. RemoveAllStalePycFiles(os.path.dirname(__file__))
  39. from systrace import run_systrace
  40. sys.exit(run_systrace.main())