aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMilosz Wasilewski <milosz.wasilewski@linaro.org>2016-10-06 14:47:17 +0100
committerMilosz Wasilewski <milosz.wasilewski@linaro.org>2016-10-06 14:47:17 +0100
commit5abae0ae4c0862eaacf65acab6ad60b2c7cd1244 (patch)
tree7348a2eec89a53f5dc68b4eb9895ae7727095d05
parent14beb7b08f7a9dbe6598ee52d237a667c03d2e42 (diff)
moved file writing to common function
Signed-off-by: Milosz Wasilewski <milosz.wasilewski@linaro.org>
-rwxr-xr-xandroid-report-toolbox.py6
-rw-r--r--tools/__init__.py3
-rw-r--r--tools/benchmarks.py20
-rw-r--r--tools/common.py21
-rw-r--r--tools/cts.py12
5 files changed, 36 insertions, 26 deletions
diff --git a/android-report-toolbox.py b/android-report-toolbox.py
index 9e078b9..1b332a7 100755
--- a/android-report-toolbox.py
+++ b/android-report-toolbox.py
@@ -55,6 +55,12 @@ if __name__ == '__main__':
dest="output",
default="results",
help="name of directory to store results (relative to PWD)")
+ parser.add_argument("-f",
+ "--force",
+ dest="force",
+ action="store_true",
+ default=False,
+ help="Force overwrite existing files")
parser.set_defaults(func=retrieve_build_data)
subparsers = parser.add_subparsers(
help='sub-command help')
diff --git a/tools/__init__.py b/tools/__init__.py
index 5aeaa47..ecb56d5 100644
--- a/tools/__init__.py
+++ b/tools/__init__.py
@@ -1,7 +1,7 @@
import os
import requests
-from common import get_auth
+from common import get_auth, ensure_output
from collections import OrderedDict
from jinja2 import Environment, FileSystemLoader
from tabulate import tabulate
@@ -113,6 +113,7 @@ def result_report(args):
)
basedir = os.path.join(os.getcwd(), args.output)
+ ensure_output(basedir)
with open(os.path.join(basedir, "index.rst"), "wb") as indexfile:
indexfile.write(index.encode('utf-8'))
diff --git a/tools/benchmarks.py b/tools/benchmarks.py
index 032aca4..95684d6 100644
--- a/tools/benchmarks.py
+++ b/tools/benchmarks.py
@@ -10,7 +10,7 @@ from constants import (
RESULTS,
)
from collections import OrderedDict
-from common import get_auth
+from common import get_auth, write_to_file
from jinja2 import Environment, FileSystemLoader
from lava import LavaServerProxy
from netrc import netrc
@@ -208,20 +208,7 @@ def download_benchmarks(args):
tablefmt="rst")
)
- # ToDo: move to some common place
- basedir = os.path.join(os.getcwd(), args.output)
- try:
- os.makedirs(basedir)
- except:
- if not args.force:
- print "{0} already exists".format(basedir)
- exit(1)
- else:
- shutil.rmtree(basedir)
- os.makedirs(basedir)
-
- with open(os.path.join(basedir, "benchmarks.rst"), "w") as benchmarksfile:
- benchmarksfile.write(benchmark_output)
+ write_to_file(args, "benchmarks.rst", benchmark_output)
# create benchmarks styles
benchmark_style_template = env.get_template("benchmarks.styles")
@@ -229,7 +216,6 @@ def download_benchmarks(args):
benchmarkrows = styles_list
)
- with open(os.path.join(basedir, "benchmarks.styles"), "w") as stylesfile:
- stylesfile.write(benchmark_style_output)
+ write_to_file(args, "benchmarks.styles", benchmark_style_output)
diff --git a/tools/common.py b/tools/common.py
index 6c0d7d8..0d92112 100644
--- a/tools/common.py
+++ b/tools/common.py
@@ -1,4 +1,5 @@
import requests
+import shutil
from constants import *
from netrc import netrc
@@ -18,4 +19,24 @@ def get_auth_http(host):
return auth
+def ensure_output(path):
+ try:
+ shutil.mkdirs(path)
+ except OSError as exc: # Python >2.5
+ if exc.errno == errno.EEXIST and os.path.isdir(path):
+ pass
+ else:
+ raise
+
+def write_to_file(args, filename, content):
+ basedir = os.path.join(os.getcwd(), args.output)
+ ensure_output(basedir)
+
+ outfile = os.path.join(basedir, filename)
+ if os.path.exists(outfle) and not args.force:
+ print "{0} exists. Skipping".format(outfile)
+ else:
+ with open(outfile, "w") as o:
+ o.write(content)
+
diff --git a/tools/cts.py b/tools/cts.py
index 344ae2c..6e0b520 100644
--- a/tools/cts.py
+++ b/tools/cts.py
@@ -1,4 +1,4 @@
-from common import get_auth
+from common import get_auth, write_to_file
from constants import (
GREEN,
YELLOW,
@@ -229,16 +229,14 @@ def cts_summary(args, focused_list=None, generate_style=False):
floatfmt=".2f",
tablefmt="rst")
)
- with open(os.path.join(sourcepath, "cts-{0}.rst".format(name)), "w") as ctsfile:
- ctsfile.write(cts_output)
+ write_to_file(args, "cts-{0}.rst".format(name), cts_output)
cts_style = env.get_template("cts.styles")
styles_output = cts_style.render(
name = name,
ctsrows = cts_styles
)
- with open(os.path.join(sourcepath, "{0}.styles".format(name)), "w") as ctsstyles:
- ctsstyles.write(styles_output)
+ write_to_file(args, "cts-{0}.styles".format(name), style_output)
if args.savecsv:
import csv
@@ -307,9 +305,7 @@ def produce_html(args):
xslt = LET.parse(xslt_result_file)
transform = LET.XSLT(xslt)
newdom = transform(dom)
- print final_report_file
- with open(final_report_file,'w') as f:
- f.write(LET.tostring(newdom, pretty_print=True))
+ write_to_file(args, FINAL_REPORT, LET.tostring(newdom, pretty_print=True))
def produce_pdf(args):
# call wkhtmltopdf