def check_xform(path_to_xform): """ Check the form with the Enketo validator. - return code 1: raise error with the stderr content. - return code 0: append warning with the stdout content (possibly none). :param path_to_xform: Path to the XForm to be validated. :return: warnings or List[str] """ if not _node_installed(): raise EnvironmentError( "pyxform enketo validate dependency: node not found") returncode, timeout, stdout, stderr = run_popen_with_timeout( ["node", ENKETO_VALIDATE_JS, path_to_xform], 100) warnings = [] stderr = decode_stream(stderr) stdout = decode_stream(stdout) if timeout: return ["XForm took to long to completely validate."] else: if returncode > 0: # Error invalid raise EnketoValidateError('Enketo Validate Errors:\n' + ErrorCleaner.enketo_validate(stderr)) elif returncode == 0: if stdout: warnings.append('Enketo Validate Warnings:\n' + stdout) return warnings elif returncode < 0: return ["Bad return code from Enketo Validate."]
def check_java_version(): """Check java version is greater than or equal to java 8. Raises EnvironmentError exception if java version is less than java 8. """ try: stderr = str( run_popen_with_timeout( ["java", "-Djava.awt.headless=true", "-version"], 100)[3]) except OSError as os_error: stderr = str(os_error) if "java version" not in stderr and "openjdk version" not in stderr: raise EnvironmentError( "pyxform odk validate dependency: java not found") # extract version number from version string java_version_str = stderr.split("\n")[0] # version number is usually inside double-quotes. # Using regex to find that in the string java_version = re.findall(r"\"(.+?)\"", java_version_str)[0] if "." in java_version: major, minor = java_version.split(".")[0], java_version.split(".")[1] elif "-" in java_version: major, minor = java_version.split("-")[0], 0 else: major, minor = java_version, 0 if not ((int(major) == 1 and int(minor) >= 8) or int(major) >= 8): raise EnvironmentError("pyxform odk validate dependency: " "java 8 or newer version not found")
def check_xform(path_to_xform): """ Returns an array of warnings if the form is valid. Throws an exception if it is not """ # provide useful error message if java is not installed if not _java_installed(): raise EnvironmentError( "pyxform odk validate dependency: java not found") # resultcode indicates validity of the form # timeout indicates whether validation ran out of time to complete # stdout is not used because it has some warnings that always # appear and can be ignored. # stderr is treated as a warning if the form is valid or an error # if it is invalid. returncode, timeout, stdout, stderr = run_popen_with_timeout( ["java", "-jar", ODK_VALIDATE_JAR, path_to_xform], 100) warnings = [] stderr = decode_stream(stderr) if timeout: return ["XForm took to long to completely validate."] else: if returncode > 0: # Error invalid raise ODKValidateError( 'ODK Validate Errors:\n' + ErrorCleaner.odk_validate(stderr)) elif returncode == 0: if stderr: warnings.append('ODK Validate Warnings:\n' + stderr) return warnings elif returncode < 0: return ["Bad return code from ODK Validate."]
def check_java_version(): """Check java version is greater than or equal to java 8. Raises EnvironmentError exception if java version is less than java 8. """ try: stderr = str(run_popen_with_timeout(["java", "-version"], 100)[3]) except OSError as os_error: stderr = str(os_error) # convert string to unicode for python2 if sys.version_info.major < 3: stderr = stderr.strip().decode("utf-8") if "java version" not in stderr and "openjdk version" not in stderr: raise EnvironmentError("pyxform odk validate dependency: java not found") # extract version number from version string java_version_str = stderr.split("\n")[0] # version number is usually inside double-quotes. # Using regex to find that in the string java_version = re.findall(r"\"(.+?)\"", java_version_str)[0] major, minor, _ = java_version.split(".") if not ((int(major) == 1 and int(minor) >= 8) or int(major) >= 8): raise EnvironmentError( "pyxform odk validate dependency: " "java 8 or newer version not found" )
def _node_installed(): stdout = run_popen_with_timeout(["node", "--help"], 100)[2] stdout = stdout.strip().decode('utf-8') return "Usage: node" in stdout
def _call_validator(path_to_xform, bin_file_path=ODK_VALIDATE_PATH): return run_popen_with_timeout([ "java", "-Djava.awt.headless=true", "-jar", bin_file_path, path_to_xform ], 100)
def _java_installed(): stderr = run_popen_with_timeout(["java", "-version"], 100)[3] stderr = stderr.strip().decode('utf-8') return "java version" in stderr or "openjdk version" in stderr
def _call_validator(path_to_xform, bin_file_path=ENKETO_VALIDATE_PATH): return run_popen_with_timeout([bin_file_path, path_to_xform], 100)
def _call_validator(path_to_xform, bin_file_path=ODK_VALIDATE_PATH): return run_popen_with_timeout(["java", "-jar", bin_file_path, path_to_xform], 100)
def _call_validator(path_to_xform, bin_file_path=ENKETO_VALIDATE_PATH): return run_popen_with_timeout([bin_file_path, path_to_xform], 100)