-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgy.py
139 lines (111 loc) · 3.29 KB
/
gy.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
# coding: utf8
"""
gy generates .gitignore files from the command line for you.
"""
import os
import shutil
import zipfile
import click
import requests
HERE = os.path.abspath(os.path.dirname(__file__))
IGNORE_ZIP_URL = 'https://codeload.github.com/github/gitignore/zip/master'
IGNORE_ZIP_NAME = 'ignore-master.zip'
ARCHIVE_DIR = 'archive'
SUFFIX = '.gitignore'
def _unzip(archive, target):
"""
:param archive:
:param target:
:return:
"""
with zipfile.ZipFile(archive, 'r') as z:
z.extractall(target)
def _download_zip(url):
"""
:param url:
:return:
"""
r = requests.get(url)
if r.status_code == 200:
parent = os.path.join(HERE, ARCHIVE_DIR)
if not os.path.exists(parent):
os.mkdir(parent)
path = os.path.join(parent, IGNORE_ZIP_NAME)
with open(path, 'w') as f:
f.write(r.content)
_unzip(path, parent)
os.remove(path)
def _remove_files(target):
"""
:param target:
:return:
"""
path = os.path.join(HERE, target)
if os.path.exists(path):
shutil.rmtree(path)
def wrap_files():
"""
:return:
"""
gd = dict()
path = os.path.join(HERE, ARCHIVE_DIR)
for root, _, files in os.walk(path):
for f in files:
if f.endswith(SUFFIX):
gd[f.replace(SUFFIX, '', 1).lower()] = os.path.join(root, f)
return gd
def ls_ignores():
"""
:return:
"""
return [k for k in wrap_files()]
def ge_ignores(names):
"""
:param names:
:return:
"""
ignores_map = wrap_files()
if not ignores_map:
click.secho(click.style('archive folder is empty, please run `gy update` first.', fg='red'))
return
unknown_list = list()
output_list = list()
for l in names:
if l.lower() not in ignores_map:
unknown_list.append(l)
else:
with open(ignores_map[l.lower()], 'r') as f:
hint = '--- {0} begin ---\n{1}---- {2} end ----\n'.format(l, f.read(), l)
output_list.append(hint)
click.echo('\n'.join(output_list))
if unknown_list:
click.secho(click.style('unsupported files: {0}'.format(', '.join(unknown_list)), fg='yellow'))
click.secho(click.style('run `gy ls` to see all supported languages.'), fg='yellow')
@click.group()
def cli():
"""Yet another .gitignore magician in your command line."""
pass
@cli.command()
def update():
"""update archive folder"""
tasks = [('_remove_files', 'ARCHIVE_DIR'), ('_download_zip', 'IGNORE_ZIP_URL')]
with click.progressbar(tasks, label='updating archive folder') as bar:
for t in bar:
exec ('{0}({1})'.format(t[0], t[1]))
@cli.command()
def ls():
"""list all supported languages"""
ignores_list = ls_ignores()
if not ignores_list:
click.secho(click.style('archive folder is empty, please run `gy update` first.', fg='red'))
return
click.secho(click.style('{0} supported .gitignore files:'.format(len(ignores_list)), fg='blue'))
ignores_list.sort()
click.secho(click.style(', '.join(ignores_list), fg='green'))
@cli.command()
@click.argument('languages', nargs=-1)
def generate(languages):
"""generate .gitignore file"""
ge_ignores(languages)
if __name__ == '__main__':
cli()