def test_write_file(self): root_path = tempfile.mkdtemp("monorepo") package_path = "projects/libs/pastry/abstractions" os.makedirs(os.path.join(root_path, package_path)) mdfiles.write_file(FILE_CONTENT, root_path, package_path, "MYFILE") content = self._read_file(root_path, package_path, "MVN-INF", "MYFILE") self.assertEqual(FILE_CONTENT, content)
def test_write_file__package_must_exist(self): root_path = tempfile.mkdtemp("monorepo") package_path = "projects/libs/pastry/abstractions" with self.assertRaises(Exception) as ex: mdfiles.write_file(FILE_CONTENT, root_path, package_path, "MYFILE") self.assertTrue( "expected bazel package path to exist" in str(ex.exception))
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 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 main(args): args = _parse_arguments(args) repo_root = common.get_repo_root(args.repo_root) cfg = config.load(repo_root, args.verbose) pom_content = pomcontentm.PomContent() if args.pom_description is not None: pom_content.description = args.pom_description if args.verbose: logger.debug("Global pom content: %s" % pom_content) mvn_install_info = maveninstallinfo.MavenInstallInfo( cfg.maven_install_paths) ws = workspace.Workspace(repo_root, cfg.excluded_dependency_paths, cfg.all_src_exclusions, mvn_install_info, pom_content) packages = argsupport.get_all_packages(repo_root, args.package) packages = ws.filter_artifact_producing_packages(packages) if len(packages) == 0: raise Exception( "Did not find any artifact producing BUILD.pom packages at [%s]" % args.package) spider = crawler.Crawler(ws, cfg.pom_template, args.verbose) result = spider.crawl(packages, follow_monorepo_references=args.recursive, force_release=args.force) if len(result.pomgens) == 0: logger.info( "No releases are required. pomgen will not generate any pom files. To force pom generation, use pomgen's --force option." ) else: output_dir = _get_output_dir(args) for pomgen in result.pomgens: pom_dest_dir = os.path.join(output_dir, pomgen.bazel_package) if not os.path.exists(pom_dest_dir): os.makedirs(pom_dest_dir) # the goldfile pom is actually a pomgen metadata file, so we # write it using the mdfiles module, which ensures it goes # into the proper location within the specified bazel package if args.pom_goldfile: pom_content = pomgen.gen(pom.PomContentType.GOLDFILE) pom_goldfile_path = mdfiles.write_file( pom_content, output_dir, pomgen.bazel_package, mdfiles.POM_XML_RELEASED_FILE_NAME) logger.info("Wrote pom goldfile to [%s]" % pom_goldfile_path) else: pom_content = pomgen.gen(pom.PomContentType.RELEASE) pom_path = os.path.join(pom_dest_dir, "pom.xml") _write_file(pom_path, pom_content) logger.info("Wrote pom file to [%s]" % pom_path) for i, companion_pomgen in enumerate( pomgen.get_companion_generators()): pom_content = companion_pomgen.gen( pom.PomContentType.RELEASE) pom_path = os.path.join(pom_dest_dir, "pom_companion%s.xml" % i) _write_file(pom_path, pom_content) logger.info("Wrote companion pom file to [%s]" % pom_path)
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