github.com/jenkins-x/test-infra@v0.0.7/scenarios/execute.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  # Need to figure out why this only fails on travis
    18  # pylint: disable=bad-continuation
    19  
    20  """Executes a command."""
    21  
    22  import argparse
    23  import os
    24  import subprocess
    25  import sys
    26  
    27  def check(*cmd):
    28      """Log and run the command, raising on errors."""
    29      print >>sys.stderr, 'Run:', cmd
    30      subprocess.check_call(cmd)
    31  
    32  
    33  def main(envs, cmd):
    34      """Run script and verify it exits 0."""
    35      for env in envs:
    36          key, val = env.split('=', 1)
    37          print >>sys.stderr, '%s=%s' % (key, val)
    38          os.environ[key] = val
    39      if not cmd:
    40          raise ValueError(cmd)
    41      check(*cmd)
    42  
    43  
    44  if __name__ == '__main__':
    45      PARSER = argparse.ArgumentParser()
    46      PARSER.add_argument('--env', default=[], action='append')
    47      PARSER.add_argument('cmd', nargs=1)
    48      PARSER.add_argument('args', nargs='*')
    49      ARGS = PARSER.parse_args()
    50      main(ARGS.env, ARGS.cmd + ARGS.args)