diff options
Diffstat (limited to 'scripts/oe-pkgdata-browser')
-rwxr-xr-x | scripts/oe-pkgdata-browser | 241 |
1 files changed, 241 insertions, 0 deletions
diff --git a/scripts/oe-pkgdata-browser b/scripts/oe-pkgdata-browser new file mode 100755 index 0000000000..09544fff82 --- /dev/null +++ b/scripts/oe-pkgdata-browser | |||
@@ -0,0 +1,241 @@ | |||
1 | #! /usr/bin/env python3 | ||
2 | |||
3 | import os, sys, enum, ast | ||
4 | |||
5 | scripts_path = os.path.dirname(os.path.realpath(__file__)) | ||
6 | lib_path = scripts_path + '/lib' | ||
7 | sys.path = sys.path + [lib_path] | ||
8 | |||
9 | import scriptpath | ||
10 | bitbakepath = scriptpath.add_bitbake_lib_path() | ||
11 | if not bitbakepath: | ||
12 | print("Unable to find bitbake by searching parent directory of this script or PATH") | ||
13 | sys.exit(1) | ||
14 | import bb | ||
15 | |||
16 | import gi | ||
17 | gi.require_version('Gtk', '3.0') | ||
18 | from gi.repository import Gtk, Gdk, GObject | ||
19 | |||
20 | RecipeColumns = enum.IntEnum("RecipeColumns", {"Recipe": 0}) | ||
21 | PackageColumns = enum.IntEnum("PackageColumns", {"Package": 0, "Size": 1}) | ||
22 | FileColumns = enum.IntEnum("FileColumns", {"Filename": 0, "Size": 1}) | ||
23 | |||
24 | import time | ||
25 | def timeit(f): | ||
26 | def timed(*args, **kw): | ||
27 | ts = time.time() | ||
28 | print ("func:%r calling" % f.__name__) | ||
29 | result = f(*args, **kw) | ||
30 | te = time.time() | ||
31 | print ('func:%r args:[%r, %r] took: %2.4f sec' % \ | ||
32 | (f.__name__, args, kw, te-ts)) | ||
33 | return result | ||
34 | return timed | ||
35 | |||
36 | def human_size(nbytes): | ||
37 | import math | ||
38 | suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'] | ||
39 | human = nbytes | ||
40 | rank = 0 | ||
41 | if nbytes != 0: | ||
42 | rank = int((math.log10(nbytes)) / 3) | ||
43 | rank = min(rank, len(suffixes) - 1) | ||
44 | human = nbytes / (1000.0 ** rank) | ||
45 | f = ('%.2f' % human).rstrip('0').rstrip('.') | ||
46 | return '%s %s' % (f, suffixes[rank]) | ||
47 | |||
48 | def load(filename, suffix=None): | ||
49 | from configparser import ConfigParser | ||
50 | from itertools import chain | ||
51 | |||
52 | parser = ConfigParser() | ||
53 | if suffix: | ||
54 | parser.optionxform = lambda option: option.replace("_" + suffix, "") | ||
55 | with open(filename) as lines: | ||
56 | lines = chain(("[fake]",), lines) | ||
57 | parser.read_file(lines) | ||
58 | |||
59 | # TODO extract the data and put it into a real dict so we can transform some | ||
60 | # values to ints? | ||
61 | return parser["fake"] | ||
62 | |||
63 | def find_pkgdata(): | ||
64 | import subprocess | ||
65 | output = subprocess.check_output(("bitbake", "-e"), universal_newlines=True) | ||
66 | for line in output.splitlines(): | ||
67 | if line.startswith("PKGDATA_DIR="): | ||
68 | return line.split("=", 1)[1].strip("\'\"") | ||
69 | # TODO exception or something | ||
70 | return None | ||
71 | |||
72 | def packages_in_recipe(pkgdata, recipe): | ||
73 | """ | ||
74 | Load the recipe pkgdata to determine the list of runtime packages. | ||
75 | """ | ||
76 | data = load(os.path.join(pkgdata, recipe)) | ||
77 | packages = data["PACKAGES"].split() | ||
78 | return packages | ||
79 | |||
80 | def load_runtime_package(pkgdata, package): | ||
81 | return load(os.path.join(pkgdata, "runtime", package), suffix=package) | ||
82 | |||
83 | def recipe_from_package(pkgdata, package): | ||
84 | data = load(os.path.join(pkgdata, "runtime", package), suffix=package) | ||
85 | return data["PN"] | ||
86 | |||
87 | def summary(data): | ||
88 | s = "" | ||
89 | s += "{0[PKG]} {0[PKGV]}-{0[PKGR]}\n{0[LICENSE]}\n{0[SUMMARY]}\n".format(data) | ||
90 | |||
91 | return s | ||
92 | |||
93 | |||
94 | class PkgUi(): | ||
95 | def __init__(self, pkgdata): | ||
96 | self.pkgdata = pkgdata | ||
97 | self.current_recipe = None | ||
98 | self.recipe_iters = {} | ||
99 | self.package_iters = {} | ||
100 | |||
101 | builder = Gtk.Builder() | ||
102 | builder.add_from_file(os.path.join(os.path.dirname(__file__), "oe-pkgdata-browser.glade")) | ||
103 | |||
104 | self.window = builder.get_object("window") | ||
105 | self.window.connect("delete-event", Gtk.main_quit) | ||
106 | |||
107 | self.recipe_store = builder.get_object("recipe_store") | ||
108 | self.recipe_view = builder.get_object("recipe_view") | ||
109 | self.package_store = builder.get_object("package_store") | ||
110 | self.package_view = builder.get_object("package_view") | ||
111 | |||
112 | # Somehow resizable does not get set via builder xml | ||
113 | package_name_column = builder.get_object("package_name_column") | ||
114 | package_name_column.set_resizable(True) | ||
115 | file_name_column = builder.get_object("file_name_column") | ||
116 | file_name_column.set_resizable(True) | ||
117 | |||
118 | self.recipe_view.get_selection().connect("changed", self.on_recipe_changed) | ||
119 | self.package_view.get_selection().connect("changed", self.on_package_changed) | ||
120 | |||
121 | self.package_store.set_sort_column_id(PackageColumns.Package, Gtk.SortType.ASCENDING) | ||
122 | builder.get_object("package_size_column").set_cell_data_func(builder.get_object("package_size_cell"), lambda column, cell, model, iter, data: cell.set_property("text", human_size(model[iter][PackageColumns.Size]))) | ||
123 | |||
124 | self.label = builder.get_object("label1") | ||
125 | self.depends_label = builder.get_object("depends_label") | ||
126 | self.recommends_label = builder.get_object("recommends_label") | ||
127 | self.suggests_label = builder.get_object("suggests_label") | ||
128 | self.provides_label = builder.get_object("provides_label") | ||
129 | |||
130 | self.depends_label.connect("activate-link", self.on_link_activate) | ||
131 | self.recommends_label.connect("activate-link", self.on_link_activate) | ||
132 | self.suggests_label.connect("activate-link", self.on_link_activate) | ||
133 | |||
134 | self.file_store = builder.get_object("file_store") | ||
135 | self.file_store.set_sort_column_id(FileColumns.Filename, Gtk.SortType.ASCENDING) | ||
136 | self.files_view = builder.get_object("files_scrollview") | ||
137 | self.files_label = builder.get_object("files_label") | ||
138 | |||
139 | self.load_recipes() | ||
140 | |||
141 | self.recipe_view.set_cursor(Gtk.TreePath.new_first()) | ||
142 | |||
143 | self.window.show() | ||
144 | |||
145 | def on_link_activate(self, label, url_string): | ||
146 | from urllib.parse import urlparse | ||
147 | url = urlparse(url_string) | ||
148 | if url.scheme == "package": | ||
149 | package = url.path | ||
150 | recipe = recipe_from_package(self.pkgdata, package) | ||
151 | |||
152 | it = self.recipe_iters[recipe] | ||
153 | path = self.recipe_store.get_path(it) | ||
154 | self.recipe_view.set_cursor(path) | ||
155 | self.recipe_view.scroll_to_cell(path) | ||
156 | |||
157 | self.on_recipe_changed(self.recipe_view.get_selection()) | ||
158 | |||
159 | it = self.package_iters[package] | ||
160 | path = self.package_store.get_path(it) | ||
161 | self.package_view.set_cursor(path) | ||
162 | self.package_view.scroll_to_cell(path) | ||
163 | |||
164 | return True | ||
165 | else: | ||
166 | return False | ||
167 | |||
168 | def on_recipe_changed(self, selection): | ||
169 | self.package_store.clear() | ||
170 | self.package_iters = {} | ||
171 | |||
172 | (model, it) = selection.get_selected() | ||
173 | if not it: | ||
174 | return | ||
175 | |||
176 | recipe = model[it][RecipeColumns.Recipe] | ||
177 | for package in packages_in_recipe(self.pkgdata, recipe): | ||
178 | # TODO also show PKG after debian-renaming? | ||
179 | data = load_runtime_package(self.pkgdata, package) | ||
180 | # TODO stash data to avoid reading in on_package_changed | ||
181 | self.package_iters[package] = self.package_store.append([package, int(data["PKGSIZE"])]) | ||
182 | |||
183 | def on_package_changed(self, selection): | ||
184 | self.label.set_text("") | ||
185 | self.file_store.clear() | ||
186 | self.depends_label.hide() | ||
187 | self.recommends_label.hide() | ||
188 | self.suggests_label.hide() | ||
189 | |||
190 | (model, it) = selection.get_selected() | ||
191 | if it is None: | ||
192 | return | ||
193 | |||
194 | package = model[it][PackageColumns.Package] | ||
195 | data = load_runtime_package(self.pkgdata, package) | ||
196 | |||
197 | self.label.set_text(summary(data)) | ||
198 | |||
199 | files = ast.literal_eval(data["FILES_INFO"]) | ||
200 | if files: | ||
201 | self.files_label.set_text("{0} files take {1}.".format(len(files), human_size(int(data["PKGSIZE"])))) | ||
202 | self.files_view.show() | ||
203 | for filename, size in files.items(): | ||
204 | self.file_store.append([filename, size]) | ||
205 | else: | ||
206 | self.files_view.hide() | ||
207 | self.files_label.set_text("This package has no files.") | ||
208 | |||
209 | def update_deps(field, prefix, label, clickable=True): | ||
210 | if field in data: | ||
211 | l = [] | ||
212 | for name, version in bb.utils.explode_dep_versions2(data[field]).items(): | ||
213 | if clickable: | ||
214 | l.append("<a href='package:{0}'>{0}</a> {1}".format(name, " ".join(version))) | ||
215 | else: | ||
216 | l.append("{0} {1}".format(name, " ".join(version))) | ||
217 | label.set_markup(prefix + ", ".join(l)) | ||
218 | label.show() | ||
219 | else: | ||
220 | label.hide() | ||
221 | update_deps("RDEPENDS", "Depends: ", self.depends_label) | ||
222 | update_deps("RRECOMMENDS", "Recommends: ", self.recommends_label) | ||
223 | update_deps("RSUGGESTS", "Suggests: ", self.suggests_label) | ||
224 | update_deps("RPROVIDES", "Provides: ", self.provides_label, clickable=False) | ||
225 | |||
226 | def load_recipes(self): | ||
227 | for recipe in sorted(os.listdir(pkgdata)): | ||
228 | if os.path.isfile(os.path.join(pkgdata, recipe)): | ||
229 | self.recipe_iters[recipe] = self.recipe_store.append([recipe]) | ||
230 | |||
231 | if __name__ == "__main__": | ||
232 | import argparse | ||
233 | |||
234 | parser = argparse.ArgumentParser(description='pkgdata browser') | ||
235 | parser.add_argument('-p', '--pkgdata', help="Optional location of pkgdata") | ||
236 | |||
237 | args = parser.parse_args() | ||
238 | pkgdata = args.pkgdata if args.pkgdata else find_pkgdata() | ||
239 | # TODO assert pkgdata is a directory | ||
240 | window = PkgUi(pkgdata) | ||
241 | Gtk.main() | ||