def rest_validate(f): if f is None: return None url = getattr(settings, "TS_OM_VALIDATE_URL", None) validate_url = None if url is not None: validate_url = check_url(url, "validate") if validate_url is not None: response = requests.post(validate_url, data=f) if response is not None: return response.text else: validation_data = validate_scenario(f) if validation_data: return json.dumps(validation_data) return None
def rest_validate(f): if f is None: return None url = getattr(settings, "TS_OM_VALIDATE_URL", None) validate_url = None if url is not None: validate_url = check_url(url, "validate") logger.debug("Validation URL: %s" % validate_url) if validate_url is not None: response = requests.post(validate_url, data=f) if response is not None: return response.text else: validation_data = validate_scenario(f) if validation_data: return json.dumps(validation_data) return None
def test_4(self): self.assertEqual(check_url(None, "validate"), "http://127.0.0.1:8000/om_validate/validate/")
def test_2(self): self.assertEqual(check_url("http://google.com/", "validate"), "http://google.com/")
def test_1(self): self.assertEqual(check_url("http://google.com", ""), "http://google.com/")
def validate_scenario(scenario_file): om_dir = check_url(getattr(settings, "OPENMALARIA_EXEC_DIR", None), "openmalaria") scenarios_dir = check_dir(getattr(settings, "TS_OM_SCENARIOS_DIR", None), "scenarios") return_code = 0 out = None data = {} os.chdir(om_dir) schema = [] for f in glob.glob(om_dir + '*.xsd'): schema.append(f) xmlSchemaDoc = etree.parse(schema[0]) xmlSchema = etree.XMLSchema(xmlSchemaDoc) try: tree = etree.fromstring(scenario_file) except etree.ParseError as e: return_code = -1 out = "" for entry in e.error_log: out += entry.message + "\n" if not out: try: xmlSchema.assertValid(tree) except etree.DocumentInvalid as e: return_code = -1 out = "" for entry in e.error_log: out += entry.message + "\n" except etree.XMLSchemaValidateError as e: return_code = -1 out = "" for entry in e.error_log: out += entry.message + "\n" filename = None if not out: logger.info("scenarios_dir: %s" % scenarios_dir) if not os.path.isdir(scenarios_dir): logger.info("Created %s" % scenarios_dir) os.mkdir(scenarios_dir) filename = os.path.join(scenarios_dir, 'scenario_' + ''.join(random.sample(string.lowercase+string.digits,10)) + ".xml") logger.info("Filename: %s" % filename) with open(filename, 'w') as destination: destination.write(scenario_file) if os.name == "nt": cmd = ['openMalaria.exe', '--scenario', filename, '--validate-only'] else: cmd = ['./openMalaria', '--scenario', filename, '--validate-only'] try: out = subprocess.check_output(cmd, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: return_code = e.returncode out = e.output # Remove temp file if filename: os.unlink(filename) logger.info("Return code: %s" % return_code) data['result'] = return_code data['om_output'] = out.split("\n") return data