Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _ensure_path_absolute(maybe_relpath, cfg_path):
""" Ensure that a possibly relative path is absolute. """
if not isinstance(maybe_relpath, str):
raise TypeError(
"Attempting to ensure non-text value is absolute path: {} ({})".
format(maybe_relpath, type(maybe_relpath)))
if os.path.isabs(maybe_relpath) or is_url(maybe_relpath):
_LOGGER.debug("Already absolute")
return maybe_relpath
# Maybe we have env vars that make the path absolute?
expanded = os.path.expanduser(os.path.expandvars(maybe_relpath))
if os.path.isabs(expanded):
_LOGGER.debug("Expanded: {}".format(expanded))
return expanded
# Set path to an absolute path, relative to project config.
config_dirpath = os.path.dirname(cfg_path)
_LOGGER.debug("config_dirpath: {}".format(config_dirpath))
abs_path = os.path.join(config_dirpath, maybe_relpath)
_LOGGER.debug("Expanded and/or made absolute: {}".format(abs_path))
return abs_path
def _ensure_path_absolute(maybe_relpath, cfg_path):
""" Ensure that a possibly relative path is absolute. """
if not isinstance(maybe_relpath, str):
raise TypeError(
"Attempting to ensure non-text value is absolute path: {} ({})".
format(maybe_relpath, type(maybe_relpath)))
_LOGGER.debug("Ensuring absolute: '{}'".format(maybe_relpath))
if os.path.isabs(maybe_relpath) or is_url(maybe_relpath):
_LOGGER.debug("Already absolute")
return maybe_relpath
# Maybe we have env vars that make the path absolute?
expanded = os.path.expanduser(os.path.expandvars(maybe_relpath))
_LOGGER.debug("Expanded: '{}'".format(expanded))
if os.path.isabs(expanded):
_LOGGER.debug("Expanded is absolute")
return expanded
_LOGGER.debug("Making non-absolute path '{}' be absolute".
format(maybe_relpath))
# Set path to an absolute path, relative to project config.
config_dirpath = os.path.dirname(cfg_path)
_LOGGER.debug("config_dirpath: {}".format(config_dirpath))
abs_path = os.path.join(config_dirpath, maybe_relpath)
return abs_path
def copy_or_download_file(input_string, outfolder):
"""
Given an input file, which can be a local file or a URL, and output folder,
this downloads or copies the file into the output folder.
:param str input_string: Can be either a URL or a path to a local file
:param str outfolder: Where to store the result.
:return str, str: output/result file and command
"""
result_file = os.path.join(outfolder, os.path.basename(input_string))
parts = ["wget -O", result_file, input_string] \
if is_url(input_string) else ["cp", input_string, result_file]
return result_file, " ".join(parts)