def _read_config(self): """ This method reads provided json config file. """ this_dir = os.path.dirname(__file__) config_file = os.path.abspath(os.path.join(this_dir, "..", "config", "config.json")) self.generic_enum_attrs = [] self.base_attrs = [] self.generic_enums = [] self.named_entity_attrs = [] self.overide_generic_enums = [] Printer.log("Configuration file: %s" % (config_file)) if (os.path.isfile(config_file)): with open(config_file, 'r') as input_json: json_config_data = json.load(input_json) self.base_attrs = json_config_data['base_attrs'] self.generic_enums = json_config_data['generic_enums'] self.named_entity_attrs = json_config_data['named_entity_attrs'] self.overide_generic_enums = json_config_data['overide_generic_enums'] for enum_name, values in self.generic_enums.iteritems(): enum_attr = SpecificationAttribute() enum_attr.name = enum_name enum_attr.allowed_choices = values self.generic_enum_attrs.append(enum_attr) else: Printer.log("Configuration file missing: %s" % (config_file))
def install_user_vanilla(self, user_vanilla_path, output_path, multi_lang=True): """ """ if not user_vanilla_path or not len(user_vanilla_path): return if multi_lang: user_vanilla_path = os.path.join(user_vanilla_path, self.config.language) else: user_vanilla_path = os.path.join(user_vanilla_path) if not os.path.exists(user_vanilla_path): Printer.warn( "Could not find user vanilla folder at path %s. Ignoring" % user_vanilla_path) return for item in os.listdir(user_vanilla_path): s = os.path.join(user_vanilla_path, item) d = os.path.join(output_path, item) if os.path.isdir(s): shutil.copytree(s, d, False, None) else: shutil.copy2(s, d)
def generate(self): """ """ Printer.log("generating %s documentation..." % self._sdk_name) sys.path.append(os.path.abspath(self._sdk_output)) subprocess_environ = {"PYTHONPATH": ":".join(sys.path), "PATH": os.environ["PATH"]} self._install_system_vanilla() self._install_user_vanilla() writer = SDKDocWriter(monolithe_config=self.monolithe_config) writer.write() origin_path = os.getcwd() os.chdir(self._sdkdoc_tmp_path) process = subprocess.Popen(["make", "html"], env=subprocess_environ) process.communicate() os.chdir(origin_path) if os.path.exists(self._sdkdoc_output): shutil.rmtree(self._sdkdoc_output) shutil.copytree("%s/_build/html/" % self._sdkdoc_tmp_path, self._sdkdoc_output) self._cleanup() Printer.success("done")
def generate(self): """ """ Printer.log("generating %s documentation..." % self._sdk_name) sys.path.append(os.path.abspath(self._sdk_output)) subprocess_environ = { "PYTHONPATH": ":".join(sys.path), "PATH": os.environ["PATH"] } self._install_system_vanilla() self._install_user_vanilla() writer = SDKDocWriter(monolithe_config=self.monolithe_config) writer.write() origin_path = os.getcwd() os.chdir(self._sdkdoc_tmp_path) process = subprocess.Popen(["make", "html"], env=subprocess_environ) process.communicate() os.chdir(origin_path) if os.path.exists(self._sdkdoc_output): shutil.rmtree(self._sdkdoc_output) shutil.copytree("%s/_build/html/" % self._sdkdoc_tmp_path, self._sdkdoc_output) self._cleanup() Printer.success("done")
def generate(self, specification_info): """ """ sdk_user_vanilla = self.monolithe_config.get_option("sdk_user_vanilla", "sdk") sdk_output = self.monolithe_config.get_option("sdk_output", "sdk") sdk_name = self.monolithe_config.get_option("sdk_name", "sdk") self.install_system_vanilla(current_file=__file__, output_path=sdk_output) self.install_user_vanilla(user_vanilla_path=sdk_user_vanilla, output_path=sdk_output) generator = SDKAPIVersionGenerator(monolithe_config=self.monolithe_config) apiversions = [] for info in specification_info: apiversions.append(info["api"]["version"]) generator.generate(specification_info=specification_info) Printer.log("assembling all packages...") sdk_writer = SDKWriter(monolithe_config=self.monolithe_config) sdk_writer.write(apiversions=apiversions) cli_writer = CLIWriter(monolithe_config=self.monolithe_config) cli_writer.write() self.cleanup() Printer.success("%s generation complete and available at \"%s\"" % (sdk_name, sdk_output))
def generate_from_repo(self, branches): """ """ specification_info = [] for branch in branches: Printer.log( 'retrieving specifications from github "%s/%s%s@%s"' % ( self.repository_manager.organization.lower(), self.repository_manager.repository.lower(), self.repository_manager.repository_path, branch, ) ) api_info = self.repository_manager.get_api_info(branch=branch) specifications = self.repository_manager.get_all_specifications(branch=branch) self._resolve_parent_apis(specifications) specification_info.append({"specifications": specifications, "api": api_info}) Printer.log( '%d specifications retrieved from branch "%s" (api version: %s)' % (len(specifications), branch, api_info["version"]) ) self.generate(specification_info=specification_info)
def addSuccess(self, test): """ Add success to the result """ TestResult.addSuccess(self, test) self.tests[self.getDescription(test)] = {"status": "SUCCESS"} Printer.success("OK")
def addError(self, test, err, connection=None): """ Add error to the result """ TestResult.addError(self, test, err) self.tests[self.getDescription(test)] = {"status": "ERROR", "stacktrace": err, "connection": connection} Printer.warn("ERROR") Printer.warn(err[1]) TestHelper.trace(connection)
def addFailure(self, test, err, connection): """ Add failure to the result """ TestResult.addFailure(self, test, err) self.tests[self.getDescription(test)] = {"status": "FAILURE", "stacktrace": err, "connection": connection} Printer.warn("Failure") Printer.warn(err[1]) TestHelper.trace(connection)
def get_specifications_infos_from_folder(self): Printer.log("retrieving specifications from folder \"%s\"" % (self.directory_manager.folder)) api_info = self.directory_manager.get_api_info() specifications = self.directory_manager.get_all_specifications() self._resolve_parent_apis(specifications) Printer.log( "%d specifications retrieved from folder \"%s\" (api version: %s)" % (len(specifications), self.directory_manager.folder, api_info["version"])) return {"specifications": specifications, "api": api_info}
def generate_from_folder(self): """ """ specification_info = [] Printer.log("retrieving specifications from folder \"%s\"" % (self.folder_manager.folder)) api_info = self.folder_manager.get_api_info() specifications = self.folder_manager.get_all_specifications() self._resolve_parent_apis(specifications) specification_info.append({"specifications": specifications, "api": api_info}) Printer.log("%d specifications retrieved from folder \"%s\" (api version: %s)" % (len(specifications), self.folder_manager.folder, api_info["version"])) self.generate(specification_info=specification_info)
def run(self, branches=None): specification_info = [] if branches: for branch in branches: Printer.log("Switching to branch %s" % branch) self.directory_manager.switch_branch(branch) specification_info.append( self.get_specifications_infos_from_folder()) else: specification_info.append( self.get_specifications_infos_from_folder()) self.generate(specification_info=specification_info)
def run(self, result=None): """ """ if result is None: result = self.defaultTestResult() result.startTest(self) testMethod = getattr(self, self._testMethodName) try: try: self.setUp() BambouConfig.set_should_raise_bambou_http_error(False) except KeyboardInterrupt: raise except BambouHTTPError as error: result.addError(self, [None, "Test setUp has failed", None], error.connection) return except: result.addError(self, sys.exc_info()) return ok = False try: Printer.log("%s..." % self._testMethodName) testMethod() BambouConfig.set_should_raise_bambou_http_error(True) ok = True except self.failureException: result.addFailure(self, sys.exc_info(), self.last_connection) except KeyboardInterrupt: raise except: result.addError(self, sys.exc_info(), self.last_connection) try: self.tearDown() except KeyboardInterrupt: raise except BambouHTTPError as error: result.addError(self, [None, "Test tearDown has failed", None], error.connection) return except: result.addError(self, sys.exc_info()) ok = False if ok: result.addSuccess(self) finally: result.stopTest(self)
def install_user_vanilla(self, user_vanilla_path, output_path): """ """ if not user_vanilla_path or not len(user_vanilla_path): return if not os.path.exists(user_vanilla_path): Printer.raiseError("Could not find user vanilla folder at path %s" % user_vanilla_path) for item in os.listdir(user_vanilla_path): s = os.path.join(user_vanilla_path, item) d = os.path.join(output_path, item) if os.path.isdir(s): shutil.copytree(s, d, False, None) else: shutil.copy2(s, d)
def install_user_vanilla(self, user_vanilla_path, output_path): """ """ if not user_vanilla_path or not len(user_vanilla_path): return if not os.path.exists(user_vanilla_path): Printer.raiseError( "Could not find user vanilla folder at path %s" % user_vanilla_path) for item in os.listdir(user_vanilla_path): s = os.path.join(user_vanilla_path, item) d = os.path.join(output_path, item) if os.path.isdir(s): shutil.copytree(s, d, False, None) else: shutil.copy2(s, d)
def generate_from_folder(self): """ """ specification_info = [] Printer.log("retrieving specifications from folder \"%s\"" % (self.folder_manager.folder)) api_info = self.folder_manager.get_api_info() specifications = self.folder_manager.get_all_specifications() self._resolve_parent_apis(specifications) specification_info.append({ "specifications": specifications, "api": api_info }) Printer.log( "%d specifications retrieved from folder \"%s\" (api version: %s)" % (len(specifications), self.folder_manager.folder, api_info["version"])) self.generate(specification_info=specification_info)
def generate(self, specification_info): """ """ user_vanilla = self.monolithe_config.get_option("user_vanilla", "transformer") output = self.monolithe_config.get_option("output", "transformer") name = self.monolithe_config.get_option("name", "transformer") lang = self.monolithe_config.language if not os.path.exists(os.path.join(output, lang)): os.makedirs(os.path.join(output, lang)) vanilla_manager = VanillaManager(monolithe_config=self.monolithe_config) vanilla_manager.execute(output_path="%s/%s" % (output, lang)) self.install_user_vanilla(user_vanilla_path=user_vanilla, output_path="%s/%s" % (output, lang)) version_generator = SDKAPIVersionGenerator(monolithe_config=self.monolithe_config) apiversions = [] for info in specification_info: Printer.log("transforming specifications into %s for version %s..." % (lang, info["api"]["version"])) apiversions.append(info["api"]["version"]) version_generator.generate(specification_info=specification_info) Printer.log("assembling...") manager = MainManager(monolithe_config=self.monolithe_config) manager.execute(apiversions=apiversions) cli_manager = CLIManager(monolithe_config=self.monolithe_config) cli_manager.execute() self.cleanup() Printer.success("%s generation complete and available in \"%s/%s\"" % (name, output, self.monolithe_config.language))
def install_user_vanilla(self, user_vanilla_path, output_path, multi_lang=True): """ """ if not user_vanilla_path or not len(user_vanilla_path): return if multi_lang: user_vanilla_path = os.path.join(user_vanilla_path, self.monolithe_config.language) else: user_vanilla_path = os.path.join(user_vanilla_path) if not os.path.exists(user_vanilla_path): Printer.warn("Could not find user vanilla folder at path %s. Ignoring" % user_vanilla_path) return for item in os.listdir(user_vanilla_path): s = os.path.join(user_vanilla_path, item) d = os.path.join(output_path, item) if os.path.isdir(s): shutil.copytree(s, d, False, None) else: shutil.copy2(s, d)
def generate(self, specification_info): """ Start generation ofthe API Documentation """ apidoc_output = self.monolithe_config.get_option("apidoc_output", "apidoc") apidoc_user_vanilla = self.monolithe_config.get_option("apidoc_user_vanilla", "apidoc") sdk_name = self.monolithe_config.get_option("product_name") product_name = self.monolithe_config.get_option("product_name") writer = APIDocWriter(self.monolithe_config) apiversions = [] for info in specification_info: vanilla_output_path = "%s/%s/%s" % (apidoc_output, sdk_name, info["api"]["version"]) self.install_system_vanilla(current_file=__file__, output_path=vanilla_output_path) self.install_user_vanilla(user_vanilla_path=apidoc_user_vanilla, output_path=vanilla_output_path) Printer.log("generating %s api documentation for api version: %s" % (product_name, info["api"]["version"])) writer.write(specifications=info["specifications"], api_info=info["api"]) Printer.success("%s api documentation generation complete and available at \"%s\"" % (product_name, apidoc_output))
def generate_from_repo(self, branches): """ """ specification_info = [] for branch in branches: Printer.log( "retrieving specifications from github \"%s/%s%s@%s\"" % (self.repository_manager.organization.lower(), self.repository_manager.repository.lower(), self.repository_manager.repository_path, branch)) api_info = self.repository_manager.get_api_info(branch=branch) specifications = self.repository_manager.get_all_specifications( branch=branch) self._resolve_parent_apis(specifications) specification_info.append({ "specifications": specifications, "api": api_info }) Printer.log( "%d specifications retrieved from branch \"%s\" (api version: %s)" % (len(specifications), branch, api_info["version"])) self.generate(specification_info=specification_info)
def generate_documentation(self): """ """ name = self.monolithe_config.get_option("name", "transformer") output = self.monolithe_config.get_option("output", "transformer") doc_output = self.monolithe_config.get_option("doc_output", "transformer") input_path = os.path.join(output, self.monolithe_config.language, name) output_path = os.path.join(doc_output, self.monolithe_config.language) if self.monolithe_config.language == 'python': Printer.log("generating documentation...") os.system("pdoc --overwrite --html --html-dir '%s' '%s' >/dev/null 2>&1" % (output_path, input_path)) Printer.success("%s documentation generation complete and available in \"%s\"" % (name, output_path)) else: Printer.warn("no documentation generator for this language. ignoring")
def run(self, test): """ Run the given test case or test suite. """ result = self._makeResult() startTime = time.time() test(result) stopTime = time.time() timeTaken = stopTime - startTime run = result.testsRun Printer.log("Ran %d test%s in %.3fs" % (run, run != 1 and "s" or "", timeTaken)) if not result.wasSuccessful(): Printer.warn("FAILED (failures=%i, errors=%i)" % (len(result.failures), len(result.errors))) else: Printer.success("OK") return result
def generate(self, specification_info): user_vanilla = self.config.get_option("user_vanilla", "transformer") output = self.config.get_option("output", "transformer") name = self.config.get_option("name", "transformer") lang = self.config.language if not os.path.exists(os.path.join(output, lang)): os.makedirs(os.path.join(output, lang)) vanilla_manager = VanillaManager(monolithe_config=self.config) vanilla_manager.execute(output_path="%s/%s" % (output, lang)) self.install_user_vanilla(user_vanilla_path=user_vanilla, output_path="%s/%s" % (output, lang)) version_generator = SDKAPIVersionGenerator(self.config) apiversions = [] for info in specification_info: Printer.log( "transforming specifications into %s for version %s..." % (lang, info["api"]["version"])) apiversions.append(info["api"]["version"]) version_generator.generate(specification_info=specification_info) Printer.log("assembling...") manager = MainManager(monolithe_config=self.config) manager.execute(apiversions=apiversions) cli_manager = CLIManager(monolithe_config=self.config) cli_manager.execute() self.cleanup() Printer.success("%s generation complete and available in \"%s/%s\"" % (name, output, self.config.language))
def _write_model(self, specification): """ This method writes the ouput for a particular specification. """ if specification.allowed_job_commands and not (set( specification.allowed_job_commands).issubset( self.job_commands)): raise Exception( "Invalid allowed_job_commands %s specified in entity %s" % (specification.allowed_job_commands, specification.entity_name)) specification.supportsAlarms = len( filter(lambda child_api: child_api.rest_name == "alarm", specification.child_apis)) == 1 specification.supportsPermissions = len( filter( lambda child_api: child_api.rest_name == "enterprisepermission" or child_api.rest_name == "permission", specification.child_apis)) > 0 specification.supportsDeploymentFailures = len( filter( lambda child_api: child_api.rest_name == "deploymentfailure", specification.child_apis)) == 1 filename = "%s%s.js" % (self._class_prefix, specification.entity_name) self.model_list.append("%s%s" % (self._class_prefix, specification.entity_name)) isNamedEntity = self._isNamedEntity( attributes=specification.attributes ) if specification.rest_name else False superclass_name = "RootEntity" if specification.rest_name == self.api_root else "AbstractNamedEntity" if isNamedEntity else "AbstractModel" if not specification.rest_name else "Entity" # write will write a file using a template. # mandatory params: destination directory, destination file name, template file name # optional params: whatever that is needed from inside the Jinja template specification.attributes_modified = [ attribute for attribute in specification.attributes if (attribute.name not in self.base_attrs and ( not isNamedEntity or attribute.name not in self.named_entity_attrs)) ] enum_attributes = [ attribute for attribute in specification.attributes_modified if attribute.allowed_choices ] enum_attrs_to_import = enum_attributes[:] generic_enum_attrs_in_entity = {} generic_enum_attributes_to_import = [] for attr in enum_attributes: if attr.local_type == "list" and attr.subtype == "enum" and attr.default_value: attr.default_value = attr.default_value.translate( {ord(i): None for i in ' []"'}).split(",") if not all(defval in attr.allowed_choices for defval in attr.default_value): raise Exception( "Invalid default value specified for attribute %s in entity %s" % (attr.name, specification.entity_name)) if specification.rest_name in self.overide_generic_enums and attr.name in self.overide_generic_enums[ specification.rest_name]: continue for generic_enum_attr in self.generic_enum_attrs: if set(attr.allowed_choices) & set( generic_enum_attr.allowed_choices): generic_enum_attrs_in_entity[attr.name] = generic_enum_attr enum_attrs_to_import.remove(attr) generic_enum_attributes_to_import.append( generic_enum_attr.name) self._write_enums(entity_name=specification.entity_name, attributes=enum_attrs_to_import) self.generic_enum_attrs_for_locale[ specification.entity_name] = generic_enum_attrs_in_entity.values() object_subtypes = set([ attribute.subtype for attribute in specification.attributes if (attribute.local_type == "object" and attribute.subtype and attribute.subtype not in self.list_subtypes_generic) ]) invalid_object_attributes = [ attribute.name for attribute in specification.attributes_modified if (attribute.local_type == "object" and not attribute.subtype in self.entity_names) ] if invalid_object_attributes: Printer.log("Spec: %s: Attributes %s use invalid subtypes %s" % (filename, invalid_object_attributes, object_subtypes)) list_subtypes = set([ attribute.subtype for attribute in specification.attributes if (attribute.local_type == "list" and attribute.subtype not in self.list_subtypes_generic) ]) invalid_list_attributes = [ attribute.name for attribute in specification.attributes_modified if (attribute.local_type == "list" and not attribute.subtype in self.entity_names and not attribute.subtype in self.list_subtypes_generic) ] if invalid_list_attributes: Printer.log( "Spec: %s: Attributes %s use invalid list subtypes %s" % (filename, invalid_list_attributes, list_subtypes)) if 'object' in list_subtypes: list_subtypes.remove('object') if 'entity' in list_subtypes: list_subtypes.remove('entity') self.write(destination=self.model_directory, filename=filename, template_name="entity.js.tpl", class_prefix=self._class_prefix, specification=specification, superclass_name=superclass_name, enum_attrs_to_import=enum_attrs_to_import, generic_enum_attributes=generic_enum_attrs_in_entity, generic_enum_attributes_to_import=set( generic_enum_attributes_to_import), subtypes_for_import=object_subtypes.union(list_subtypes))
def trace(cls, connection): """ Trace connection information """ if not connection: return request = connection.request response = connection.response Printer.warn("%s %s [Response %s]" % (request.method, request.url, response.status_code)) Printer.log("Header") Printer.json(request.headers) Printer.log("Body") Printer.json(request.data) Printer.log("Response") Printer.json(response.data) if len(response.errors): Printer.log("Errors") Printer.json(response.errors)