Back to home page

OSCL-LXR

 
 

    


0001 #
0002 # Licensed to the Apache Software Foundation (ASF) under one or more
0003 # contributor license agreements.  See the NOTICE file distributed with
0004 # this work for additional information regarding copyright ownership.
0005 # The ASF licenses this file to You under the Apache License, Version 2.0
0006 # (the "License"); you may not use this file except in compliance with
0007 # the License.  You may obtain a copy of the License at
0008 #
0009 #    http://www.apache.org/licenses/LICENSE-2.0
0010 #
0011 # Unless required by applicable law or agreed to in writing, software
0012 # distributed under the License is distributed on an "AS IS" BASIS,
0013 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014 # See the License for the specific language governing permissions and
0015 # limitations under the License.
0016 #
0017 
0018 import os
0019 import shutil
0020 import subprocess
0021 import sys
0022 
0023 subprocess_check_output = subprocess.check_output
0024 
0025 
0026 def exit_from_command_with_retcode(cmd, retcode):
0027     if retcode < 0:
0028         print("[error] running", ' '.join(cmd), "; process was terminated by signal", -retcode)
0029     else:
0030         print("[error] running", ' '.join(cmd), "; received return code", retcode)
0031     sys.exit(int(os.environ.get("CURRENT_BLOCK", 255)))
0032 
0033 
0034 def rm_r(path):
0035     """
0036     Given an arbitrary path, properly remove it with the correct Python construct if it exists.
0037     From: http://stackoverflow.com/a/9559881
0038     """
0039 
0040     if os.path.isdir(path):
0041         shutil.rmtree(path)
0042     elif os.path.exists(path):
0043         os.remove(path)
0044 
0045 
0046 def run_cmd(cmd, return_output=False):
0047     """
0048     Given a command as a list of arguments will attempt to execute the command
0049     and, on failure, print an error message and exit.
0050     """
0051 
0052     if not isinstance(cmd, list):
0053         cmd = cmd.split()
0054     try:
0055         if return_output:
0056             return subprocess_check_output(cmd).decode('utf-8')
0057         else:
0058             return subprocess.run(cmd, universal_newlines=True, check=True)
0059     except subprocess.CalledProcessError as e:
0060         exit_from_command_with_retcode(e.cmd, e.returncode)
0061 
0062 
0063 def is_exe(path):
0064     """
0065     Check if a given path is an executable file.
0066     From: http://stackoverflow.com/a/377028
0067     """
0068 
0069     return os.path.isfile(path) and os.access(path, os.X_OK)
0070 
0071 
0072 def which(program):
0073     """
0074     Find and return the given program by its absolute path or 'None' if the program cannot be found.
0075     From: http://stackoverflow.com/a/377028
0076     """
0077 
0078     fpath = os.path.split(program)[0]
0079 
0080     if fpath:
0081         if is_exe(program):
0082             return program
0083     else:
0084         for path in os.environ.get("PATH").split(os.pathsep):
0085             path = path.strip('"')
0086             exe_file = os.path.join(path, program)
0087             if is_exe(exe_file):
0088                 return exe_file
0089     return None