-
Notifications
You must be signed in to change notification settings - Fork 1
/
task.py
199 lines (171 loc) · 7.15 KB
/
task.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# -*- coding: utf-8 -*-
#
# Copyright (C) 2018 Sandro Lutz <code@temparus.ch>
#
# This software is licensed under GPLv3, see LICENSE for details.
import sys
from hoster import BaseHoster
from repo import Repository, RemoteRepository
def getTaskInstance(config, hoster):
'''
Get a Task instance
:param dict config: task configuration
:param dict hoster: hoster collection
:return: Task object
:rtype: Task
:raises ValueError: if the given configuration is invalid
'''
if any (key not in config for key in ['source', 'destinations']):
raise ValueError('Missing some required properties (source, destinations)')
if config['source'] not in hoster:
raise ValueError('Source hoster\'' + config['source'] + '\' not found')
if len(config['destinations']) == 0:
raise ValueError('Not destinations specified')
source = hoster[config['source']]
destinations = dict()
for destination in config['destinations']:
if destination not in hoster:
raise ValueError('Destination hoster \'' + destination + '\' not found')
destinations[destination] = hoster[destination]
if 'sync' in config:
if config['sync'] not in ['all', 'public', 'internal', 'private', 'manual']:
raise ValueError('sync mode \'' + config['sync'] + '\' is not supported')
sync = config['sync']
else:
sync = 'manual'
task = Task(source, destinations, sync)
if 'create' in config and config['create'] == False:
task.create = False
if 'delete' in config and config['delete'] == True:
task.delete = True
if 'name' in config:
task.name = config['name']
if 'repositories' in config and len(config['repositories']) > 0:
task.repositories = config['repositories']
if 'ignored-repositories' in config:
task.ignored_repositories = config['ignored-repositories']
return task
class Task():
def __init__(self, source, destinations, sync):
'''
Initialize a Task instance
:param BaseHoster source: source hoster
:param dict destinations: dictionary with BaseHoster as destinations
:param str sync: sync mode (all, public, internal, private, manual)
'''
self.source = source
self.destinations = destinations
self.sync = sync
# Default values
self.create = True
self.delete = False
self.name = None
self.repositories = None
self.ignored_repositories = list()
def run(self, verbose):
'''
Run mirror task now. This is a blocking method!
'''
repositories = list()
if self.sync == 'manual':
if self.repositories == None:
return
for repo_name in self.repositories:
if repo_name not in self.ignored_repositories:
try:
source_remote = self.source.getRepository(repo_name)
if not source_remote.description.startswith('MIRROR:'):
repository = self._createRepository(source_remote, self.destinations)
if repository != None:
repositories.append(repository)
except LookupError:
if verbose:
print('Repository \'' + repo_name + '\' not found on \'' + self.source.name + '\'')
except PermissionError:
if verbose:
print('Permission denied on hoster \'' + self.source.name + '\'')
except KeyboardInterrupt as e:
raise e
except Exception as e:
if verbose:
print('ERROR: ' + str(e))
else:
try:
source_remotes = self.source.getRepositoryList(self.sync)
for source_remote in source_remotes:
if source_remote.name not in self.ignored_repositories and \
source_remote.description != None and not source_remote.description.startswith('MIRROR:'):
repository = self._createRepository(source_remote, self.destinations)
if repository != None:
repositories.append(repository)
if verbose:
print('Found repository \'' + source_remote.name + '\'')
except LookupError:
if verbose:
print('Repository \'' + repo_name + '\' not found on \'' + self.source.name + '\'')
except PermissionError:
if verbose:
print('Permission denied on hoster \'' + self.source.name + '\'')
return # Skip this hoster
except KeyboardInterrupt as e:
raise e
except Exception as e:
if verbose:
print('ERROR: ' + str(e))
for repository in repositories:
try:
if verbose:
print('Mirror repository \'' + repository.source.name + '\'')
repository.clone()
repository.push()
except KeyboardInterrupt as e:
raise e
except:
if verbose:
print('SyncError: Skip repository \'' + repository.source.name + '\'')
pass # ignore this repository when an error occures
if self.delete:
source_repositories = self.source.getRepositoryList('public')
source_repositories += self.source.getRepositoryList('internal')
source_repositories += self.source.getRepositoryList('private')
source_names = list()
for source_repo in source_repositories:
if source_repo.description != None and not source_repo.description.startswith('MIRROR:'):
source_names.append(source_repo.name)
# Delete non-existent repositories on mirror destinations
for key in self.destinations:
destination_repositories = self.destinations[key].getRepositoryList('all')
for repo in destination_repositories:
if repo.description != None and repo.description.startswith('MIRROR:') and repo.name not in source_names:
self.destinations[key].deleteRepository(repo)
if verbose:
print('Repository \'' + repo.name + '\' deleted from \'' + self.destinations[key].name + '\'')
def _createRepository(self, source_remote, destinations):
param = {'description': source_remote.description, 'web_url': source_remote.web_url}
description = 'MIRROR: %(description)s // Contribute at %(web_url)s' % param
destination_remotes = dict()
for key in destinations:
try:
remote_repo = destinations[key].getRepository(source_remote.name)
remote_repo.description = description
remote_repo.website = source_remote.website
destinations[key].updateRepository(remote_repo)
destination_remotes[key] = remote_repo
except LookupError:
# Repository does not exist -> create it
if self.create and source_remote.name not in destinations[key].ignored_repositories:
try:
destination_remotes[destinations[key].name] = destinations[key].createRepository(
source_remote.name, source_remote.visibility, description, source_remote.web_url
)
except KeyboardInterrupt as e:
raise e
except:
pass # Skip this destination when communication errors occur
except KeyboardInterrupt as e:
raise e
except:
pass # Skip this destination when communication errors occur
if len(destination_remotes) == 0:
return None
return Repository(source_remote, destination_remotes)