51 lines
1.7 KiB
Python
Raw Normal View History

2019-01-27 20:26:50 -08:00
"""Helper functions for dealing with package files."""
import re
from pathlib import Path
2019-01-27 20:26:50 -08:00
2019-01-28 10:53:56 -08:00
# ========== Constants ==========
# Match any pkgfile of any name with the .pkg.tar.* extension
PKGREGEX = "^[\w]+[.]pkg[.]tar([.][\w]+)?$"
# Match any sigfile of any name with the .pkg.tar.*.sig extension
SIGREGEX = "^[\w]+[.]pkg[.]tar[.][\w]*[.]sig$"
2019-01-27 20:26:50 -08:00
2019-01-28 10:53:56 -08:00
# ========== Functions ==========
def get_pkgfiles(query=None, *, directory=None, signatures_only=False):
2019-01-27 20:26:50 -08:00
"""Return a list of package files in the current working directory.
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
:type directory: str or path-like object
:param signatures_only: include only signature files
:type signatures_only: bool
2019-01-27 20:26:50 -08:00
:returns: paths of package files
:rtype: list
2019-01-27 20:26:50 -08:00
"""
path = Path.cwd() if directory is None else Path(directory)
filequery = f"*{query}*" if query is not None else "*"
if signatures_only:
yield from _filter_by_regex(filequery, SIGREGEX, path)
2019-01-27 20:26:50 -08:00
else:
yield from _filter_by_regex(filequery, PKGREGEX, path)
def _filter_by_regex(query, regex_expression, path):
"""Filter package files only.
:param query: names of package files to search for
:type query: str
:param regex_expression: the expression to filter by
:type regex_expression: str
:param path: directory to look for files
:type path: path-like object
:yields: package files from either the current working directory
or an arbitrary directory
"""
for pkgfile in path.glob(query):
if re.match(regex_expression, str(pkgfile)):
yield pkgfile