| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 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 |
|
|---|
| 14 |
initFileData = '''\ |
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
specFileData = '''\ |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 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 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
zsh_actions = {"type":"(tap xml source python)", |
|---|
| 156 |
"rpmfile":'_files -g "*.rpm"'} |
|---|
| 157 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 202 |
fp = open(rcFile, 'w') |
|---|
| 203 |
fp.write(rpmrcData) |
|---|
| 204 |
fp.close() |
|---|
| 205 |
|
|---|
| 206 |
return(( tmpDir, rcFile )) |
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
def run(): |
|---|
| 211 |
|
|---|
| 212 |
try: |
|---|
| 213 |
config = MyOptions() |
|---|
| 214 |
config.parseOptions() |
|---|
| 215 |
except usage.error, ue: |
|---|
| 216 |
sys.exit("%s: %s" % (sys.argv[0], ue)) |
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 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 |
|
|---|
| 235 |
if not maintainer: |
|---|
| 236 |
maintainer = 'tap2rpm' |
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
tmp_dir, rpmrc_file = makeBuildDir('/var/tmp') |
|---|
| 240 |
source_dir = os.path.join(tmp_dir, directory) |
|---|
| 241 |
os.makedirs(source_dir) |
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 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 |
|
|---|
| 253 |
os.system('cd "%(tmp_dir)s"; tar cfz "%(tarfile_name)s" "%(directory)s"' |
|---|
| 254 |
% vars()) |
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 273 |
shutil.rmtree(tmp_dir) |
|---|