CacheDir

class tfsnippet.utils.CacheDir(name, cache_root=None)

Bases: object

Class to manipulate a cache directory.

Attributes Summary

cache_root Get the cache root directory.
name Get the name of this cache directory under cache_root.
path Get the absolute path of this cache directory.

Methods Summary

download(uri[, filename, show_progress, …]) Download a file into this CacheDir.
download_and_extract(uri[, filename, …]) Download a file into this CacheDir, and extract it.
extract_file(archive_file[, extract_dir, …]) Extract an archive file into this CacheDir.
purge_all() Delete everything in this CacheDir.
resolve(sub_path) Resolve a sub path relative to self.path.

Attributes Documentation

cache_root

Get the cache root directory.

name

Get the name of this cache directory under cache_root.

path

Get the absolute path of this cache directory.

Methods Documentation

download(uri, filename=None, show_progress=None, progress_file=<open file '<stderr>', mode 'w'>, hasher=None, expected_hash=None)

Download a file into this CacheDir.

Parameters:
  • uri (str) – The URI to be retrieved.
  • filename (str) – The filename to use as the downloaded file. If filename already exists in this CacheDir, will not download uri. Default None, will automatically infer filename according to uri.
  • show_progress (bool) – Whether or not to show interactive progress bar? If not specified, will show progress only if progress_file is std.stdout or std.stderr, and if progress_file.isatty() is True.
  • progress_file – The file object where to write the progress. (default sys.stderr)
  • hasher – A hasher algorithm instance from hashlib. If specified, will compute the hash of downloaded content, and validate against expected_hash.
  • expected_hash (str) – The expected hash of downloaded content.
Returns:

The absolute path of the downloaded file.

Return type:

str

Raises:

ValueError – If filename cannot be inferred.

download_and_extract(uri, filename=None, extract_dir=None, show_progress=None, progress_file=<open file '<stderr>', mode 'w'>, hasher=None, expected_hash=None)

Download a file into this CacheDir, and extract it.

Parameters:
  • uri (str) – The URI to be retrieved.
  • filename (str) – The filename to use as the downloaded file. If filename already exists in this CacheDir, will not download uri. Default None, will automatically infer filename according to uri.
  • extract_dir (str) – The name to use as the extracted directory. If extract_dir already exists in this CacheDir, will not extract archive_file. Default None, will automatically infer extract_dir according to filename.
  • show_progress (bool) – Whether or not to show interactive progress bar? If not specified, will show progress only if progress_file is std.stdout or std.stderr, and if progress_file.isatty() is True.
  • progress_file – The file object where to write the progress. (default sys.stderr)
  • hasher – A hasher algorithm instance from hashlib. If specified, will compute the hash of downloaded content, and validate against expected_hash.
  • expected_hash (str) – The expected hash of downloaded content.
Returns:

The absolute path of the extracted directory.

Return type:

str

Raises:

ValueError – If filename or extract_dir cannot be inferred.

extract_file(archive_file, extract_dir=None, show_progress=None, progress_file=<open file '<stderr>', mode 'w'>)

Extract an archive file into this CacheDir.

Parameters:
  • archive_file (str) – The path of the archive file.
  • extract_dir (str) – The name to use as the extracted directory. If extract_dir already exists in this CacheDir, will not extract archive_file. Default None, will automatically infer extract_dir according to archive_file.
  • show_progress (bool) – Whether or not to show interactive progress bar? If not specified, will show progress only if progress_file is std.stdout or std.stderr, and if progress_file.isatty() is True.
  • progress_file – The file object where to write the progress. (default sys.stderr)
Returns:

The absolute path of the extracted directory.

Return type:

str

Raises:

ValueError – If extract_dir cannot be inferred.

purge_all()

Delete everything in this CacheDir.

resolve(sub_path)

Resolve a sub path relative to self.path.

Parameters:sub_path – The sub path to resolve.
Returns:The resolved absolute path of sub_path.