Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_dir_digest(path, pm=None):
"""
Generate a MD5 digest that reflects just the contents of the files in the selected directory.
:param str path: path to the directory to digest
:param pypiper.PipelineManager pm: a pipeline object, optional. The subprocess module will be used if not provided
:return str: a digest, e.g. a3c46f201a3ce7831d85cf4a125aa334
"""
if not is_command_callable("md5sum"):
raise OSError("md5sum command line tool is required for asset digest calculation. \n"
"Install and try again, e.g on macOS: 'brew install md5sha1sum'")
cmd = "cd {}; find . -type f -not -path './" + BUILD_STATS_DIR + \
"*' -exec md5sum {{}} \; | sort -k 2 | awk '{{print $1}}' | md5sum"
if isinstance(pm, pypiper.PipelineManager):
x = pm.checkprint(cmd.format(path))
else:
try:
from subprocess import check_output
x = check_output(cmd.format(path), shell=True).decode("utf-8")
except Exception as e:
_LOGGER.warning("{}: could not calculate digest for '{}'".format(e.__class__.__name__, path))
return
return str(sub(r'\W+', '', x)) # strips non-alphanumeric