OLD | NEW |
| (Empty) |
1 # based off the tap2deb.py file | |
2 # tap2rpm.py built by Sean Reifschneider, <jafo@tummy.com> | |
3 | |
4 # TODO: need to implement log-file rotation | |
5 | |
6 import sys, os, shutil, time, glob | |
7 | |
8 from twisted.python import usage | |
9 from twisted.scripts import tap2deb | |
10 | |
11 | |
12 ################################# | |
13 # data that goes in /etc/inittab | |
14 initFileData = '''\ | |
15 #!/bin/sh | |
16 # | |
17 # Startup script for a Twisted service. | |
18 # | |
19 # chkconfig: - 85 15 | |
20 # description: Start-up script for the Twisted service "%(tap_file)s". | |
21 | |
22 PATH=/usr/bin:/bin:/usr/sbin:/sbin | |
23 | |
24 pidfile=/var/run/%(rpm_file)s.pid | |
25 rundir=/var/lib/twisted-taps/%(rpm_file)s/ | |
26 file=/etc/twisted-taps/%(tap_file)s | |
27 logfile=/var/log/%(rpm_file)s.log | |
28 | |
29 # load init function library | |
30 . /etc/init.d/functions | |
31 | |
32 [ -r /etc/default/%(rpm_file)s ] && . /etc/default/%(rpm_file)s | |
33 | |
34 # check for required files | |
35 if [ ! -x /usr/bin/twistd ] | |
36 then | |
37 echo "$0: Aborting, no /usr/bin/twistd found" | |
38 exit 0 | |
39 fi | |
40 if [ ! -r "$file" ] | |
41 then | |
42 echo "$0: Aborting, no file $file found." | |
43 exit 0 | |
44 fi | |
45 | |
46 # set up run directory if necessary | |
47 if [ ! -d "${rundir}" ] | |
48 then | |
49 mkdir -p "${rundir}" | |
50 fi | |
51 | |
52 | |
53 case "$1" in | |
54 start) | |
55 echo -n "Starting %(rpm_file)s: twistd" | |
56 daemon twistd \\ | |
57 --pidfile=$pidfile \\ | |
58 --rundir=$rundir \\ | |
59 --%(twistd_option)s=$file \\ | |
60 --logfile=$logfile | |
61 status %(rpm_file)s | |
62 ;; | |
63 | |
64 stop) | |
65 echo -n "Stopping %(rpm_file)s: twistd" | |
66 kill `cat "${pidfile}"` | |
67 status %(rpm_file)s | |
68 ;; | |
69 | |
70 restart) | |
71 "${0}" stop | |
72 "${0}" start | |
73 ;; | |
74 | |
75 *) | |
76 echo "Usage: ${0} {start|stop|restart|}" >&2 | |
77 exit 1 | |
78 ;; | |
79 esac | |
80 | |
81 exit 0 | |
82 ''' | |
83 | |
84 ####################################### | |
85 # the data for creating the spec file | |
86 specFileData = '''\ | |
87 Summary: %(description)s | |
88 Name: %(rpm_file)s | |
89 Version: %(version)s | |
90 Release: 1 | |
91 Copyright: Unknown | |
92 Group: Networking/Daemons | |
93 Source: %(tarfile_basename)s | |
94 BuildRoot: /var/tmp/%%{name}-%%{version}-root | |
95 Requires: /usr/bin/twistd | |
96 BuildArch: noarch | |
97 | |
98 %%description | |
99 %(long_description)s | |
100 | |
101 %%prep | |
102 %%setup | |
103 %%build | |
104 | |
105 %%install | |
106 [ ! -z "$RPM_BUILD_ROOT" -a "$RPM_BUILD_ROOT" != '/' ] \ | |
107 && rm -rf "$RPM_BUILD_ROOT" | |
108 mkdir -p "$RPM_BUILD_ROOT"/etc/twisted-taps | |
109 mkdir -p "$RPM_BUILD_ROOT"/etc/init.d | |
110 mkdir -p "$RPM_BUILD_ROOT"/var/lib/twisted-taps | |
111 cp "%(tap_file)s" "$RPM_BUILD_ROOT"/etc/twisted-taps/ | |
112 cp "%(rpm_file)s.init" "$RPM_BUILD_ROOT"/etc/init.d/"%(rpm_file)s" | |
113 | |
114 %%clean | |
115 [ ! -z "$RPM_BUILD_ROOT" -a "$RPM_BUILD_ROOT" != '/' ] \ | |
116 && rm -rf "$RPM_BUILD_ROOT" | |
117 | |
118 %%post | |
119 /sbin/chkconfig --add %(rpm_file)s | |
120 /sbin/chkconfig --level 35 %(rpm_file)s | |
121 /etc/init.d/%(rpm_file)s start | |
122 | |
123 %%preun | |
124 /etc/init.d/%(rpm_file)s stop | |
125 /sbin/chkconfig --del %(rpm_file)s | |
126 | |
127 %%files | |
128 %%defattr(-,root,root) | |
129 %%attr(0755,root,root) /etc/init.d/%(rpm_file)s | |
130 %%attr(0660,root,root) /etc/twisted-taps/%(tap_file)s | |
131 | |
132 %%changelog | |
133 * %(date)s %(maintainer)s | |
134 - Created by tap2rpm: %(rpm_file)s (%(version)s) | |
135 ''' | |
136 | |
137 ############################### | |
138 class MyOptions(usage.Options): | |
139 optFlags = [["unsigned", "u"]] | |
140 optParameters = [ | |
141 ["tapfile", "t", "twistd.tap"], | |
142 ["maintainer", "m", ""], | |
143 ["protocol", "p", ""], | |
144 ["description", "e", ""], | |
145 ["long_description", "l", ""], | |
146 ["set-version", "V", "1.0"], | |
147 ["rpmfile", "r", None], | |
148 ["type", "y", "tap", "type of configuration: 'tap', 'xml, " | |
149 "'source' or 'python'"], | |
150 ] | |
151 | |
152 #zsh_altArgDescr = {"foo":"use this description for foo instead"} | |
153 #zsh_multiUse = ["foo", "bar"] | |
154 #zsh_mutuallyExclusive = [("foo", "bar"), ("bar", "baz")] | |
155 zsh_actions = {"type":"(tap xml source python)", | |
156 "rpmfile":'_files -g "*.rpm"'} | |
157 #zsh_actionDescr = {"logfile":"log file name", "random":"random seed"} | |
158 | |
159 | |
160 type_dict = { | |
161 'tap': 'file', | |
162 'python': 'python', | |
163 'source': 'source', | |
164 'xml': 'xml', | |
165 } | |
166 | |
167 | |
168 ########################## | |
169 def makeBuildDir(baseDir): | |
170 '''Set up the temporary directory for building RPMs. | |
171 Returns: Tuple: ( buildDir, rpmrcFile ) | |
172 ''' | |
173 import random, string | |
174 | |
175 # make top directory | |
176 oldMask = os.umask(0077) | |
177 while 1: | |
178 tmpDir = os.path.join(baseDir, 'tap2rpm-%s-%s' % ( os.getpid(), | |
179 random.randint(0, 999999999) )) | |
180 if not os.path.exists(tmpDir): | |
181 os.makedirs(tmpDir) | |
182 break | |
183 os.umask(oldMask) | |
184 | |
185 # set up initial directory contents | |
186 os.makedirs(os.path.join(tmpDir, 'RPMS', 'noarch')) | |
187 os.makedirs(os.path.join(tmpDir, 'SPECS')) | |
188 os.makedirs(os.path.join(tmpDir, 'BUILD')) | |
189 os.makedirs(os.path.join(tmpDir, 'SOURCES')) | |
190 os.makedirs(os.path.join(tmpDir, 'SRPMS')) | |
191 | |
192 # set up rpmmacros file | |
193 macroFile = os.path.join(tmpDir, 'rpmmacros') | |
194 rcFile = os.path.join(tmpDir, 'rpmrc') | |
195 rpmrcData = open('/usr/lib/rpm/rpmrc', 'r').read() | |
196 rpmrcData = string.replace(rpmrcData, '~/.rpmmacros', macroFile) | |
197 fp = open(macroFile, 'w') | |
198 fp.write('%%_topdir %s\n' % tmpDir) | |
199 fp.close() | |
200 | |
201 # set up the rpmrc file | |
202 fp = open(rcFile, 'w') | |
203 fp.write(rpmrcData) | |
204 fp.close() | |
205 | |
206 return(( tmpDir, rcFile )) | |
207 | |
208 | |
209 ########## | |
210 def run(): | |
211 # parse options | |
212 try: | |
213 config = MyOptions() | |
214 config.parseOptions() | |
215 except usage.error, ue: | |
216 sys.exit("%s: %s" % (sys.argv[0], ue)) | |
217 | |
218 # set up some useful local variables | |
219 tap_file = config['tapfile'] | |
220 base_tap_file = os.path.basename(config['tapfile']) | |
221 protocol = (config['protocol'] or os.path.splitext(base_tap_file)[0]) | |
222 rpm_file = config['rpmfile'] or 'twisted-'+protocol | |
223 version = config['set-version'] | |
224 maintainer = config['maintainer'] | |
225 description = config['description'] or ('A TCP server for %(protocol)s' % | |
226 vars()) | |
227 long_description = (config['long_description'] | |
228 or 'Automatically created by tap2deb') | |
229 twistd_option = type_dict[config['type']] | |
230 date = time.strftime('%a %b %d %Y', time.localtime(time.time())) | |
231 directory = rpm_file + '-' + version | |
232 python_version = '%s.%s' % sys.version_info[:2] | |
233 | |
234 # set up a blank maintainer if not present | |
235 if not maintainer: | |
236 maintainer = 'tap2rpm' | |
237 | |
238 # create source archive directory | |
239 tmp_dir, rpmrc_file = makeBuildDir('/var/tmp') | |
240 source_dir = os.path.join(tmp_dir, directory) | |
241 os.makedirs(source_dir) | |
242 | |
243 # populate source directory | |
244 tarfile_name = source_dir + '.tar.gz' | |
245 tarfile_basename = os.path.basename(tarfile_name) | |
246 tap2deb.save_to_file(os.path.join(source_dir, '%s.spec' % rpm_file), | |
247 specFileData % vars()) | |
248 tap2deb.save_to_file(os.path.join(source_dir, '%s.init' % rpm_file), | |
249 initFileData % vars()) | |
250 shutil.copy(tap_file, source_dir) | |
251 | |
252 # create source tar | |
253 os.system('cd "%(tmp_dir)s"; tar cfz "%(tarfile_name)s" "%(directory)s"' | |
254 % vars()) | |
255 | |
256 # build rpm | |
257 print 'Starting build...' | |
258 print '=' * 70 | |
259 sys.stdout.flush() | |
260 os.system('rpmbuild -ta --rcfile "%s" %s' % ( rpmrc_file, tarfile_name )) | |
261 print 'Done with build...' | |
262 print '=' * 70 | |
263 | |
264 # copy the RPMs to the local directory | |
265 rpm_path = glob.glob(os.path.join(tmp_dir, 'RPMS', 'noarch', '*'))[0] | |
266 srpm_path = glob.glob(os.path.join(tmp_dir, 'SRPMS', '*'))[0] | |
267 print 'Writing "%s"...' % os.path.basename(rpm_path) | |
268 shutil.copy(rpm_path, '.') | |
269 print 'Writing "%s"...' % os.path.basename(srpm_path) | |
270 shutil.copy(srpm_path, '.') | |
271 | |
272 # remove the build directory | |
273 shutil.rmtree(tmp_dir) | |
OLD | NEW |