Skip to content

Instantly share code, notes, and snippets.

@toabctl
Last active September 14, 2015 12:51
Show Gist options
  • Save toabctl/48e0f931ddb413a054c2 to your computer and use it in GitHub Desktop.
Save toabctl/48e0f931ddb413a054c2 to your computer and use it in GitHub Desktop.
#!/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()
@toabctl
Copy link
Author

toabctl commented Sep 11, 2015

python tmpl2spec.py SUSE ./python-oslo.i18n.spec.j2 with:

$ cat python-oslo.i18n.spec.j2
%global sname oslo.i18n

Name:           {{ "oslo.i18n" | pypackname_normalize }}
Version:        2.2.0
Release:        0
Summary:        OpenStack i18n library
License:        {{ "Apache-2.0" | license_normalize }}
Group:          Development/Languages/Python
Url:            https://launchpad.net/%{sname}
Source0:        https://pypi.python.org/packages/source/o/%{sname}/%{sname}-%{version}.tar.gz
BuildRequires:  openstack-macros
BuildRequires:  {{ "Babel"  | pypackname_normalize }}
BuildRequires:  {{ "oslotest"  | pypackname_normalize }}
BuildRequires:  {{ "pbr"  | pypackname_normalize }}
BuildRequires:  {{ "setuptools" | pypackname_normalize }}
Requires:       {{ "Babel"  | pypackname_normalize }}
Requires:       {{ "six"  | pypackname_normalize }}
BuildArch:      noarch

%description
The oslo.i18n library contain utilities for working with internationalization
(i18n) features, especially translation for text strings in an application
or library.

%package doc
Summary:        Documentation for OpenStack i18n library
Group:          Development/Languages/Python
BuildRequires:  python-Sphinx
BuildRequires:  python-oslosphinx

%description doc
Documentation for the oslo.i18n library.

%prep
%setup -q -n %{sname}-%{version}

%build
%{__python2} setup.py build

%install
%{__python2} setup.py install --skip-build --root %{buildroot}

# generate html docs
sphinx-build doc/source html
# remove the sphinx-build leftovers
rm -rf html/.{doctrees,buildinfo}

%check
export PYTHONPATH="%{python2_sitelib}:%{buildroot}%{python2_sitelib}"
export PYTHON="python -S"
testr init && testr run --parallel

%files
%defattr(-,root,root,-)
%doc AUTHORS ChangeLog CONTRIBUTING.rst HACKING.rst LICENSE PKG-INFO README.rst
%{python2_sitelib}/oslo_i18n
%{python2_sitelib}/*.egg-info

%files doc
%doc html
%doc LICENSE

%changelog

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment