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