Last active
September 12, 2020 14:07
-
-
Save cloudhan/122b5222d35131d7322415613aedf8dc to your computer and use it in GitHub Desktop.
semi-auto port deb package from one distro to another.
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
#!/bin/bash | |
EMAIL=noreply@github.com | |
if [[ -z "$2" ]] | |
then | |
PPA_NUM="1" | |
else | |
PPA_NUM="$2" | |
fi | |
set -ex | |
sudo apt update | |
mkdir -p $1 | |
cd $1 | |
rm -r `find . -type d -maxdepth 1 -mindepth 1` || echo | |
apt source $1 | |
sudo mk-build-deps -i $1 | |
# clean changes | |
rm -f *_source.changes | |
cd `find . -type d -maxdepth 1 -mindepth 1` | |
python ~/auto-port-deb/port.py ${PWD} -n cloudhan -m ${EMAIL} -p ${PPA_NUM} | |
debuild -S -sa -k${EMAIL} | |
rm ../*_source.ppa.upload || echo | |
dput ppa:cloudhan/test ../*_source.changes |
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
import os | |
import sys | |
import argparse | |
import apt | |
import re | |
import datetime as dt | |
def gen_changelog(old_changelog, distro, name, email, ppa_num, version_suffix): | |
now = dt.datetime.utcnow().strftime("%a, %d %b %Y %X +0000") | |
changelog_lines = old_changelog.split("\n") | |
package, version, _, urgency = changelog_lines[0].strip().split(" ") | |
version = version.strip("()") | |
match = re.match("(^[.0-9-]+)", version) | |
if match is None or "git" in version: | |
version = version + "ppa" + str(ppa_num) + version_suffix | |
else: | |
version = match.group(0) + "ppa" + str(ppa_num) + version_suffix | |
template = """\ | |
{package} ({version}) {distro}; {urgency} | |
* port to {distro} | |
-- {name} <{email}> {date} | |
""" | |
return template.format(package=package, version=version, distro=distro, | |
urgency=urgency, name=name, email=email, date=now) | |
def patch_rules(old_rules): | |
cflags_line = "CFLAGS:=$(shell dpkg-buildflags --get CFLAGS) -O3" | |
cppflags_line = "CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS) -O3" | |
cxxflags_line = "CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS) -O3" | |
if "CFLAGS" in old_rules or "CXXFLAGS" in old_rules or "CPPFLAGS" in old_rules: | |
raise RuntimeError("cannot auto patch") | |
old_rules = old_rules.split("\n") | |
assert old_rules[0].startswith("#!/usr/bin/make -f") | |
old_rules.insert(1, "") | |
old_rules.insert(1, cxxflags_line) | |
old_rules.insert(1, cppflags_line) | |
old_rules.insert(1, cflags_line) | |
old_rules.insert(1, "") | |
return "\n".join(old_rules) | |
sys.path.append(os.path.dirname(os.path.realpath(__file__))) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument("path") | |
parser.add_argument("--distro", "-d", default="bionic") | |
parser.add_argument("--name", "-n") | |
parser.add_argument("--email", "-m") | |
parser.add_argument("--ppa-num", "-p", type=int, default=1) | |
parser.add_argument("--version-suffix", "-vs", default="~ubuntu18.04") | |
args = parser.parse_args() | |
with open(os.path.join(args.path, "debian/changelog")) as f: | |
new_changelog = gen_changelog( | |
f.read(), args.distro, args.name, args.email, args.ppa_num, args.version_suffix) | |
with open(os.path.join(args.path, "debian/changelog"), "w") as f: | |
f.write(new_changelog) | |
with open(os.path.join(args.path, "debian/rules")) as f: | |
new_rules = patch_rules(f.read()) | |
with open(os.path.join(args.path, "debian/rules"), "w") as f: | |
f.write(new_rules) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment