Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 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 | builder.get_object("file_size_column").set_cell_data_func(builder.get_object("file_size_cell"), lambda column, cell, model, iter, data: cell.set_property("text", human_size(model[iter][FileColumns.Size]))) |
| 137 | |
| 138 | self.files_view = builder.get_object("files_scrollview") |
| 139 | self.files_label = builder.get_object("files_label") |
| 140 | |
| 141 | self.load_recipes() |
| 142 | |
| 143 | self.recipe_view.set_cursor(Gtk.TreePath.new_first()) |
| 144 | |
| 145 | self.window.show() |
| 146 | |
| 147 | def on_link_activate(self, label, url_string): |
| 148 | from urllib.parse import urlparse |
| 149 | url = urlparse(url_string) |
| 150 | if url.scheme == "package": |
| 151 | package = url.path |
| 152 | recipe = recipe_from_package(self.pkgdata, package) |
| 153 | |
| 154 | it = self.recipe_iters[recipe] |
| 155 | path = self.recipe_store.get_path(it) |
| 156 | self.recipe_view.set_cursor(path) |
| 157 | self.recipe_view.scroll_to_cell(path) |
| 158 | |
| 159 | self.on_recipe_changed(self.recipe_view.get_selection()) |
| 160 | |
| 161 | it = self.package_iters[package] |
| 162 | path = self.package_store.get_path(it) |
| 163 | self.package_view.set_cursor(path) |
| 164 | self.package_view.scroll_to_cell(path) |
| 165 | |
| 166 | return True |
| 167 | else: |
| 168 | return False |
| 169 | |
| 170 | def on_recipe_changed(self, selection): |
| 171 | self.package_store.clear() |
| 172 | self.package_iters = {} |
| 173 | |
| 174 | (model, it) = selection.get_selected() |
| 175 | if not it: |
| 176 | return |
| 177 | |
| 178 | recipe = model[it][RecipeColumns.Recipe] |
| 179 | packages = packages_in_recipe(self.pkgdata, recipe) |
| 180 | for package in packages: |
| 181 | # TODO also show PKG after debian-renaming? |
| 182 | data = load_runtime_package(self.pkgdata, package) |
| 183 | # TODO stash data to avoid reading in on_package_changed |
| 184 | self.package_iters[package] = self.package_store.append([package, int(data["PKGSIZE"])]) |
| 185 | |
| 186 | package = recipe if recipe in packages else sorted(packages)[0] |
| 187 | path = self.package_store.get_path(self.package_iters[package]) |
| 188 | self.package_view.set_cursor(path) |
| 189 | self.package_view.scroll_to_cell(path) |
| 190 | |
| 191 | def on_package_changed(self, selection): |
| 192 | self.label.set_text("") |
| 193 | self.file_store.clear() |
| 194 | self.depends_label.hide() |
| 195 | self.recommends_label.hide() |
| 196 | self.suggests_label.hide() |
| 197 | self.provides_label.hide() |
| 198 | self.files_view.hide() |
| 199 | self.files_label.hide() |
| 200 | |
| 201 | (model, it) = selection.get_selected() |
| 202 | if it is None: |
| 203 | return |
| 204 | |
| 205 | package = model[it][PackageColumns.Package] |
| 206 | data = load_runtime_package(self.pkgdata, package) |
| 207 | |
| 208 | self.label.set_text(summary(data)) |
| 209 | |
| 210 | files = ast.literal_eval(data["FILES_INFO"]) |
| 211 | if files: |
| 212 | self.files_label.set_text("{0} files take {1}.".format(len(files), human_size(int(data["PKGSIZE"])))) |
| 213 | self.files_view.show() |
| 214 | for filename, size in files.items(): |
| 215 | self.file_store.append([filename, size]) |
| 216 | else: |
| 217 | self.files_view.hide() |
| 218 | self.files_label.set_text("This package has no files.") |
| 219 | self.files_label.show() |
| 220 | |
| 221 | def update_deps(field, prefix, label, clickable=True): |
| 222 | if field in data: |
| 223 | l = [] |
| 224 | for name, version in bb.utils.explode_dep_versions2(data[field]).items(): |
| 225 | if clickable: |
| 226 | l.append("<a href='package:{0}'>{0}</a> {1}".format(name, " ".join(version)).strip()) |
| 227 | else: |
| 228 | l.append("{0} {1}".format(name, " ".join(version)).strip()) |
| 229 | label.set_markup(prefix + ", ".join(l)) |
| 230 | label.show() |
| 231 | else: |
| 232 | label.hide() |
| 233 | update_deps("RDEPENDS", "Depends: ", self.depends_label) |
| 234 | update_deps("RRECOMMENDS", "Recommends: ", self.recommends_label) |
| 235 | update_deps("RSUGGESTS", "Suggests: ", self.suggests_label) |
| 236 | update_deps("RPROVIDES", "Provides: ", self.provides_label, clickable=False) |
| 237 | |
| 238 | def load_recipes(self): |
| 239 | for recipe in sorted(os.listdir(pkgdata)): |
| 240 | if os.path.isfile(os.path.join(pkgdata, recipe)): |
| 241 | self.recipe_iters[recipe] = self.recipe_store.append([recipe]) |
| 242 | |
| 243 | if __name__ == "__main__": |
| 244 | import argparse |
| 245 | |
| 246 | parser = argparse.ArgumentParser(description='pkgdata browser') |
| 247 | parser.add_argument('-p', '--pkgdata', help="Optional location of pkgdata") |
| 248 | |
| 249 | args = parser.parse_args() |
| 250 | pkgdata = args.pkgdata if args.pkgdata else find_pkgdata() |
| 251 | # TODO assert pkgdata is a directory |
| 252 | window = PkgUi(pkgdata) |
| 253 | Gtk.main() |