SHOW:
|
|
- or go back to the newest paste.
1 | #!/usr/bin/python3 | |
2 | import sys | |
3 | import subprocess | |
4 | import argparse | |
5 | import os.path | |
6 | ||
7 | def create_proc(cmd): | |
8 | proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
9 | out, err = proc.communicate() | |
10 | out, err = str(out.decode("utf-8")), str(err.decode("utf-8")) | |
11 | exit_code = proc.returncode | |
12 | return {"out": out, "err": err, "exit_code": exit_code} | |
13 | ||
14 | class PkgManager: | |
15 | def __init__(self): | |
16 | self.manager = "" | |
17 | self.variants = { | |
18 | "dpkg": "dpkg --help", | |
19 | "rpm": "rpm --help", | |
20 | "pacman": "pacman --help" | |
21 | } | |
22 | self._define() | |
23 | @staticmethod | |
24 | def print_package(package): | |
25 | spaces = 5 | |
26 | print("Package:" + " "*(len(package["name"])+spaces-len("Package:")) + "Version:") | |
27 | print(package["name"]+" "*spaces+package["version"]) | |
28 | ||
29 | @staticmethod | |
30 | def print_packages(packages): | |
31 | spaces = 5 | |
32 | maxlen_name = max(len(x['name']) for x in packages) | |
33 | maxlen_formatted_name = 0 | |
34 | end_strings = [] | |
35 | for package in packages: | |
36 | st = package["name"] | |
37 | st += " "*(maxlen_name-len(package["name"])+spaces) | |
38 | if len(st) > maxlen_formatted_name: | |
39 | maxlen_formatted_name = len(st) | |
40 | st += package["version"] | |
41 | end_strings.append(st) | |
42 | print("Package:" + " "*(maxlen_formatted_name-len("Package:")) + "Version:") | |
43 | for st in end_strings: | |
44 | print(st) | |
45 | ||
46 | def _define(self): | |
47 | for manager, test_cmd in self.variants.items(): | |
48 | proc = create_proc(test_cmd) | |
49 | exit_code = proc["exit_code"] | |
50 | if exit_code == 0: | |
51 | self.manager = manager | |
52 | ||
53 | def find(self, requested_pkg, find_all=False): | |
54 | package = "" | |
55 | packages = [] | |
56 | if self.manager == "dpkg": | |
57 | package = self._find_dpkg(requested_pkg) | |
58 | packages = self._findall_dpkg(requested_pkg) | |
59 | elif self.manager == "rpm": | |
60 | package = self._find_rpm(requested_pkg) | |
61 | packages = self._findall_rpm(requested_pkg) | |
62 | elif self.manager == "pacman": | |
63 | package = self._find_pacman(requested_pkg) | |
64 | packages = self._findall_pacman(requested_pkg) | |
65 | if package and not find_all: | |
66 | self.print_package(package) | |
67 | return True | |
68 | if not package and not find_all: | |
69 | if packages: | |
70 | print("Package {} not found with {}, but " | |
71 | "were found next packages:\n".format(requested_pkg, self.manager)) | |
72 | self.print_packages(packages) | |
73 | return True | |
74 | if packages and find_all: | |
75 | self.print_packages(packages) | |
76 | return True | |
77 | print("Package( -s) not found with {}".format(self.manager)) | |
78 | return False | |
79 | ||
80 | def _find_dpkg(self, package): | |
81 | package = {"name": package, "version":""} | |
82 | cmd = "dpkg-query --showformat='${{Version}}' --show {}".format(package["name"]) | |
83 | proc = create_proc(cmd) | |
84 | if proc["exit_code"] != 0: | |
85 | #print("Package doesn't exists. dpkg return: {}".format(proc["err"])) | |
86 | return {} | |
87 | package["version"] = proc["out"] | |
88 | return package | |
89 | ||
90 | def _findall_dpkg(self, package): | |
91 | packages = [] | |
92 | cmd = "dpkg -l | awk '{print $2" + ' " " ' + "$3}" + "' | grep '^{}'".format(package) | |
93 | proc = create_proc(cmd) | |
94 | if proc["exit_code"] != 0: | |
95 | #print("Packages doesn't exists. dpkg return: {}".format(proc["err"])) | |
96 | return {} | |
97 | packages_src = proc["out"].split("\n") | |
98 | packages_src = [x for x in packages_src if x] | |
99 | for package in packages_src: | |
100 | package = package.split(" ") | |
101 | packages.append({"name": package[0], "version": package[1]}) | |
102 | return packages | |
103 | ||
104 | def _find_rpm(self, package): | |
105 | package = {"name": package, "version":""} | |
106 | cmd = "rpm -q --queryformat %{{VERSION}} {}".format(package["name"]) | |
107 | proc = create_proc(cmd) | |
108 | if proc["exit_code"] != 0: | |
109 | #print("Package doesn't exists. rpm return: {}".format(proc["out"])) | |
110 | return {} | |
111 | package["version"] = proc["out"] | |
112 | return package | |
113 | ||
114 | def _findall_rpm(self, package): | |
115 | packages = [] | |
116 | cmd = 'rpm -qa --qf "%{NAME} %{VERSION}\n" |' + "grep '^{}'".format(package) | |
117 | proc = create_proc(cmd) | |
118 | if proc["exit_code"] != 0: | |
119 | return {} | |
120 | packages_src = proc["out"].split("\n") | |
121 | packages_src = [x for x in packages_src if x] | |
122 | for package in packages_src: | |
123 | package = package.split(" ") | |
124 | packages.append({"name": package[0], "version": package[1]}) | |
125 | return packages | |
126 | ||
127 | ||
128 | def _find_pacman(self, package): | |
129 | package = {"name": package, "version":""} | |
130 | pass | |
131 | ||
132 | def _findall_pacman(self, package): | |
133 | pass | |
134 | ||
135 | class FileSearch: | |
136 | def __init__(self): | |
137 | self.search_paths = ["/bin/", "/usr/bin", "/sbin", "/usr/local/bin"] | |
138 | ||
139 | def _find_bins(self, package): | |
140 | bin_files = [] | |
141 | for path in self.search_paths: | |
142 | files = os.listdir(path) | |
143 | for f in sorted(files): | |
144 | if f.startswith(package): | |
145 | bin_files.append(os.path.join(path, f)) | |
146 | return bin_files | |
147 | ||
148 | def try_get_version(self, path_to_bin): | |
149 | versions = ["--version", "-v"] | |
150 | for version in versions: | |
151 | cmd = path_to_bin + " " + version | |
152 | proc = create_proc(cmd) | |
153 | if proc["exit_code"] == 0: | |
154 | return proc["out"] | |
155 | else: | |
156 | return "" | |
157 | ||
158 | def find(self, package): | |
159 | bin_files = self._find_bins(package) | |
160 | for bin_file in bin_files: | |
161 | ver = self.try_get_version(bin_file) | |
162 | if ver: | |
163 | print(ver) | |
164 | return True | |
165 | print("Package not found with file search") | |
166 | return False | |
167 | ||
168 | if __name__ == "__main__": | |
169 | pkg_manager = PkgManager() | |
170 | #pkg_manager.find(sys.argv[1]) | |
171 | parser = argparse.ArgumentParser(description="Get version of software") | |
172 | parser.add_argument('-a','--all', action='store_true') | |
173 | parser.add_argument('package') | |
174 | args = parser.parse_args() | |
175 | if not args.package: | |
176 | print("No arguments") | |
177 | sys.exit(0) | |
178 | if args.all: | |
179 | status = pkg_manager.find(args.package, find_all=True) | |
180 | else: | |
181 | status = pkg_manager.find(args.package) | |
182 | ||
183 | if not status: | |
184 | fs = FileSearch() | |
185 | fs.find(args.package) |