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