Merge commit 'remotes/ctb/xmlrpc_patches' into tryme
[pygr.git] / tests / testlib / pathfix.py
blob3122729e71f48f819d187570380cc3122c065277
1 """
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
4 @CTB finish comment?
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
11 """
13 import sys, os, distutils.util, platform
15 import testoptions
17 def path_join(*args):
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
23 def info(msg):
24 "Prints a message"
25 sys.stderr.write(msg + '\n')
27 def stop(msg):
28 "A fatal unrecoverable error"
29 info(msg)
30 sys.exit()
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()
52 # parse the arguments
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
59 path_errmsg = None
61 if use_pathfix:
62 # alter the import path
63 if options.builddir:
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
67 else:
68 path_errmsg = "Importing pygr from source directory %s" % base_dir
69 sys.path = [ base_dir ] + sys.path
70 required_prefix = pygr_source_dir
71 else:
72 path_errmsg = "Importing pygr from default path"
74 ###
76 # also, start coverage
78 def start_coverage():
79 import figleaf
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
85 import logging
86 root = logging.getLogger()
88 # remove all root handlers
89 for hand in root.handlers:
90 root.removeHandler(hand)
92 figleaf.start()
94 if options.coverage:
95 start_coverage()
97 ###
99 try:
100 # try to import the main pygr module
101 import pygr
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:
111 stop("""
114 Error: '%s'
116 Possible solutions:
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))
128 if use_pathfix:
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__))