From 2e15825258468894693fc75d215f1d353b617015 Mon Sep 17 00:00:00 2001 From: Louis Abel Date: Wed, 14 Aug 2024 21:11:38 -0700 Subject: [PATCH] add tarutil --- pv2/importer/operation.py | 19 ++++++++++++- pv2/util/tarutil.py | 59 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 pv2/util/tarutil.py diff --git a/pv2/importer/operation.py b/pv2/importer/operation.py index 15c0997..e70ef7d 100644 --- a/pv2/importer/operation.py +++ b/pv2/importer/operation.py @@ -34,7 +34,8 @@ __all__ = [ 'SrpmImport', 'GitImport', 'ModuleImport', - 'JavaPortableImport' + 'JavaPortableImport', + 'SourceCodeImport' ] # todo: add in logging and replace print with log @@ -1398,3 +1399,19 @@ class JavaPortableImport(Import): Returns the portable java git URL """ return self.__portable_git_url + +class SourceCodeImport(Import): + """ + Grabs source code of a package and imports it to a separate org/repo for + code only. + """ + # pylint: disable=too-many-arguments + def __init__( + self, + pkg_name: str, + git_url_path: str, + branch: str, + git_user: str = 'git', + org: str = 'src', + ): + print() diff --git a/pv2/util/tarutil.py b/pv2/util/tarutil.py new file mode 100644 index 0000000..d80b469 --- /dev/null +++ b/pv2/util/tarutil.py @@ -0,0 +1,59 @@ +""" +Tar functions +""" + +import tarfile +from pv2.util import error as err +from pv2.util import fileutil + +# Tar utilities +__all__ = [ + 'tar_members', + 'type_of_tar', + 'tarextract' +] + +def tar_members(tf, subdir): + """ + Acts as a "strip component" for tar + """ + l = len(f"{subdir}/") + for member in tf.getmembers(): + if member.path.startswith(f"{subdir}/"): + member.path = member.path[l:] + yield member + +def type_of_tar(tf): + """ + Determines what compression method was used. + """ + tardata = fileutil.get_magic_file(tf) + + if 'XZ compressed data' in tardata.name: + return 'xz' + if 'gzip compressed data' in tardata.name: + return 'gz' + if 'bzip2 compressed data' in tardata.name: + return 'bz2' + + return None + +def tarextract(source, dest, topdir_to_strip='', strip=False): + """ + Extracts a given tar ball to a specific location + """ + try: + with tarfile.open(source) as tar: + if strip: + tar.extractall(members=tar_members(tar, topdir_to_strip), filter='tar', path=dest) + else: + tar.extractall(filter='tar', path=dest) + tar.close() + except tarfile.ReadError as re: + raise err.GenericError(f'Could not read tar file: {re}') + except tarfile.CompressionError as ce: + raise err.GenericError(f'This system does not support compression type used: {ce}') + except tarfile.ExtractError as ee: + raise err.GenericError(f'Extraction error: {ee}') + except Exception as exc: + raise err.GenericError(f'Uncaught error: {exc}')