diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-03-26 09:00:31 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-03-26 11:27:51 +0200 |
commit | d68ed957f785f4e6969a213e05e4a6bbfc7c391a (patch) | |
tree | 27f29481bb21112449802bd071592e8e143852f4 /subplot | |
parent | 2288d53ce4e55e28cf8f17e3cd06cc5905b88223 (diff) | |
download | jt2-d68ed957f785f4e6969a213e05e4a6bbfc7c391a.tar.gz |
feat! rewrite code
This started out as a change to re-do the command line parsing. I
ended up rewriting everything, and failed to do it in a way that could
be rebased into a sensible series of small commits.
Diffstat (limited to 'subplot')
-rw-r--r-- | subplot/jt.py | 14 | ||||
-rw-r--r-- | subplot/jt.yaml | 8 | ||||
-rw-r--r-- | subplot/vendored/files.py | 194 | ||||
-rw-r--r-- | subplot/vendored/files.yaml | 83 | ||||
-rw-r--r-- | subplot/vendored/runcmd.py (renamed from subplot/runcmd.py) | 0 | ||||
-rw-r--r-- | subplot/vendored/runcmd.yaml (renamed from subplot/runcmd.yaml) | 0 |
6 files changed, 289 insertions, 10 deletions
diff --git a/subplot/jt.py b/subplot/jt.py index 380cde4..31bceb7 100644 --- a/subplot/jt.py +++ b/subplot/jt.py @@ -2,6 +2,14 @@ import logging import os +def install_jt(ctx): + runcmd_prepend_to_path = globals()["runcmd_prepend_to_path"] + srcdir = globals()["srcdir"] + + bindir = os.path.join(srcdir, "target", "debug") + runcmd_prepend_to_path(ctx, bindir) + + def create_script(ctx, filename=None): get_file = globals()["get_file"] text = get_file(filename) @@ -50,12 +58,6 @@ def _binary(name): return os.path.join(srcdir, "target", "debug", "jt2") -def is_directory(ctx, dirname=None): - assert_eq = globals()["assert_eq"] - logging.debug("checking if %r is a directory", dirname) - assert_eq(os.path.isdir(dirname), True) - - def output_contains(ctx, pattern=None): assert_eq = globals()["assert_eq"] logging.debug("checking if %r contains", ctx["stdout"], pattern) diff --git a/subplot/jt.yaml b/subplot/jt.yaml index 6d10a3a..9c0983c 100644 --- a/subplot/jt.yaml +++ b/subplot/jt.yaml @@ -1,3 +1,6 @@ +- given: "an installed jt" + function: install_jt + - given: "an executable script {filename}" function: create_script @@ -24,9 +27,6 @@ - when: I edit draft {draftno} in {dirname} to also contain "{text}" function: edit_draft -- then: directory {dirname} exists - function: is_directory - - then: output contains "(?P<pattern>.*)" regex: true function: output_contains @@ -46,5 +46,5 @@ - then: there is one journal entry in {dirname}, at {variable} function: journal_has_one_entry -- then: file <{variable}> contains "{pattern:text}" +- then: journal entry <{variable}> contains "{pattern:text}" function: file_contains diff --git a/subplot/vendored/files.py b/subplot/vendored/files.py new file mode 100644 index 0000000..dd5b9f8 --- /dev/null +++ b/subplot/vendored/files.py @@ -0,0 +1,194 @@ +import logging +import os +import re +import shutil +import time + + +def files_create_from_embedded(ctx, filename=None): + files_make_directory(ctx, path=os.path.dirname(filename) or ".") + files_create_from_embedded_with_other_name( + ctx, filename_on_disk=filename, embedded_filename=filename + ) + + +def files_create_from_embedded_with_other_name( + ctx, filename_on_disk=None, embedded_filename=None +): + get_file = globals()["get_file"] + + files_make_directory(ctx, path=os.path.dirname(filename_on_disk) or ".") + with open(filename_on_disk, "wb") as f: + f.write(get_file(embedded_filename)) + + +def files_create_from_text(ctx, filename=None, text=None): + files_make_directory(ctx, path=os.path.dirname(filename) or ".") + with open(filename, "w") as f: + f.write(text) + + +def files_make_directory(ctx, path=None): + path = "./" + path + if not os.path.exists(path): + os.makedirs(path) + + +def files_remove_directory(ctx, path=None): + path = "./" + path + shutil.rmtree(path) + + +def files_file_exists(ctx, filename=None): + assert_eq = globals()["assert_eq"] + assert_eq(os.path.exists(filename), True) + + +def files_file_does_not_exist(ctx, filename=None): + assert_eq = globals()["assert_eq"] + assert_eq(os.path.exists(filename), False) + + +def files_directory_exists(ctx, path=None): + assert_eq = globals()["assert_eq"] + assert_eq(os.path.isdir(path), True) + + +def files_directory_does_not_exist(ctx, path=None): + assert_eq = globals()["assert_eq"] + assert_eq(os.path.isdir(path), False) + + +def files_directory_is_empty(ctx, path=None): + assert_eq = globals()["assert_eq"] + assert_eq(os.listdir(path), []) + + +def files_directory_is_not_empty(ctx, path=None): + assert_ne = globals()["assert_ne"] + assert_ne(os.listdir(path), False) + + +def files_only_these_exist(ctx, filenames=None): + assert_eq = globals()["assert_eq"] + filenames = filenames.replace(",", "").split() + assert_eq(set(os.listdir(".")), set(filenames)) + + +def files_file_contains(ctx, filename=None, data=None): + assert_eq = globals()["assert_eq"] + with open(filename, "rb") as f: + actual = f.read() + actual = actual.decode("UTF-8") + assert_eq(data in actual, True) + + +def files_file_matches_regex(ctx, filename=None, regex=None): + assert_eq = globals()["assert_eq"] + with open(filename) as f: + content = f.read() + m = re.search(regex, content) + if m is None: + logging.debug(f"files_file_matches_regex: no match") + logging.debug(f" filenamed: {filename}") + logging.debug(f" regex: {regex}") + logging.debug(f" content: {regex}") + logging.debug(f" match: {m}") + assert_eq(bool(m), True) + + +def files_match(ctx, filename1=None, filename2=None): + assert_eq = globals()["assert_eq"] + with open(filename1, "rb") as f: + data1 = f.read() + with open(filename2, "rb") as f: + data2 = f.read() + assert_eq(data1, data2) + + +def files_touch_with_timestamp( + ctx, + filename=None, + year=None, + month=None, + day=None, + hour=None, + minute=None, + second=None, +): + t = ( + int(year), + int(month), + int(day), + int(hour), + int(minute), + int(second), + -1, + -1, + -1, + ) + ts = time.mktime(t) + _files_touch(filename, ts) + + +def files_touch(ctx, filename=None): + _files_touch(filename, None) + + +def _files_touch(filename, ts): + if not os.path.exists(filename): + open(filename, "w").close() + times = None + if ts is not None: + times = (ts, ts) + os.utime(filename, times=times) + + +def files_mtime_is_recent(ctx, filename=None): + st = os.stat(filename) + age = abs(st.st_mtime - time.time()) + assert age < 1.0 + + +def files_mtime_is_ancient(ctx, filename=None): + st = os.stat(filename) + age = abs(st.st_mtime - time.time()) + year = 365 * 24 * 60 * 60 + required = 39 * year + logging.debug(f"ancient? mtime={st.st_mtime} age={age} required={required}") + assert age > required + + +def files_remember_metadata(ctx, filename=None): + meta = _files_remembered(ctx) + meta[filename] = _files_get_metadata(filename) + logging.debug("files_remember_metadata:") + logging.debug(f" meta: {meta}") + logging.debug(f" ctx: {ctx}") + + +# Check that current metadata of a file is as stored in the context. +def files_has_remembered_metadata(ctx, filename=None): + assert_eq = globals()["assert_eq"] + meta = _files_remembered(ctx) + logging.debug("files_has_remembered_metadata:") + logging.debug(f" meta: {meta}") + logging.debug(f" ctx: {ctx}") + assert_eq(meta[filename], _files_get_metadata(filename)) + + +def files_has_different_metadata(ctx, filename=None): + assert_ne = globals()["assert_ne"] + meta = _files_remembered(ctx) + assert_ne(meta[filename], _files_get_metadata(filename)) + + +def _files_remembered(ctx): + ns = ctx.declare("_files") + return ns.get("remembered-metadata", {}) + + +def _files_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} diff --git a/subplot/vendored/files.yaml b/subplot/vendored/files.yaml new file mode 100644 index 0000000..f18b8cd --- /dev/null +++ b/subplot/vendored/files.yaml @@ -0,0 +1,83 @@ +- given: file {filename} + function: files_create_from_embedded + types: + filename: file + +- given: file {filename_on_disk} from {embedded_filename} + function: files_create_from_embedded_with_other_name + types: + embedded_filename: file + +- given: file {filename} has modification time {year}-{month}-{day} {hour}:{minute}:{second} + function: files_touch_with_timestamp + +- when: I write "(?P<text>.*)" to file (?P<filename>\S+) + regex: true + function: files_create_from_text + +- when: I remember metadata for file {filename} + function: files_remember_metadata + +- when: I touch file {filename} + function: files_touch + +- then: file {filename} exists + function: files_file_exists + +- then: file {filename} does not exist + function: files_file_does_not_exist + +- then: only files (?P<filenames>.+) exist + function: files_only_these_exist + regex: true + +- then: file (?P<filename>\S+) contains "(?P<data>.*)" + regex: true + function: files_file_contains + +- then: file (?P<filename>\S+) matches regex /(?P<regex>.*)/ + regex: true + function: files_file_matches_regex + +- then: file (?P<filename>\S+) matches regex "(?P<regex>.*)" + regex: true + function: files_file_matches_regex + +- then: files {filename1} and {filename2} match + function: files_match + +- then: file {filename} has same metadata as before + function: files_has_remembered_metadata + +- then: file {filename} has different metadata from before + function: files_has_different_metadata + +- then: file {filename} has changed from before + function: files_has_different_metadata + +- then: file {filename} has a very recent modification time + function: files_mtime_is_recent + +- then: file {filename} has a very old modification time + function: files_mtime_is_ancient + +- given: a directory {path} + function: files_make_directory + +- when: I create directory {path} + function: files_make_directory + +- when: I remove directory {path} + function: files_remove_directory + +- then: directory {path} exists + function: files_directory_exists + +- then: directory {path} does not exist + function: files_directory_does_not_exist + +- then: directory {path} is empty + function: files_directory_is_empty + +- then: directory {path} is not empty + function: files_directory_is_not_empty diff --git a/subplot/runcmd.py b/subplot/vendored/runcmd.py index a2564c6..a2564c6 100644 --- a/subplot/runcmd.py +++ b/subplot/vendored/runcmd.py diff --git a/subplot/runcmd.yaml b/subplot/vendored/runcmd.yaml index 48dde90..48dde90 100644 --- a/subplot/runcmd.yaml +++ b/subplot/vendored/runcmd.yaml |