diff --git a/examples/import_java.py b/examples/import_java.py new file mode 100644 index 0000000..5e2ae35 --- /dev/null +++ b/examples/import_java.py @@ -0,0 +1,20 @@ +#!/usr/bin/python3 + +import argparse +import pv2.importer as importutil + +parser = argparse.ArgumentParser(description="Java Portable Importer") + +parser.add_argument('--name', type=str, required=True) +parser.add_argument('--giturl', type=str, required=True) +parser.add_argument('--gitorg', type=str, required=False, default='rpms') +parser.add_argument('--branch', type=str, required=False, default='') +results = parser.parse_args() +classy = importutil.JavaPortableImport( + results.name, + git_url_path=results.giturl, + org=results.gitorg, + branch=results.branch, +) + +classy.pkg_import() diff --git a/pv2/importer/__init__.py b/pv2/importer/__init__.py index 3850011..551d789 100644 --- a/pv2/importer/__init__.py +++ b/pv2/importer/__init__.py @@ -7,4 +7,4 @@ This assists packagers by taking input as srpm or git location, importing and tagging it as appropriate. """ -from .operation import Import, SrpmImport, GitImport, ModuleImport +from .operation import Import, SrpmImport, GitImport, ModuleImport, JavaPortableImport diff --git a/pv2/importer/operation.py b/pv2/importer/operation.py index 278b9e4..c6c8a60 100644 --- a/pv2/importer/operation.py +++ b/pv2/importer/operation.py @@ -33,7 +33,8 @@ __all__ = [ 'Import', 'SrpmImport', 'GitImport', - 'ModuleImport' + 'ModuleImport', + 'JavaPortableImport' ] # todo: add in logging and replace print with log @@ -1243,3 +1244,157 @@ class ModuleImport(Import): Returns the release """ return self.__release + +class JavaPortableImport(Import): + """ + Does some mangling for java portable packages + """ + # pylint: disable=too-many-arguments + def __init__( + self, + pkg_name: str, + git_url_path: str, + branch: str, + git_user: str = 'git', + org: str = 'rpms', + ): + """ + Init the class. + """ + java_package_name = pkg_name + java_git_url = f'ssh://{git_user}@{git_url_path}/{org}/{java_package_name}.git' + portable_git_url = f'ssh://{git_user}@{git_url_path}/{org}/{java_package_name}-portable.git' + self.__java_git_url = java_git_url + self.__portable_git_url = portable_git_url + self.__branch = branch + self.__java_name = pkg_name + + def pkg_import(self): + """ + Do the import + """ + fileutil.mkdir('/var/tmp/java') + check_repo = gitutil.lsremote(self.java_git_url) + portable_check_repo = gitutil.lsremote(self.portable_git_url) + java_git_repo_path = f'/var/tmp/java/{self.java_name}' + portable_git_repo_path = f'/var/tmp/java/{self.java_name_portable}' + branch = self.branch + repo_tags = [] + if check_repo: + # check for specific ref name + ref_check = f'refs/heads/{branch}' in check_repo + print(f'Cloning: {self.java_name}') + if ref_check: + java_repo = gitutil.clone( + git_url_path=self.java_git_url, + repo_name=self.java_name, + to_path=java_git_repo_path, + branch=branch + ) + else: + raise err.GitCommitError('Invalid branch or information in general') + else: + raise err.GitCommitError('This repository does not exist.') + + if portable_check_repo: + # check for specific ref name + ref_check = f'refs/heads/{branch}' in check_repo + # if our check is correct, clone it. if not, clone normally and + # orphan. + print(f'Cloning: {self.java_name_portable}') + if ref_check: + portable_repo = gitutil.clone( + git_url_path=self.__portable_git_url, + repo_name=f'{self.java_name_portable}', + to_path=portable_git_repo_path, + branch=branch + ) + else: + portable_repo = gitutil.clone( + git_url_path=self.__portable_git_url, + repo_name=f'{self.java_name_portable}', + to_path=portable_git_repo_path, + branch=None + ) + gitutil.checkout(portable_repo, branch=branch, orphan=True) + for tag_name in portable_repo.tags: + repo_tags.append(tag_name.name) + else: + print('Repo may not exist or is private. Try to import anyway.') + portable_repo = gitutil.init( + git_url_path=self.portable_git_url, + repo_name=f'{self.java_name_portable}', + to_path=portable_git_repo_path, + branch=branch + ) + + # Get tag + java_current_tag = java_repo.git.describe() + portable_tag = java_current_tag.replace('openjdk', 'openjdk-portable') + portable_msg = f'importing from {java_current_tag}' + + if portable_tag in repo_tags: + self.perform_cleanup(['/var/tmp/java']) + raise err.GitCommitError(f'Git tag already exists: {portable_tag}') + + print('Copying metadata') + shutil.copy2(f'{java_git_repo_path}/.{self.java_name}.metadata', f'{portable_git_repo_path}/.{self.java_name}-portable.metadata') + print('Copying SOURCE tree') + shutil.rmtree(f'{portable_git_repo_path}/SOURCES') + shutil.copytree(f'{java_git_repo_path}/SOURCES', f'{portable_git_repo_path}/SOURCES') + print('Copying portable spec file') + shutil.copy2(f'{portable_git_repo_path}/SOURCES/{self.java_name}-portable.specfile', f'{portable_git_repo_path}/SPECS/{self.java_name}-portable.spec') + print(f'Committing {portable_tag}') + + # Temporary hack like with git. + dest_gitignore_file = f'{portable_git_repo_path}/.gitignore' + if os.path.exists(dest_gitignore_file): + os.remove(dest_gitignore_file) + + gitutil.add_all(portable_repo) + verify = portable_repo.is_dirty() + if verify: + gitutil.commit(portable_repo, portable_msg) + ref = gitutil.tag(portable_repo, portable_tag, portable_msg) + gitutil.push(portable_repo, ref=ref) + self.perform_cleanup(['/var/tmp/java']) + return True + + print('Nothing to push') + self.perform_cleanup(['/var/tmp/java']) + return False + + @property + def java_name(self): + """ + Returns the name of the java we're working with + """ + return self.__java_name + + @property + def java_name_portable(self): + """ + Returns the name of the java we're working with + """ + return self.__java_name + '-portable' + + @property + def branch(self): + """ + Returns the branch + """ + return self.__java_name + + @property + def java_git_url(self): + """ + Returns the java git URL + """ + return self.__java_git_url + + @property + def portable_git_url(self): + """ + Returns the portable java git URL + """ + return self.__portable_git_url diff --git a/pv2/util/fileutil.py b/pv2/util/fileutil.py index 8d3627a..f2473f6 100644 --- a/pv2/util/fileutil.py +++ b/pv2/util/fileutil.py @@ -13,7 +13,8 @@ __all__ = [ 'filter_files_inverse', 'get_checksum', 'get_magic_file', - 'get_magic_content' + 'get_magic_content', + 'mkdir' ] def filter_files(directory_path: str, filter_filename: str) -> list: @@ -82,3 +83,16 @@ def get_magic_content(data): """ detect = magic.detect_from_content(data) return detect + +def mkdir(file_path: str): + """ + Creates a new directory + """ + #if os.path.exists(file_path): + # raise err.GenericError('Path already exists') + try: + os.mkdir(file_path) + except FileExistsError as exc: + raise err.GenericError('Path already exists') from exc + except Exception as exc: + raise err.GenericError(f'There was another error: {exc}') from exc