]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - misc/build_helpers/build-deb.py
setup: organize misc/ scripts and tools and remove obsolete ones
[tahoe-lafs/tahoe-lafs.git] / misc / build_helpers / build-deb.py
1 #!/bin/false # invoke this with a specific python
2
3 import sys, shutil, os.path
4 from subprocess import Popen, PIPE
5
6 PYTHON = sys.executable
7 ARCH = sys.argv[1]
8
9 class SubprocessError(Exception):
10     pass
11
12 def get_output(*cmd, **kwargs):
13     tolerate_stderr = kwargs.get("tolerate_stderr", False)
14     print " " + " ".join(cmd)
15     p = Popen(cmd, stdout=PIPE)
16     (out,err) = p.communicate()
17     rc = p.returncode
18     if rc != 0:
19         print >>sys.stderr, err
20         raise SubprocessError("command %s exited with rc=%s", (cmd, rc))
21     if err and not tolerate_stderr:
22         print >>sys.stderr, "stderr:", err
23         raise SubprocessError("command emitted unexpected stderr")
24     print " =>", out,
25     return out
26
27 def run(*cmd, **kwargs):
28     print " " + " ".join(cmd)
29 #    if "stdin" in kwargs:
30 #        stdin = kwargs.pop("stdin")
31 #        p = Popen(cmd, stdin=PIPE, **kwargs)
32 #        p.stdin.write(stdin)
33 #        p.stdin.close()
34 #    else:
35 #        p = Popen(cmd, **kwargs)
36     p = Popen(cmd, **kwargs)
37     rc = p.wait()
38     if rc != 0:
39         raise SubprocessError("command %s exited with rc=%s", (cmd, rc))
40
41 # the very first time you run setup.py, it will download+build darcsver and
42 # whatnot, emitting noise to stdout. Run it once (and throw away that junk)
43 # to avoid treating that noise as the package name.
44 run(PYTHON, "setup.py", "darcsver")
45
46 NAME = get_output(PYTHON, "setup.py", "--name").strip()
47 VERSION = get_output(PYTHON, "setup.py", "--version").strip()
48
49 TARBALL = "%s-%s.tar.gz" % (NAME, VERSION)
50 DEBIAN_TARBALL = "%s_%s.orig.tar.gz" % (NAME, VERSION)
51 BUILDDIR = "build/debian/%s-%s" % (NAME, VERSION)
52
53 run(PYTHON, "setup.py", "sdist", "--formats=gztar")
54 if os.path.exists("build/debian"):
55     shutil.rmtree("build/debian")
56 os.makedirs("build/debian")
57 shutil.copyfile("dist/%s" % TARBALL, "build/debian/%s" % DEBIAN_TARBALL)
58 run("tar", "xf", DEBIAN_TARBALL, cwd="build/debian")
59
60 # now modify the tree for debian packaging. This is an algorithmic way of
61 # applying the debian .diff, which factors out some of the similarities
62 # between various debian/ubuntu releases. Everything we do after this point
63 # will show up in the generated .diff, and thus form the debian-specific part
64 # of the source package.
65 DEBDIR = os.path.join(BUILDDIR, "debian")
66 os.makedirs(DEBDIR)
67
68 # The 'aliases' section in setup.cfg causes problems, so get rid of it. We
69 # could get rid of the whole file, but 1: find_links is still sort of useful,
70 # and 2: dpkg-buildpackage prefers to ignore file removal (as opposed to
71 # file-modification)
72
73 #os.unlink(os.path.join(BUILDDIR, "setup.cfg"))
74 SETUPCFG = os.path.join(BUILDDIR, "setup.cfg")
75 lines = open(SETUPCFG, "r").readlines()
76 f = open(SETUPCFG, "w")
77 for l in lines:
78     if l.startswith("[aliases]"):
79         break
80     f.write(l)
81 f.close()
82
83 for n in ["compat", "control", "copyright", "pycompat", "rules"]:
84     fn = "misc/debian/%s.%s" % (n, ARCH)
85     if not os.path.exists(fn):
86         fn = "misc/debian/%s" % n
87     assert os.path.exists(fn)
88     
89     shutil.copyfile(fn, os.path.join(DEBDIR, n))
90     if n == "rules":
91         os.chmod(os.path.join(DEBDIR, n), 0755) # +x
92
93 # We put "local package" on the first line of the changelog entry to suppress
94 # the lintian NMU warnings (since debchange's new entry's "author" will
95 # probably be different than the what the debian/control Maintainer: field
96 # says)
97
98 DISTRIBUTION_MAP = {"sid": "unstable"}
99
100 run("debchange", "--create",
101     "--package", NAME,
102     "--newversion", VERSION+"-1",
103     "--distribution", DISTRIBUTION_MAP.get(ARCH, ARCH),
104     "local package: 'make deb' build", cwd=BUILDDIR)
105
106 # the package is ready to build. 'debuild' will produce the source package
107 # (.dsc+.diff.gz), then build the .deb and produce a .changes file ready for
108 # upload to an APT archive. The build log will go into a .build file.
109
110 run("debuild", "-uc", "-us", cwd=BUILDDIR)