File size: 15,509 Bytes
36ba3ef |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 |
"""
File system utils.
"""
import collections
import os
import pickle
import sys
import errno
import shutil
import glob
# import pwd
import codecs
import hashlib
import tarfile
import fnmatch
import tempfile
from datetime import datetime
from socket import gethostname
import logging
f_ext = os.path.splitext
f_size = os.path.getsize
is_file = os.path.isfile
is_dir = os.path.isdir
get_dir = os.path.dirname
def host_name():
"Get host name, alias with ``socket.gethostname()``"
return gethostname()
def host_id():
"""
Returns: first part of hostname up to '.'
"""
return host_name().split(".")[0]
def utf_open(fname, mode):
"""
Wrapper for codecs.open
"""
return codecs.open(fname, mode=mode, encoding="utf-8")
def is_sequence(obj):
"""
Returns:
True if the sequence is a collections.Sequence and not a string.
"""
return isinstance(obj, collections.abc.Sequence) and not isinstance(obj, str)
def pack_varargs(args):
"""
Pack *args or a single list arg as list
def f(*args):
arg_list = pack_varargs(args)
# arg_list is now packed as a list
"""
assert isinstance(args, tuple), "please input the tuple `args` as in *args"
if len(args) == 1 and is_sequence(args[0]):
return args[0]
else:
return args
def f_not_empty(*fpaths):
"""
Returns:
True if and only if the file exists and file size > 0
if fpath is a dir, if and only if dir exists and has at least 1 file
"""
fpath = f_join(*fpaths)
if not os.path.exists(fpath):
return False
if os.path.isdir(fpath):
return len(os.listdir(fpath)) > 0
else:
return os.path.getsize(fpath) > 0
def f_expand(fpath):
return os.path.expandvars(os.path.expanduser(fpath))
def f_exists(*fpaths):
return os.path.exists(f_join(*fpaths))
def f_join(*fpaths):
"""
join file paths and expand special symbols like `~` for home dir
"""
fpaths = pack_varargs(fpaths)
fpath = f_expand(os.path.join(*fpaths))
if isinstance(fpath, str):
fpath = fpath.strip()
return fpath
def f_listdir(
*fpaths,
filter_ext=None,
filter=None,
sort=True,
full_path=False,
nonexist_ok=True,
recursive=False,
):
"""
Args:
full_path: True to return full paths to the dir contents
filter: function that takes in file name and returns True to include
nonexist_ok: True to return [] if the dir is non-existent, False to raise
sort: sort the file names by alphabetical
recursive: True to use os.walk to recursively list files. Note that `filter`
will be applied to the relative path string to the root dir.
e.g. filter will take "a/data1.txt" and "a/b/data3.txt" as input, instead of
just the base file names "data1.txt" and "data3.txt".
if False, will simply call os.listdir()
"""
assert not (filter_ext and filter), "filter_ext and filter are mutually exclusive"
dir_path = f_join(*fpaths)
if not os.path.exists(dir_path) and nonexist_ok:
return []
if recursive:
files = [
os.path.join(os.path.relpath(root, dir_path), file)
for root, _, files in os.walk(dir_path)
for file in files
]
else:
files = os.listdir(dir_path)
if filter is not None:
files = [f for f in files if filter(f)]
elif filter_ext is not None:
files = [f for f in files if f.endswith(filter_ext)]
if sort:
files.sort()
if full_path:
return [os.path.join(dir_path, f) for f in files]
else:
return files
def f_mkdir(*fpaths):
"""
Recursively creates all the subdirs
If exist, do nothing.
"""
fpath = f_join(*fpaths)
os.makedirs(fpath, exist_ok=True)
return fpath
def f_mkdir_in_path(*fpaths):
"""
fpath is a file,
recursively creates all the parent dirs that lead to the file
If exist, do nothing.
"""
os.makedirs(get_dir(f_join(*fpaths)), exist_ok=True)
def last_part_in_path(fpath):
"""
https://stackoverflow.com/questions/3925096/how-to-get-only-the-last-part-of-a-path-in-python
"""
return os.path.basename(os.path.normpath(f_expand(fpath)))
def is_abs_path(*fpath):
return os.path.isabs(f_join(*fpath))
def is_relative_path(*fpath):
return not is_abs_path(f_join(*fpath))
def f_time(*fpath):
"File modification time"
return str(os.path.getctime(f_join(*fpath)))
def f_append_before_ext(fpath, suffix):
"""
Append a suffix to file name and retain its extension
"""
name, ext = f_ext(fpath)
return name + suffix + ext
def f_add_ext(fpath, ext):
"""
Append an extension if not already there
Args:
ext: will add a preceding `.` if doesn't exist
"""
if not ext.startswith("."):
ext = "." + ext
if fpath.endswith(ext):
return fpath
else:
return fpath + ext
def f_has_ext(fpath, ext):
"Test if file path is a text file"
_, actual_ext = f_ext(fpath)
return actual_ext == "." + ext.lstrip(".")
def f_glob(*fpath):
return glob.glob(f_join(*fpath), recursive=True)
def f_remove(*fpath, verbose=False, dry_run=False):
"""
If exist, remove. Supports both dir and file. Supports glob wildcard.
"""
assert isinstance(verbose, bool)
fpath = f_join(fpath)
if dry_run:
print("Dry run, delete:", fpath)
return
for f in glob.glob(fpath):
try:
shutil.rmtree(f)
except OSError as e:
if e.errno == errno.ENOTDIR:
try:
os.remove(f)
except: # final resort safeguard
pass
if verbose:
print(f'Deleted "{fpath}"')
def f_copy(fsrc, fdst, ignore=None, include=None, exists_ok=True, verbose=False):
"""
Supports both dir and file. Supports glob wildcard.
"""
fsrc, fdst = f_expand(fsrc), f_expand(fdst)
for f in glob.glob(fsrc):
try:
f_copytree(f, fdst, ignore=ignore, include=include, exist_ok=exists_ok)
except OSError as e:
if e.errno == errno.ENOTDIR:
shutil.copy(f, fdst)
else:
raise
if verbose:
print(f'Copied "{fsrc}" to "{fdst}"')
def _f_copytree(
src,
dst,
symlinks=False,
ignore=None,
exist_ok=True,
copy_function=shutil.copy2,
ignore_dangling_symlinks=False,
):
"""Copied from python standard lib shutil.copytree
except that we allow exist_ok
Use f_copytree as entry
"""
names = os.listdir(src)
if ignore is not None:
ignored_names = ignore(src, names)
else:
ignored_names = set()
os.makedirs(dst, exist_ok=exist_ok)
errors = []
for name in names:
if name in ignored_names:
continue
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if os.path.islink(srcname):
linkto = os.readlink(srcname)
if symlinks:
# We can't just leave it to `copy_function` because legacy
# code with a custom `copy_function` may rely on copytree
# doing the right thing.
os.symlink(linkto, dstname)
shutil.copystat(srcname, dstname, follow_symlinks=not symlinks)
else:
# ignore dangling symlink if the flag is on
if not os.path.exists(linkto) and ignore_dangling_symlinks:
continue
# otherwise let the copy occurs. copy2 will raise an error
if os.path.isdir(srcname):
_f_copytree(
srcname, dstname, symlinks, ignore, exist_ok, copy_function
)
else:
copy_function(srcname, dstname)
elif os.path.isdir(srcname):
_f_copytree(srcname, dstname, symlinks, ignore, exist_ok, copy_function)
else:
# Will raise a SpecialFileError for unsupported file types
copy_function(srcname, dstname)
# catch the Error from the recursive copytree so that we can
# continue with other files
except shutil.Error as err:
errors.extend(err.args[0])
except OSError as why:
errors.append((srcname, dstname, str(why)))
try:
shutil.copystat(src, dst)
except OSError as why:
# Copying file access times may fail on Windows
if getattr(why, "winerror", None) is None:
errors.append((src, dst, str(why)))
if errors:
raise shutil.Error(errors)
return dst
def _include_patterns(*patterns):
"""Factory function that can be used with copytree() ignore parameter.
Arguments define a sequence of glob-style patterns
that are used to specify what files to NOT ignore.
Creates and returns a function that determines this for each directory
in the file hierarchy rooted at the source directory when used with
shutil.copytree().
"""
def _ignore_patterns(path, names):
keep = set(
name for pattern in patterns for name in fnmatch.filter(names, pattern)
)
ignore = set(
name
for name in names
if name not in keep and not os.path.isdir(os.path.join(path, name))
)
return ignore
return _ignore_patterns
def f_copytree(fsrc, fdst, symlinks=False, ignore=None, include=None, exist_ok=True):
fsrc, fdst = f_expand(fsrc), f_expand(fdst)
assert (ignore is None) or (
include is None
), "ignore= and include= are mutually exclusive"
if ignore:
ignore = shutil.ignore_patterns(*ignore)
elif include:
ignore = _include_patterns(*include)
_f_copytree(fsrc, fdst, ignore=ignore, symlinks=symlinks, exist_ok=exist_ok)
def f_move(fsrc, fdst):
fsrc, fdst = f_expand(fsrc), f_expand(fdst)
for f in glob.glob(fsrc):
shutil.move(f, fdst)
def f_split_path(fpath, normpath=True):
"""
Splits path into a list of its component folders
Args:
normpath: call os.path.normpath to remove redundant '/' and
up-level references like ".."
"""
if normpath:
fpath = os.path.normpath(fpath)
allparts = []
while 1:
parts = os.path.split(fpath)
if parts[0] == fpath: # sentinel for absolute paths
allparts.insert(0, parts[0])
break
elif parts[1] == fpath: # sentinel for relative paths
allparts.insert(0, parts[1])
break
else:
fpath = parts[0]
allparts.insert(0, parts[1])
return allparts
def get_script_dir():
"""
Returns: the dir of current script
"""
return os.path.dirname(os.path.realpath(sys.argv[0]))
def get_script_file_name():
"""
Returns: the dir of current script
"""
return os.path.basename(sys.argv[0])
def get_script_self_path():
"""
Returns: the dir of current script
"""
return os.path.realpath(sys.argv[0])
def get_parent_dir(location, abspath=False):
"""
Args:
location: current directory or file
Returns:
parent directory absolute or relative path
"""
_path = os.path.abspath if abspath else os.path.relpath
return _path(f_join(location, os.pardir))
def md5_checksum(*fpath):
"""
File md5 signature
"""
hash_md5 = hashlib.md5()
with open(f_join(*fpath), "rb") as f:
for chunk in iter(lambda: f.read(65536), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def create_tar(fsrc, output_tarball, include=None, ignore=None, compress_mode="gz"):
"""
Args:
fsrc: source file or folder
output_tarball: output tar file name
compress_mode: "gz", "bz2", "xz" or "" (empty for uncompressed write)
include: include pattern, will trigger copy to temp directory
ignore: ignore pattern, will trigger copy to temp directory
"""
fsrc, output_tarball = f_expand(fsrc), f_expand(output_tarball)
assert compress_mode in ["gz", "bz2", "xz", ""]
src_base = os.path.basename(fsrc)
tempdir = None
if include or ignore:
tempdir = tempfile.mkdtemp()
tempdest = f_join(tempdir, src_base)
f_copy(fsrc, tempdest, include=include, ignore=ignore)
fsrc = tempdest
with tarfile.open(output_tarball, "w:" + compress_mode) as tar:
tar.add(fsrc, arcname=src_base)
if tempdir:
f_remove(tempdir)
def extract_tar(source_tarball, output_dir=".", members=None):
"""
Args:
source_tarball: extract members from archive
output_dir: default to current working dir
members: must be a subset of the list returned by getmembers()
"""
source_tarball, output_dir = f_expand(source_tarball), f_expand(output_dir)
with tarfile.open(source_tarball, "r:*") as tar:
tar.extractall(output_dir, members=members)
def move_with_backup(*fpath, suffix=".bak"):
"""
Ensures that a path is not occupied. If there is a file, rename it by
adding @suffix. Resursively backs up everything.
Args:
fpath: file path to clear
suffix: Add to backed up files (default: {'.bak'})
"""
fpath = str(f_join(*fpath))
if os.path.exists(fpath):
move_with_backup(fpath + suffix)
shutil.move(fpath, fpath + suffix)
def insert_before_ext(name, insert):
"""
log.txt -> log.ep50.txt
"""
name, ext = os.path.splitext(name)
return name + insert + ext
def timestamp_file_name(fname):
timestr = datetime.now().strftime("_%H-%M-%S_%m-%d-%y")
return insert_before_ext(fname, timestr)
def get_file_lock(*fpath, timeout: int = 15, logging_level="critical"):
"""
NFS-safe filesystem-backed lock. `pip install flufl.lock`
https://flufllock.readthedocs.io/en/stable/apiref.html
Args:
fpath: should be a path on NFS so that every process can see it
timeout: seconds
"""
from flufl.lock import Lock
logging.getLogger("flufl.lock").setLevel(logging_level.upper())
return Lock(f_join(*fpath), lifetime=timeout)
def load_pickle(*fpaths):
with open(f_join(*fpaths), "rb") as fp:
return pickle.load(fp)
def dump_pickle(data, *fpaths):
with open(f_join(*fpaths), "wb") as fp:
pickle.dump(data, fp)
def load_text(*fpaths, by_lines=False):
with open(f_join(*fpaths), "r") as fp:
if by_lines:
return fp.readlines()
else:
return fp.read()
def load_text_lines(*fpaths):
return load_text(*fpaths, by_lines=True)
def dump_text(s, *fpaths):
with open(f_join(*fpaths), "w") as fp:
fp.write(s)
def dump_text_lines(lines: list[str], *fpaths, add_newline=True):
with open(f_join(*fpaths), "w") as fp:
for line in lines:
print(line, file=fp, end="\n" if add_newline else "")
# aliases to be consistent with other load_* and dump_*
pickle_load = load_pickle
pickle_dump = dump_pickle
text_load = load_text
read_text = load_text
read_text_lines = load_text_lines
write_text = dump_text
write_text_lines = dump_text_lines
text_dump = dump_text
|