root / trunk / twisted / scripts / tap2deb.py

Revision 24240, 7.2 kB (checked in by exarkun, 1 year ago)

Merge pypy-compatibility-3318

Author: fijal
Reviewer: therve
Fixes: #3318

Remove a number of occurrences of the assumption that refcounting will cause
bytes written to a file without a flush or close to be available to be read
from that file immediately.. Also fix one occurrence of the assumption that
without an explicit call to gc.collect() a synchronously failing Deferred will
log its failure in a timely manner.

Line 
1 # Copyright (c) 2001-2004 Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4
5
6 import sys, os, string, shutil
7
8 from twisted.python import usage
9
10 class MyOptions(usage.Options):
11     optFlags = [["unsigned", "u"]]
12     optParameters = [["tapfile", "t", "twistd.tap"],
13                   ["maintainer", "m", "", "The maintainer's name and email in a specific format: "
14                    "'John Doe <johndoe@example.com>'"],
15                   ["protocol", "p", ""],
16                   ["description", "e", ""],
17                   ["long_description", "l", ""],
18                   ["set-version", "V", "1.0"],
19                   ["debfile", "d", None],
20                   ["type", "y", "tap", "type of configuration: 'tap', 'xml, 'source' or 'python' for .tac files"]]
21
22     #zsh_altArgDescr = {"foo":"use this description for foo instead"}
23     #zsh_multiUse = ["foo", "bar"]
24     #zsh_mutuallyExclusive = [("foo", "bar"), ("bar", "baz")]
25     zsh_actions = {"type":"(tap xml source python)"}
26     #zsh_actionDescr = {"logfile":"log file name", "random":"random seed"}
27
28     def postOptions(self):
29         if not self["maintainer"]:
30             raise usage.UsageError, "maintainer must be specified."
31
32
33 type_dict = {
34 'tap': 'file',
35 'python': 'python',
36 'source': 'source',
37 'xml': 'xml',
38 }
39
40 def save_to_file(file, text):
41     f = open(file, 'w')
42     f.write(text)
43     f.close()
44
45
46 def run():
47
48     try:
49         config = MyOptions()
50         config.parseOptions()
51     except usage.error, ue:
52         sys.exit("%s: %s" % (sys.argv[0], ue))
53
54     tap_file = config['tapfile']
55     base_tap_file = os.path.basename(config['tapfile'])
56     protocol = (config['protocol'] or os.path.splitext(base_tap_file)[0])
57     deb_file = config['debfile'] or 'twisted-'+protocol
58     version = config['set-version']
59     maintainer = config['maintainer']
60     description = config['description'] or ('A Twisted-based server for %(protocol)s' %
61                                             vars())
62     long_description = config['long_description'] or 'Automatically created by tap2deb'
63     twistd_option = type_dict[config['type']]
64     date = string.strip(os.popen('822-date').read())
65     directory = deb_file + '-' + version
66     python_version = '%s.%s' % sys.version_info[:2]
67
68     if os.path.exists(os.path.join('.build', directory)):
69         os.system('rm -rf %s' % os.path.join('.build', directory))
70     os.makedirs(os.path.join('.build', directory, 'debian'))
71
72     shutil.copy(tap_file, os.path.join('.build', directory))
73
74     save_to_file(os.path.join('.build', directory, 'debian', 'README.Debian'),
75     '''This package was auto-generated by tap2deb\n''')
76
77     save_to_file(os.path.join('.build', directory, 'debian', 'conffiles'),
78     '''\
79 /etc/init.d/%(deb_file)s
80 /etc/default/%(deb_file)s
81 /etc/%(base_tap_file)s
82 ''' % vars())
83
84     save_to_file(os.path.join('.build', directory, 'debian', 'default'),
85     '''\
86 pidfile=/var/run/%(deb_file)s.pid
87 rundir=/var/lib/%(deb_file)s/
88 file=/etc/%(tap_file)s
89 logfile=/var/log/%(deb_file)s.log
90  ''' % vars())
91
92     save_to_file(os.path.join('.build', directory, 'debian', 'init.d'),
93     '''\
94 #!/bin/sh
95
96 PATH=/sbin:/bin:/usr/sbin:/usr/bin
97
98 pidfile=/var/run/%(deb_file)s.pid \
99 rundir=/var/lib/%(deb_file)s/ \
100 file=/etc/%(tap_file)s \
101 logfile=/var/log/%(deb_file)s.log
102
103 [ -r /etc/default/%(deb_file)s ] && . /etc/default/%(deb_file)s
104
105 test -x /usr/bin/twistd%(python_version)s || exit 0
106 test -r $file || exit 0
107 test -r /usr/share/%(deb_file)s/package-installed || exit 0
108
109
110 case "$1" in
111     start)
112         echo -n "Starting %(deb_file)s: twistd"
113         start-stop-daemon --start --quiet --exec /usr/bin/twistd%(python_version)s -- \
114                           --pidfile=$pidfile \
115                           --rundir=$rundir \
116                           --%(twistd_option)s=$file \
117                           --logfile=$logfile
118         echo "."       
119     ;;
120
121     stop)
122         echo -n "Stopping %(deb_file)s: twistd"
123         start-stop-daemon --stop --quiet  \
124             --pidfile $pidfile
125         echo "."       
126     ;;
127
128     restart)
129         $0 stop
130         $0 start
131     ;;
132
133     force-reload)
134         $0 restart
135     ;;
136
137     *)
138         echo "Usage: /etc/init.d/%(deb_file)s {start|stop|restart|force-reload}" >&2
139         exit 1
140     ;;
141 esac
142
143 exit 0
144 ''' % vars())
145
146     os.chmod(os.path.join('.build', directory, 'debian', 'init.d'), 0755)
147
148     save_to_file(os.path.join('.build', directory, 'debian', 'postinst'),
149     '''\
150 #!/bin/sh
151 update-rc.d %(deb_file)s defaults >/dev/null
152 invoke-rc.d %(deb_file)s start
153 ''' % vars())
154
155     save_to_file(os.path.join('.build', directory, 'debian', 'prerm'),
156     '''\
157 #!/bin/sh
158 invoke-rc.d %(deb_file)s stop
159 ''' % vars())
160
161     save_to_file(os.path.join('.build', directory, 'debian', 'postrm'),
162     '''\
163 #!/bin/sh
164 if [ "$1" = purge ]; then
165         update-rc.d %(deb_file)s remove >/dev/null
166 fi
167 ''' % vars())
168
169     save_to_file(os.path.join('.build', directory, 'debian', 'changelog'),
170     '''\
171 %(deb_file)s (%(version)s) unstable; urgency=low
172
173   * Created by tap2deb
174
175  -- %(maintainer)s  %(date)s
176
177 ''' % vars())
178
179     save_to_file(os.path.join('.build', directory, 'debian', 'control'),
180     '''\
181 Source: %(deb_file)s
182 Section: net
183 Priority: extra
184 Maintainer: %(maintainer)s
185 Build-Depends-Indep: debhelper
186 Standards-Version: 3.5.6
187
188 Package: %(deb_file)s
189 Architecture: all
190 Depends: python%(python_version)s-twisted
191 Description: %(description)s
192  %(long_description)s
193 ''' % vars())
194
195     save_to_file(os.path.join('.build', directory, 'debian', 'copyright'),
196     '''\
197 This package was auto-debianized by %(maintainer)s on
198 %(date)s
199
200 It was auto-generated by tap2deb
201
202 Upstream Author(s):
203 Moshe Zadka <moshez@twistedmatrix.com> -- tap2deb author
204
205 Copyright:
206
207 Insert copyright here.
208 ''' % vars())
209
210     save_to_file(os.path.join('.build', directory, 'debian', 'dirs'),
211     '''\
212 etc/init.d
213 etc/default
214 var/lib/%(deb_file)s
215 usr/share/doc/%(deb_file)s
216 usr/share/%(deb_file)s
217 ''' % vars())
218
219     save_to_file(os.path.join('.build', directory, 'debian', 'rules'),
220     '''\
221 #!/usr/bin/make -f
222
223 export DH_COMPAT=1
224
225 build: build-stamp
226 build-stamp:
227         dh_testdir
228         touch build-stamp
229
230 clean:
231         dh_testdir
232         dh_testroot
233         rm -f build-stamp install-stamp
234         dh_clean
235
236 install: install-stamp
237 install-stamp: build-stamp
238         dh_testdir
239         dh_testroot
240         dh_clean -k
241         dh_installdirs
242
243         # Add here commands to install the package into debian/tmp.
244         cp %(base_tap_file)s debian/tmp/etc/
245         cp debian/init.d debian/tmp/etc/init.d/%(deb_file)s
246         cp debian/default debian/tmp/etc/default/%(deb_file)s
247         cp debian/copyright debian/tmp/usr/share/doc/%(deb_file)s/
248         cp debian/README.Debian debian/tmp/usr/share/doc/%(deb_file)s/
249         touch debian/tmp/usr/share/%(deb_file)s/package-installed
250         touch install-stamp
251
252 binary-arch: build install
253
254 binary-indep: build install
255         dh_testdir
256         dh_testroot
257         dh_strip
258         dh_compress
259         dh_installchangelogs
260         dh_fixperms
261         dh_installdeb
262         dh_shlibdeps
263         dh_gencontrol
264         dh_md5sums
265         dh_builddeb
266
267 source diff:                                                                 
268         @echo >&2 'source and diff are obsolete - use dpkg-source -b'; false
269
270 binary: binary-indep binary-arch
271 .PHONY: build clean binary-indep binary-arch binary install
272 ''' % vars())
273
274     os.chmod(os.path.join('.build', directory, 'debian', 'rules'), 0755)
275
276     os.chdir('.build/%(directory)s' % vars())
277     os.system('dpkg-buildpackage -rfakeroot'+ ['', ' -uc -us'][config['unsigned']])
278
279 if __name__ == '__main__':
280     run()
281
Note: See TracBrowser for help on using the browser.