def get_pom_generator(workspace, pom_template, artifact_def, dependency): """ Returns a concrete implementation of AbstractPomGen. Arguments: workspace: the crawl.workspace.Workspace singleton pom_template: the template to use for generating dynamic (jar) pom.xmls artifact_def: the crawl.buildpom.MavenArtifactDef instance for access to the parsed MVN-INF/* metadata files dependency: the dependency pointing to this artifact_def """ assert artifact_def is not None assert dependency is not None mode = artifact_def.pom_generation_mode if mode is pomgenmode.DYNAMIC: return DynamicPomGen(workspace, artifact_def, dependency, pom_template) elif mode is pomgenmode.TEMPLATE: content, _ = mdfiles.read_file(workspace.repo_root_path, artifact_def.bazel_package, artifact_def.pom_template_file) return TemplatePomGen(workspace, artifact_def, dependency, content) elif mode is pomgenmode.SKIP: return NoopPomGen(workspace, artifact_def, dependency) else: raise Exception("Bug: unknown pom_generation_mode [%s] for %s" % (mode, artifact_def.bazel_package))
def update_released_artifact(root_path, packages, source_exclusions, new_version=None, new_artifact_hash=None, use_current_artifact_hash=False): """ Updates the version and/or artifact hash attributes in the BUILD.pom.released files in the specified packages. Creates the BUILD.pom.released file if it does not exist. """ for package in packages: path = os.path.join(root_path, package, "BUILD.pom.released") try: if use_current_artifact_hash: assert new_artifact_hash is None # we need to load the BUILD.pom file to see whether additional # packages are specified packages = [package] art_def = buildpom.parse_maven_artifact_def(root_path, package) if art_def is not None: # if the BUILD.pom file doesn't exist, then by definition # additional packages cannot have been specified packages += art_def.additional_change_detected_packages artifact_hash = git.get_dir_hash(root_path, packages, source_exclusions) assert artifact_hash is not None else: artifact_hash = new_artifact_hash content, _ = mdfiles.read_file( root_path, package, mdfiles.BUILD_POM_RELEASED_FILE_NAME) if content is not None: if new_version is not None: content = _update_version_in_build_pom_released_content( content, new_version) if artifact_hash is not None: content = _update_artifact_hash_in_build_pom_released_content( content, artifact_hash) mdfiles.write_file(content, root_path, package, mdfiles.BUILD_POM_RELEASED_FILE_NAME) else: if not os.path.exists(os.path.join(root_path, package)): raise Exception("Bad package %s" % package) content = _get_build_pom_released_content( new_version, artifact_hash) mdfiles.write_file(content, root_path, package, mdfiles.BUILD_POM_RELEASED_FILE_NAME) except: print("[ERROR] Cannot update BUILD.pom.released [%s]: %s" % (path, sys.exc_info())) raise
def parse_maven_artifact_def(root_path, package): """ Parses the BUILD.pom file *and* BUILD.pom.released file at the specified path and returns a MavenArtifactDef instance. Returns None if there is no BUILD.pom file at the specified path. """ content, path = mdfiles.read_file(root_path, package, mdfiles.BUILD_POM_FILE_NAME) if content is None: return None maven_artifact_func = code.get_function_block(content, "maven_artifact") try: art_def = eval(maven_artifact_func) pom_generation_mode = pomgenmode.from_string( art_def.pom_generation_mode) if art_def.custom_pom_template_content is not None: # load the template right away template_path = art_def.custom_pom_template_content template_content, _ = mdfiles.read_file(root_path, package, template_path) art_def.custom_pom_template_content = template_content except: print("[ERROR] Cannot parse [%s]: %s" % (path, sys.exc_info())) raise if pom_generation_mode.produces_artifact: rel_art_def = _parse_released_maven_artifact_def(root_path, package) released_pom_content = _read_released_pom(root_path, package) vers_incr_strat = version.get_version_increment_strategy(content, path) return _augment_art_def_values(art_def, rel_art_def, package, released_pom_content, vers_incr_strat, pom_generation_mode) else: return _augment_art_def_values(art_def, rel_art_def=None, bazel_package=package, released_pom_content=None, version_increment_strategy=None, pom_generation_mode=pom_generation_mode)
def test_read_file(self): root_path = tempfile.mkdtemp("monorepo") package_path = "projects/libs/pastry/abstractions" self._write_file(root_path, package_path, "MVN-INF", "MYFILE", content=FILE_CONTENT) content, _ = mdfiles.read_file(root_path, package_path, "MYFILE") self.assertEqual(FILE_CONTENT, content)
def test_read_file__md_dir_name(self): root_path = tempfile.mkdtemp("monorepo") package_path = "projects/libs/pastry/abstractions" md_dir_name = "MVN-INF" self._write_file(root_path, package_path, md_dir_name, "MYFILE", content=FILE_CONTENT) mdfiles.MD_DIR_NAME = md_dir_name content, _ = mdfiles.read_file(root_path, package_path, "MYFILE") self.assertEqual(FILE_CONTENT, content)
def update_released_artifact(root_path, packages, source_exclusions, new_version=None, new_artifact_hash=None, use_current_artifact_hash=False): """ Updates the version and/or artifact hash attributes in the BUILD.pom.released files in the specified packages. Creates the BUILD.pom.released file if it does not exist. """ for package in packages: path = os.path.join(root_path, package, "BUILD.pom.released") try: if use_current_artifact_hash: assert new_artifact_hash is None artifact_hash = git.get_dir_hash(root_path, package, source_exclusions) assert artifact_hash is not None else: artifact_hash = new_artifact_hash content, _ = mdfiles.read_file( root_path, package, mdfiles.BUILD_POM_RELEASED_FILE_NAME) if content is not None: if new_version is not None: content = _update_version_in_build_pom_released_content( content, new_version) if artifact_hash is not None: content = _update_artifact_hash_in_build_pom_released_content( content, artifact_hash) mdfiles.write_file(content, root_path, package, mdfiles.BUILD_POM_RELEASED_FILE_NAME) else: if not os.path.exists(os.path.join(root_path, package)): raise Exception("Bad package %s" % package) content = _get_build_pom_released_content( new_version, artifact_hash) mdfiles.write_file(content, root_path, package, mdfiles.BUILD_POM_RELEASED_FILE_NAME) except: print("[ERROR] Cannot update BUILD.pom.released [%s]: %s" % (path, sys.exc_info())) raise
def _parse_released_maven_artifact_def(root_path, package): """ Parses the BUILD.pom.released file at the specified path and returns a MavenArtifactDef instance. Returns None if there is no BUILD.pom.released file at the specified path. """ content, path = mdfiles.read_file(root_path, package, mdfiles.BUILD_POM_RELEASED_FILE_NAME) if content is None: return None try: return eval(content) except: print("[ERROR] Cannot parse [%s]: %s" % (path, sys.exc_info())) raise
def update_build_pom_file(root_path, packages, new_version=None, update_version_using_version_incr_strat=False, new_version_incr_strat=None, set_version_to_last_released_version=False, version_qualifier_to_add=None, new_pom_generation_mode=None, add_pom_generation_mode_if_missing=False): """ If a non-None value is provided, updates the following values in BUILD.pom files in the specified packages: - version (also version qualifier) - version_increment_strategy - pom_generation_mode - pom_generation_mode """ for package in packages: build_pom_content, build_pom_path = mdfiles.read_file( root_path, package, mdfiles.BUILD_POM_FILE_NAME) if build_pom_content is None: raise Exception("Invalid package [%s]" % package) try: current_version = version.parse_build_pom_version( build_pom_content) if current_version is None: # only possible if pom_generation_mode=skip. this isn't quite # right, but we'll just ignore these type of packages # for simplicitly, because there isn't much metadata to # update anyway (only pom_generation_mode is specified) continue updated_version = new_version # increment current version using version increment strategy if updated_version is None and update_version_using_version_incr_strat: vers_incr_strat = version.get_version_increment_strategy( build_pom_content, build_pom_path) updated_version = vers_incr_strat(current_version) # set version back to previously released version if updated_version is None and set_version_to_last_released_version: build_pom_released_content, _ = mdfiles.read_file( root_path, package, "BUILD.pom.released") if build_pom_released_content is None: # if the BUILD.pom.released file cannot be read (because it # doesn't exist (yet), this is a noop - we don't want to # fail here because typical usage is to update many # artifacts at once pass else: updated_version = version.parse_build_pom_released_version( build_pom_released_content) # add version qualifier to current version if updated_version is None and version_qualifier_to_add is not None: update_strategy = _get_version_qualifier_update_strategy( version_qualifier_to_add) updated_version = version.version_update_handler( current_version, update_strategy) if updated_version is not None: build_pom_content = _update_version_in_build_pom_content( build_pom_content, updated_version) if new_version_incr_strat is not None: build_pom_content = _update_version_incr_strategy_in_build_pom_content( build_pom_content, new_version_incr_strat) if new_pom_generation_mode is not None: build_pom_content = _update_pom_generation_mode_in_build_pom_content( build_pom_content, new_pom_generation_mode) if add_pom_generation_mode_if_missing: build_pom_content = _add_pom_generation_mode_if_missing_in_build_pom_content( build_pom_content) mdfiles.write_file(build_pom_content, root_path, package, mdfiles.BUILD_POM_FILE_NAME) except: print("[ERROR] Cannot update BUILD.pom [%s]: %s" % (build_pom_path, sys.exc_info())) raise
def _read_released_pom(root_path, package): content, _ = mdfiles.read_file(root_path, package, mdfiles.POM_XML_RELEASED_FILE_NAME) return content