1
|
|
|
# -*- coding: utf-8 -*- |
2
|
1 |
|
''' |
3
|
|
|
Main module |
4
|
|
|
''' |
5
|
|
|
# from __future__ import unicode_literals |
6
|
|
|
|
7
|
1 |
|
import os |
8
|
1 |
|
import re |
9
|
|
|
|
10
|
1 |
|
import click |
11
|
1 |
|
from git.exc import InvalidGitRepositoryError, NoSuchPathError |
12
|
1 |
|
from tabulate import tabulate |
13
|
|
|
|
14
|
1 |
|
import git_app_version |
15
|
1 |
|
from git_app_version.dumper import FileDumper |
16
|
1 |
|
from git_app_version.githandler import RESERVED_KEYS, GitHandler |
17
|
|
|
|
18
|
|
|
|
19
|
1 |
|
def print_version(ctx, param, value): |
20
|
|
|
''' |
21
|
|
|
display application version |
22
|
|
|
''' |
23
|
1 |
|
if not value or ctx.resilient_parsing: |
24
|
1 |
|
return |
25
|
1 |
|
click.echo('git-app-version ' + git_app_version.__version__) |
26
|
1 |
|
ctx.exit() |
27
|
|
|
|
28
|
|
|
|
29
|
1 |
|
CONTEXT_SETTINGS = {'help_option_names': ['-h', '--help']} |
30
|
|
|
|
31
|
|
|
|
32
|
1 |
|
class MetadataParamType(click.ParamType): |
33
|
|
|
''' |
34
|
|
|
Click paramerer Type to parse <key>=<value> option |
35
|
|
|
''' |
36
|
1 |
|
name = 'metadata' |
37
|
|
|
|
38
|
1 |
|
def convert(self, value, param, ctx): |
39
|
|
|
''' |
40
|
|
|
convert row option value to dict |
41
|
|
|
''' |
42
|
1 |
|
try: |
43
|
1 |
|
match = re.match(r'^([^=]+)=(.*)$', value) |
44
|
1 |
|
if not match: |
45
|
1 |
|
raise ValueError( |
46
|
|
|
u'%s is not a valid meta data string e.g. : <key>=<value>' |
47
|
|
|
% value |
48
|
|
|
) |
49
|
|
|
|
50
|
1 |
|
if match.group(1) in RESERVED_KEYS: |
51
|
1 |
|
raise ValueError(u'%s is a reserved key' % match.group(1)) |
52
|
|
|
|
53
|
1 |
|
return {match.group(1): match.group(2).strip('"\'')} |
54
|
|
|
|
55
|
1 |
|
except ValueError as exc: |
56
|
1 |
|
self.fail(str(exc), param, ctx) |
57
|
|
|
|
58
|
|
|
|
59
|
1 |
|
METADATA = MetadataParamType() |
60
|
|
|
|
61
|
|
|
|
62
|
1 |
|
@click.command(context_settings=CONTEXT_SETTINGS) |
63
|
1 |
|
@click.option( |
64
|
|
|
'--version', |
65
|
|
|
'-V', |
66
|
|
|
is_flag=True, |
67
|
|
|
callback=print_version, |
68
|
|
|
expose_value=False, |
69
|
|
|
is_eager=True |
70
|
|
|
) |
71
|
|
|
# @click.option('--verbose', '-v', count=True) |
72
|
1 |
|
@click.option( |
73
|
|
|
'--quiet', '-q', is_flag=True, help='silent mode' |
74
|
|
|
) |
75
|
1 |
|
@click.option( |
76
|
|
|
'--output', |
77
|
|
|
'-o', |
78
|
|
|
default='version', |
79
|
|
|
help='output file path (without extension).' |
80
|
|
|
' Default is \'<repository-path>/version\'.' |
81
|
|
|
) |
82
|
1 |
|
@click.option( |
83
|
|
|
'--format', |
84
|
|
|
'-f', |
85
|
|
|
'output_formats', |
86
|
|
|
type=click.Choice(['all', 'json', 'yml', 'xml', 'ini', 'csv', 'sh']), |
87
|
|
|
multiple=True, |
88
|
|
|
default=['json'], |
89
|
|
|
help='output file format and extension,' |
90
|
|
|
' use \'all\' to output all format, can be set several times ,' |
91
|
|
|
' Default is json.' |
92
|
|
|
) |
93
|
1 |
|
@click.option( |
94
|
|
|
'--namespace', |
95
|
|
|
'-n', |
96
|
|
|
default='', |
97
|
|
|
help='namespace like notation in version file, use dot separator' |
98
|
|
|
' to segment namespaces e.g.: \'foo.bar.git\'.' |
99
|
|
|
' Default is \'app_version\' for XML and INI' |
100
|
|
|
' and no namespace for JSON and YAML.' |
101
|
|
|
' Never used for CSV or Shell file.' |
102
|
|
|
) |
103
|
1 |
|
@click.option( |
104
|
|
|
'--meta', |
105
|
|
|
'-m', |
106
|
|
|
type=METADATA, |
107
|
|
|
multiple=True, |
108
|
|
|
help='meta data to add, format = "<key>=<value>", can be set several times' |
109
|
|
|
) |
110
|
1 |
|
@click.option( |
111
|
|
|
'--csv-delimiter', |
112
|
|
|
'-d', |
113
|
|
|
'csv_delimiter', |
114
|
|
|
default=u',', |
115
|
|
|
help='CSV delimiter, default=","' |
116
|
|
|
) |
117
|
1 |
|
@click.option( |
118
|
|
|
'--csv-eol', |
119
|
|
|
'-e', |
120
|
|
|
'csv_eol', |
121
|
|
|
type=click.Choice(['lf', 'crlf']), |
122
|
|
|
default="lf", |
123
|
|
|
help='CSV end of line,' |
|
|
|
|
124
|
|
|
' lf = Unix new line, crlf = windows new line, default=lf' |
125
|
|
|
) |
126
|
1 |
|
@click.option( |
127
|
|
|
'--csv-quote', |
128
|
|
|
'-u', |
129
|
|
|
'csv_quote', |
130
|
|
|
default=u'"', |
131
|
|
|
help='CSV quoting character, default=\'"\'' |
132
|
|
|
) |
133
|
1 |
|
@click.argument( |
134
|
|
|
'repository', |
135
|
|
|
type=click.Path( |
136
|
|
|
exists=True, resolve_path=True, file_okay=False, readable=True |
137
|
|
|
), |
138
|
|
|
default=os.getcwd() |
139
|
|
|
) |
140
|
1 |
|
@click.argument('commit', default='HEAD') |
141
|
1 |
|
@click.pass_context |
142
|
|
|
def dump( |
143
|
|
|
ctx, repository, commit, output, output_formats, namespace, meta, quiet, |
|
|
|
|
144
|
|
|
csv_delimiter, csv_quote, csv_eol |
|
|
|
|
145
|
|
|
): |
146
|
|
|
''' |
147
|
|
|
Get Git commit informations and store them in a config file |
148
|
|
|
|
149
|
|
|
\b |
150
|
|
|
REPOSITORY git repository path, Default is the current directory. |
151
|
|
|
COMMIT git commit to check, Default is HEAD. |
152
|
|
|
''' |
153
|
|
|
|
154
|
1 |
|
try: |
155
|
|
|
# pprint(repository) |
156
|
|
|
# pprint(type(repository)) |
157
|
1 |
|
vcs = GitHandler(repository) |
158
|
|
|
|
159
|
1 |
|
data = vcs.get_infos(commit=commit) |
160
|
|
|
|
161
|
|
|
# add metadatas |
162
|
1 |
|
for item in meta: |
163
|
1 |
|
for key, val in item.items(): |
164
|
1 |
|
data[key] = val |
165
|
|
|
|
166
|
1 |
|
if not quiet: |
167
|
1 |
|
print_commit_table(data) |
168
|
|
|
|
169
|
1 |
|
dumper = FileDumper() |
170
|
1 |
|
if not quiet and len(output_formats): |
171
|
1 |
|
click.echo("written to :") |
172
|
|
|
|
173
|
1 |
|
if 'all' in output_formats: |
174
|
1 |
|
output_formats = ['json', 'yml', 'xml', 'ini', 'csv', 'sh'] |
175
|
|
|
|
176
|
1 |
|
for output_format in output_formats: |
177
|
1 |
|
dest = dumper.dump( |
178
|
|
|
data=data, |
179
|
|
|
fileformat=output_format, |
180
|
|
|
target=output, |
181
|
|
|
cwd=repository, |
182
|
|
|
namespace=namespace, |
183
|
|
|
csv_delimiter=csv_delimiter, |
184
|
|
|
csv_quote=csv_quote, |
185
|
|
|
csv_eol=csv_eol |
186
|
|
|
) |
187
|
1 |
|
if not quiet: |
188
|
1 |
|
click.echo(dest) |
189
|
|
|
|
190
|
1 |
|
ctx.exit(0) |
191
|
|
|
|
192
|
1 |
|
except (InvalidGitRepositoryError, NoSuchPathError): |
193
|
1 |
|
msg = u'The directory \'{}\' is not a git repository.' |
194
|
1 |
|
click.echo(msg.format(click.format_filename(repository))) |
195
|
1 |
|
ctx.exit(1) |
196
|
|
|
|
197
|
|
|
|
198
|
1 |
|
def print_commit_table(data): |
199
|
|
|
''' |
200
|
|
|
display a dict as a Table in standard output |
201
|
|
|
''' |
202
|
1 |
|
click.echo('Git commit :') |
203
|
1 |
|
keys = sorted(data.keys()) |
204
|
1 |
|
table = [] |
205
|
1 |
|
for key in keys: |
206
|
1 |
|
if isinstance(data[key], list): |
207
|
1 |
|
item = ' '.join(data[key]) |
208
|
|
|
else: |
209
|
1 |
|
item = data[key] |
210
|
|
|
|
211
|
1 |
|
table.append([key, item]) |
212
|
|
|
|
213
|
1 |
|
click.echo(tabulate(table, tablefmt='simple')) |
214
|
|
|
|
215
|
|
|
|
216
|
|
|
if __name__ == '__main__': |
217
|
|
|
dump() |
218
|
|
|
|