Mass Update
* Util Module
* Provides: color class (for specialty stdout logging)
* Provides: constants classes for rpm, errors, and mock
* Provides: error classes for generic error handling and future fault
handler
* Provides: generic classes for generic, repeatable use cases
* Provides: rpmutil with rpm utilities that range from basic to
advanced metadata handling
* Add mock module
* Can generate a usable mock config based on input provided
* Can generate mock plugin configuration as provided
* cache related plugins are hardcoded as disabled
* Supports plugins: chroot scanning, embedding files, bind mounts
* Can generate basic dnf configs with repo information
* (Currently limited) Error handler
* Runs mock commands (such as build, buildsrpm, init, shell)
* Add modularity module (very limited, doesn't really do much)
* Add peridotpb example (does nothing, will likely be its own thing)
* Add MIT license
2023-06-14 07:39:36 +00:00
|
|
|
"""
|
|
|
|
File functions
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import hashlib
|
2023-06-28 00:20:44 +00:00
|
|
|
import magic
|
Mass Update
* Util Module
* Provides: color class (for specialty stdout logging)
* Provides: constants classes for rpm, errors, and mock
* Provides: error classes for generic error handling and future fault
handler
* Provides: generic classes for generic, repeatable use cases
* Provides: rpmutil with rpm utilities that range from basic to
advanced metadata handling
* Add mock module
* Can generate a usable mock config based on input provided
* Can generate mock plugin configuration as provided
* cache related plugins are hardcoded as disabled
* Supports plugins: chroot scanning, embedding files, bind mounts
* Can generate basic dnf configs with repo information
* (Currently limited) Error handler
* Runs mock commands (such as build, buildsrpm, init, shell)
* Add modularity module (very limited, doesn't really do much)
* Add peridotpb example (does nothing, will likely be its own thing)
* Add MIT license
2023-06-14 07:39:36 +00:00
|
|
|
from pv2.util import error as err
|
|
|
|
|
|
|
|
# File utilities
|
|
|
|
__all__ = [
|
|
|
|
'filter_files',
|
2023-06-28 00:20:44 +00:00
|
|
|
'filter_files_inverse',
|
|
|
|
'get_checksum',
|
|
|
|
'get_magic_file',
|
|
|
|
'get_magic_content'
|
Mass Update
* Util Module
* Provides: color class (for specialty stdout logging)
* Provides: constants classes for rpm, errors, and mock
* Provides: error classes for generic error handling and future fault
handler
* Provides: generic classes for generic, repeatable use cases
* Provides: rpmutil with rpm utilities that range from basic to
advanced metadata handling
* Add mock module
* Can generate a usable mock config based on input provided
* Can generate mock plugin configuration as provided
* cache related plugins are hardcoded as disabled
* Supports plugins: chroot scanning, embedding files, bind mounts
* Can generate basic dnf configs with repo information
* (Currently limited) Error handler
* Runs mock commands (such as build, buildsrpm, init, shell)
* Add modularity module (very limited, doesn't really do much)
* Add peridotpb example (does nothing, will likely be its own thing)
* Add MIT license
2023-06-14 07:39:36 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def filter_files(directory_path: str, filter_filename: str) -> list:
|
|
|
|
"""
|
|
|
|
Filter out specified files
|
|
|
|
"""
|
|
|
|
return_list = []
|
2023-06-28 00:20:44 +00:00
|
|
|
for file in os.scandir(directory_path):
|
|
|
|
if filter_filename(file.name):
|
|
|
|
return_list.append(os.path.join(directory_path, file.name))
|
|
|
|
|
|
|
|
return return_list
|
|
|
|
|
|
|
|
def filter_files_inverse(directory_path: str, filter_filename: str) -> list:
|
|
|
|
"""
|
|
|
|
Filter out specified files (inverse)
|
|
|
|
"""
|
|
|
|
return_list = []
|
|
|
|
for file in os.scandir(directory_path):
|
|
|
|
if not filter_filename(file.name):
|
|
|
|
return_list.append(os.path.join(directory_path, file.name))
|
Mass Update
* Util Module
* Provides: color class (for specialty stdout logging)
* Provides: constants classes for rpm, errors, and mock
* Provides: error classes for generic error handling and future fault
handler
* Provides: generic classes for generic, repeatable use cases
* Provides: rpmutil with rpm utilities that range from basic to
advanced metadata handling
* Add mock module
* Can generate a usable mock config based on input provided
* Can generate mock plugin configuration as provided
* cache related plugins are hardcoded as disabled
* Supports plugins: chroot scanning, embedding files, bind mounts
* Can generate basic dnf configs with repo information
* (Currently limited) Error handler
* Runs mock commands (such as build, buildsrpm, init, shell)
* Add modularity module (very limited, doesn't really do much)
* Add peridotpb example (does nothing, will likely be its own thing)
* Add MIT license
2023-06-14 07:39:36 +00:00
|
|
|
|
|
|
|
return return_list
|
|
|
|
|
|
|
|
def get_checksum(file_path: str, hashtype: str = 'sha256') -> str:
|
|
|
|
"""
|
|
|
|
Generates a checksum from the provided path by doing things in chunks. This
|
|
|
|
reduces the time needed to make the hashes and avoids memory issues.
|
|
|
|
|
|
|
|
Borrowed from empanadas with some modifications
|
|
|
|
"""
|
|
|
|
# We shouldn't be using sha1 or md5.
|
|
|
|
if hashtype in ('sha', 'sha1', 'md5'):
|
|
|
|
raise err.ProvidedValueError(f'{hashtype} is not allowed.')
|
|
|
|
|
|
|
|
try:
|
|
|
|
checksum = hashlib.new(hashtype)
|
|
|
|
except ValueError as exc:
|
|
|
|
raise err.GenericError(f'hash type not available: {ValueError}') from exc
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(file_path, 'rb') as input_file:
|
|
|
|
while True:
|
|
|
|
chunk = input_file.read(8192)
|
|
|
|
if not chunk:
|
|
|
|
break
|
|
|
|
checksum.update(chunk)
|
|
|
|
|
|
|
|
input_file.close()
|
|
|
|
return checksum.hexdigest()
|
|
|
|
except IOError as exc:
|
|
|
|
raise err.GenericError(f'Could not open or process file {file_path}: {exc})')
|
2023-06-28 00:20:44 +00:00
|
|
|
|
|
|
|
def get_magic_file(file_path: str):
|
|
|
|
"""
|
|
|
|
Returns the magic data from a file. Use this to get mimetype and other info
|
|
|
|
you'd get by just running `file`
|
|
|
|
"""
|
|
|
|
detect = magic.detect_from_filename(file_path)
|
|
|
|
return detect
|
|
|
|
|
|
|
|
def get_magic_content(data):
|
|
|
|
"""
|
|
|
|
Returns the magic data from content. Use this to get mimetype and other info
|
|
|
|
you'd get by just running `file` on a file (but only pass read file data)
|
|
|
|
"""
|
|
|
|
detect = magic.detect_from_content(data)
|
|
|
|
return detect
|