From 09310121fb713e180659983a8c5174b5eb31f92d Mon Sep 17 00:00:00 2001 From: Dikola Date: Tue, 8 Mar 2022 18:25:56 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BB=D0=BE=D0=B3=D0=B8=20=D0=B8=20=D0=BE=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D0=BA=D1=83=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E?= =?UTF-8?q?=D1=87=D0=B5=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 95 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 59 insertions(+), 36 deletions(-) diff --git a/main.py b/main.py index 0060ee1..78629c3 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ import glob import json import argparse +import sys from typing import List from github import Github @@ -20,6 +21,9 @@ fh = logging.FileHandler('repos_log.log') fh.setLevel(logging.INFO) logger.addHandler(fh) +console = logging.StreamHandler(sys.stdout) +logging.getLogger().addHandler(console) + def parse_args(): """ @@ -98,7 +102,6 @@ def clone_repos(repos, :return: """ - callbacks = pygit2.RemoteCallbacks(pygit2.UserPass(token, 'x-oauth-basic')) github_clonned = '' if os.path.exists("github_clonned.txt"): @@ -108,8 +111,6 @@ def clone_repos(repos, file = open("github_clonned.txt", "a+") - - for i, repo in enumerate(repos): id = repo.id name = repo.name @@ -118,6 +119,7 @@ def clone_repos(repos, description = get_description(repo) language = repo.language + logger.info(f'\nCopying {name}:') if str(id) in github_clonned: logger.info(f'Repository {org}/{name} already copied') @@ -125,45 +127,68 @@ def clone_repos(repos, if is_private is not None: if is_private.lower() == 'true' and not private: + logger.info('Repository is public, skipping...') continue if is_private.lower() == 'false' and private: + logger.info('Repository is private, skipping...') continue + try: + logger.info(f'Creating directory to copy.') + local_path = os.path.join(dst_dir, org, name) + os.makedirs(local_path) + authed_clone_url = insert_token(repo.clone_url, token) + + logger.info(f'Cloning: {org}/{name} into {local_path}') + github_repo = Repo.clone_from(authed_clone_url, local_path) + except Exception: + logger.info(f'Error copying repository from github. Probably repository has already been copied locally.') + continue + json_data = { - "title": f"{org}-{name}", - "description": f"{description}", - "alias": f"{org}-{name}", - "language": f"{language}", - "private": "true" - } + "title": f"{org}-{name}", + "description": f"{description}", + "alias": f"{org}-{name}", + "language": f"{language}", + "private": "true" + } - # Создаем репозиторий на гитфлике - gitflic_repo = requests.post( - 'http://localhost:8047/project', - headers = { - "Authorization": f"token {gitflic_token}", - "Content-Type": "application/json" - }, - data = json.dumps(json_data) - ) - - gitflic_url = gitflic_repo.json().get("sshTransportUrl") - - local_path = os.path.join(dst_dir, org, name) - os.makedirs(local_path) - authed_clone_url = insert_token(repo.clone_url, token) - - logger.info(f'Cloning: {org}/{name} into {local_path}') - github_repo = Repo.clone_from(authed_clone_url, local_path) - remote = github_repo.create_remote("gitflic", url = gitflic_url) - remote.push(refspec='--all') - - file.write(str(id) + '\n') + try: + logger.info(f'Creating repository {org}-{name} on gitflic.') + # Создаем репозиторий на гитфлике + gitflic_repo = requests.post( + 'http://localhost:8047/project', + headers={ + "Authorization": f"token {gitflic_token}", + "Content-Type": "application/json" + }, + data=json.dumps(json_data) + ) + except Exception: + logger.info(f'Error while creating repository on gitflic.') + continue -def upload_repos(local_repos: List[str]): - raise NotImplementedError + try: + logger.info(f'Geting ssh link to repository...') + gitflic_url = gitflic_repo.json().get("sshTransportUrl") + + logger.info(f'Establishing a connection to the repository...') + remote = github_repo.create_remote("gitflic", url = gitflic_url) + except Exception: + logger.info(f'Error creating connection to gitflic repository.') + continue + + try: + logger.info(f'Pushing files into repository...') + remote.push(refspec='--all') + + file.write(str(id) + '\n') + logger.info(f'Repository {org}-{name} successfully cloned.') + except Exception: + logger.info(f'Error while pushing files into gitflic repository.') + continue if __name__ == '__main__': @@ -180,7 +205,7 @@ if __name__ == '__main__': for i, repo in enumerate(repos): logger.info('Existing repos:') - logger.info(f'ORG: {get_org_name(repo)} - REPO: {repo.name}') + logger.info(f'ORG: {get_org_name(repo)} - REPO: {repo.name}\n') clone_repos(repos, token, dst_folder, gitflic_token, is_private) @@ -188,6 +213,4 @@ if __name__ == '__main__': # all repos are cloned assert i + 1 == len(local_repos) - upload_repos(local_repos) -