summaryrefslogtreecommitdiff
path: root/chromium/third_party/catapult/catapult_build/remove_stale_files.py
blob: 76329f0846101d17636664fbc79c67fb08754b7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import argparse
import os
import sys


def RemoveAllStaleFiles(base_dir, types_to_purge):
  """Scan directories for old files and delete them."""
  for type_ext in types_to_purge:
    for dirname, _, filenames in os.walk(base_dir):
      if '.git' in dirname:
        continue
      for filename in filenames:
        _, ext = os.path.splitext(filename)
        if ext != type_ext:
          continue

        full_path = os.path.join(dirname, filename)

        try:
          os.remove(full_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


def Main():
  parser = argparse.ArgumentParser()
  parser.add_argument('path', help='Path to clear stale files from.')
  parser.add_argument('types', help='Comma separated list of types to purge.')
  args = parser.parse_args(sys.argv[1:])

  RemoveAllStaleFiles(args.path, args.types.split(','))


if __name__ == '__main__':
  Main()