Skip to content

Instantly share code, notes, and snippets.

@TristanCacqueray
Created July 14, 2017 01:31
Show Gist options
  • Save TristanCacqueray/5461622cc62001dab0539282c9665974 to your computer and use it in GitHub Desktop.
Save TristanCacqueray/5461622cc62001dab0539282c9665974 to your computer and use it in GitHub Desktop.
#!/bin/env python3
import argparse
import json
import sys
import os
import tarfile
import time
import subprocess
from urllib.request import urlopen
PYTHON_SPEC = """%%{?scl:%%scl_package %(pkg_name)s}
%%{!?scl:%%global pkg_name %%{name}}
Name: %%{?scl_prefix}%(pkg_name)s
Version: %(version)s
Release: 1%%{?dist}
Summary: %(short_description)s
License: %(license)s
URL: %(home_page)s
Source0: %(sdist_url)s
BuildArch: noarch
BuildRequires: %%{?scl_prefix}python-devel
BuildRequires: %%{?scl_prefix}python-setuptools
%%{?scl:Requires: %%{scl}-runtime}
%%{?scl:BuildRequires: %%{scl}-runtime}
%%description
%(short_description)s
%%prep
%%autosetup -n %(name)s-%%{version} -p1
%%build
rm -Rf requirements.txt test-requirements.txt *.egg-info
%%{?scl:scl enable %%{scl} - << \EOF}
PBR_VERSION=%%{version} %%{__python3} setup.py build
%%{?scl:EOF}
%%install
%%{?scl:scl enable %%{scl} - << \EOF}
PBR_VERSION=%%{version} %%{__python3} setup.py install -O1 --skip-build --root %%{buildroot}
%%{?scl:EOF}
%%check
%%{?scl:scl enable %%{scl} - << \EOF}
%%{__python3} -c "import %(name)s"
%%{?scl:EOF}
%%files
%%defattr(644,root,root,755)
%%{python3_sitelib}/*
%%changelog
* %(changelog)s
- Initial packaging
"""
class Pkg:
def __init__(self, name):
self.name = name
self.root = "python-%s" % name
def getInfo(self):
uri = "https://pypi.python.org/pypi/%s/json" % self.name
self.inf = json.loads(urlopen(uri).read().decode('utf-8'))
self.sdist = None
for url in self.inf['urls']:
if url["packagetype"] == "sdist":
self.sdist = url
break
if self.sdist is None:
raise RuntimeError("Couldn't find sdist url for %s" % self.name)
def renderSpec(self, pkg_name):
self.getInfo()
template_data = {
'pkg_name': pkg_name,
'sdist_url': self.sdist["url"],
'changelog': time.strftime("%a %b %d %Y Tristan Cacqueray <tdecacqu@redhat.com> - ") + "%s-1" % self.inf["info"]["version"],
'short_description': self.inf["info"]["summary"].split("\n")[0],
}
template_data.update(self.inf["info"])
spec = PYTHON_SPEC % template_data
dir_name = "%s-distgit" % pkg_name
if not os.path.isdir(dir_name):
os.mkdir(dir_name, 0o750)
open("%s/%s.spec" % (dir_name, pkg_name), "w").write(spec)
if not os.path.isdir("%s/.git" % dir_name):
subprocess.Popen(["git", "init", "."], cwd=dir_name).wait()
subprocess.Popen(["git", "add", "%s.spec" % pkg_name], cwd=dir_name).wait()
subprocess.Popen(["git", "commit", "-m", "Initial import"], cwd=dir_name).wait()
def main():
p = argparse.ArgumentParser()
p.add_argument("pip_name")
p.add_argument("pkg_name")
args = p.parse_args()
pkg = Pkg(args.pip_name)
pkg.renderSpec(args.pkg_name)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment