11#!/usr/bin/env python3
22# -*- coding: utf-8 -*-
3- from pathlib import Path
43
54from src .defines .FlattenLevel import FlattenLevel
65from src .defines .RenameStrategy import RenameStrategy
76from src .model .Repository import Repository
87from src .service .GitHubService import GitHubService , build_github_official_provider
9- from src .service .ProviderService import ProviderService , build_provider
8+ from src .service .ProviderService import ProviderService , build_provider , build_custom_provider
109from src .service .RepositoryService import compute_path
11- from src .service .TokenService import get_github_token
10+ from src .service .TokenService import get_github_official_token , get_custom_provider_token , get_gitlab_official_token
1211from src .defines .ProviderType import ProviderType
1312from src .service .GitLabService import build_gitlab_official_provider , GitLabService
1413from src .service .ArgumentParserService import build_argument_parser , parse_arguments
@@ -36,64 +35,65 @@ def build_model(args):
3635 if args .custom_providers :
3736 for custom_provider in args .custom_providers :
3837 if args .exclude_github :
39- providers .append (build_provider ( custom_provider , ProviderType .GITLAB ))
38+ providers .append (build_custom_provider ( ProviderType .GITLAB , custom_provider ))
4039 if args .exclude_gitlab :
41- providers .append (build_provider ( custom_provider , ProviderType .GITHUB ))
40+ providers .append (build_custom_provider ( ProviderType .GITHUB , custom_provider ))
4241 if not args .exclude_gitlab and not args .exclude_github :
43- providers .append (build_provider ( custom_provider , ProviderType .GITLAB ))
44- providers .append (build_provider ( custom_provider , ProviderType .GITHUB ))
42+ providers .append (build_custom_provider ( ProviderType .GITLAB , custom_provider ))
43+ providers .append (build_custom_provider ( ProviderType .GITHUB , custom_provider ))
4544
4645 model = {}
4746 for username in args .usernames :
4847 for provider in providers :
4948 provider_service = None
50- if provider .provider == ProviderType .GITLAB :
49+ if provider .provider is ProviderType .GITLAB :
5150 provider_service = GitLabService (provider .token , provider .url )
52- elif provider .provider == ProviderType .GITHUB :
51+ elif provider .provider is ProviderType .GITHUB :
5352 provider_service = GitHubService (provider .token , provider .url )
5453
5554 organizations = [username ]
56- organizations .extend (provider_service .get_user_organization_names (username ))
55+ names = provider_service .get_user_organization_names (username )
56+ organizations .extend (names )
5757 for organization in organizations :
5858 repos = provider_service .get_organization_repo_names (organization )
5959 for repo in repos :
6060 new_repo = Repository (args .backup_name , username , provider , organization , repo ,
6161 provider .url + "/" + organization + "/" + repo )
62- computed_path = compute_path (repo , FlattenLevel .ROOT .name in args .flatten_directories ,
62+ compute_path (new_repo , FlattenLevel .ROOT .name in args .flatten_directories ,
6363 FlattenLevel .USER .name in args .flatten_directories ,
6464 FlattenLevel .PROVIDER .name in args .flatten_directories ,
6565 FlattenLevel .ORGANIZATION .name in args .flatten_directories )
66- new_repo . path = computed_path
66+ computed_path = new_repo . path
6767 if computed_path not in model :
68- model [computed_path ] = [ new_repo ]
68+ model [computed_path ] = new_repo
6969 else :
7070 if args .rename_strategy == RenameStrategy .IGNORE :
7171 pass
7272 elif args .rename_strategy == RenameStrategy .SHORTEST :
7373 new_repo = Repository (args .backup_name , username , provider , organization , repo ,
74- provider .url + "/" + organization + "/" + repo )
74+ provider .url + "/" + organization + "/" + repo , computed_path )
7575 repo_name_level = FlattenLevel .USER
7676 while computed_path in model :
7777 repo_name_level = FlattenLevel (repo_name_level .value - 1 )
78- computed_path = compute_path (new_repo , FlattenLevel .ROOT .name in args .flatten_directories ,
78+ compute_path (new_repo , FlattenLevel .ROOT .name in args .flatten_directories ,
7979 FlattenLevel .USER .name in args .flatten_directories ,
8080 FlattenLevel .PROVIDER .name in args .flatten_directories ,
8181 FlattenLevel .ORGANIZATION .name in args .flatten_directories ,
8282 FlattenLevel (repo_name_level .value - 1 ))
83- new_repo . path = computed_path
84- model [computed_path ] = [ new_repo ]
83+ computed_path = new_repo . path
84+ model [computed_path ] = new_repo
8585 elif args .rename_strategy == RenameStrategy .SYSTEMATIC :
8686 repo1 = model .pop (computed_path )
87- repo1 . path = compute_path (repo1 , FlattenLevel .ROOT .name in args .flatten_directories ,
87+ compute_path (repo1 , FlattenLevel .ROOT .name in args .flatten_directories ,
8888 FlattenLevel .USER .name in args .flatten_directories ,
8989 FlattenLevel .PROVIDER .name in args .flatten_directories ,
9090 FlattenLevel .ORGANIZATION .name in args .flatten_directories ,
9191 FlattenLevel .ROOT )
92- model [repo1 .path ] = [ repo1 ]
92+ model [repo1 .path ] = repo1
9393
9494 new_repo = Repository (args .backup_name , username , provider , organization , repo ,
9595 provider .url + "/" + organization + "/" + repo )
96- new_repo . path = computed_path ( repo , FlattenLevel .ROOT .name in args .flatten_directories ,
96+ compute_path ( new_repo , FlattenLevel .ROOT .name in args .flatten_directories ,
9797 FlattenLevel .USER .name in args .flatten_directories ,
9898 FlattenLevel .PROVIDER .name in args .flatten_directories ,
9999 FlattenLevel .ORGANIZATION .name in args .flatten_directories ,
@@ -103,20 +103,36 @@ def build_model(args):
103103 pass
104104 # TODO
105105
106- print (model )
106+ return model
107+
108+
109+ def clone_repos (model , backup_folder ):
110+ for key , value in model .items ():
111+ provider_service = None
112+ if value .provider .provider is ProviderType .GITLAB :
113+ provider_service = GitLabService (value .provider .token , value .provider .url )
114+ elif value .provider .provider is ProviderType .GITHUB :
115+ provider_service = GitHubService (value .provider .token , value .provider .url )
116+ print (value .link + " " + backup_folder + "/" + key .__str__ ())
117+ provider_service .clone_repo (value .link , backup_folder / key )
118+
107119
108120def main ():
109121 parser = build_argument_parser ()
110122 args = parse_arguments (parser )
111123 if args .is_verbose :
112124 print_summary (args )
113125 model = build_model (args )
126+ clone_repos (model , args .backup_folder )
114127
115128
116129if __name__ == "__main__" :
117130 # Generate same date string for all entities
131+ import sys
132+ import os
133+ sys .path .append (os .path .abspath (os .path .dirname (__file__ )))
118134
119- gh : ProviderService = GitHubService (get_github_token ())
120- print (gh .get_user_collaboration_repo_names ("AleixMT" ))
135+ # gh: ProviderService = GitHubService(get_github_official_token ())
136+ # print(gh.get_user_organization_names ("AleixMT"))
121137
122138 main ()
0 commit comments