dpdispatcher.utils package#
Utils.
Subpackages#
Submodules#
dpdispatcher.utils.archive module#
Safely extract archives received from remote execution backends.
- exception dpdispatcher.utils.archive.UnsafeArchiveError[source]#
Bases:
ValueErrorRaised when an archive member could create an unsafe filesystem entry.
- dpdispatcher.utils.archive.safe_extract_tar(archive: TarFile, destination: str) None[source]#
Safely extract regular files and directories from a tar archive.
The full manifest is validated before creating the output root. Extraction rejects links, devices, traversal, path collisions, and existing symlink components. Files are streamed to fresh temporary files and atomically replaced so existing hardlinks are not modified.
The destination must not be concurrently renamed or mutated by another process during extraction; portable Python 3.7 has no cross-platform descriptor-relative API that can close that final local TOCTOU window.
- Parameters:
- archivetarfile.TarFile
Open tar archive containing remote result files.
- destinationstr
Directory under which every archive member must remain.
- Raises:
- UnsafeArchiveError
If the archive manifest or an existing output path is unsafe.
- dpdispatcher.utils.archive.safe_extract_zip(archive: ZipFile, destination: str) set[str][source]#
Safely extract regular files and directories from a zip archive.
This applies the same manifest, symlink, and atomic-replacement guarantees as
safe_extract_tar()while also validating Unix file types stored in zip metadata.- Parameters:
- archivezipfile.ZipFile
Open zip archive containing remote result files.
- destinationstr
Directory under which every archive member must remain.
- Raises:
- UnsafeArchiveError
If the archive manifest or an existing output path is unsafe.
dpdispatcher.utils.hdfs_cli module#
Wrap the Hadoop CLI operations used by the HDFS context.
- class dpdispatcher.utils.hdfs_cli.HDFS[source]#
Bases:
objectFundamental class for HDFS basic manipulation.
Methods
copy_from_local(local_path, to_uri)Copy a readable local path to an HDFS URI.
copy_to_local(from_uri, local_path)Copy one or more HDFS paths into a local directory.
exists(uri)Return whether an HDFS URI exists.
mkdir(uri)Create an HDFS directory and any missing parents.
move(from_uri, to_uri)Move an HDFS path to a new URI.
read_hdfs_file(uri)Return the decoded output of
hadoop fs -textfor an HDFS URI.remove(uri)Remove an HDFS URI recursively.
- static copy_from_local(local_path: str, to_uri: str) tuple[bool, bytes][source]#
Copy a readable local path to an HDFS URI.
- static copy_to_local(from_uri: str | list[str] | tuple[str, ...], local_path: str) bool[source]#
Copy one or more HDFS paths into a local directory.
- exception dpdispatcher.utils.hdfs_cli.HDFSMissingPathError[source]#
Bases:
RuntimeErrorRaised when Hadoop reports that a requested source path is absent.
dpdispatcher.utils.job_status module#
Define scheduler-independent states for jobs and tasks.
dpdispatcher.utils.record module#
Persist recoverable submission state in the user’s data directory.
dpdispatcher.utils.utils module#
Provide hashing, authentication, retry, transfer, and script helpers.
- exception dpdispatcher.utils.utils.RetrySignal[source]#
Bases:
ExceptionException to give a signal to retry the function.
- dpdispatcher.utils.utils.customized_script_header_template(filename: PathLike, resources: Resources) str[source]#
Render a user-provided scheduler header with resource values.
- dpdispatcher.utils.utils.generate_totp(secret: str, period: int = 30, token_length: int = 6) str[source]#
Generate time-based one time password (TOTP) from the secret.
Some HPCs use TOTP for two-factor authentication for safety.
- Parameters:
- secretstr
The encoded secret provided by the HPC. It’s usually extracted from a 2D code and base32 encoded.
- periodint, default=30
Time period where the code is valid in seconds.
- token_lengthint, default=6
The token length.
- Returns:
- token: str
The generated token.
References
- dpdispatcher.utils.utils.get_sha256(filename: str) str[source]#
Get sha256 of a file.
- Parameters:
- filenamestr
The filename.
- Returns:
- sha256: str
The sha256.
- dpdispatcher.utils.utils.hotp(key: str, period: int, token_length: int = 6, digest: str = 'sha1') str[source]#
Generate an HMAC-based one-time password for a counter value.
- dpdispatcher.utils.utils.retry(max_retry: int = 3, sleep: int | float = 60, catch_exception: type[BaseException] = <class 'dpdispatcher.utils.utils.RetrySignal'>) Callable[source]#
Retry the function until it succeeds or fails for certain times.
- Parameters:
- max_retryint, default=3
The maximum retry times. If None, it will retry forever.
- sleepint or float, default=60
The sleep time in seconds.
- catch_exceptionException, default=Exception
The exception to catch.
- Returns:
- decorator: Callable
The decorator.
Examples
>>> @retry(max_retry=3, sleep=60, catch_exception=RetrySignal) ... def func(): ... raise RetrySignal("Failed")
- dpdispatcher.utils.utils.rsync(from_file: str, to_file: str, port: int = 22, key_filename: str | None = None, timeout: int | float = 10, proxy_command: str | None = None) None[source]#
Call rsync to transfer files.
- Parameters:
- from_filestr
SRC
- to_filestr
DEST
- portint, default=22
port for ssh
- key_filenamestr, optional
identity file name
- timeoutint, default=10
timeout for ssh
- proxy_commandstr, optional
ProxyCommand to use for SSH connection
- Raises:
- RuntimeError
when return code is not 0