Coverage for pyguymer3/find_program_version.py: 5%
20 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
1#!/usr/bin/env python3
3# Define function ...
4def find_program_version(
5 prog,
6 /,
7 *,
8 pkgPath = None,
9 portPath = None,
10 timeout = 60.0,
11 zypperPath = None,
12):
13 # Import standard modules ...
14 import shutil
15 import subprocess
17 # **************************************************************************
19 # Try to find the paths if the user did not provide them ...
20 if pkgPath is None:
21 pkgPath = shutil.which("pkg")
22 if portPath is None:
23 portPath = shutil.which("port")
24 if zypperPath is None:
25 zypperPath = shutil.which("zypper")
27 # Try lots of different package managers ...
28 if pkgPath is not None:
29 # NOTE: It is FreeBSD.
31 # Find raw string ...
32 resp = subprocess.run(
33 [
34 pkgPath,
35 "info",
36 prog,
37 ],
38 check = True,
39 encoding = "utf-8",
40 stderr = subprocess.STDOUT,
41 stdout = subprocess.PIPE,
42 timeout = timeout,
43 )
44 elif portPath is not None:
45 # NOTE: It is MacPorts.
47 # Find raw string ...
48 resp = subprocess.run(
49 [
50 portPath,
51 "info",
52 "--version",
53 prog,
54 ],
55 check = True,
56 encoding = "utf-8",
57 stderr = subprocess.STDOUT,
58 stdout = subprocess.PIPE,
59 timeout = timeout,
60 )
61 elif zypperPath is not None:
62 # NOTE: It is OpenSUSE.
64 # Find raw string ...
65 resp = subprocess.run(
66 [
67 zypperPath,
68 "--disable-repositories",
69 "info",
70 prog,
71 ],
72 check = True,
73 encoding = "utf-8",
74 stderr = subprocess.STDOUT,
75 stdout = subprocess.PIPE,
76 timeout = timeout,
77 )
78 else:
79 raise Exception("neither \"pkg\" nor \"port\" nor \"zypper\" have been found") from None
81 # Find clean string ...
82 for line in resp.stdout.splitlines():
83 if line.strip().lower().startswith("version"):
84 return line.strip().lower().split(":")[1].strip()
86 # Create final catch-all ...
87 raise Exception(f"failed to extract version number for \"{prog}\"") from None