configurationengine/build-scripts/utils.py
changeset 0 2e8eeb919028
child 3 e7e0ae78773e
equal deleted inserted replaced
-1:000000000000 0:2e8eeb919028
       
     1 #
       
     2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description: 
       
    15 #   Utility functions for use in build scripts.
       
    16 #
       
    17 
       
    18 import sys, os, subprocess, shutil, logging
       
    19 
       
    20 log = logging.getLogger()
       
    21 
       
    22 def run_command(cmd, env_overrides={}):
       
    23     env = os.environ.copy()
       
    24     for key, val in env_overrides.iteritems():
       
    25         env[key] = val
       
    26     
       
    27     p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, env=env)
       
    28     out, err = p.communicate()
       
    29     if p.returncode != 0:
       
    30         log.error("Could not execute command (%s)" % cmd)
       
    31         log.debug("Output:\n%s" % out)
       
    32         return False
       
    33     else:
       
    34         return True
       
    35 
       
    36 def recreate_dir(path):
       
    37     log.debug('recreate_dir(%s)' % path)
       
    38     if os.path.exists(path):
       
    39         for name in os.listdir(path):
       
    40             p = os.path.join(path, name)
       
    41             if os.path.isdir(p):    shutil.rmtree(p)
       
    42             else:                   os.remove(p)
       
    43     else:
       
    44         os.makedirs(path)
       
    45 
       
    46 def build_egg(source_dir, target_dir):
       
    47     """
       
    48     Build an egg file from the given source directory (must contain a setup.py)
       
    49     into the given target directory.
       
    50     """
       
    51     log.debug("Building egg from '%s'" % source_dir)
       
    52     
       
    53     orig_workdir = os.getcwd()
       
    54     os.chdir(source_dir)
       
    55     try:
       
    56         cmd = 'python setup.py bdist_egg --dist-dir "%s"' % target_dir
       
    57         return run_command(cmd)
       
    58     finally:
       
    59         os.chdir(orig_workdir)
       
    60 
       
    61 def copy_file(source_path, target_path):
       
    62     log.debug("Copying '%s' -> '%s'" % (source_path, target_path))
       
    63     target_dir = os.path.dirname(target_path)
       
    64     if target_dir != '' and not os.path.exists(target_dir):
       
    65         os.makedirs(target_dir)
       
    66     shutil.copy2(source_path, target_path)
       
    67 
       
    68 def get_python_version():
       
    69     """
       
    70     Return the version of the Python that is run when the command 'python'
       
    71     is run (not the Python where this script is executing).
       
    72     """
       
    73     p = subprocess.Popen('python -c "import sys; print sys.version[:3]"', stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
       
    74     out, err = p.communicate()
       
    75     if p.returncode != 0:
       
    76         log.critical("Failed to get python version")
       
    77         log.critical("Command output: %s" % out)
       
    78         return 1
       
    79     
       
    80     return out.strip()
       
    81 
       
    82 def setup_logging(logfile):
       
    83     root_logger = logging.getLogger()
       
    84     root_logger.setLevel(logging.DEBUG)
       
    85     
       
    86     console_handler = logging.StreamHandler()
       
    87     console_handler.setLevel(logging.INFO)
       
    88     console_handler.setFormatter(logging.Formatter('%(levelname)s - %(message)s'))    
       
    89     root_logger.addHandler(console_handler)
       
    90     
       
    91     file_handler = logging.FileHandler(logfile, mode='w')
       
    92     file_handler.setLevel(logging.DEBUG)
       
    93     file_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
       
    94     root_logger.addHandler(file_handler)