1
# based off the tap2deb.py file
2
# tap2rpm.py built by Sean Reifschneider, <jafo@tummy.com>
4
# TODO: need to implement log-file rotation
6
import sys, os, shutil, time, glob
8
from twisted.python import usage
9
from twisted.scripts import tap2deb
12
#################################
13
# data that goes in /etc/inittab
17
# Startup script for a Twisted service.
20
# description: Start-up script for the Twisted service "%(tap_file)s".
22
PATH=/usr/bin:/bin:/usr/sbin:/sbin
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
29
# load init function library
30
. /etc/init.d/functions
32
[ -r /etc/default/%(rpm_file)s ] && . /etc/default/%(rpm_file)s
34
# check for required files
35
if [ ! -x /usr/bin/twistd ]
37
echo "$0: Aborting, no /usr/bin/twistd found"
42
echo "$0: Aborting, no file $file found."
46
# set up run directory if necessary
47
if [ ! -d "${rundir}" ]
55
echo -n "Starting %(rpm_file)s: twistd"
59
--%(twistd_option)s=$file \\
65
echo -n "Stopping %(rpm_file)s: twistd"
66
kill `cat "${pidfile}"`
76
echo "Usage: ${0} {start|stop|restart|}" >&2
84
#######################################
85
# the data for creating the spec file
87
Summary: %(description)s
92
Group: Networking/Daemons
93
Source: %(tarfile_basename)s
94
BuildRoot: /var/tmp/%%{name}-%%{version}-root
95
Requires: /usr/bin/twistd
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"
115
[ ! -z "$RPM_BUILD_ROOT" -a "$RPM_BUILD_ROOT" != '/' ] \
116
&& rm -rf "$RPM_BUILD_ROOT"
119
/sbin/chkconfig --add %(rpm_file)s
120
/sbin/chkconfig --level 35 %(rpm_file)s
121
/etc/init.d/%(rpm_file)s start
124
/etc/init.d/%(rpm_file)s stop
125
/sbin/chkconfig --del %(rpm_file)s
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
133
* %(date)s %(maintainer)s
134
- Created by tap2rpm: %(rpm_file)s (%(version)s)
137
###############################
138
class MyOptions(usage.Options):
139
optFlags = [["unsigned", "u"]]
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'"],
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"}
168
##########################
169
def makeBuildDir(baseDir):
170
'''Set up the temporary directory for building RPMs.
171
Returns: Tuple: ( buildDir, rpmrcFile )
173
import random, string
176
oldMask = os.umask(0077)
178
tmpDir = os.path.join(baseDir, 'tap2rpm-%s-%s' % ( os.getpid(),
179
random.randint(0, 999999999) ))
180
if not os.path.exists(tmpDir):
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'))
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)
201
# set up the rpmrc file
202
fp = open(rcFile, 'w')
206
return(( tmpDir, rcFile ))
214
config.parseOptions()
215
except usage.error, ue:
216
sys.exit("%s: %s" % (sys.argv[0], ue))
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' %
227
long_description = (config['long_description']
228
or "Automatically created by tap2rpm")
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]
234
# set up a blank maintainer if not present
236
maintainer = 'tap2rpm'
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)
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)
253
os.system('cd "%(tmp_dir)s"; tar cfz "%(tarfile_name)s" "%(directory)s"'
257
print 'Starting build...'
260
os.system('rpmbuild -ta --rcfile "%s" %s' % ( rpmrc_file, tarfile_name ))
261
print 'Done with build...'
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, '.')
272
# remove the build directory
273
shutil.rmtree(tmp_dir)