0
|
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 |
|
3
|
46 |
def build_egg(source_dir, target_dir, python_executable='python'):
|
0
|
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:
|
3
|
56 |
cmd = '%s setup.py bdist_egg --dist-dir "%s"' % (python_executable, target_dir)
|
0
|
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 |
|
3
|
68 |
def get_python_version(executable='python'):
|
0
|
69 |
"""
|
3
|
70 |
Return the version of the Python that is run when the given Python
|
|
71 |
executable is run (not the Python where this script is executing).
|
|
72 |
@param executable: The Python executable to run, defaults to 'python'.
|
0
|
73 |
"""
|
3
|
74 |
p = subprocess.Popen('%s -c "import sys; print sys.version[:3]"' % executable, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
|
0
|
75 |
out, err = p.communicate()
|
|
76 |
if p.returncode != 0:
|
|
77 |
log.critical("Failed to get python version")
|
|
78 |
log.critical("Command output: %s" % out)
|
|
79 |
return 1
|
|
80 |
|
|
81 |
return out.strip()
|
|
82 |
|
|
83 |
def setup_logging(logfile):
|
|
84 |
root_logger = logging.getLogger()
|
|
85 |
root_logger.setLevel(logging.DEBUG)
|
|
86 |
|
|
87 |
console_handler = logging.StreamHandler()
|
|
88 |
console_handler.setLevel(logging.INFO)
|
|
89 |
console_handler.setFormatter(logging.Formatter('%(levelname)s - %(message)s'))
|
|
90 |
root_logger.addHandler(console_handler)
|
|
91 |
|
|
92 |
file_handler = logging.FileHandler(logfile, mode='w')
|
|
93 |
file_handler.setLevel(logging.DEBUG)
|
|
94 |
file_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
|
|
95 |
root_logger.addHandler(file_handler)
|