| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
g> |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
import logging |
| 25 |
import os |
| 26 |
import sys |
| 27 |
from os.path import exists, join |
| 28 |
from shutil import copy as fcopy |
| 29 |
from typing import cast |
| 30 |
|
| 31 |
from dhpython.debhelper import DebHelper, Options |
| 32 |
from dhpython.depends import Dependencies |
| 33 |
from dhpython.interpreter import Interpreter, EXTFILE_RE |
| 34 |
from dhpython.version import supported, default, Version, VersionRange |
| 35 |
from dhpython.pydist import validate as validate_pydist |
| 36 |
from dhpython.fs import fix_locations, Scan |
| 37 |
from dhpython.options import DHPythonOptions, build_parser |
| 38 |
from dhpython.tools import pyinstall, pyremove |
| 39 |
|
| 40 |
|
| 41 |
logging.basicConfig( |
| 42 |
format="%(levelname).1s: dh_python3 " "%(module)s:%(lineno)d: %(message)s" |
| 43 |
) |
| 44 |
log = logging.getLogger("dhpython") |
| 45 |
os.umask(0o22) |
| 46 |
DEFAULT = default("cpython3") |
| 47 |
SUPPORTED = supported("cpython3") |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
class Scanner(Scan): |
| 55 |
def handle_ext(self, fpath: str) -> Version | None: |
| 56 |
_, fname = fpath.rsplit("/", 1) |
| 57 |
if not (m := EXTFILE_RE.search(fname)): |
| 58 |
|
| 59 |
|
| 60 |
return None |
| 61 |
tagver = m.groupdict()["ver"] |
| 62 |
if tagver is None: |
| 63 |
return None |
| 64 |
tagver = Version(f"{tagver[0]}.{tagver[1:]}") |
| 65 |
return tagver |
| 66 |
|
| 67 |
|
| 68 |
def main() -> None: |
| 69 |
parser = build_parser() |
| 70 |
options = parser.parse_args( |
| 71 |
os.environ.get("DH_OPTIONS", "").split() + sys.argv[1:], DHPythonOptions() |
| 72 |
) |
| 73 |
if options.O: |
| 74 |
parser.parse_known_args(options.O, options) |
| 75 |
|
| 76 |
private_dir = options.private_dir |
| 77 |
if private_dir: |
| 78 |
if not private_dir.startswith("/"): |
| 79 |
|
| 80 |
private_dir = "/" + private_dir |
| 81 |
|
| 82 |
if options.skip_private: |
| 83 |
private_dir = None |
| 84 |
|
| 85 |
if options.verbose: |
| 86 |
log.setLevel(logging.DEBUG) |
| 87 |
log.debug("version: DEVELV") |
| 88 |
log.debug("argv: %s", sys.argv) |
| 89 |
log.debug("options: %s", options) |
| 90 |
log.debug( |
| 91 |
"supported Python versions: %s (default=%s)", |
| 92 |
",".join(str(v) for v in SUPPORTED), |
| 93 |
DEFAULT, |
| 94 |
) |
| 95 |
else: |
| 96 |
log.setLevel(logging.INFO) |
| 97 |
|
| 98 |
options.write_log = False |
| 99 |
if os.environ.get("DH_INTERNAL_OVERRIDE", ""): |
| 100 |
options.write_log = True |
| 101 |
|
| 102 |
try: |
| 103 |
dh = DebHelper(cast(Options, options), impl="cpython3") |
| 104 |
except Exception as e: |
| 105 |
log.error("cannot initialize DebHelper: %s", e) |
| 106 |
sys.exit(2) |
| 107 |
if not dh.packages: |
| 108 |
log.error( |
| 109 |
"no package to act on (python3-foo or one with ${python3:Depends} in Depends)" |
| 110 |
) |
| 111 |
|
| 112 |
if not options.vrange and dh.python_version: |
| 113 |
options.vrange = VersionRange(dh.python_version) |
| 114 |
|
| 115 |
interpreter = Interpreter("python3") |
| 116 |
for package, _ in dh.packages.items(): |
| 117 |
log.debug("processing package %s...", package) |
| 118 |
interpreter.debug = package.endswith("-dbg") |
| 119 |
|
| 120 |
if not private_dir: |
| 121 |
try: |
| 122 |
pyinstall(interpreter, package, options.vrange) |
| 123 |
except Exception as err: |
| 124 |
log.error("%s.pyinstall: %s", package, err) |
| 125 |
sys.exit(4) |
| 126 |
try: |
| 127 |
pyremove(interpreter, package, options.vrange) |
| 128 |
except Exception as err: |
| 129 |
log.error("%s.pyremove: %s", package, err) |
| 130 |
sys.exit(5) |
| 131 |
fix_locations(package, interpreter, SUPPORTED, options) |
| 132 |
stats = Scanner(interpreter, package, private_dir, options=options).result |
| 133 |
|
| 134 |
dependencies = Dependencies(package, "cpython3", dh.build_depends) |
| 135 |
dependencies.parse(stats, options) |
| 136 |
|
| 137 |
pyclean_added = False |
| 138 |
if stats["compile"]: |
| 139 |
args = "" |
| 140 |
if options.vrange: |
| 141 |
args += "-V %s" % options.vrange |
| 142 |
dh.autoscript(package, "postinst", "postinst-py3compile", args) |
| 143 |
dh.autoscript(package, "prerm", "prerm-py3clean", "") |
| 144 |
pyclean_added = True |
| 145 |
for pdir, details in sorted(stats["private_dirs"].items()): |
| 146 |
if not details["compile"]: |
| 147 |
continue |
| 148 |
if not pyclean_added: |
| 149 |
dh.autoscript(package, "prerm", "prerm-py3clean", "") |
| 150 |
pyclean_added = True |
| 151 |
|
| 152 |
args = pdir |
| 153 |
|
| 154 |
ext_for = details["ext_vers"] |
| 155 |
ext_no_version = details["ext_no_version"] |
| 156 |
if not ext_for and not ext_no_version: |
| 157 |
shebang_versions = list( |
| 158 |
i.version |
| 159 |
for i in details["shebangs"] |
| 160 |
if i.version and i.version.minor |
| 161 |
) |
| 162 |
if not options.ignore_shebangs and len(shebang_versions) == 1: |
| 163 |
|
| 164 |
args += " -V %s" % shebang_versions[0] |
| 165 |
elif options.vrange: |
| 166 |
args += " -V %s" % options.vrange |
| 167 |
elif ext_no_version: |
| 168 |
|
| 169 |
if options.vrange and "-" not in str(options.vrange): |
| 170 |
ver = str(options.vrange) |
| 171 |
else: |
| 172 |
v = DEFAULT |
| 173 |
for i in details["shebangs"]: |
| 174 |
if i.version and i.version.minor: |
| 175 |
v = i.version |
| 176 |
break |
| 177 |
ver = str(v) |
| 178 |
dependencies.depend("python%s" % ver) |
| 179 |
args += " -V %s" % ver |
| 180 |
else: |
| 181 |
extensions = sorted(ext_for) |
| 182 |
vr = VersionRange(minver=extensions[0], maxver=extensions[-1]) |
| 183 |
args += " -V %s" % vr |
| 184 |
|
| 185 |
for regex in options.regexpr or []: |
| 186 |
args += " -X '%s'" % regex.pattern.replace("'", r"'\''") |
| 187 |
|
| 188 |
dh.autoscript(package, "postinst", "postinst-py3compile", args) |
| 189 |
|
| 190 |
dependencies.export_to(dh) |
| 191 |
|
| 192 |
pydist_file = join("debian", "%s.pydist" % package) |
| 193 |
|
| 194 |
if exists(pydist_file): |
| 195 |
if not validate_pydist(pydist_file): |
| 196 |
log.warning("%s.pydist file is invalid", package) |
| 197 |
else: |
| 198 |
dstdir = join("debian", package, "usr/share/python3/dist/") |
| 199 |
if not exists(dstdir): |
| 200 |
os.makedirs(dstdir) |
| 201 |
fcopy(pydist_file, join(dstdir, package)) |
| 202 |
bcep_file = join("debian", "%s.bcep" % package) |
| 203 |
if exists(bcep_file): |
| 204 |
dstdir = join("debian", package, "usr/share/python3/bcep/") |
| 205 |
if not exists(dstdir): |
| 206 |
os.makedirs(dstdir) |
| 207 |
fcopy(bcep_file, join(dstdir, package)) |
| 208 |
|
| 209 |
dh.save() |
| 210 |
|
| 211 |
|
| 212 |
if __name__ == "__main__": |
| 213 |
main() |