Commit 666b9d7f authored by juga's avatar juga Committed by Matt Traudt
Browse files

Move back write function to V3BWFile

but not allowing /dev/stdout
parent a00625b0
...@@ -4,7 +4,6 @@ from sbws.lib.resultdump import load_recent_results_in_datadir ...@@ -4,7 +4,6 @@ from sbws.lib.resultdump import load_recent_results_in_datadir
from argparse import ArgumentDefaultsHelpFormatter from argparse import ArgumentDefaultsHelpFormatter
import os import os
import logging import logging
from sbws.util.filelock import DirectoryLock
from sbws.util.timestamp import now_fname from sbws.util.timestamp import now_fname
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -33,31 +32,6 @@ def gen_parser(sub): ...@@ -33,31 +32,6 @@ def gen_parser(sub):
'out, and we do so proportionally') 'out, and we do so proportionally')
def _write_v3bw_file(v3bwfile, output):
log.info('Writing v3bw file to %s', output)
out_dir = os.path.dirname(output)
out_link = os.path.join(out_dir, 'latest.v3bw')
if os.path.exists(out_link):
log.debug('Deleting existing symlink before creating a new one.')
os.remove(out_link)
# to keep test_generate.py working
if output != '/dev/stdout':
with DirectoryLock(out_dir):
with open(output, 'wt') as fd:
fd.write(str(v3bwfile.header))
for line in v3bwfile.bw_lines:
fd.write(str(line))
output_basename = os.path.basename(output)
log.debug('Creating symlink from {} to {}.'
.format(output_basename, out_link))
os.symlink(output_basename, out_link)
else:
with open(output, 'wt') as fd:
fd.write(str(v3bwfile.header))
for line in v3bwfile.bw_lines:
fd.write(str(line))
def main(args, conf): def main(args, conf):
if not is_initted(args.directory): if not is_initted(args.directory):
fail_hard('Sbws isn\'t initialized. Try sbws init') fail_hard('Sbws isn\'t initialized. Try sbws init')
...@@ -83,5 +57,5 @@ def main(args, conf): ...@@ -83,5 +57,5 @@ def main(args, conf):
return return
bw_file = V3BWFile.from_arg_results(args, conf, results) bw_file = V3BWFile.from_arg_results(args, conf, results)
output = args.output or conf['paths']['v3bw_fname'].format(now_fname()) output = args.output or conf['paths']['v3bw_fname'].format(now_fname())
_write_v3bw_file(bw_file, output) V3BWFile.write(output)
log.info('Mean bandwidth per line: %f "KiB"', bw_file.avg_bw) log.info('Mean bandwidth per line: %f "KiB"', bw_file.avg_bw)
...@@ -3,12 +3,13 @@ ...@@ -3,12 +3,13 @@
(v3bw) used by bandwidth authorities.""" (v3bw) used by bandwidth authorities."""
import logging import logging
import os
from statistics import median from statistics import median
from sbws import __version__ from sbws import __version__
from sbws.globals import SPEC_VERSION, BW_LINE_SIZE from sbws.globals import SPEC_VERSION, BW_LINE_SIZE
from sbws.lib.resultdump import ResultSuccess, _ResultType from sbws.lib.resultdump import ResultSuccess, _ResultType
from sbws.util.filelock import FileLock from sbws.util.filelock import FileLock, DirectoryLock
from sbws.util.timestamp import now_isodt_str, unixts_to_isodt_str from sbws.util.timestamp import now_isodt_str, unixts_to_isodt_str
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -405,3 +406,23 @@ class V3BWFile(object): ...@@ -405,3 +406,23 @@ class V3BWFile(object):
header = V3BWHeader.from_results(conf, results) header = V3BWHeader.from_results(conf, results)
f = cls(header, bw_lines) f = cls(header, bw_lines)
return f return f
def write(self, output):
if output == '/dev/stdout':
log.info("Writing to stdout is not supported.")
return
log.info('Writing v3bw file to %s', output)
out_dir = os.path.dirname(output)
out_link = os.path.join(out_dir, 'latest.v3bw')
if os.path.exists(out_link):
log.debug('Deleting existing symlink before creating a new one.')
os.remove(out_link)
with DirectoryLock(out_dir):
with open(output, 'wt') as fd:
fd.write(str(self.header))
for line in self.bw_lines:
fd.write(str(line))
output_basename = os.path.basename(output)
log.debug('Creating symlink from {} to {}.'
.format(output_basename, out_link))
os.symlink(output_basename, out_link)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment