configurationengine/build-scripts/utils.py
changeset 3 e7e0ae78773e
parent 0 2e8eeb919028
equal deleted inserted replaced
2:87cfa131b535 3:e7e0ae78773e
    41             if os.path.isdir(p):    shutil.rmtree(p)
    41             if os.path.isdir(p):    shutil.rmtree(p)
    42             else:                   os.remove(p)
    42             else:                   os.remove(p)
    43     else:
    43     else:
    44         os.makedirs(path)
    44         os.makedirs(path)
    45 
    45 
    46 def build_egg(source_dir, target_dir):
    46 def build_egg(source_dir, target_dir, python_executable='python'):
    47     """
    47     """
    48     Build an egg file from the given source directory (must contain a setup.py)
    48     Build an egg file from the given source directory (must contain a setup.py)
    49     into the given target directory.
    49     into the given target directory.
    50     """
    50     """
    51     log.debug("Building egg from '%s'" % source_dir)
    51     log.debug("Building egg from '%s'" % source_dir)
    52     
    52     
    53     orig_workdir = os.getcwd()
    53     orig_workdir = os.getcwd()
    54     os.chdir(source_dir)
    54     os.chdir(source_dir)
    55     try:
    55     try:
    56         cmd = 'python setup.py bdist_egg --dist-dir "%s"' % target_dir
    56         cmd = '%s setup.py bdist_egg --dist-dir "%s"' % (python_executable, target_dir)
    57         return run_command(cmd)
    57         return run_command(cmd)
    58     finally:
    58     finally:
    59         os.chdir(orig_workdir)
    59         os.chdir(orig_workdir)
    60 
    60 
    61 def copy_file(source_path, target_path):
    61 def copy_file(source_path, target_path):
    63     target_dir = os.path.dirname(target_path)
    63     target_dir = os.path.dirname(target_path)
    64     if target_dir != '' and not os.path.exists(target_dir):
    64     if target_dir != '' and not os.path.exists(target_dir):
    65         os.makedirs(target_dir)
    65         os.makedirs(target_dir)
    66     shutil.copy2(source_path, target_path)
    66     shutil.copy2(source_path, target_path)
    67 
    67 
    68 def get_python_version():
    68 def get_python_version(executable='python'):
    69     """
    69     """
    70     Return the version of the Python that is run when the command 'python'
    70     Return the version of the Python that is run when the given Python
    71     is run (not the Python where this script is executing).
    71     executable is run (not the Python where this script is executing).
       
    72     @param executable: The Python executable to run, defaults to 'python'.
    72     """
    73     """
    73     p = subprocess.Popen('python -c "import sys; print sys.version[:3]"', stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
    74     p = subprocess.Popen('%s -c "import sys; print sys.version[:3]"' % executable, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
    74     out, err = p.communicate()
    75     out, err = p.communicate()
    75     if p.returncode != 0:
    76     if p.returncode != 0:
    76         log.critical("Failed to get python version")
    77         log.critical("Failed to get python version")
    77         log.critical("Command output: %s" % out)
    78         log.critical("Command output: %s" % out)
    78         return 1
    79         return 1