-
Notifications
You must be signed in to change notification settings - Fork 86
/
service.py
430 lines (343 loc) · 15.3 KB
/
service.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/usr/bin/env python
import logging
log = logging.getLogger('git_repo.base')
import os
import sys
from git import RemoteProgress, config as git_config
from progress.bar import IncrementalBar as Bar
from subprocess import call
from ..exceptions import ArgumentError
'''select open command'''
if 'darwin' in sys.platform: #pragma: no cover
OPEN_COMMAND = 'open'
else: #pragma: no cover
OPEN_COMMAND = 'xdg-open'
class ProgressBar(RemoteProgress): # pragma: no cover
'''Nice looking progress bar for long running commands'''
def setup(self, repo_name):
self.bar = Bar(message='Pulling from {}'.format(repo_name), suffix='')
def update(self, op_code, cur_count, max_count=100, message=''):
#log.info("{}, {}, {}, {}".format(op_code, cur_count, max_count, message))
max_count = int(max_count or 100)
if max_count != self.bar.max:
self.bar.max = max_count
self.bar.goto(int(cur_count))
def register_target(repo_cmd, repo_service):
"""Decorator to register a class with an repo_service"""
def decorate(klass):
log.debug('Loading service module class: {}'.format(klass.__name__) )
klass.command = repo_cmd
klass.name = repo_service
RepositoryService.service_map[repo_service] = klass
RepositoryService.command_map[repo_cmd] = repo_service
return klass
return decorate
class RepositoryService:
'''Base class for all repository services'''
service_map = dict()
command_map = dict()
# this symbol is made available for testing purposes
_current = None
config_options = ['type', 'token', 'alias', 'fqdn']
@classmethod
def get_config(cls, config):
out = {}
with git_config.GitConfigParser(config, read_only=True) as config:
section = 'gitrepo "{}"'.format(cls.name)
if config.has_section(section):
for option in cls.config_options:
if config.has_option(section, option):
out[option] = config.get(section, option)
return out
@classmethod
def store_config(cls, config, **kwarg):
with git_config.GitConfigParser(config, read_only=False) as config:
section = 'gitrepo "{}"'.format(cls.name)
for option, value in kwarg.items():
if option not in cls.config_options:
raise ArgumentError('Option {} is invalid and cannot be setup.')
config.set_value(section, option, value)
@classmethod
def set_alias(cls, config):
with git_config.GitConfigParser(config, read_only=False) as config:
config.set_value('alias', cls.command, 'repo {}'.format(cls.command))
@classmethod
def get_service(cls, repository, command):
'''Accessor for a repository given a command
:param repository: git-python repository instance
:param command: aliased name of the service
:return: instance for using the service
'''
if not repository:
config = git_config.GitConfigParser(os.path.join(os.environ['HOME'], '.gitconfig'))
else:
config = repository.config_reader()
target = cls.command_map.get(command, command)
conf_section = list(filter(lambda n: 'gitrepo' in n and target in n, config.sections()))
# check configuration constraints
if len(conf_section) == 0:
if not target:
raise ValueError('Service {} unknown'.format(target))
else:
config = dict()
elif len(conf_section) > 1:
raise ValueError('Too many configurations for service {}'.format(target))
# get configuration section as a dict
else:
config = config._sections[conf_section[0]]
if target in cls.service_map:
service = cls.service_map.get(target, cls)
service.name = target
else:
if 'type' not in config:
raise ValueError('Missing service type for custom service.')
if config['type'] not in cls.service_map:
raise ValueError('Service type {} does not exists.')
service = cls.service_map.get(config['type'], cls)
cls._current = service(repository, config)
return cls._current
@classmethod
def get_auth_token(cls, login, password, prompt=None):
raise NotImplementedError
def __init__(self, r=None, c=None):
'''
:param r: git-python repository instance
:param c: configuration data
Build a repository service instance, store configuration and parameters
And launch the connection to the service
'''
self.repository = r
self.config = c
# if there's a configuration file, update the names accordingly
if c:
name = ' '.join(c['__name__'].replace('"', '').split(' ')[1:])
if name != self.name:
if 'fqdn' not in c:
raise ValueError('Custom services SHALL have an URL setting.')
self.fqdn = c['fqdn']
self.name = name
# if not in the configuration file, retrieve the private key from the
# environment (useful for travis configuration), otherwise, make it None.
# using "token" > "private_token" > "privatekey" in configuration file to avoid
# confusion with the SSH keys (yes that happened).
# NB: `git config` doesn't parse underscores in option names, token.
self._privatekey = os.environ.get('PRIVATE_KEY_{}'.format(self.name.upper()),
c.get('token',
c.get('private_token',
c.get('privatekey', None))))
self._alias = c.get('alias', self.name)
self.fqdn = c.get('fqdn', self.fqdn)
# if service has a repository configured, connect
if r:
self.connect()
'''URL handling'''
'''name of the git user to use for SSH remotes'''
git_user = 'git'
@property
def url_ro(self):
'''Property that returns the HTTP URL of the service'''
return 'https://{}'.format(self.fqdn)
@property
def url_rw(self):
return '{}@{}'.format(self.git_user, self.fqdn)
def format_path(self, repository, namespace=None, rw=False):
'''format the repository's URL
:param repository: name of the repository
:param namespace: namespace of the repository
:param rw: return a git+ssh URL if true, an https URL otherwise
:return: the full URI of the repository ready to use as remote
if namespace is not given, repository is expected to be of format
`<namespace>/<repository>`.
'''
repo = repository
if namespace:
repo = '{}/{}'.format(namespace, repository)
if not rw and '/' in repo:
return '{}/{}'.format(self.url_ro, repo)
elif rw and '/' in repo:
return '{}:{}'.format(self.url_rw, repo)
else:
raise ArgumentError("Cannot tell how to handle this url: `{}/{}`!".format(namespace, repo))
def pull(self, remote, branch=None):
'''Pull a repository
:param remote: git-remote instance
:param branch: name of the branch to pull
'''
pb = ProgressBar()
pb.setup(self.name)
if branch:
remote.pull(branch, progress=pb)
else: # pragma: no cover
remote.pull(progress=pb)
print()
def fetch(self, remote, remote_branch, local_branch):
'''Pull a repository
:param remote: git-remote instance
:param branch: name of the branch to pull
'''
pb = ProgressBar()
pb.setup(self.name)
remote.fetch(':'.join([remote_branch, local_branch]), progress=pb)
print()
def clone(self, user, repo, branch='master', rw=True):
'''Clones a new repository
:param user: namespace of the repository
:param repo: name slug of the repository
:Param branch: branch to pull as tracking
This command is fairly simple, and pretty close to the real `git clone`
command, except it does not take a full path, but just a namespace/slug
path for a given service.
'''
log.info('Cloning {}…'.format(repo))
remote = self.add(user=user, repo=repo, tracking=True, rw=rw)
self.pull(remote, branch)
def add(self, repo, user=None, name=None, tracking=False, alone=False, rw=True):
'''Adding repository as remote
:param repo: Name slug of the repository to add
:param name: Name of the remote when stored
:param tracking: When set, makes this remote the tracking for master operations
:param alone: When set, exclude this remote from the "all" remote
This method creates a new remote within the current repository *repo*.
It chooses the name of the repository based on the service's name as
held by current instance of RepositoryService, or uses the *name*
parameter.
It also creates an *all* remote that contains all the remotes added by
this tool, to make it possible to push to all remotes at once.
'''
name = name or self.name
if not user:
if '/' in repo:
user, repo = repo.split('/')
else:
raise ArgumentError('Unable to parse repository {}, missing path separator.'.format(repo))
# removing remote if it already exists
# and extract all repository
all_remote = None
for r in self.repository.remotes:
if r.name == name:
self.repository.delete_remote(r)
# find and stash the remote 'all'
elif r.name == 'all':
all_remote = r
# update remote 'all'
if not alone:
# if remote all does not exists
if not all_remote:
self.repository.create_remote('all', self.format_path(repo, user, rw=rw))
else:
url = self.format_path(repo, user, rw=True)
# check if url not already in remote all
if url not in all_remote.urls:
all_remote.set_url(new_url=self.format_path(repo, user, rw=rw), add=True)
# adding "self" as the tracking remote
if tracking:
remote = self.repository.create_remote(name, self.format_path(repo, user, rw=rw))
# lookup tracking branch (usually master)
for branch in self.repository.branches:
if tracking == branch.name:
# set that branch as tracking
branch.set_tracking_branch(remote.refs[0])
break
return remote
else:
return self.repository.create_remote(name, self.format_path(repo, user, rw=rw))
def run_fork(self, user, repo, branch):
if user == self.user:
# forking the repository on the service
raise ResourceError("Cannot fork a project from yourself.")
log.info("Forking repository {}/{}…".format(user, repo))
# checking for an 'upstream' remote.
if self.repository:
upstream_remotes = list(filter(lambda x: x.name == 'upstream', self.repository.remotes))
if len(upstream_remotes) != 0:
raise ResourceExistsError('A remote named `upstream` already exists. Has this repo already been forked?')
fork_name = self.fork(user, repo)
# checking if a remote with the service's name already exists
if self.repository:
service_remotes = list(filter(lambda x: x.name == self.name, self.repository.remotes))
if len(service_remotes) != 0:
# if it does, rename it to upstream
self.repository.create_remote('upstream', service_remotes[0].url)
self.repository.delete_remote(service_remotes[0].name)
else:
# otherwise create an upstream remote with the source y
self.add(user=user, repo=repo, name='upstream', alone=True)
# add the service named repository
remote = self.add(repo=repo, user=self.user, tracking=self.name)
log.info("New forked repository available at {}/{}".format(self.url_ro,
fork_name))
def open(self, user=None, repo=None):
'''Open the URL of a repository in the user's browser'''
call([OPEN_COMMAND, self.format_path(repo, namespace=user, rw=False)])
def connect(self): #pragma: no cover
'''Brings up the connection to the remote service's API
Meant to be overloaded by subclass
'''
raise NotImplementedError
def delete(self, repo, user=None): #pragma: no cover
'''Delete a remote repository on the service
:param repo: name of the remote repository to delete
Meant to be implemented by subclasses
'''
raise NotImplementedError
def create(self, user, repo, add=False): #pragma: no cover
'''Create a new remote repository on the service
:param repo: name of the repository to create
Meant to be implemented by subclasses
'''
raise NotImplementedError
def fork(self, user, repo): #pragma: no covr
'''Forks a new remote repository on the service
and pulls commits from it
:param repo: name of the repository to create
Meant to be implemented by subclasses
'''
raise NotImplementedError
def gist_list(self):
'''Lists gists
Meant to be implemented by subclasses
'''
raise NotImplementedError
def gist_fetch(self, gist): #pragma: no cover
'''Fetches a published gist
Meant to be implemented by subclasses
'''
raise NotImplementedError
def gist_clone(self, gist): #pragma: no cover
'''Clones a gist
Meant to be implemented by subclasses
'''
raise NotImplementedError
def gist_create(self, gist_path, secret=False): #pragma: no cover
'''Pushes a new gist
Meant to be implemented by subclasses
'''
raise NotImplementedError
def gist_delete(self, gist_path, secret=False): #pragma: no cover
'''Deletes a new gist
Meant to be implemented by subclasses
'''
raise NotImplementedError
def request_list(self, user, repo): #pragma: no cover
'''Lists all available request for merging code
sent to the remote repository
:param repo: name of the repository to create
Meant to be implemented by subclasses
'''
raise NotImplementedError
def request_fetch(self, user, repo, request, pull=False): #pragma: no cover
'''Fetches given request as a branch, and switch if pull is true
:param repo: name of the repository to create
Meant to be implemented by subclasses
'''
raise NotImplementedError
@property
def user(self): #pragma: no cover
raise NotImplementedError
'''
register all services by importing their modules, from the ext pagckage
they are registered using the `register_target()` decorator, and added
to the `RepositorService.service_map` dictionary, and is accessed by the
`main()` function using the `RepositoryService.get_service()` method.
'''
from .ext import *