Coverage for pyguymer3/xz.py: 11%
9 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
1#!/usr/bin/env python3
3# Define function ...
4def xz(
5 fname,
6 /,
7 *,
8 cwd = None,
9 stderr = None,
10 stdout = None,
11 threads = 0,
12 timeout = 60.0,
13 xzPath = None,
14):
15 """Compress a file using "xz" (with SHA-256 integrity checks).
17 Parameters
18 ----------
19 fname : str
20 the name of the file to compress
21 cwd : str, optional
22 the child working directory (default None)
23 stderr : subprocess.PIPE, subprocess.DEVNULL, io.TextIOWrapper, optional
24 the destination of STDERR (default None)
25 stdout : subprocess.PIPE, subprocess.DEVNULL, io.TextIOWrapper, optional
26 the destination of STDOUT (default None)
27 threads : int, optional
28 the number of threads to use (default 0)
29 timeout : float, optional
30 the timeout for any requests/subprocess calls
31 xzPath : str, optional
32 the path to the "xz" binary (if not provided then Python will attempt to
33 find the binary itself)
35 Notes
36 -----
37 I still need to provide justification for using this function over https://docs.python.org/3.12/library/lzma.html
39 Copyright 2017 Thomas Guymer [1]_
41 References
42 ----------
43 .. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3
44 """
46 # Import standard modules ...
47 import shutil
48 import subprocess
50 # **************************************************************************
52 # Try to find the paths if the user did not provide them ...
53 if xzPath is None:
54 xzPath = shutil.which("xz")
55 assert xzPath is not None, "\"xz\" is not installed"
57 # Check inputs ...
58 if not isinstance(threads, int):
59 raise Exception("\"threads\" is not an integer") from None
61 # Compress file ...
62 subprocess.run(
63 [
64 xzPath,
65 "--compress",
66 "-9e",
67 "--check=sha256",
68 "--format=xz",
69 f"--threads={threads:d}",
70 fname,
71 ],
72 check = True,
73 cwd = cwd,
74 encoding = "utf-8",
75 stderr = stderr,
76 stdout = stdout,
77 timeout = timeout,
78 )