def testGetJsonSchemaFileNames(self): ret = incrementVersion._getJsonSchemaFileNames('./resources') self.assertEqual(8, len(ret)) for file in ret: self.assertTrue(doesFileExist(file)) ret2 = incrementVersion._getJsonSchemaFileNames('./tests') self.assertEqual(24, len(ret2)) for file in ret2: self.assertTrue(doesFileExist(file)) # negative test self.assertFalse(doesFileExist(ret2[0] + "xx"))
def main(): args = parser.parse_args() if not doesFileExist(args.model): printError('\nModel file not found ... cancel: {}'.format(args.model)) sys.exit(1) if not _checkValidVersion(args.version): printError( '\nNo valid version argument was given, check the help: {}'.format( args.version)) sys.exit(1) if not _checkDirToCheckForRefs(args.dirToCheckForRefs): printError( "\nThe given directory to check for references isn't a valid dir: {}" .format(args.dirToCheckForRefs)) sys.exit(1) parsedSchema = builder.getParsedSchemaFromJson(args.model) currentVersion = parsedSchema.get("version", None) if currentVersion is None: printInfo('\nModel file does not contain a version: {}'.format( args.model)) sys.exit(0) if not _checkValidVersion(currentVersion): printError( '\nCurrent version is no valid semver: {}'.format(currentVersion)) sys.exit(1) newVersion = _calcNewVersion(currentVersion, args.version, True) logging.info("modelFile: {}, currentVersion: {}, newVersion: {}".format( args.model, currentVersion, newVersion)) _printOutput(args.model, args.backupExt, args.dryRun, newVersion, parsedSchema, currentVersion) if args.dirToCheckForRefs is not None: filesToCheckList = _getJsonSchemaFileNames(args.dirToCheckForRefs) _checkForReferences(args, newVersion, args.model, filesToCheckList, [args.model])
def getJobConfigurationsFromConfigFile(configFile, additionalVarsDict={}, jobsToInclude=[], tasksToInclude=[]): if not doesFileExist(configFile): printError('\ncan not find config file: {}'.format(configFile)) return [] configurations = None if configFile.endswith('.json'): with open(configFile) as configFileContent: configurations = json.load(configFileContent) elif configFile.endswith('.yaml') or configFile.endswith('.yaml'): with open(configFile) as configFileContent: configurations = yaml.load(configFileContent) else: printError( '\nunknown config file extension, only json or yaml are supportted' ) return [] jobArray = [] for conf in configurations: job = config.Job(conf) if (len(jobsToInclude) > 0) and (job.name not in jobsToInclude): continue __removeUndesiredTasksIfNeeded(tasksToInclude, job) jobArray.append(job) __replaceEnvVars(jobArray, additionalVarsDict) return jobArray
def testConvertFile(self): dirpath = Path('tmp', 'model2yaml') if dirpath.exists() and dirpath.is_dir(): shutil.rmtree(dirpath) os.mkdir(dirpath) model = 'resources/models/json/yacg_config_schema.json' convertModel(model, False, 'tmp/model2yaml') self.assertTrue( doesFileExist('tmp/model2yaml/yacg_config_schema.yaml'))
def getPreviousMetaData(protocolFile, noLogs): if protocolFile is None: return {} if doesFileExist(protocolFile): with open(protocolFile) as input: return json.load(input) else: if not noLogs: logging.info( 'No protocol file found, skip import: {}'.format(protocolFile)) return {}
def _tryToFindTemplate(templateFile): """tests if the given file name is a external or an internal template. If it is an internal template, then the file name is changed to a relative path. Function return a tupel with the true or false as first element, and the file name to the found file as second element """ fileExists = False templateFileToReturn = templateFile if doesFileExist(templateFile): fileExists = True else: internalTemplateName = 'yacg/generators/templates/{}.mako'.format( templateFile) fileExists = doesFileExist(internalTemplateName) templateFileToReturn = internalTemplateName fileExistsString = getOkTxt('found') if fileExists else getErrorTxt( 'missing') logging.info(' {}\t{}'.format(fileExistsString, templateFile)) return (fileExists, templateFileToReturn)
def getParsedSchemaFromYaml(model): """reads a JSON schema file in yaml format and returns the parsed dictionary from it Keyword arguments: model -- file name and path to the model to load or model content from stdin """ if doesFileExist(model): # model is treaten as file to input with open(model) as json_schema: return yaml.load(json_schema, Loader=yaml.FullLoader) else: # model is treaten as string content to get parsed return yaml.load(model, Loader=yaml.FullLoader)
def main(): args = parser.parse_args() if not args.stdin: if args.model is None: printError('\nModel file not given. It can be passed as parameter or over stdin ... cancel') sys.exit(1) if not doesFileExist(args.model): printError('\nModel file not found ... cancel: {}'.format(args.model)) sys.exit(1) model = args.model else: model = readStdin() if (not args.dryRun) and ((args.destDir is None) or (not os.path.isdir(args.destDir))): printError('\nDest dir for yaml output not found ... cancel: {}'.format(args.destDir)) sys.exit(1) convertModel(model, args.dryRun, args.destDir)
def _foundAllModels(codeGenerationJobs): """checks up if all model file are accessible. For internal templates the template file name is changed returns True if all templates are available, else False """ foundAll = True for job in codeGenerationJobs: for model in job.models: fileExists = doesFileExist(model.schema) fileExistsString = getOkTxt('found') if fileExists \ else getErrorTxt('missing') if not fileExists: foundAll = False logging.info(' {}\t{}'.format(fileExistsString, model.schema)) return foundAll
def getParsedSchemaFromJson(model): """reads a JSON schema file in json format and returns the parsed dictionary from it Keyword arguments: model -- file name and path to the model to load or model content from stdin """ if __isHttpLoadableModel(model): with urllib.request.urlopen(model) as url: return json.loads(url.read().decode()) elif doesFileExist(model): # model is treaten as file to input with open(model) as json_schema: return json.load(json_schema) else: # model is treaten as string content to get parsed return json.loads(model)
def getJobConfigurationsFromConfigFile(configFile, additionalVarsDict={}): if not doesFileExist(configFile): printError('\ncan not find config file: {}'.format(configFile)) return [] configurations = None if configFile.endswith('.json'): with open(configFile) as configFileContent: configurations = json.load(configFileContent) elif configFile.endswith('.yaml') or configFile.endswith('.yaml'): with open(configFile) as configFileContent: configurations = yaml.load(configFileContent) else: printError('\nunknown config file extension, only json or yaml are supportted') return [] jobArray = [] for conf in configurations: job = config.Job(conf) jobArray.append(job) __replaceEnvVars(jobArray, additionalVarsDict) return jobArray