]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - misc/build_helpers/test-with-fake-dists.py
d78df97481bf54dfa7df083f074e6d7206f015ca
[tahoe-lafs/tahoe-lafs.git] / misc / build_helpers / test-with-fake-dists.py
1 #!/usr/bin/env python
2
3 import StringIO, glob, os, platform, shutil, subprocess, sys, tarfile, zipfile
4 import pkg_resources
5
6 def test():
7     # We put a fake "pycryptopp-0.5.24.egg" package and a fake
8     # "pycryptopp-9.9.99.tar.gz" into a directory, but the latter is
9     # booby-trapped so it will raise an exception when you try to build
10     # it.
11
12     # Then we run "python setup.py test -s
13     # buildtest.test_with_fake_dist", which imports pycryptopp
14     # and passes if pycryptopp.__version__ == '0.5.24'.
15
16     # (If building succeeded -- meaning that you didn't try to build the
17     # booby-trapped 9.9.99 -- but pycryptopp.__version__ != '0.5.24' then
18     # that means a different version of pycryptopp was already installed
19     # so neither of the two fake pycryptopp packages were needed. In that
20     # case this test should be treated as a "skip" -- the functionality
21     # under test can't be exercised on the current system.)
22
23     # The goal is to turn red if the build system tries to build the
24     # source dist when it could have used the binary dist.
25
26     # (Note that for this test to make sense, tahoe-lafs needs to be
27     # asking for a version of pycryptopp which can be satisfied by either
28     # 0.5.24 or 0.5.25. At the time of this writing it requires >= 0.5.20
29     # on x86 and >= 0.5.14 on other architectures.)
30
31     fake_distdir = 'tahoe-deps'
32     fake_distname = "pycryptopp"
33     fake_sdistversion = "9.9.99"
34     fake_bdistversion = "0.5.24"
35     sdist_setup = "raise Exception('Aha I caught you trying to build me. I am a fake pycryptopp 9.9.99 sdist and you should be satisfied with a bdist.')"
36
37     testsuite = "buildtest.test_build_with_fake_dist"
38
39     dist_dirname = os.path.join(os.getcwd(), fake_distdir)
40
41     try:
42         os.makedirs(dist_dirname)
43     except OSError:
44         # probably already exists
45         pass
46
47     bdist_egg_name = os.path.join(dist_dirname, '%s-%s-py%s.%s-%s.egg' % (fake_distname, fake_bdistversion, platform.python_version_tuple()[0], platform.python_version_tuple()[1], pkg_resources.get_supported_platform()))
48     try:
49         bdist_egg = zipfile.ZipFile(bdist_egg_name, 'w')
50         bdist_egg.writestr('pycryptopp/__init__.py', '__version__ = "%s"\n' % (fake_bdistversion,))
51         bdist_egg.close()
52
53         sdist_name = os.path.join(dist_dirname, '%s-%s.tar' % (fake_distname, fake_sdistversion))
54         sdist = tarfile.open(sdist_name, 'w:gz')
55         sdist.errorlevel =2
56         tarinfo = tarfile.TarInfo('setup.py')
57         tarinfo.errorlevel =2
58         tarinfo.size = len(sdist_setup)
59         sdist.addfile(tarinfo, StringIO.StringIO(sdist_setup))
60         sdist.close()
61
62         sys.exit(subprocess.call([sys.executable, "setup.py", "-v", "test", "-s", testsuite], env=os.environ))
63     finally:
64         os.remove(bdist_egg_name)
65         os.remove(sdist_name)
66         cleanup()
67
68 def cleanup():
69     for path, subdnames, fnames in os.walk('.'):
70         for fdname in subdnames + fnames:
71             if fdname.startswith('pycryptopp-0.5.24'):
72                 shutil.rmtree(os.path.join(path, fdname))
73
74 if __name__ == '__main__':
75     test()