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