erplibre/script/fork_project.py

122 lines
4.2 KiB
Python
Raw Normal View History

#!./venv/bin/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.git_tool import GitTool
_logger = logging.getLogger(__name__)
CST_EL_GITHUB_TOKEN = "EL_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 = 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('--organization', dest="organization", default="ERPLibre",
help="Choose organization to fork and change all repo.")
parser.add_argument('--github_token', dest="github_token",
default=config.get(CST_EL_GITHUB_TOKEN),
help="GitHub token generated by user")
args = parser.parse_args()
return args
def main():
config = get_config()
github_token = config.github_token
if not github_token:
raise ValueError("Missing github_token")
repo_root = Repo(config.dir)
lst_repo = GitTool().get_source_repo_addons(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")
branch_name = None
upstream_name = "upstream"
organization_name = config.organization
# Exception, if repo existing, create appropriate remote to keep access
# and remove origin
# TODO do it
# Don't overwrite upstream_name if already exist
# Create origin of organization of upstream if not exist
# Move actual origin to is organization
working_repo = Repo(repo.get("relative_path"))
dct_remote_name = {a.name: a for a in working_repo.remotes}
remote_origin = dct_remote_name.get("origin")
# for remote in working_repo.remotes:
if remote_origin:
repo_origin_info = GitTool.get_transformed_repo_info_from_url(
remote_origin.url, repo_path=repo.get("relative_path"))
if repo_origin_info.organization not in dct_remote_name.keys():
# Add remote
upstream_remote = retry(
wait_exponential_multiplier=1000,
stop_max_delay=15000
)(working_repo.create_remote)(repo_origin_info.organization,
repo_origin_info.url_https)
print('Remote "%s" created for %s' % (
repo_origin_info.organization, repo_origin_info.url_https))
# Fetch the remote
retry(wait_exponential_multiplier=1000, stop_max_delay=15000)(
upstream_remote.fetch)()
print('Remote "%s" fetched' % repo_origin_info.organization)
# working_repo.git.remote.remove("origin")
upstream_name = organization_name
# TODO a la première création, on choisit le URL source en cas de non existance de upstream,
# sinon on choisit celui de ERPLibre
# [<git.Remote "origin">, <git.Remote "upstream">, <git.Remote "MathBenTech">]
_logger.info(
f"Fork {url} on dir {repo_dir_root} for organization {organization_name}")
fork_github_repo.fork_and_clone_repo(
url,
github_token,
repo_dir_root,
branch_name=branch_name,
upstream_name=upstream_name,
organization_name=organization_name,
# fork_only=True,
repo_root=repo_root,
)
# Update origin to new repo
GitTool().generate_install_locally()
if __name__ == '__main__':
main()