github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/jobs/move_extract.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  """Migrate --extract flag from an JENKINS_FOO env to a scenario flag."""
    18  
    19  import json
    20  import os
    21  import re
    22  import sys
    23  
    24  ORIG_CWD = os.getcwd()  # Checkout changes cwd
    25  
    26  def test_infra(*paths):
    27      """Return path relative to root of test-infra repo."""
    28      return os.path.join(ORIG_CWD, os.path.dirname(__file__), '..', *paths)
    29  
    30  def sort():
    31      """Sort config.json alphabetically."""
    32      # pylint: disable=too-many-branches,too-many-statements,too-many-locals
    33      with open(test_infra('jobs/config.json'), 'r+') as fp:
    34          configs = json.loads(fp.read())
    35      regexp = re.compile('|'.join([
    36          r'^KUBE_OS_DISTRIBUTION=(.*)$',
    37      ]))
    38      problems = []
    39      for job, values in configs.items():
    40          if values.get('scenario') != 'kubernetes_e2e':
    41              continue
    42          if 'args' not in values:
    43              continue
    44          args = values['args']
    45          if any('None' in a for a in args):
    46              problems.append('Bad flag with None: %s' % job)
    47              continue
    48  
    49          if not os.path.isfile(test_infra('jobs/env/%s.env' % job)):
    50              continue
    51          with open(test_infra('jobs/env/%s.env' % job)) as fp:
    52              env = fp.read()
    53          lines = []
    54          mod = False
    55          os_image = ''
    56          for line in env.split('\n'):
    57              mat = regexp.search(line)
    58              if mat:
    59                  os_image = mat.group(1)
    60                  mod = True
    61                  continue
    62              lines.append(line)
    63          if not mod:
    64              continue
    65  
    66          args = list(args)
    67          if os_image:
    68              args.append('--gcp-node-image=%s' % os_image)
    69              args.append('--gcp-master-image=%s' % os_image)
    70          flags = set()
    71          okay = False
    72          for arg in args:
    73              try:
    74                  flag, _ = arg.split('=', 1)
    75              except ValueError:
    76                  flag = ''
    77              if flag and flag not in ['--env-file', '--extract']:
    78                  if flag in flags:
    79                      problems.append('Multiple %s in %s' % (flag, job))
    80                      break
    81                  flags.add(flag)
    82          else:
    83              okay = True
    84          if not okay:
    85              continue
    86          values['args'] = args
    87          with open(test_infra('jobs/env/%s.env' % job), 'w') as fp:
    88              fp.write('\n'.join(lines))
    89      with open(test_infra('jobs/config.json'), 'w') as fp:
    90          fp.write(json.dumps(configs, sort_keys=True, indent=2, separators=(',', ': ')))
    91          fp.write('\n')
    92      if not problems:
    93          sys.exit(0)
    94      print >>sys.stderr, '%d problems' % len(problems)
    95      print '\n'.join(problems)
    96  
    97  if __name__ == '__main__':
    98      sort()