Last active
July 6, 2019 21:47
-
-
Save jimporter/7bee8aabab359cb886652a949d26ce9f to your computer and use it in GitHub Desktop.
Tool to help debianize Python packages for uploading to a PPA
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/env python | |
import argparse | |
import os | |
import re | |
import subprocess | |
import tarfile | |
from contextlib import contextmanager | |
description = """ | |
pydebianize performs all the necessary steps to build a set of Debian source | |
packages for multiple Debian suites, suitable for uploading to your PPA. After | |
building a source distribution, run pydebianize something like this: | |
pydebianize -d dist/yourpackage-1.2.3.tar.gz precise trusty xenial | |
Once this is finished, you can upload to your PPA with dput: | |
dput ppa:yourusername/yourppa deb_dist/yourpackage_1.2.3-*_source.changes | |
""" | |
@contextmanager | |
def pushd(dirname, mode=0o777, verbose=False, dry=False): | |
if verbose or dry: | |
print('pushd {}'.format(dirname)) | |
if not dry: | |
old = os.getcwd() | |
os.chdir(dirname) | |
yield | |
if verbose or dry: | |
print('popd') | |
if not dry: | |
os.chdir(old) | |
def check_call(args, verbose=False, dry=False, **kwargs): | |
if verbose or dry: | |
print(' '.join(args)) | |
if not dry: | |
subprocess.check_call(args, **kwargs) | |
def split_suite(suite, default_suffix): | |
bits = suite.split(':', 1) | |
if len(bits) == 1: | |
return bits[0], bits[0] + default_suffix | |
return bits | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser( | |
formatter_class=argparse.RawDescriptionHelpFormatter, | |
description=description | |
) | |
parser.add_argument('-d', '--dist', metavar='FILE', required=True, | |
help='the .tar.gz made by `sdist`') | |
parser.add_argument('-s', '--version-suffix', metavar='SUFFIX', | |
default='1', | |
help=('the default version suffix for the debian ' + | |
'version (default: %(default)s)')) | |
parser.add_argument('-x', dest='forward_opts', metavar='OPTION', | |
action='append', default=[], | |
help='arguments to forward to sdist_dsc') | |
parser.add_argument('-v', '--verbose', action='store_true', | |
help='run verbosely') | |
parser.add_argument('-D', '--dry-run', action='store_true', | |
help='display commands without executing') | |
parser.add_argument('suite', nargs='+', | |
help=('the debian suite(s) to package for; specify ' + | |
'as "suite" or "suite:debianversion"')) | |
args = parser.parse_args() | |
with tarfile.open(args.dist) as f: | |
# Get the directory name from the tarball, and convert dev versions to | |
# Debian-friendly syntax (e.g. ".dev0" -> "~dev0"). | |
dist_dir = re.sub(r'\.(?=[A-Za-z])', '~', f.next().name) | |
for s in args.suite: | |
suite, version = split_suite(s, args.version_suffix) | |
check_call( | |
['python', 'setup.py', '--command-packages=stdeb.command', | |
'sdist_dsc', '-P', args.dist, '--suite', suite, | |
'--debian-version', version] + args.forward_opts, | |
verbose=args.verbose, dry=args.dry_run | |
) | |
with pushd('deb_dist/{}'.format(dist_dir), verbose=args.verbose, | |
dry=args.dry_run): | |
check_call(['debuild', '-S', '-sa'], verbose=args.verbose, | |
dry=args.dry_run) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment