]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - misc/build_helpers/test-with-fake-dists.py
misc/build_helpers/test-with-fake-dists.py: clean up directories and files only if...
[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 "fakedependency-1.0.0.egg" package and a
8     # "fakedependency-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 it.
10     #
11     # Then we run
12     #
13     #   python setup.py --fakedependency -v test -s buildtest.test_build_with_fake_dist
14     #
15     # which imports fakedependency and passes if fakedependency.__version__ == '1.0.0'.
16     #
17     # The goal is to turn red if the build system tries to build the
18     # source dist when it could have used the binary dist.
19     #
20     # Note that for this test to make sense, Tahoe-LAFS needs to be asking
21     # for a version of fakedependency which can be satisfied by 1.0.0.
22     # The --fakedependency option to setup.py arranges that.
23
24     fake_distdir = 'tahoe-deps'
25     fake_distname = "fakedependency"
26     fake_sdistversion = "9.9.99"
27     fake_bdistversion = "1.0.0"
28     sdist_setup = "raise Exception('Aha I caught you trying to build me. I am a fakedependency 9.9.99 sdist and you should be satisfied with a bdist.')"
29
30     testsuite = "buildtest.test_build_with_fake_dist"
31
32     dist_dirname = os.path.join(os.getcwd(), fake_distdir)
33
34     try:
35         os.makedirs(dist_dirname)
36     except OSError:
37         # probably already exists
38         pass
39
40     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()))
41     try:
42         bdist_egg = zipfile.ZipFile(bdist_egg_name, 'w')
43         bdist_egg.writestr('fakedependency/__init__.py', '__version__ = "%s"\n' % (fake_bdistversion,))
44         bdist_egg.close()
45
46         sdist_name = os.path.join(dist_dirname, '%s-%s.tar' % (fake_distname, fake_sdistversion))
47         sdist = tarfile.open(sdist_name, 'w:gz')
48         sdist.errorlevel = 2
49         tarinfo = tarfile.TarInfo('setup.py')
50         tarinfo.errorlevel = 2
51         tarinfo.size = len(sdist_setup)
52         sdist.addfile(tarinfo, StringIO.StringIO(sdist_setup))
53         sdist.close()
54
55         sys.exit(subprocess.call([sys.executable, "setup.py", "--fakedependency", "-v", "test", "-s", testsuite],
56                                  env=os.environ))
57     finally:
58         os.remove(bdist_egg_name)
59         os.remove(sdist_name)
60         cleanup()
61
62 def cleanup():
63     egg_info = os.path.join('src', 'allmydata_tahoe.egg-info')
64     bin_tahoe = os.path.join('bin', 'tahoe')
65     bin_tahoe_pyscript = os.path.join('bin', 'tahoe.pyscript')
66
67     if os.path.exists('build'):
68         shutil.rmtree('build')
69     if os.path.exists('support'):
70         shutil.rmtree('support')
71     if os.path.exists(egg_info):
72         shutil.rmtree(egg_info)
73     if os.path.exists(bin_tahoe):
74         os.remove(bin_tahoe)
75     if os.path.exists(bin_tahoe_pyscript):
76         os.remove(bin_tahoe_pyscript)
77
78 if __name__ == '__main__':
79     test()