| 1 |
#!/usr/bin/python3 |
| 2 |
import os |
| 3 |
import re |
| 4 |
import subprocess |
| 5 |
import tempfile |
| 6 |
|
| 7 |
import debian.deb822 |
| 8 |
import debian.debian_support |
| 9 |
|
| 10 |
from mini_buildd import cli, util |
| 11 |
|
| 12 |
#: Needed for man page hack in setup.py |
| 13 |
DESCRIPTION = "Import package from an old mini-buildd 08x distribution into a reprepro distribution in cwd." |
| 14 |
|
| 15 |
|
| 16 |
def call(cmd, show=True, dry_run=False): |
| 17 |
if show: |
| 18 |
print(f"Calling (dry_run={dry_run}): {cmd}") |
| 19 |
|
| 20 |
if dry_run: |
| 21 |
return 0, f"Dry run, skipped: {cmd}" |
| 22 |
|
| 23 |
with tempfile.TemporaryFile() as output: |
| 24 |
res = subprocess.call(cmd.split(), stdout=output, stderr=subprocess.STDOUT) |
| 25 |
output.seek(0) |
| 26 |
output_str = output.read().decode("UTF-8") |
| 27 |
if show: |
| 28 |
print(f"Result={res}:") |
| 29 |
print(output_str) |
| 30 |
return res, output_str |
| 31 |
|
| 32 |
|
| 33 |
def cmp_versions(pair0, pair1): |
| 34 |
"""Compare Debian package versions (on first item of pair)""" |
| 35 |
return debian.debian_support.version_compare(pair0[0], pair1[0]) |
| 36 |
|
| 37 |
|
| 38 |
class CLI(cli.CLI): |
| 39 |
def __init__(self): |
| 40 |
super().__init__("mini-buildd-import-08x", DESCRIPTION) |
| 41 |
self.parser.add_argument("path", help="complete path to a 08x dist repo") |
| 42 |
self.parser.add_argument("distribution", help="reprepro distribution to import to") |
| 43 |
self.parser.add_argument("-n", "--dry-run", action='store_true', |
| 44 |
help="dry run, just show what commands would be run") |
| 45 |
|
| 46 |
def runcli(self): |
| 47 |
# Check we are in a reprepro dist |
| 48 |
assert os.path.exists("conf/distributions"), "No reprepro repository detected in cwd." |
| 49 |
|
| 50 |
# Check 08x path |
| 51 |
old_dist = os.path.basename(self.args.path) |
| 52 |
old_split = old_dist.split("-") |
| 53 |
assert len(old_split) >= 2 and len(old_split) <= 3, f"Malformed 08x dist '{old_dist}'" |
| 54 |
|
| 55 |
old_codename, old_repoid = old_split[0], old_split[1] |
| 56 |
|
| 57 |
codename, repoid, _suite = self.args.distribution.split("-") |
| 58 |
assert old_codename == codename, f"08x and 1.0 base dist (codename) are DIFFERENT: {old_codename} vs {codename}" |
| 59 |
|
| 60 |
print(f""" |
| 61 |
Parsing packages from '{self.args.path}'... |
| 62 |
""") |
| 63 |
|
| 64 |
# { PACKAGE: {VERSION: {"dsc": dsc, 'debs': [deb, deb,...]}} |
| 65 |
packages = {} |
| 66 |
|
| 67 |
for dsc in debian.deb822.Sources.iter_paragraphs(util.fopen(os.path.join(self.args.path, "Sources"))): |
| 68 |
print("Scanning source", dsc["package"], dsc["version"]) |
| 69 |
v = packages.setdefault(dsc["package"], {}) |
| 70 |
v[dsc["version"]] = {"dsc": dsc, "debs": []} |
| 71 |
for deb in debian.deb822.Packages.iter_paragraphs(util.fopen(os.path.join(self.args.path, "Packages"))): |
| 72 |
if deb.get("source", deb["package"]) == dsc["package"] and deb["version"] == dsc["version"]: |
| 73 |
v[dsc["version"]]["debs"].append(deb) |
| 74 |
|
| 75 |
if old_repoid != repoid: |
| 76 |
input(f""" |
| 77 |
WARN: Old/new repo ids different: '{old_repoid}' vs '{repoid}'. |
| 78 |
WARN: If this is just a typo, STOP HERE. |
| 79 |
WARN: If you did not create a new repo with the same id, and/or want to force the packages in, force continue. |
| 80 |
WARN: Continue (Ctr-C to cancel)?""") |
| 81 |
|
| 82 |
print(f""" |
| 83 |
{old_dist}: {len(packages)} source packages. Trying to import to '{self.args.distribution}' in reprepo/cwd |
| 84 |
""") |
| 85 |
input(f"Start import (dry_run={self.args.dry_run}) (Ctrl-C to cancel)?") |
| 86 |
print() |
| 87 |
|
| 88 |
for package, versions in packages.items(): |
| 89 |
# The only possible check is to check that repropro's ls output is empty |
| 90 |
res, output = call(f"reprepro list {self.args.distribution} {package}", show=False) |
| 91 |
if output: |
| 92 |
print(f"** Skipping {package}: Already in dist '{self.args.distribution}'") |
| 93 |
else: |
| 94 |
print(f"** Importing {package}") |
| 95 |
r = -1 |
| 96 |
for v, items in sorted(list(versions.items()), cmp=cmp_versions, reverse=True): |
| 97 |
dist = self.args.distribution |
| 98 |
if r >= 0: |
| 99 |
dist += f"-rollback{r}" |
| 100 |
r += 1 |
| 101 |
print(f"* Importing {package}-{v} to {dist}") |
| 102 |
|
| 103 |
dsc_file = None |
| 104 |
for s in items["dsc"]["files"]: |
| 105 |
if re.compile(r"^.*\.dsc$").match(s["name"]): |
| 106 |
dsc_file = os.path.join(self.args.path, os.path.basename(s["name"])) |
| 107 |
break |
| 108 |
|
| 109 |
res, output = call(f"reprepro includedsc {dist} {dsc_file}", dry_run=self.args.dry_run) |
| 110 |
if res != 0: |
| 111 |
print("WARN: includedsc failed; retrying with priority/section=extra/misc...") |
| 112 |
call(f"reprepro --priority extra --section misc includedsc {dist} {dsc_file}", dry_run=self.args.dry_run) |
| 113 |
|
| 114 |
for deb in items["debs"]: |
| 115 |
deb_file = os.path.join(self.args.path, os.path.basename(deb["filename"])) |
| 116 |
_dummy, ext = os.path.splitext(deb_file) |
| 117 |
typ = ext[1:] |
| 118 |
call(f"reprepro include{typ} {dist} {deb_file}", dry_run=self.args.dry_run) |
| 119 |
print() |
| 120 |
|
| 121 |
|
| 122 |
if __name__ == "__main__": |
| 123 |
CLI().run() |