Last active
September 14, 2015 12:51
-
-
Save toabctl/48e0f931ddb413a054c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from __future__ import print_function | |
import argparse | |
import os | |
from jinja2 import Environment, Template, FileSystemLoader | |
from yaml import load, Loader | |
############### | |
# jinja2 filter | |
############### | |
def _filter_noop(value): | |
"""do nothing filter""" | |
return value | |
def _filter_license_spdx2fedora(value): | |
"""convert a given known spdx license to another one""" | |
# more values can be taken from from https://github.com/hughsie/appstream-glib/\ | |
# blob/master/libappstream-builder/asb-package-rpm.c#L76 | |
mapping = { | |
"Apache-1.1": "ASL 1.1", | |
"Apache-2.0": "ASL 2.0", | |
"BSD-3-Clause": "BSD", | |
"GPL-1.0+": "GPL+", | |
"GPL-2.0": "GPLv2", | |
"GPL-2.0+": "GPLv2+", | |
"GPL-3.0": "GPLv3", | |
"GPL-3.0+": "GPLv3+", | |
"LGPL-2.1": "LGPLv2.1", | |
"LGPL-2.1+": "LGPLv2+", | |
"LGPL-2.0": "LGPLv2 with exceptions", | |
"LGPL-2.0+": "LGPLv2+ with exceptions", | |
"LGPL-3.0": "LGPLv3", | |
"LGPL-3.0+": "LGPLv3+", | |
"MIT": "MIT with advertising", | |
"MPL-1.0": "MPLv1.0", | |
"MPL-1.1": "MPLv1.1", | |
"MPL-2.0": "MPLv2.0", | |
"Python-2.0": "Python", | |
} | |
return mapping[value] | |
def _filter_pypackname(value): | |
# FIXME: use pymod2pkg ! | |
return "python-%s" % (value) | |
def generate_spec(spec_style, input_template_path): | |
"""generate a spec file with the given style and the given template""" | |
env = Environment(loader=FileSystemLoader( | |
os.path.dirname(input_template_path))) | |
# register dist specific filter | |
if spec_style == 'SUSE': | |
env.filters['license_normalize'] = _filter_noop | |
elif spec_style == 'Fedora': | |
env.filters['license_normalize'] = _filter_license_spdx2fedora | |
else: | |
raise Exception("Unknown spec style '%s' given" % (spec_style)) | |
# common filters | |
env.filters['pypackname_normalize'] = _filter_pypackname | |
template = env.get_template(os.path.basename(input_template_path)) | |
return template.render() | |
def process_args(): | |
parser = argparse.ArgumentParser( | |
description="Convert a .spec.j2 template into a .spec") | |
parser.add_argument("-o", "--output", | |
help="output filename instead of stdout") | |
parser.add_argument("spec-style", help="distro style you want to use", | |
choices=['SUSE', 'Fedora']) | |
parser.add_argument("input-template", | |
help="specfile jinja2 template to use") | |
return vars(parser.parse_args()) | |
def main(): | |
args = process_args() | |
spec = generate_spec(args['spec-style'], args['input-template']) | |
if args['output']: | |
with open(args['output'], "w") as o: | |
o.write(spec) | |
else: | |
print(spec) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
python tmpl2spec.py SUSE ./python-oslo.i18n.spec.j2
with: