commit b94f7ae022d7de761235fa80f69a21c9ba99aa7b Author: juga0 juga@riseup.net Date: Thu Jun 14 14:27:59 2018 +0000
Generate v3bw files in a dir and named with timestamp --- sbws/config.default.ini | 3 ++- sbws/core/generate.py | 2 ++ sbws/lib/v3bwfile.py | 4 ++-- sbws/util/config.py | 2 +- sbws/util/timestamp.py | 10 ++++++++++ 5 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/sbws/config.default.ini b/sbws/config.default.ini index 15ab014..faa0ab1 100644 --- a/sbws/config.default.ini +++ b/sbws/config.default.ini @@ -1,6 +1,7 @@ [paths] datadir = ${sbws_home}/datadir -v3bw_fname = ${sbws_home}/v3bw.txt +v3bw_dname = ${sbws_home}/v3bw +v3bw_fname = ${v3bw_dname}/{}.v3bw started_filepath = ${sbws_home}/started_at log_filepath = ${sbws_home}/sbws.log
diff --git a/sbws/core/generate.py b/sbws/core/generate.py index 4921f57..61f807d 100644 --- a/sbws/core/generate.py +++ b/sbws/core/generate.py @@ -35,6 +35,8 @@ def main(args, conf): if not is_initted(args.directory): fail_hard('Sbws isn't initialized. Try sbws init')
+ os.makedirs(conf['paths']['v3bw_dname'], exist_ok=True) + datadir = conf['paths']['datadir'] if not os.path.isdir(datadir): fail_hard('%s does not exist', datadir) diff --git a/sbws/lib/v3bwfile.py b/sbws/lib/v3bwfile.py index c6ba9af..ba15d14 100644 --- a/sbws/lib/v3bwfile.py +++ b/sbws/lib/v3bwfile.py @@ -9,7 +9,7 @@ from sbws import __version__ from sbws.globals import SPEC_VERSION, BW_LINE_SIZE from sbws.lib.resultdump import ResultSuccess, _ResultType from sbws.util.filelock import FileLock -from sbws.util.timestamp import now_isodt_str, unixts_to_isodt_str +from sbws.util.timestamp import now_isodt_str, unixts_to_isodt_str, now_fname
log = logging.getLogger(__name__)
@@ -395,7 +395,7 @@ class V3BwFile(object): bw_lines = scale_lines(bw_lines, args.scale_constant) header = V3BwHeader.from_results(conf, results) f = cls(header, bw_lines) - output = args.output or conf['paths']['v3bw_fname'] + output = args.output or conf['paths']['v3bw_fname'].format(now_fname()) f.write(output) return f
diff --git a/sbws/util/config.py b/sbws/util/config.py index cdf8601..1425dda 100644 --- a/sbws/util/config.py +++ b/sbws/util/config.py @@ -149,7 +149,7 @@ def _validate_paths(conf): sec = 'paths' err_tmpl = Template('$sec/$key ($val): $e') unvalidated_keys = [ - 'datadir', 'sbws_home', 'v3bw_fname', + 'datadir', 'sbws_home', 'v3bw_fname', 'v3bw_dname', 'started_filepath', 'log_filepath'] all_valid_keys = unvalidated_keys allow_missing = ['sbws_home'] diff --git a/sbws/util/timestamp.py b/sbws/util/timestamp.py index 0de7551..d4629a3 100644 --- a/sbws/util/timestamp.py +++ b/sbws/util/timestamp.py @@ -47,6 +47,16 @@ def now_isodt_str(): return dt_obj_to_isodt_str(datetime.utcnow())
+def now_fname(): + """ + Return now timestamp in UTC formatted as %Y%m%d_%H%M%S string for file + names. + + :returns: now timestamp in UTC formatted as %Y%m%d_%H%M%S string + """ + return datetime.utcnow().strftime("%Y%m%d_%H%M%S") + + def unixts_to_str(unixts): """Convert unix timestamp integer or float to string""" # even if it is only converting to str, ensure that input is nothing else
tor-commits@lists.torproject.org