11#!/usr/bin/env python3
22# -*- coding: utf-8 -*-
3+ from pathlib import Path
34
5+ from src .defines .FlattenLevel import FlattenLevel
6+ from src .defines .RenameStrategy import RenameStrategy
7+ from src .model .Repository import Repository
48from src .service .GitHubService import GitHubService , build_github_official_provider
59from src .service .ProviderService import ProviderService , build_provider
10+ from src .service .RepositoryService import compute_path
611from src .service .TokenService import get_github_token
712from src .defines .ProviderType import ProviderType
8- from src .service .GitLabService import build_gitlab_official_provider
13+ from src .service .GitLabService import build_gitlab_official_provider , GitLabService
914from src .service .ArgumentParserService import build_argument_parser , parse_arguments
1015from src .service .UnparserService import print_summary
1116
17+ '''
18+ /backup/owner/provider/organization/repo
19+ '''
20+
1221
1322def build_model (args ):
23+ providers = []
1424
15- model = {args .backup_name : {}}
25+ if args .custom_providers :
26+ providers .extend (args .custom_providers )
1627
17- for username in args .usernames :
18- model [args .backup_name ][username ] = {}
19- if not args .exclude_enterprise :
28+ if not args .exclude_enterprise :
29+ if args .exclude_github :
30+ providers .append (build_gitlab_official_provider ())
31+ if args .exclude_gitlab :
32+ providers .append (build_github_official_provider ())
33+ if not args .exclude_gitlab and not args .exclude_github :
34+ providers .append (build_gitlab_official_provider ())
35+ providers .append (build_github_official_provider ())
36+ if args .custom_providers :
37+ for custom_provider in args .custom_providers :
2038 if args .exclude_github :
21- model [ args . backup_name ][ username ][ "GitLab" ] = build_gitlab_official_provider ( )
39+ providers . append ( build_provider ( custom_provider , ProviderType . GITLAB ) )
2240 if args .exclude_gitlab :
23- model [ args . backup_name ][ username ][ "GitHub" ] = build_github_official_provider ( )
41+ providers . append ( build_provider ( custom_provider , ProviderType . GITHUB ) )
2442 if not args .exclude_gitlab and not args .exclude_github :
25- model [ args . backup_name ][ username ][ "GitLab" ] = build_gitlab_official_provider ( )
26- model [ args . backup_name ][ username ][ "GitHub" ] = build_github_official_provider ( )
27- if args . custom_providers :
28- for custom_provider in args . custom_providers :
29- if args .exclude_github :
30- model [ args . backup_name ][ username ][ custom_provider ] = build_provider ( custom_provider , ProviderType . GITLAB )
31- if args . exclude_gitlab :
32- model [ args . backup_name ][ username ][ custom_provider ] = build_provider ( custom_provider , ProviderType .GITHUB )
33- if not args . exclude_gitlab and not args . exclude_github :
34- model [ args . backup_name ][ username ][ custom_provider ] = build_provider ( custom_provider , ProviderType .GITLAB )
35- model [ args . backup_name ][ username ][ custom_provider ] = build_provider ( custom_provider , ProviderType . GITHUB )
43+ providers . append ( build_provider ( custom_provider , ProviderType . GITLAB ) )
44+ providers . append ( build_provider ( custom_provider , ProviderType . GITHUB ) )
45+
46+ model = {}
47+ for username in args .usernames :
48+ for provider in providers :
49+ provider_service = None
50+ if provider . provider == ProviderType .GITLAB :
51+ provider_service = GitLabService ( provider . token , provider . url )
52+ elif provider . provider == ProviderType .GITHUB :
53+ provider_service = GitHubService ( provider . token , provider . url )
3654
55+ organizations = [username ]
56+ organizations .extend (provider_service .get_user_organization_names (username ))
57+ for organization in organizations :
58+ repos = provider_service .get_organization_repo_names (organization )
59+ for repo in repos :
60+ new_repo = Repository (args .backup_name , username , provider , organization , repo ,
61+ provider .url + "/" + organization + "/" + repo )
62+ computed_path = compute_path (repo , FlattenLevel .ROOT .name in args .flatten_directories ,
63+ FlattenLevel .USER .name in args .flatten_directories ,
64+ FlattenLevel .PROVIDER .name in args .flatten_directories ,
65+ FlattenLevel .ORGANIZATION .name in args .flatten_directories )
66+ new_repo .path = computed_path
67+ if computed_path not in model :
68+ model [computed_path ] = [new_repo ]
69+ else :
70+ if args .rename_strategy == RenameStrategy .IGNORE :
71+ pass
72+ elif args .rename_strategy == RenameStrategy .SHORTEST :
73+ new_repo = Repository (args .backup_name , username , provider , organization , repo ,
74+ provider .url + "/" + organization + "/" + repo )
75+ repo_name_level = FlattenLevel .USER
76+ while computed_path in model :
77+ repo_name_level = FlattenLevel (repo_name_level .value - 1 )
78+ computed_path = compute_path (new_repo , FlattenLevel .ROOT .name in args .flatten_directories ,
79+ FlattenLevel .USER .name in args .flatten_directories ,
80+ FlattenLevel .PROVIDER .name in args .flatten_directories ,
81+ FlattenLevel .ORGANIZATION .name in args .flatten_directories ,
82+ FlattenLevel (repo_name_level .value - 1 ))
83+ new_repo .path = computed_path
84+ model [computed_path ] = [new_repo ]
85+ elif args .rename_strategy == RenameStrategy .SYSTEMATIC :
86+ repo1 = model .pop (computed_path )
87+ repo1 .path = compute_path (repo1 , FlattenLevel .ROOT .name in args .flatten_directories ,
88+ FlattenLevel .USER .name in args .flatten_directories ,
89+ FlattenLevel .PROVIDER .name in args .flatten_directories ,
90+ FlattenLevel .ORGANIZATION .name in args .flatten_directories ,
91+ FlattenLevel .ROOT )
92+ model [repo1 .path ] = [repo1 ]
3793
94+ new_repo = Repository (args .backup_name , username , provider , organization , repo ,
95+ provider .url + "/" + organization + "/" + repo )
96+ new_repo .path = computed_path (repo , FlattenLevel .ROOT .name in args .flatten_directories ,
97+ FlattenLevel .USER .name in args .flatten_directories ,
98+ FlattenLevel .PROVIDER .name in args .flatten_directories ,
99+ FlattenLevel .ORGANIZATION .name in args .flatten_directories ,
100+ FlattenLevel .ROOT )
101+ model [new_repo .path ] = new_repo
102+ elif args .rename_strategy == RenameStrategy .SHORTEST_SYSTEMATIC :
103+ pass
104+ # TODO
105+
106+ print (model )
38107
39108def main ():
40109 parser = build_argument_parser ()
@@ -44,8 +113,6 @@ def main():
44113 model = build_model (args )
45114
46115
47-
48-
49116if __name__ == "__main__" :
50117 # Generate same date string for all entities
51118
0 commit comments