1 | """ |
---|
2 | Prepare python system path to point to right locations |
---|
3 | """ |
---|
4 | import imp |
---|
5 | import os |
---|
6 | import sys |
---|
7 | |
---|
8 | def exe_run(): |
---|
9 | """ |
---|
10 | Check if the process is run as .exe or as .py |
---|
11 | Primitive extension check of the name of the running process |
---|
12 | """ |
---|
13 | # Could be checking for .exe, but on mac/linux this wouldn't work well |
---|
14 | return os.path.splitext(sys.argv[0])[1].lower() != ".py" |
---|
15 | |
---|
16 | def addpath(path): |
---|
17 | """ |
---|
18 | Add a directory to the python path environment, and to the PYTHONPATH |
---|
19 | environment variable for subprocesses. |
---|
20 | """ |
---|
21 | path = os.path.abspath(path) |
---|
22 | if 'PYTHONPATH' in os.environ: |
---|
23 | PYTHONPATH = path + os.pathsep + os.environ['PYTHONPATH'] |
---|
24 | else: |
---|
25 | PYTHONPATH = path |
---|
26 | os.environ['PYTHONPATH'] = PYTHONPATH |
---|
27 | sys.path.insert(0, path) |
---|
28 | |
---|
29 | def import_package(modname, path): |
---|
30 | """Import a package into a particular point in the python namespace""" |
---|
31 | mod = imp.load_source(modname, os.path.abspath(os.path.join(path,'__init__.py'))) |
---|
32 | sys.modules[modname] = mod |
---|
33 | mod.__path__ = [os.path.abspath(path)] |
---|
34 | return mod |
---|
35 | |
---|
36 | root = "" |
---|
37 | if exe_run(): |
---|
38 | root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) |
---|
39 | addpath(os.path.join(root, 'src')) |
---|
40 | addpath('src') |
---|
41 | else: |
---|
42 | root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "..")) |
---|
43 | #addpath(os.path.join(root, 'src')) |
---|
44 | #addpath('src') |
---|
45 | # Add the local build directory to PYTHONPATH |
---|
46 | from distutils.util import get_platform |
---|
47 | platform = '%s-%s'%(get_platform(),sys.version[:3]) |
---|
48 | build_path = os.path.join(root, 'build','lib.'+platform) |
---|
49 | sys.path.append(build_path) |
---|
50 | |
---|