ClickHouse/tests/ci/compress_files.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

81 lines
2.6 KiB
Python
Raw Normal View History

2021-09-10 14:27:03 +00:00
#!/usr/bin/env python3
import subprocess
import logging
import os
2021-09-10 14:27:03 +00:00
def compress_file_fast(path, archive_path):
2023-01-01 20:17:43 +00:00
if archive_path.endswith(".zst"):
subprocess.check_call("zstd < {} > {}".format(path, archive_path), shell=True)
elif os.path.exists("/usr/bin/pigz"):
2021-09-10 14:27:03 +00:00
subprocess.check_call("pigz < {} > {}".format(path, archive_path), shell=True)
else:
subprocess.check_call("gzip < {} > {}".format(path, archive_path), shell=True)
def compress_fast(path, archive_path, exclude=None):
2023-01-01 20:17:43 +00:00
program_part = ""
if archive_path.endswith(".zst"):
logging.info("zstd will be used for compression")
program_part = "--use-compress-program='zstd --threads=0'"
elif os.path.exists("/usr/bin/pigz"):
2021-09-10 14:27:03 +00:00
logging.info("pigz found, will compress and decompress faster")
2023-01-01 20:17:43 +00:00
program_part = "--use-compress-program='pigz'"
2021-09-10 14:27:03 +00:00
else:
2023-01-01 20:17:43 +00:00
program_part = "-z"
2021-09-10 14:27:03 +00:00
logging.info("no pigz, compressing with default tar")
if exclude is None:
exclude_part = ""
elif isinstance(exclude, list):
exclude_part = " ".join(["--exclude {}".format(x) for x in exclude])
else:
exclude_part = "--exclude {}".format(str(exclude))
fname = os.path.basename(path)
if os.path.isfile(path):
path = os.path.dirname(path)
else:
path += "/.."
2023-01-01 20:17:43 +00:00
cmd = "tar {} {} -cf {} -C {} {}".format(
2023-01-01 20:17:43 +00:00
program_part, exclude_part, archive_path, path, fname
)
logging.debug("compress_fast cmd: %s", cmd)
2021-09-10 14:27:03 +00:00
subprocess.check_call(cmd, shell=True)
def decompress_fast(archive_path, result_path=None):
2023-01-01 20:17:43 +00:00
program_part = ""
if archive_path.endswith(".zst"):
2023-01-01 21:09:18 +00:00
logging.info(
"zstd will be used for decompression ('%s' -> '%s')",
2023-01-01 20:17:43 +00:00
archive_path,
result_path,
)
program_part = "--use-compress-program='zstd --threads=0'"
elif os.path.exists("/usr/bin/pigz"):
logging.info(
"pigz found, will compress and decompress faster ('%s' -> '%s')",
archive_path,
result_path,
)
2023-01-01 20:17:43 +00:00
program_part = "--use-compress-program='pigz'"
2021-09-10 14:27:03 +00:00
else:
2023-01-01 20:17:43 +00:00
program_part = "-z"
logging.info(
"no pigz, decompressing with default tar ('%s' -> '%s')",
archive_path,
result_path,
)
2021-09-10 14:27:03 +00:00
if result_path is None:
subprocess.check_call(
2023-01-01 20:17:43 +00:00
"tar {} -xf {}".format(program_part, archive_path), shell=True
)
2021-09-10 14:27:03 +00:00
else:
subprocess.check_call(
2023-01-01 20:17:43 +00:00
"tar {} -xf {} -C {}".format(program_part, archive_path, result_path),
shell=True,
)