]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - misc/build_helpers/run_trial.py
246572b9494a638a920d50385901c943e7d2dbb3
[tahoe-lafs/tahoe-lafs.git] / misc / build_helpers / run_trial.py
1 #!/usr/bin/env python
2
3 import os, sys, re, glob
4
5 if sys.platform == "win32":
6     # Buildbot depends on pywin32, but we don't want it to be available to the code under test.
7     def not_pywin32(entry):
8         dirs = os.path.normcase(entry).replace('/', '\\').split('\\')
9         return "win32" not in dirs and "pythonwin" not in dirs
10
11     print sys.path
12     sys.path = filter(not_pywin32, sys.path)
13     print sys.path
14
15     try:
16         import win32api
17     except ImportError:
18         pass  # expected
19     else:
20         raise AssertionError("We were unable to expunge pywin32 from the sys.path.")
21
22
23 def read_version_py(infname):
24     try:
25         verstrline = open(infname, "rt").read()
26     except EnvironmentError:
27         return None
28     else:
29         VSRE = r"^verstr = ['\"]([^'\"]*)['\"]"
30         mo = re.search(VSRE, verstrline, re.M)
31         if mo:
32             return mo.group(1)
33
34 version = read_version_py(os.path.join('..', 'src', 'allmydata', '_version.py'))
35
36 if version is None:
37     raise AssertionError("We don't know which version we're supposed to be testing.")
38
39 APPNAME='allmydata-tahoe'
40
41 adglobals = {}
42 execfile(os.path.join('..', 'src', 'allmydata', '_auto_deps.py'), adglobals)
43 install_requires = adglobals['install_requires']
44 test_requires = adglobals.get('test_requires', ['mock'])
45
46 # setuptools/zetuptoolz looks in __main__.__requires__ for a list of
47 # requirements.
48
49 __requires__ = [APPNAME + '==' + version] + install_requires + test_requires
50
51 print "Requirements: %r" % (__requires__,)
52
53 eggz = glob.glob(os.path.join('..', 'setuptools-*.egg'))
54 if len(eggz) > 0:
55    egg = os.path.realpath(eggz[0])
56    print "Inserting egg on sys.path: %r" % (egg,)
57    sys.path.insert(0, egg)
58
59 import pkg_resources
60 pkg_resources  # hush pyflakes
61
62 modulename = None
63 for i in xrange(1, len(sys.argv)):
64     if not sys.argv[i].startswith('-'):
65         modulename = sys.argv[i]
66         break
67
68 if modulename is None:
69     raise AssertionError("no test module specified")
70
71 __import__(modulename)
72 srcfile = sys.modules[modulename].__file__
73 srcdir = os.path.dirname(os.path.realpath(srcfile))
74
75 components = modulename.split('.')
76 leaf = os.path.normcase(components[-1])
77 if os.path.normcase(os.path.basename(srcfile)) in (leaf + '.py', leaf + '.pyc'):
78     # strip the leaf module name
79     components = components[:-1]
80
81 for i in components:
82     srcdir = os.path.dirname(srcdir)
83
84 if os.path.normcase(srcdir).endswith('.egg'):
85     srcdir = os.path.dirname(srcdir)
86 elif os.path.normcase(os.path.basename(srcdir)) == 'site-packages':
87     srcdir = os.path.dirname(srcdir)
88     if re.search(r'python.+\..+', os.path.normcase(os.path.basename(srcdir))):
89         srcdir = os.path.dirname(srcdir)
90     if os.path.normcase(os.path.basename(srcdir)) == 'lib':
91         srcdir = os.path.dirname(srcdir)
92
93 rootdir = os.path.normcase(os.path.normpath(srcdir))
94 if os.path.basename(rootdir) == 'src':
95     rootdir = os.path.dirname(rootdir)
96
97 root_from_cwd = os.path.normcase(os.path.normpath(os.getcwd()))
98 if os.path.basename(root_from_cwd) == 'src':
99     root_from_cwd = os.path.dirname(root_from_cwd)
100
101 same = (root_from_cwd == rootdir)
102 if not same:
103     try:
104         same = os.path.samefile(root_from_cwd, rootdir)
105     except AttributeError, e:
106         e  # hush pyflakes
107
108 if not same:
109     msg = ("We seem to be testing the code at %r\n"
110            "(according to the source filename %r),\n"
111            "but expected to be testing the code at %r.\n"
112            % (rootdir, srcfile, root_from_cwd))
113
114     root_from_cwdu = os.path.normcase(os.path.normpath(os.getcwdu()))
115     if os.path.basename(root_from_cwdu) == u'src':
116         root_from_cwdu = os.path.dirname(root_from_cwdu)
117
118     if not isinstance(root_from_cwd, unicode) and root_from_cwd.decode(sys.getfilesystemencoding(), 'replace') != root_from_cwdu:
119         msg += ("However, this may be a false alarm because the current directory path\n"
120                 "is not representable in the filesystem encoding. This script needs to be\n"
121                 "run from the source directory to be tested, at a non-Unicode path.")
122     else:
123         msg += "This script needs to be run from the source directory to be tested."
124
125     raise AssertionError(msg)
126
127 from twisted.scripts.trial import run
128 run()