| 1 |
import functools |
| 2 |
import logging |
| 3 |
import os |
| 4 |
|
| 5 |
from pbcommand.utils import nfs_exists_check |
| 6 |
from pbcommand.pb_io import load_report_from_json |
| 7 |
|
| 8 |
log = logging.getLogger(__name__) |
| 9 |
log.addHandler(logging.NullHandler()) |
| 10 |
|
| 11 |
|
| 12 |
def trigger_nfs_refresh(ff): |
| 13 |
|
| 14 |
return nfs_exists_check(ff) |
| 15 |
|
| 16 |
|
| 17 |
def _validate_resource(func, resource): |
| 18 |
"""Validate the existence of a file/dir""" |
| 19 |
_ = nfs_exists_check(resource) |
| 20 |
|
| 21 |
if func(resource): |
| 22 |
return os.path.abspath(resource) |
| 23 |
else: |
| 24 |
raise IOError("Unable to find '{f}'".format(f=resource)) |
| 25 |
|
| 26 |
|
| 27 |
validate_file = functools.partial(_validate_resource, os.path.isfile) |
| 28 |
validate_dir = functools.partial(_validate_resource, os.path.isdir) |
| 29 |
validate_output_dir = functools.partial(_validate_resource, os.path.isdir) |
| 30 |
|
| 31 |
|
| 32 |
def validate_or(f1, f2, error_msg): |
| 33 |
""" |
| 34 |
Apply Valid functions f1, then f2 (if failure occurs) |
| 35 |
|
| 36 |
:param error_msg: Default message to print |
| 37 |
""" |
| 38 |
def wrapper(path): |
| 39 |
try: |
| 40 |
return f1(path) |
| 41 |
except Exception: |
| 42 |
try: |
| 43 |
return f2(path) |
| 44 |
except Exception as e: |
| 45 |
log.error( |
| 46 |
"{m} {p} \n. {e}".format( |
| 47 |
m=error_msg, |
| 48 |
p=path, |
| 49 |
e=repr(e))) |
| 50 |
raise |
| 51 |
|
| 52 |
return wrapper |
| 53 |
|
| 54 |
|
| 55 |
def validate_report_file(report_file_name): |
| 56 |
""" |
| 57 |
Raise ValueError if report contains path seps |
| 58 |
""" |
| 59 |
if not os.path.basename(report_file_name) == report_file_name: |
| 60 |
raise ValueError( |
| 61 |
"Path separators are not allowed: {r}".format( |
| 62 |
r=report_file_name)) |
| 63 |
return report_file_name |
| 64 |
|
| 65 |
|
| 66 |
def validate_fofn(fofn): |
| 67 |
"""Validate existence of FOFN and files within the FOFN. |
| 68 |
|
| 69 |
:param fofn: (str) Path to File of file names. |
| 70 |
:raises: IOError if any file is not found. |
| 71 |
:return: (str) abspath of the input fofn |
| 72 |
""" |
| 73 |
_ = nfs_exists_check(fofn) |
| 74 |
|
| 75 |
if os.path.isfile(fofn): |
| 76 |
file_names = fofn_to_files(os.path.abspath(fofn)) |
| 77 |
log.debug( |
| 78 |
"Found {n} files in FOFN {f}.".format( |
| 79 |
n=len(file_names), f=fofn)) |
| 80 |
return os.path.abspath(fofn) |
| 81 |
else: |
| 82 |
raise IOError("Unable to find {f}".format(f=fofn)) |
| 83 |
|
| 84 |
|
| 85 |
def validate_nonempty_file(resource): |
| 86 |
try: |
| 87 |
resource_path = validate_file(resource) |
| 88 |
except IOError as e: |
| 89 |
raise e |
| 90 |
try: |
| 91 |
with open(resource_path) as handle: |
| 92 |
l = [next(handle) for i in range(2)] |
| 93 |
except StopIteration: |
| 94 |
raise IOError("{f} appears to be empty".format(f=resource)) |
| 95 |
return resource_path |
| 96 |
|
| 97 |
|
| 98 |
def fofn_to_files(fofn): |
| 99 |
"""Util func to convert a fofn file to a list of files.""" |
| 100 |
|
| 101 |
_ = nfs_exists_check(fofn) |
| 102 |
|
| 103 |
if os.path.exists(fofn): |
| 104 |
with open(fofn, 'r') as f: |
| 105 |
files = [line.strip() |
| 106 |
for line in f.readlines() if len(line.strip()) > 0] |
| 107 |
|
| 108 |
for _file in files: |
| 109 |
if not os.path.isfile(_file): |
| 110 |
|
| 111 |
|
| 112 |
found = nfs_exists_check(_file) |
| 113 |
if not found: |
| 114 |
raise IOError("Unable to find file '{f}'".format(f=_file)) |
| 115 |
|
| 116 |
return list(files) |
| 117 |
else: |
| 118 |
raise IOError("Unable to find FOFN {f}".format(f=fofn)) |
| 119 |
|
| 120 |
|
| 121 |
def validate_report(file_name): |
| 122 |
e = [] |
| 123 |
base_path = os.path.dirname(file_name) |
| 124 |
r = load_report_from_json(file_name) |
| 125 |
if r.title is None: |
| 126 |
e.append("Report {i} is missing a title".format(i=r.id)) |
| 127 |
for t in r.tables: |
| 128 |
if t.title is None: |
| 129 |
e.append("Table {r}.{t} is missing a title".format(r=r.id, t=t.id)) |
| 130 |
for col in t.columns: |
| 131 |
if col.header is None: |
| 132 |
e.append("Column {r}.{t}.{c} is missing a header".format( |
| 133 |
r=r.id, t=t.id, c=col.id)) |
| 134 |
lengths = set([len(col.values) for col in t.columns]) |
| 135 |
if len(lengths) != 1: |
| 136 |
e.append("Inconsistent column sizes in table {r}.{t}: {s}".format( |
| 137 |
r=r.id, t=t.id, s=",".join( |
| 138 |
[str(x) for x in sorted(list(lengths))]))) |
| 139 |
for pg in r.plotGroups: |
| 140 |
if pg.title is None: |
| 141 |
e.append("Plot group {r}.{g} is missing a title".format( |
| 142 |
r=r.id, g=pg.id)) |
| 143 |
for plot in pg.plots: |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
if plot.image is None: |
| 148 |
e.append("Plot {r}.{g}.{p} does not have an image".format( |
| 149 |
r=r.id, g=pg.id, p=plot.id)) |
| 150 |
img_path = os.path.join(base_path, plot.image) |
| 151 |
if not os.path.exists(img_path): |
| 152 |
e.append( |
| 153 |
"The plot image {f} does not exist".format( |
| 154 |
f=img_path)) |
| 155 |
if plot.thumbnail is None: |
| 156 |
pass |
| 157 |
|
| 158 |
|
| 159 |
else: |
| 160 |
thumbnail = os.path.join(base_path, plot.thumbnail) |
| 161 |
if not os.path.exists(thumbnail): |
| 162 |
e.append( |
| 163 |
"The thumbnail image {f} does not exist".format( |
| 164 |
f=img_path)) |
| 165 |
if pg.thumbnail is not None: |
| 166 |
thumbnail = os.path.join(base_path, pg.thumbnail) |
| 167 |
if not os.path.exists(thumbnail): |
| 168 |
e.append( |
| 169 |
"The thumbnail image {f} does not exist".format( |
| 170 |
f=img_path)) |
| 171 |
if len(e) > 0: |
| 172 |
raise ValueError("\n".join(e)) |
| 173 |
return r |