diff options
author | Lars Wirzenius <liw@liw.fi> | 2020-09-23 10:49:42 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2020-09-23 10:49:42 +0300 |
commit | 5944ae60330babebefd42c27ecea9641dceb68f4 (patch) | |
tree | 489894c7e9c22f99e68ec7f4778673b3993da89b /files.py | |
parent | 35b545208202127663cc357fb401444de2ae55d9 (diff) | |
download | subplot-5944ae60330babebefd42c27ecea9641dceb68f4.tar.gz |
drop: old files library
Diffstat (limited to 'files.py')
-rw-r--r-- | files.py | 90 |
1 files changed, 0 insertions, 90 deletions
diff --git a/files.py b/files.py deleted file mode 100644 index 1a7451f..0000000 --- a/files.py +++ /dev/null @@ -1,90 +0,0 @@ -# Some generic file handling step implementations. - -import os -import re -import time - - -# Create a file on disk from an embedded file. -def create_file(ctx, filename=None): - with open(filename, "wb") as f: - f.write(get_file(filename)) - - -# Update mtime of a file in datadir to a specific time. -def touch_file(ctx, filename=None, y=None, mon=None, d=None, h=None, min=None, s=None): - t = (int(y), int(mon), int(d), int(h), int(min), int(s), -1, -1, -1) - ts = time.mktime(t) - os.utime(filename, times=(ts, ts)) - - -# Update mtime of a file in datadir to current time. -def update_mtime(ctx, filename=None): - os.utime(filename) - - -# Check that a file exists. -def file_exists(ctx, filename=None): - assert_eq(os.path.exists(filename), True) - - -# Check that a file does not exist. -def file_does_not_exist(ctx, filename=None): - assert_eq(os.path.exists(filename), False) - - -# Check that only specific files exist in the current working directory. -def only_these_files_exist(ctx, filenames=None): - filenames = filenames.replace(",", "").split() - assert_eq(set(os.listdir(".")), set(filenames)) - - -# Check that a file contents match a regex. -def file_matches(ctx, filename=None, regex=None): - with open(filename) as f: - content = f.read() - m = re.search(regex, content) - if m is None: - print("content:", repr(content)) - print("regex:", repr(regex)) - assert_eq(bool(m), True) - - -# Check that two files have the same content -def files_match(ctx, filename1=None, filename2=None): - with open(filename1, "rb") as f: - data1 = f.read() - with open(filename2, "rb") as f: - data2 = f.read() - assert_eq(data1, data2) - - -# Check that a file contains a fixed string. -def file_contains(ctx, filename=None, pattern=None): - with open(filename) as f: - content = f.read() - assert_eq(pattern in content, True) - - -# Retrieve metadata for a file as a struct so it can be easily inspected. -def _get_metadata(filename): - st = os.lstat(filename) - keys = ["st_dev", "st_gid", "st_ino", "st_mode", "st_mtime", "st_size", "st_uid"] - return {key: getattr(st, key) for key in keys} - - -# Store in the context current metdata for a file. -def remember_metadata(ctx, filename=None): - meta = ctx.get("remembered-metadata", {}) - meta[filename] = _get_metadata(filename) - ctx["remembered-metadata"] = meta - - -# Check that current metadata of a file is as stored in the context. -def has_same_metadata_as_remembered(ctx, filename=None): - assert_eq(ctx["remembered-metadata"][filename], _get_metadata(filename)) - - -# Check that current metadata of a file is different than stored in the context. -def has_diff_metadata_than_remembered(ctx, filename=None): - assert_ne(ctx["remembered-metadata"][filename], _get_metadata(filename)) |