diff options
author | bill-auger <mr.j.spam.me@gmail.com> | 2022-02-15 01:02:28 -0500 |
---|---|---|
committer | bill-auger <mr.j.spam.me@gmail.com> | 2022-02-15 01:19:04 -0500 |
commit | ab807910d73ceac4f09bc509a3c6bf286dbcfeca (patch) | |
tree | 4cdb98d6266688a8d0849c17d88dc9d0ae63ddc5 | |
parent | 62116e3b57244d5cf30c8e0d5bd55b48f6007037 (diff) |
refactor _cached_*info() functions to consilidate `tar` procswip-2022-02-13
-rw-r--r-- | parabola_repolint/repocache.py | 67 |
1 files changed, 22 insertions, 45 deletions
diff --git a/parabola_repolint/repocache.py b/parabola_repolint/repocache.py index a8f0927..b8d8c37 100644 --- a/parabola_repolint/repocache.py +++ b/parabola_repolint/repocache.py @@ -146,11 +146,8 @@ class PkgFile(): self._status = status self._pkgfiles_dir = os.path.normpath(RepoCache.PKGFILES_DIR + '/' + self._path) - mtime = os.path.getmtime(self._path) - self._pkginfo = {} - pkginfo = self._cached_pkginfo('.pkginfo', mtime) - for line in pkginfo.splitlines(): + for line in self._cached_pkginfo().splitlines(): if line.startswith('#'): continue @@ -172,8 +169,7 @@ class PkgFile(): logging.warning('unhandled PKGINFO key: %s', key) self._buildinfo = {} - buildinfo = self._cached_buildinfo('.buildinfo', mtime) - for line in buildinfo.splitlines(): + for line in self._cached_buildinfo().splitlines(): key, value = line.split('=', 1) key = key.strip() value = value.strip() @@ -191,7 +187,7 @@ class PkgFile(): else: logging.warning('unhandled BUILDINFO key: %s', key) - self._siginfo = self._cached_siginfo('.siginfo', mtime) + self._siginfo = json.loads(self._cached_siginfo()) pkgbuild_cache = repo.pkgbuild_cache.get(repoarch, {}) self._pkgbuilds = pkgbuild_cache.get(self.pkgname, []) @@ -203,19 +199,19 @@ class PkgFile(): for pkgentry in self._pkgentries: pkgentry.register_pkgfile(self, repoarch) - def _cached_pkginfo(self, cachefile, mtime): + def _cached_pkginfo(self): return _cached_info('.PKGINFO') + def _cached_buildinfo(self): return _cached_info('.BUILDINFO') + def _cached_siginfo(self): return _cached_info('.SIGINFO') + + def _cached_info(self, info_file): ''' get information from a package ''' - cachefile = os.path.normpath(self._pkgfiles_dir + '/' + cachefile) + cachefile = os.path.normpath(self._pkgfiles_dir + '/' + info_file.lower()) + mtime = os.path.getmtime(self._path) if os.path.isfile(cachefile) and os.path.getmtime(cachefile) > mtime: with open(cachefile, 'r') as infile: return infile.read() - res = '' - try: - res = str(sh.tar('-xOf', self._path, '.PKGINFO')) - except sh.ErrorReturnCode as ex: - if b'.PKGINFO: Not found in archive' not in ex.stderr: - logging.exception('tar -xOf failed for %s', self) + res = _load_siginfo() if info_file is '.SIGINFO' else _load_infos() os.makedirs(os.path.dirname(cachefile), exist_ok=True) with open(cachefile, 'w') as outfile: @@ -223,42 +219,23 @@ class PkgFile(): return res - def _cached_buildinfo(self, cachefile, mtime): - ''' get build information from a package ''' - cachefile = os.path.normpath(self._pkgfiles_dir + '/' + cachefile) - if os.path.isfile(cachefile) and os.path.getmtime(cachefile) > mtime: - with open(cachefile, 'r') as infile: - return infile.read() - + def _load_infos(self): res = '' try: - res = str(sh.tar('-xOf', self._path, '.BUILDINFO')) + res = str(sh.tar('-xOf', self._path, '.PKGINFO', '.BUILDINFO')) except sh.ErrorReturnCode as ex: - if b'.BUILDINFO: Not found in archive' not in ex.stderr: + not_found_error = bytes(info_file, 'utf-8') + b': Not found in archive' + if not_found_error not in ex.stderr: logging.exception('tar -xOf failed for %s', self) - os.makedirs(os.path.dirname(cachefile), exist_ok=True) - with open(cachefile, 'w') as outfile: - outfile.write(res) - return res - def _cached_siginfo(self, cachefile, mtime): - ''' get signature information from a package ''' - cachefile = os.path.normpath(self._pkgfiles_dir + '/' + cachefile) - if os.path.isfile(cachefile) and os.path.getmtime(cachefile) > mtime: - with open(cachefile, 'r') as infile: - return json.loads(infile.read()) - + def _load_siginfo(self): sigfile = "%s.sig" % self._path with open(sigfile, 'rb') as sig: - res = GPG_PACMAN.verify_file(sig, self._path).__dict__ + res_json = GPG_PACMAN.verify_file(sig, self._path).__dict__ - os.makedirs(os.path.dirname(cachefile), exist_ok=True) - with open(cachefile, 'w') as outfile: - outfile.write(json.dumps(res, default=str)) - - return res + return json.dumps(res_json, default=str) @property def path(self): @@ -791,9 +768,9 @@ class Repo(): if arch.name not in CONFIG.parabola.arches: continue - repo_file = os.path.join(arch.path, '%s.db' % self._name) - if os.path.exists(repo_file): - mtime = os.path.getmtime(repo_file) + db_file = os.path.join(arch.path, '%s.db' % self._name) + if os.path.exists(db_file): + mtime = os.path.getmtime(db_file) dst = os.path.join(self._pkgentries_dir, 'os', arch.name) if os.path.isdir(dst) and os.path.getmtime(dst) > mtime: @@ -803,7 +780,7 @@ class Repo(): shutil.rmtree(dst) os.makedirs(dst, exist_ok=True) - sh.tar('xf', repo_file, _cwd=dst) + sh.tar('xf', db_file, _cwd=dst) i = 0 arches_dir = os.path.join(self._pkgentries_dir, 'os') |