[scripts/util] add 'lines()' helper
This commit is contained in:
@@ -185,23 +185,18 @@ def main():
|
|||||||
args.sub = extr.subcategory
|
args.sub = extr.subcategory
|
||||||
args.base = extr.basecategory
|
args.base = extr.basecategory
|
||||||
|
|
||||||
path = util.path("test", "results", f"{args.cat}.py")
|
|
||||||
with util.open(path) as fp:
|
|
||||||
lines = fp.readlines()
|
|
||||||
|
|
||||||
LOG.info("Collecting data for '%s'", args.url)
|
LOG.info("Collecting data for '%s'", args.url)
|
||||||
result = generate_test_result(args)
|
result = generate_test_result(args)
|
||||||
|
|
||||||
LOG.info("Writing '%s' results to '%s'", args.url, util.trim(path))
|
path = util.path("test", "results", f"{args.cat}.py")
|
||||||
insert_test_result(args, result, lines)
|
path_tr = util.trim(path)
|
||||||
|
LOG.info("Writing '%s' results to '%s'", args.url, path_tr)
|
||||||
with util.lazy(path) as fp:
|
with util.lines(path) as lines:
|
||||||
fp.writelines(lines)
|
insert_test_result(args, result, lines)
|
||||||
|
|
||||||
if args.git:
|
if args.git:
|
||||||
path = util.trim(path)
|
LOG.info("git add %s", path_tr)
|
||||||
LOG.info("git add %s", path)
|
util.git("add", "--", path_tr)
|
||||||
util.git("add", "--", path)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -47,13 +47,9 @@ def init_extractor(args):
|
|||||||
LOG.info(util.trim(path))
|
LOG.info(util.trim(path))
|
||||||
|
|
||||||
if lines:
|
if lines:
|
||||||
with util.open(path) as fp:
|
with util.lines(path) as lines:
|
||||||
lines = fp.readlines()
|
if not func(args, lines):
|
||||||
if func(args, lines):
|
LOG.warning("'%s' already present", category)
|
||||||
with util.lazy(path) as fp:
|
|
||||||
fp.writelines(lines)
|
|
||||||
else:
|
|
||||||
LOG.warning("'%s' already present", category)
|
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
with util.open(path, args.open_mode) as fp:
|
with util.open(path, args.open_mode) as fp:
|
||||||
|
|||||||
@@ -64,3 +64,21 @@ class lazy():
|
|||||||
else:
|
else:
|
||||||
# only update atime and mtime
|
# only update atime and mtime
|
||||||
os.utime(self.path)
|
os.utime(self.path)
|
||||||
|
|
||||||
|
|
||||||
|
class lines():
|
||||||
|
|
||||||
|
def __init__(self, path, lazy=True):
|
||||||
|
self.path = path
|
||||||
|
self.lazy = lazy
|
||||||
|
self.lines = ()
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
with open(self.path) as fp:
|
||||||
|
self.lines = lines = fp.readlines()
|
||||||
|
return lines
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_value, traceback):
|
||||||
|
ctx = lazy(self.path) if self.lazy else open(self.path, "w")
|
||||||
|
with ctx as fp:
|
||||||
|
fp.writelines(self.lines)
|
||||||
|
|||||||
Reference in New Issue
Block a user