2019-01-27 20:26:50 -08:00
|
|
|
"""Helper functions for dealing with package files."""
|
|
|
|
|
2019-03-25 14:20:36 -07:00
|
|
|
import logging
|
2019-03-25 13:46:14 -07:00
|
|
|
import re
|
2019-03-25 14:20:36 -07:00
|
|
|
import shutil
|
2019-03-25 13:46:14 -07:00
|
|
|
|
|
|
|
from pathlib import Path
|
2019-01-27 20:26:50 -08:00
|
|
|
|
2019-01-28 10:53:56 -08:00
|
|
|
# ========== Constants ==========
|
2019-03-25 13:46:14 -07:00
|
|
|
# Match any pkgfile of any name with the .pkg.tar.* extension
|
2019-03-25 16:59:49 -07:00
|
|
|
PKGREGEX = r"^[\w.+/-]+\.pkg\.tar(\.\w+)?$"
|
2019-03-25 13:46:14 -07:00
|
|
|
# Match any sigfile of any name with the .pkg.tar.*.sig extension
|
2019-03-25 16:59:49 -07:00
|
|
|
SIGREGEX = r"^[\w.+/-]+\.pkg\.tar(\.\w+)?\.sig$"
|
2019-03-25 14:20:36 -07:00
|
|
|
|
|
|
|
|
|
|
|
# ========== Logging Setup ===========
|
|
|
|
syslog = logging.getLogger(__name__)
|
2019-01-27 20:26:50 -08:00
|
|
|
|
2019-01-29 14:33:51 -08:00
|
|
|
|
2019-01-28 10:53:56 -08:00
|
|
|
# ========== Functions ==========
|
2019-03-25 14:20:36 -07:00
|
|
|
def get(query=None, *, directory=None, signatures_only=False):
|
2019-03-25 21:53:27 -07:00
|
|
|
"""Retrieve all package-related files from the filesystem.
|
2019-01-27 20:26:50 -08:00
|
|
|
|
2019-01-28 11:01:08 -08:00
|
|
|
:param query: names of package files to search for
|
|
|
|
:type query: str
|
2019-01-27 20:26:50 -08:00
|
|
|
:param directory: a directory to search in
|
2019-03-25 13:46:14 -07:00
|
|
|
:type directory: str or path-like object
|
2019-01-28 10:59:18 -08:00
|
|
|
:param signatures_only: include only signature files
|
|
|
|
:type signatures_only: bool
|
2019-03-25 14:41:42 -07:00
|
|
|
:yields: paths of package files
|
2019-01-27 20:26:50 -08:00
|
|
|
"""
|
2019-03-25 13:46:14 -07:00
|
|
|
path = Path.cwd() if directory is None else Path(directory)
|
2019-03-25 16:59:49 -07:00
|
|
|
filequery = f"*{query}*" if query else "*"
|
2019-03-25 13:46:14 -07:00
|
|
|
|
|
|
|
if signatures_only:
|
2019-03-25 21:53:27 -07:00
|
|
|
yield from _filter_by_regex(SIGREGEX, path.glob(filequery))
|
2019-01-27 20:26:50 -08:00
|
|
|
else:
|
2019-03-25 21:53:27 -07:00
|
|
|
yield from _filter_by_regex(PKGREGEX, path.glob(filequery))
|
|
|
|
|
|
|
|
|
|
|
|
def filter(iterable, *, signatures_only=False):
|
|
|
|
"""Retrive package file types from a predefined iterable.
|
|
|
|
|
|
|
|
:param signatures_only: include only signature files
|
|
|
|
:type signatures_only: bool
|
|
|
|
:yields: paths of package files
|
|
|
|
"""
|
|
|
|
if signatures_only:
|
|
|
|
yield from _filter_by_regex(SIGREGEX, iterable)
|
|
|
|
else:
|
|
|
|
yield from _filter_by_regex(PKGREGEX, iterable)
|
2019-03-25 13:46:14 -07:00
|
|
|
|
|
|
|
|
2019-03-25 14:20:36 -07:00
|
|
|
def add(pkgfile, cachedir):
|
2019-03-25 14:41:42 -07:00
|
|
|
"""Add package file to the repository cache directory.
|
2019-03-25 14:20:36 -07:00
|
|
|
|
|
|
|
:param pkg: path of package to add
|
|
|
|
:type pkg: path-like object
|
|
|
|
:param cachedir: cache directory to move package to
|
|
|
|
:type cachedir: path-like object
|
|
|
|
"""
|
|
|
|
syslog.info(f"Adding {pkgfile} to {cachedir}")
|
|
|
|
shutil.move(pkgfile, cachedir / pkgfile.name)
|
|
|
|
|
|
|
|
|
|
|
|
def delete(pkg):
|
|
|
|
"""Remove package file.
|
|
|
|
|
|
|
|
:param pkg: path of package to remove
|
|
|
|
:type pkg: path-like object
|
|
|
|
"""
|
|
|
|
pkg.unlink()
|
|
|
|
syslog.info(f"Removed {pkg}")
|
|
|
|
|
|
|
|
|
2019-03-25 21:53:27 -07:00
|
|
|
def _filter_by_regex(regex_expression, iterable):
|
2019-03-25 14:41:42 -07:00
|
|
|
"""Filter by regular expression.
|
2019-03-25 13:46:14 -07:00
|
|
|
|
|
|
|
:param regex_expression: the expression to filter by
|
|
|
|
:type regex_expression: str
|
2019-03-25 21:53:27 -07:00
|
|
|
:param iterable: iterable to filter through
|
|
|
|
:type iterable: does this really need explanation?
|
|
|
|
:yields: pathlib.Path objects to package files
|
2019-03-25 13:46:14 -07:00
|
|
|
"""
|
2019-03-25 21:53:27 -07:00
|
|
|
for item in iterable:
|
|
|
|
if re.match(regex_expression, str(item)):
|
|
|
|
yield Path(item)
|