1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 87 88 89 90 91 92 93 94 95 96 97 98 99
|
#!/usr/bin/python
# Generate deb package sources for each distro
# from the input debian.in directory contents
output_distros = [("xenial", "ubu1604", ""), ("wily", "ubu1510", ""), ("vivid", "ubu1504", ""), ("utopic", "ubu1410", 'paramiko'), ("trusty", "ubu1404",'paramiko'), ("precise", "ubu1204", 'paramiko')]
editions = ["community", "commercial"]
import shutil
import os
def preprocess(inpath, inf, outf, vars):
# Preprocessor accepts
# @ifdef <variable> [<variable>...]
# @else
# @endif
# Where variable is the name of the distro or of the edition
def evaluate(options, distro, edition, bundle, version):
return distro in options or edition in options or bundle in options or version in options
def evaluate_version(options, vars):
version = int(vars['version'])
eval_command = 'version' + ''.join(options)
return eval(eval_command)
conditions = [True]
# @ifndistro and @ifnedition also accepted
for line in inf:
for key, value in vars.items():
line = line.replace('@%s@' % key, value)
if line.startswith("@") and not line.startswith("@@"):
d, _, args = line.strip().partition(" ")
conds = [s.strip() for s in args.split()]
if d == "@ifversion":
conditions.append(evaluate_version(conds, vars))
continue
if d == "@ifdef":
conditions.append(evaluate(conds, vars['distro'], vars['edition'], vars['bundle'], vars['version']))
continue
elif d == "@ifndef":
conditions.append(not evaluate(conds, vars['distro'], vars['edition'], vars['bundle'], vars['version']))
continue
elif d == "@else":
conditions[-1] = not conditions[-1]
continue
elif d == "@endif":
conditions.pop()
continue
else:
print inpath+": unknown directive", line
if conditions[-1]:
outf.write(line)
edition_specific_file_exts = [".menu", ".mime", ".sharedmimeinfo"]
def generate_distro(source_dir, vars):
target_dir = '%s.deb-%s' % (vars['edition'], vars['distro'])
shutil.rmtree(target_dir, ignore_errors=True)
os.mkdir(target_dir)
target_source_dir = os.path.join(target_dir, "source")
os.mkdir(target_source_dir)
for f in os.listdir(source_dir):
inpath = os.path.join(source_dir, f)
outpath = os.path.join(target_dir, f)
if os.path.splitext(inpath)[-1] in edition_specific_file_exts:
if vars['edition'] not in inpath:
continue
if not os.path.isdir(os.path.join(target_dir, f)):
outf = open(outpath, "w+")
preprocess(inpath, open(inpath), outf, vars)
outf.close()
# set the same permissions as the original file
os.chmod(outpath, os.stat(inpath).st_mode)
# always copy this file, since the tar will come with the right README file
shutil.copyfile("../README", os.path.join(target_dir,"copyright"))
shutil.copyfile(os.path.join(source_dir, "source/format"), os.path.join(target_source_dir,"format"))
print target_dir, "generated"
for distro, distro_version, bundle in output_distros:
for edition in editions:
vars = {}
vars['distro'] = distro
vars['distrov'] = distro_version
vars['edition'] = edition
vars['bundle'] = bundle
vars['version'] = distro_version.replace('ubu', '')
generate_distro("debian.in", vars)
|