support/scripts/pkg-stats: allow to run script outside of the top-level directory

Currently, pkg-stats expects being executed from Buildroot's top-level
source directory. As we are going to extend pkg-stats to cover only
the packages available in the current configuration, it makes sense to
be able to run it from the output directory, which can be anywhere
compared to Buildroot's top-level directory.

This commit adjusts pkg-stats to this, by inferring all Buildroot
paths based on the location of the pkg-stats script itself.

Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
This commit is contained in:
Thomas Petazzoni 2020-11-05 17:30:19 +01:00 committed by Peter Korsgaard
parent 091e2aec68
commit ae86067a15

View File

@ -28,7 +28,9 @@ import subprocess
import json import json
import sys import sys
sys.path.append('utils/') brpath = os.path.normpath(os.path.join(os.path.dirname(__file__), "..", ".."))
sys.path.append(os.path.join(brpath, "utils"))
from getdeveloperlib import parse_developers # noqa: E402 from getdeveloperlib import parse_developers # noqa: E402
import cve as cvecheck # noqa: E402 import cve as cvecheck # noqa: E402
@ -66,7 +68,7 @@ def get_defconfig_list():
""" """
return [ return [
Defconfig(name[:-len('_defconfig')], os.path.join('configs', name)) Defconfig(name[:-len('_defconfig')], os.path.join('configs', name))
for name in os.listdir('configs') for name in os.listdir(os.path.join(brpath, 'configs'))
if name.endswith('_defconfig') if name.endswith('_defconfig')
] ]
@ -108,9 +110,10 @@ class Package:
Fills in the .url field Fills in the .url field
""" """
self.status['url'] = ("warning", "no Config.in") self.status['url'] = ("warning", "no Config.in")
for filename in os.listdir(os.path.dirname(self.path)): pkgdir = os.path.dirname(os.path.join(brpath, self.path))
for filename in os.listdir(pkgdir):
if fnmatch.fnmatch(filename, 'Config.*'): if fnmatch.fnmatch(filename, 'Config.*'):
fp = open(os.path.join(os.path.dirname(self.path), filename), "r") fp = open(os.path.join(pkgdir, filename), "r")
for config_line in fp: for config_line in fp:
if URL_RE.match(config_line): if URL_RE.match(config_line):
self.url = config_line.strip() self.url = config_line.strip()
@ -138,7 +141,7 @@ class Package:
Fills in the .infras field Fills in the .infras field
""" """
self.infras = list() self.infras = list()
with open(self.path, 'r') as f: with open(os.path.join(brpath, self.path), 'r') as f:
lines = f.readlines() lines = f.readlines()
for l in lines: for l in lines:
match = INFRA_RE.match(l) match = INFRA_RE.match(l)
@ -178,7 +181,7 @@ class Package:
return return
hashpath = self.path.replace(".mk", ".hash") hashpath = self.path.replace(".mk", ".hash")
if os.path.exists(hashpath): if os.path.exists(os.path.join(brpath, hashpath)):
self.status['hash'] = ("ok", "found") self.status['hash'] = ("ok", "found")
else: else:
self.status['hash'] = ("error", "missing") self.status['hash'] = ("error", "missing")
@ -191,7 +194,7 @@ class Package:
self.status['patches'] = ("na", "no valid package infra") self.status['patches'] = ("na", "no valid package infra")
return return
pkgdir = os.path.dirname(self.path) pkgdir = os.path.dirname(os.path.join(brpath, self.path))
for subdir, _, _ in os.walk(pkgdir): for subdir, _, _ in os.walk(pkgdir):
self.patch_files = fnmatch.filter(os.listdir(subdir), '*.patch') self.patch_files = fnmatch.filter(os.listdir(subdir), '*.patch')
@ -214,8 +217,8 @@ class Package:
""" """
Fills in the .warnings and .status['pkg-check'] fields Fills in the .warnings and .status['pkg-check'] fields
""" """
cmd = ["./utils/check-package"] cmd = [os.path.join(brpath, "utils/check-package")]
pkgdir = os.path.dirname(self.path) pkgdir = os.path.dirname(os.path.join(brpath, self.path))
self.status['pkg-check'] = ("error", "Missing") self.status['pkg-check'] = ("error", "Missing")
for root, dirs, files in os.walk(pkgdir): for root, dirs, files in os.walk(pkgdir):
for f in files: for f in files:
@ -300,11 +303,12 @@ def get_pkglist(npackages, package_list):
"toolchain/toolchain-wrapper.mk"] "toolchain/toolchain-wrapper.mk"]
packages = list() packages = list()
count = 0 count = 0
for root, dirs, files in os.walk("."): for root, dirs, files in os.walk(brpath):
root = os.path.relpath(root, brpath)
rootdir = root.split("/") rootdir = root.split("/")
if len(rootdir) < 2: if len(rootdir) < 1:
continue continue
if rootdir[1] not in WALK_USEFUL_SUBDIRS: if rootdir[0] not in WALK_USEFUL_SUBDIRS:
continue continue
for f in files: for f in files:
if not f.endswith(".mk"): if not f.endswith(".mk"):
@ -316,8 +320,7 @@ def get_pkglist(npackages, package_list):
pkgpath = os.path.join(root, f) pkgpath = os.path.join(root, f)
skip = False skip = False
for exclude in WALK_EXCLUDES: for exclude in WALK_EXCLUDES:
# pkgpath[2:] strips the initial './' if re.match(exclude, pkgpath):
if re.match(exclude, pkgpath[2:]):
skip = True skip = True
continue continue
if skip: if skip:
@ -678,7 +681,7 @@ def boolean_str(b):
def dump_html_pkg(f, pkg): def dump_html_pkg(f, pkg):
f.write(" <tr>\n") f.write(" <tr>\n")
f.write(" <td>%s</td>\n" % pkg.path[2:]) f.write(" <td>%s</td>\n" % pkg.path)
# Patch count # Patch count
td_class = ["centered"] td_class = ["centered"]
@ -945,12 +948,13 @@ def __main__():
else: else:
package_list = None package_list = None
date = datetime.datetime.utcnow() date = datetime.datetime.utcnow()
commit = subprocess.check_output(['git', 'rev-parse', commit = subprocess.check_output(['git', '-C', brpath,
'rev-parse',
'HEAD']).splitlines()[0].decode() 'HEAD']).splitlines()[0].decode()
print("Build package list ...") print("Build package list ...")
packages = get_pkglist(args.npackages, package_list) packages = get_pkglist(args.npackages, package_list)
print("Getting developers ...") print("Getting developers ...")
developers = parse_developers() developers = parse_developers(brpath)
print("Build defconfig list ...") print("Build defconfig list ...")
defconfigs = get_defconfig_list() defconfigs = get_defconfig_list()
for d in defconfigs: for d in defconfigs: