2 The sole purpose of this module is to alter the sys.path upon
3 import in such a way to get pygr from the source directory rather than
6 See the README.txt file for details on how to change the behavior.
8 NOTE in place builds are required:
10 python setup.py build_ext -i
13 import sys
, os
, distutils
.util
, platform
18 "Joins and normalizes paths"
19 return os
.path
.abspath(os
.path
.join(*args
))
21 # we cannot use the main logger, because the import paths
22 # may not be set up yet
25 sys
.stderr
.write(msg
+ '\n')
28 "A fatal unrecoverable error"
32 # get the current directory of the current module
33 curr_dir
= os
.path
.dirname(__file__
)
35 # this is the extra path that needs be added
36 base_dir
= path_join(curr_dir
, '..', '..')
38 # get the pygr source directory
39 pygr_source_dir
= path_join(base_dir
, 'pygr')
41 # build specific directories
42 os_info
= distutils
.util
.get_platform()
43 version
= ".".join([str(x
) for x
in platform
.python_version_tuple()[:2]])
44 lib_dir
= 'lib.%s-%s' % (os_info
, version
,)
45 temp_dir
= 'temp.%s-%s' % (os_info
, version
,)
46 pygr_build_dir
= path_join(base_dir
, 'build', lib_dir
)
47 pygr_temp_dir
= path_join(base_dir
, 'build', temp_dir
)
49 # we'll do a top level option parsing in this module as well
50 parser
= testoptions
.option_parser()
53 options
, args
= parser
.parse_args()
55 # this makes it less clunky
56 use_pathfix
= not options
.no_pathfix
58 # stores the error message about the import path
62 # alter the import path
64 path_errmsg
= "Importing pygr from platform build path %s" % pygr_build_dir
65 sys
.path
= [ pygr_build_dir
] + sys
.path
66 required_prefix
= pygr_build_dir
68 path_errmsg
= "Importing pygr from source directory %s" % base_dir
69 sys
.path
= [ base_dir
] + sys
.path
70 required_prefix
= pygr_source_dir
72 path_errmsg
= "Importing pygr from default path"
76 # also, start coverage
80 from figleaf
import annotate_html
82 # Fix for figleaf misbehaving. It is adding a logger at root level
83 # and that will add a handler to all subloggers (ours as well)
84 # needs to be fixed in figleaf
86 root
= logging
.getLogger()
88 # remove all root handlers
89 for hand
in root
.handlers
:
90 root
.removeHandler(hand
)
100 # try to import the main pygr module
102 from pygr
import logger
104 # we have a logger now
105 logger
.info("importing pygr from %s" % pygr
.__file
__)
107 # try to import an extension module
108 from pygr
import cnestedlist
110 except ImportError, exc
:
118 1. build the extension modules in place with:
119 python setup.py build_ext -i
121 2. add the -b flag to runtest.py
122 (see runtest.py -h for more details)
124 3. install a binary version of pygr into the system path
126 """ % (path_errmsg
, exc
))
130 for mod
in [ pygr
, cnestedlist
]:
131 # test that the imported python modules have the required prefix
132 if not mod
.__file
__.startswith(required_prefix
):
133 stop ("module %s imported from invalid path: %s" % \
134 (mod
.__name
__, mod
.__file
__))