-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsearch.py
95 lines (79 loc) · 2.97 KB
/
search.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
import os
import gevent
from gevent import monkey;monkey.patch_all()
import click
import gitlab
import json
import urllib3
from configparser import ConfigParser
from colored import fg, bg, attr
urllib3.disable_warnings()
config = ConfigParser()
cfg_path = os.path.join(os.environ['HOME'], '.gitlab.cfg')
if not os.path.exists(cfg_path):
cfg_path = '.gitlab.cfg'
config.read(cfg_path)
domain = config['default']['domain']
private_token = config['default']['private_token']
gl = gitlab.Gitlab(domain, private_token=private_token)
def color(text):
return '{}{} {} {}'.format(fg('white'), bg('yellow'), text, attr('reset'))
def replace(text, word):
return text.replace(word, color(word))
class Searcher(object):
def __init__(self, word, count, debug):
self.word = word
self.count = count
self.debug = debug
def search(self, project):
results = project.search('blobs', self.word, page=1, per_page=self.count)
if results:
print('=' * 10 + project.name + '=' * 10)
for idx in range(len(results)):
print(replace(json.dumps(results[idx]), self.word))
elif self.debug:
print('=' * 10 + project.name + '=' * 10 + ' None')
def get_all_my_stars():
stars = []
projects = gl.projects.list(starred=True, all=True, order_by='name', sort='asc')
for project in projects:
stars.append(project.name)
return stars
@click.command()
@click.option('-w', '--word', help='word to search.')
@click.option('-c', '--count', default=1, help='limit results for each repo.', show_default=True)
@click.option('-s', '--scope', default='all', help='all|star|unstar.', show_default=True)
@click.option('-r', '--repo', help='repo for searching')
@click.option('-f', '--filename', help='file of repo list for searching')
@click.option('--debug/--no-debug', default=False)
def search(word, count, scope, repo, filename, debug):
print('searching {} on {}...'.format(word, domain))
if repo:
projects = [gl.projects.get(repo)]
elif filename:
projects = []
for repo in open(filename, "r").readlines():
repo_name = repo.strip()
if repo_name:
try:
project = gl.projects.get(repo_name)
projects.append(project)
except gitlab.exceptions.GitlabGetError:
print('=' * 10 + repo_name + '=' * 10 + ' Not exists')
elif scope == 'star':
projects = gl.projects.list(as_list=False, starred=True)
elif scope == 'unstar':
projects = gl.projects.list(as_list=False)
stars = get_all_my_stars()
else:
projects = gl.projects.list(as_list=False)
s = Searcher(word, count, debug)
tasks = []
for project in projects:
if scope == 'unstar' and project.name in stars:
continue
# s.search(project)
tasks.append(gevent.spawn(s.search, project))
gevent.joinall(tasks)
if __name__ == '__main__':
search()