11import json
22import os
3- import re
4- import shlex
53import subprocess
64from pathlib import Path
75
6+ from .._package_version import PackageVersion
7+ from .._utils import run_cmd
88
9- def _run_output (cmd : str , input : str | None = None , env : dict [str , str ] | None = None , cwd : Path | None = None ) -> str :
10- input_data = None
11- if input :
12- input_data = input .encode ()
13- return subprocess .check_output (shlex .split (cmd ), input = input_data , env = env ).strip ().decode ()
149
10+ def _cmd (cmd : str , input_data : str | None = None , env : dict [str , str ] | None = None , cwd : Path | None = None ) -> str :
11+ return run_cmd (cmd , check = True , env = env , input = input_data , text = True , capture_output = True ).stdout .strip ()
1512
16- def get_flags (package_dir : Path , maint_options : str | None = None ) -> dict [str , str ]:
13+
14+ def get_pkg_env (package_dir : Path , maint_options : str | None = None ) -> tuple [dict [str , str ], PackageVersion ]:
1715 """
1816 does what including "/usr/share/dpkg/buildflags.mk" would do.
1917 """
@@ -24,7 +22,7 @@ def get_flags(package_dir: Path, maint_options: str | None = None) -> dict[str,
2422 # TODO more vars as parameters, e.g. DEB_CFLAGS_MAINT_APPEND
2523
2624 # get build flags
27- flags_raw = _run_output ("dpkg-buildflags" , env = result )
25+ flags_raw = _cmd ("dpkg-buildflags" , env = result )
2826 for flag_line in flags_raw .splitlines ():
2927 flag_name , _ , flag_value = flag_line .partition ("=" )
3028 result [flag_name ] = flag_value
@@ -37,29 +35,32 @@ def get_flags(package_dir: Path, maint_options: str | None = None) -> dict[str,
3735
3836 # architecture.mk
3937 if result .get ("DEB_HOST_ARCH" ) is None :
40- arch_flags_raw = _run_output ("dpkg-architecture" , env = result )
38+ arch_flags_raw = _cmd ("dpkg-architecture" , env = result )
4139 for arch_flag_line in arch_flags_raw .splitlines ():
4240 arch_flag_name , _ , arch_flag_value = arch_flag_line .partition ("=" )
4341 result [arch_flag_name ] = arch_flag_value
4442
4543 # pkg-info.mk
4644 if result .get ("DEB_SOURCE" ) is None or result .get ("DEB_VERSION" ) is None :
47- result ["DEB_SOURCE" ] = _run_output ("dpkg-parsechangelog -SSource" , env = result , cwd = package_dir )
48- result ["DEB_VERSION" ] = _run_output ("dpkg-parsechangelog -SVersion" , env = result , cwd = package_dir )
45+ result ["DEB_SOURCE" ] = _cmd ("dpkg-parsechangelog -SSource" , env = result , cwd = package_dir )
46+ result ["DEB_VERSION" ] = _cmd ("dpkg-parsechangelog -SVersion" , env = result , cwd = package_dir )
47+ version = PackageVersion .from_str (result ["DEB_VERSION" ])
48+
4949 # this would return DEB_VERSION in pkg-info.mk if no epoch is in version.
5050 # instead, we return "0" as oritinally intended if no epoch is in version.
51- result ["DEB_VERSION_EPOCH" ] = (
52- "0" if ":" not in result ["DEB_VERSION" ] else re .sub (r"^([0-9]+):.*$" , r"\1" , result ["DEB_VERSION" ])
53- )
54- result ["DEB_VERSION_EPOCH_UPSTREAM" ] = re .sub (r"^(.*?)(-.*)?$" , r"\1" , result ["DEB_VERSION" ])
55- result ["DEB_VERSION_UPSTREAM_REVISION" ] = re .sub (r"^([0-9]*:)?(.*?)$" , r"\2" , result ["DEB_VERSION" ])
56- result ["DEB_VERSION_UPSTREAM" ] = re .sub (r"^([0-9]*:)(.*?)" , r"\2" , result ["DEB_VERSION_EPOCH_UPSTREAM" ])
57- result ["DEB_VERSION_REVISION" ] = re .sub (r"^.*?-([^-]*)$" , r"\1" , result ["DEB_VERSION" ])
58- result ["DEB_DISTRIBUTION" ] = _run_output ("dpkg-parsechangelog -SDistribution" , env = result , cwd = package_dir )
59- result ["DEB_TIMESTAMP" ] = _run_output ("dpkg-parsechangelog -STimestamp" , env = result , cwd = package_dir )
51+ result ["DEB_VERSION_EPOCH" ] = version .epoch
52+ result ["DEB_VERSION_EPOCH_UPSTREAM" ] = version .epoch_upstream
53+ result ["DEB_VERSION_UPSTREAM_REVISION" ] = version .upstream_revision
54+ result ["DEB_VERSION_UPSTREAM" ] = version .upstream
55+ result ["DEB_VERSION_REVISION" ] = version .revision
56+
57+ result ["DEB_DISTRIBUTION" ] = _cmd ("dpkg-parsechangelog -SDistribution" , env = result , cwd = package_dir )
58+ result ["DEB_TIMESTAMP" ] = _cmd ("dpkg-parsechangelog -STimestamp" , env = result , cwd = package_dir )
6059
6160 if result .get ("SOURCE_DATE_EPOCH" ) is None :
6261 result ["SOURCE_DATE_EPOCH" ] = result ["DEB_TIMESTAMP" ]
62+ else :
63+ version = PackageVersion .from_str (result ["DEB_VERSION" ])
6364
6465 if result .get ("ELF_PACKAGE_METADATA" ) is None :
6566 elf_meta = {
@@ -74,4 +75,4 @@ def get_flags(package_dir: Path, maint_options: str | None = None) -> dict[str,
7475
7576 result ["ELF_PACKAGE_METADATA" ] = json .dumps (elf_meta , separators = ("," , ":" ))
7677
77- return result
78+ return result , version
0 commit comments