#!/usr/bin/env python import os import sys import argparse import logging from git import Repo from retrying import retry # pip install retrying new_path = os.path.normpath(os.path.join(os.path.dirname(__file__), '..')) sys.path.append(new_path) from script import fork_github_repo from script import git_tool _logger = logging.getLogger(__name__) CST_GITHUB_TOKEN = "GITHUB_TOKEN" def get_config(): """Parse command line arguments, extracting the config file name, returning the union of config file and command line arguments :return: dict of config file settings and command line arguments """ config = git_tool.GitTool.get_project_config() # TODO update description parser = argparse.ArgumentParser( formatter_class=argparse.RawDescriptionHelpFormatter, description='''\ ''', epilog='''\ ''' ) parser.add_argument('-d', '--dir', dest="dir", default="./", help="Path of repo to change remote, including submodule.") parser.add_argument('--github_token', dest="github_token", default=config.get(CST_GITHUB_TOKEN), help="GitHub token generated by user") parser.add_argument("--open_web_browser", action="store_true", help="Open web browser for each repo.") parser.add_argument("--generate_only_install_locally", action="store_true", help="Only generate script odoo_install_locally.sh.") parser.add_argument("--generate_repo_source_from_building", action="store_true", help=f"Only generate " f"{git_tool.CST_FILE_SOURCE_REPO_ADDONS_ODOO}") parser.add_argument("--sync_to", dest="sync_to", help=f"Only synchronize matching repo, use path compare to " f"repo.") args = parser.parse_args() return args def main(): # repo = Repo(root_path) # lst_repo = get_all_repo() config = get_config() if config.generate_only_install_locally: gt = git_tool.GitTool() gt.generate_odoo_install_locally() return if config.generate_repo_source_from_building: gt = git_tool.GitTool() gt.generate_repo_source_from_building() return if config.sync_to: gt = git_tool.GitTool() result = gt.get_matching_repo(repo_compare_to=config.sync_to, force_normalize_compare=True) gt.sync_to(result) return # repo_root = Repo(".") lst_repo = git_tool.GitTool.get_repo_info_submodule(repo_path=config.dir) i = 0 total = len(lst_repo) for repo in lst_repo: i += 1 print(f"Nb element {i}/{total}") url = repo.get("url") repo_dir_root = repo.get("path") upstream_name = "MathBenTech" organization_name = "ERPLibre" if config.open_web_browser: git_tool.GitTool.open_repo_web_browser(repo.get("url_https")) # Create the remote upstream split_url = url.split("/") split_url[-2] = upstream_name upstream_url = "/".join(split_url) cloned_repo = Repo(repo_dir_root) # Checkout branch 12.0 # try: # cloned_repo.git.checkout(cloned_repo.head.commit.hexsha) # cloned_repo.delete_head("12.0") # cloned_repo.git.checkout("MathBenTech/12.0", b="12.0", force=True) # except: # # cloned_repo.git.checkout("MathBenTech/12.0", b="12.0", force=True) # print(f"Cannot change branch for {repo_dir_root}") # # try: # # cloned_repo.git.checkout("12.0") # # except: # # print(f"ERROR, missing branch 12.0 for {repo_dir_root}") _logger.info( f"Fork {url} on dir {repo_dir_root} for organization {organization_name}") try: upstream_remote = cloned_repo.remote(upstream_name) print('Remote "%s" already exists in %s' % (upstream_name, repo_dir_root)) except ValueError: upstream_remote = retry( wait_exponential_multiplier=1000, stop_max_delay=15000 )(cloned_repo.create_remote)(upstream_name, upstream_url) print('Remote "%s" created for %s' % (upstream_name, upstream_url)) try: # Fetch the remote upstream retry(wait_exponential_multiplier=1000, stop_max_delay=15000)( upstream_remote.fetch)() print('Remote "%s" fetched' % upstream_name) except Exception: print(f"ERROR git {repo_dir_root} remote {upstream_name} not exist.") upstream_remote.remove(cloned_repo, upstream_name) if __name__ == '__main__': main()