github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/jobs/env_gc.py (about)

     1  #!/usr/bin/env python
     2  
     3  # Copyright 2017 The Kubernetes Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  """Garbage collect unused .env files."""
    18  
    19  import argparse
    20  import os
    21  import json
    22  
    23  ORIG_CWD = os.getcwd()  # Checkout changes cwd
    24  
    25  def test_infra(*paths):
    26      """Return path relative to root of test-infra repo."""
    27      return os.path.join(ORIG_CWD, os.path.dirname(__file__), '..', *paths)
    28  
    29  def find_orphans():
    30      with open(test_infra('jobs/config.json'), 'r') as fp:
    31          configs = json.loads(fp.read())
    32  
    33      used = {}
    34      unused = []
    35      for _, config in configs.items():
    36          if config['scenario'] == 'execute' or 'args' not in config:
    37              continue
    38          for arg in config['args']:
    39              if (arg.startswith('--env-file=') or
    40                      arg.startswith('--properties=')):
    41                  used[arg.split('=')[1]] = True
    42  
    43      basepath = test_infra()
    44      for root, _, files in os.walk(test_infra('jobs')):
    45          for name in files:
    46              if name.endswith('.env'):
    47                  path = os.path.join(root, name).replace(basepath + '/', '', 1)
    48                  if path not in used:
    49                      unused.append(path)
    50  
    51      return unused
    52  
    53  
    54  def deep_unlink(path):
    55      """Bazel symlinks to the git client, try readlink() before the unlink()."""
    56      try:
    57          under = os.path.join(os.path.dirname(path), os.readlink(path))
    58          os.unlink(under)
    59      except OSError:
    60          pass
    61      os.unlink(path)
    62  
    63  def unlink_orphans():
    64      orphans = find_orphans()
    65      for path in orphans:
    66          print "Deleting unused .env file: {}".format(path)
    67          deep_unlink(test_infra(path))
    68      if orphans:
    69          print ('\nNote: If this is a git tree, ' +
    70                 'use "git add -u" to stage orphan deletions')
    71  
    72  if __name__ == '__main__':
    73      PARSER = argparse.ArgumentParser(
    74          description='Delete jobs/*.env files not in jobs/config.json')
    75      ARGS = PARSER.parse_args()
    76  
    77      unlink_orphans()