|
| 1 | +# SBOL Validator worker class |
| 2 | +# Written by Zach Zundel |
| 3 | +# zach.zundel@utah.edu |
| 4 | +# 08/13/2016 |
| 5 | +# Imported from https://github.com/SynBioDex/SBOL-Validator |
| 6 | +import subprocess |
| 7 | +import tempfile |
| 8 | +import uuid |
| 9 | +import traceback |
| 10 | +import os |
| 11 | + |
| 12 | +from sbol2 import Config, ConfigOptions |
| 13 | + |
| 14 | + |
| 15 | +class ValidationResult: |
| 16 | + def __init__(self, output_file, equality): |
| 17 | + self.check_equality = equality |
| 18 | + self.output_file = output_file |
| 19 | + self.valid = False |
| 20 | + self.errors = [] |
| 21 | + |
| 22 | + def digest_errors(self, output): |
| 23 | + self.errors = output.strip().split('\n') |
| 24 | + |
| 25 | + def decipher(self, output, options): |
| 26 | + if self.check_equality: |
| 27 | + if "differ" in output or "not found in" in output: |
| 28 | + self.equal = False |
| 29 | + else: |
| 30 | + self.equal = True |
| 31 | + |
| 32 | + if "Validation successful, no errors." not in output: |
| 33 | + self.valid = False |
| 34 | + self.digest_errors(output) |
| 35 | + else: |
| 36 | + self.digest_errors(output.strip(u"Validation successful, no errors.")) |
| 37 | + self.valid = True |
| 38 | + |
| 39 | + if options.return_file: |
| 40 | + with open(options.output_file, 'r') as file: |
| 41 | + self.result = file.read() |
| 42 | + |
| 43 | + def broken_validation_request(self, command): |
| 44 | + self.valid = False |
| 45 | + self.errors = ["Something about your validation request is contradictory or poorly-formed!", " ".join(command)] |
| 46 | + |
| 47 | + def json(self): |
| 48 | + return self.__dict__ |
| 49 | + |
| 50 | + |
| 51 | +class ValidationRun: |
| 52 | + def __init__(self, options, validation_file, diff_file=None): |
| 53 | + self.options = options |
| 54 | + self.validation_file = validation_file |
| 55 | + self.diff_file = diff_file |
| 56 | + |
| 57 | + def execute(self): |
| 58 | + result = ValidationResult(self.options.output_file, self.options.test_equality) |
| 59 | + |
| 60 | + # Attempt to run command |
| 61 | + jar_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "libSBOLj.jar") |
| 62 | + command = self.options.command(jar_path, self.validation_file, self.diff_file) |
| 63 | + try: |
| 64 | + output = subprocess.check_output(command, universal_newlines=True, stderr=subprocess.STDOUT) |
| 65 | + result.decipher(output, self.options) |
| 66 | + except subprocess.CalledProcessError as exception: |
| 67 | + # If the command fails, the file is not valid. |
| 68 | + result.valid = False |
| 69 | + result.errors += [exception.output, ] |
| 70 | + except ValueError as ve: |
| 71 | + print(traceback.print_tb(ve.__traceback__)) |
| 72 | + result.broken_validation_request(command) |
| 73 | + |
| 74 | + return result.json() |
| 75 | + |
| 76 | + |
| 77 | +class ValidationOptions: |
| 78 | + language = "SBOL2" |
| 79 | + subset_uri = False |
| 80 | + fail_on_first_error = False |
| 81 | + provide_detailed_stack_trace = False |
| 82 | + check_uri_compliance = True |
| 83 | + check_completeness = True |
| 84 | + check_best_practices = False |
| 85 | + uri_prefix = False |
| 86 | + version = False |
| 87 | + insert_type = False |
| 88 | + test_equality = False |
| 89 | + return_file = True |
| 90 | + main_file_name = "main file" |
| 91 | + diff_file_name = "comparison file" |
| 92 | + |
| 93 | + def __init__(self, return_file): |
| 94 | + self.return_file = return_file |
| 95 | + |
| 96 | + def build(self, work_dir, data): |
| 97 | + for key, value in data.items(): |
| 98 | + setattr(self, key, value) |
| 99 | + self.output_file = os.path.join(work_dir, str(uuid.uuid4())) |
| 100 | + |
| 101 | + if self.language in ['SBOL1', 'SBOL2', 'SBML']: |
| 102 | + self.output_file = self.output_file + ".rdf" |
| 103 | + elif self.language == 'GFF3': |
| 104 | + self.output_file = self.output_file + '.gff' |
| 105 | + elif self.language == 'GenBank': |
| 106 | + self.output_file = self.output_file + '.gb' |
| 107 | + else: |
| 108 | + self.output_file = self.output_file + '.fasta' |
| 109 | + |
| 110 | + def command(self, jar_path, validation_file, diff_file=None): |
| 111 | + java_location = Config.getOption(ConfigOptions.JAVA_LOCATION) |
| 112 | + command = [java_location, "-jar", jar_path, validation_file, "-o", self.output_file, "-l", self.language] |
| 113 | + |
| 114 | + if self.test_equality and diff_file: |
| 115 | + command += ["-e", diff_file, "-mf", self.main_file_name, "-cf", self.diff_file_name] |
| 116 | + elif self.test_equality and not diff_file: |
| 117 | + raise ValueError |
| 118 | + |
| 119 | + if self.subset_uri: |
| 120 | + command += ["-s", self.subset_uri] |
| 121 | + |
| 122 | + if self.provide_detailed_stack_trace and not self.fail_on_first_error: |
| 123 | + raise ValueError |
| 124 | + |
| 125 | + if self.fail_on_first_error: |
| 126 | + command += ["-f"] |
| 127 | + |
| 128 | + if self.provide_detailed_stack_trace: |
| 129 | + command += ["-d"] |
| 130 | + |
| 131 | + if not self.check_uri_compliance: |
| 132 | + command += ["-n"] |
| 133 | + |
| 134 | + if not self.check_completeness: |
| 135 | + command += ["-i"] |
| 136 | + |
| 137 | + if self.check_best_practices: |
| 138 | + command += ["-b"] |
| 139 | + |
| 140 | + if self.uri_prefix: |
| 141 | + command += ["-p", self.uri_prefix] |
| 142 | + |
| 143 | + if self.version: |
| 144 | + command += ["-v", self.version] |
| 145 | + |
| 146 | + if self.insert_type: |
| 147 | + command += ["-t"] |
| 148 | + |
| 149 | + return command |
| 150 | + |
| 151 | + |
| 152 | +def do_validation(json): |
| 153 | + """ |
| 154 | + Performs validation based on a json request |
| 155 | + """ |
| 156 | + with tempfile.TemporaryDirectory() as work_dir: |
| 157 | + options = ValidationOptions(json['return_file']) |
| 158 | + options.build(work_dir, json['options']) |
| 159 | + |
| 160 | + main_filename = os.path.join(work_dir, str(uuid.uuid4()) + ".sbol") |
| 161 | + with open(main_filename, 'a+') as file: |
| 162 | + file.write(json["main_file"]) |
| 163 | + |
| 164 | + if json['options']['test_equality']: |
| 165 | + diff_filename = os.path.join(work_dir, str(uuid.uuid4()) + ".sbol") |
| 166 | + |
| 167 | + with open(diff_filename, 'a+') as file: |
| 168 | + file.write(json["diff_file"]) |
| 169 | + |
| 170 | + run = ValidationRun(options, main_filename, diff_filename) |
| 171 | + else: |
| 172 | + run = ValidationRun(options, main_filename) |
| 173 | + |
| 174 | + result = run.execute() |
| 175 | + return result |
0 commit comments