1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
2
|
|
|
|
3
|
|
|
import os |
4
|
|
|
import argparse |
5
|
|
|
import git_app_version.version |
6
|
|
|
from git_app_version.git import Git |
7
|
|
|
from git_app_version.dumper import FileDumper |
8
|
|
|
|
9
|
|
|
__version__ = git_app_version.version.__version__ |
10
|
|
|
__DESCRIPTION__ = 'Get Git commit informations and store them in a INI/XML/YAML/JSON file.' |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
def main(): |
|
|
|
|
14
|
|
|
parser = argparse.ArgumentParser( |
15
|
|
|
prog='git-app-version', |
16
|
|
|
description=__DESCRIPTION__) |
17
|
|
|
parser.add_argument( |
18
|
|
|
'-V', |
19
|
|
|
'--version', |
20
|
|
|
action='version', |
21
|
|
|
version='%(prog)s ' + |
22
|
|
|
__version__, |
23
|
|
|
help='display tool version') |
24
|
|
|
parser.add_argument('-v', '--verbose', action='count', |
25
|
|
|
help='increase verbosity : use -v or -vv') |
26
|
|
|
parser.add_argument( |
27
|
|
|
'-q', |
28
|
|
|
'--quiet', |
29
|
|
|
action='store_true', |
30
|
|
|
help='silent mode') |
31
|
|
|
parser.add_argument( |
32
|
|
|
'repository', |
33
|
|
|
nargs='?', |
34
|
|
|
metavar='path', |
35
|
|
|
type=str, |
36
|
|
|
help='git repository path. Default is the current directory.', |
37
|
|
|
default=os.getcwd()) |
38
|
|
|
parser.add_argument( |
39
|
|
|
'commit', |
40
|
|
|
nargs='?', |
41
|
|
|
type=str, |
42
|
|
|
help='git commit to check. Default is HEAD.', |
43
|
|
|
default='HEAD') |
44
|
|
|
parser.add_argument( |
45
|
|
|
'-o', |
46
|
|
|
'--output', |
47
|
|
|
metavar='path', |
48
|
|
|
type=str, |
49
|
|
|
help='output file path (without extension). Default is \'<repository-path>/version\'.', |
50
|
|
|
default='version') |
51
|
|
|
parser.add_argument( |
52
|
|
|
'-f', |
53
|
|
|
'--format', |
54
|
|
|
metavar='format', |
55
|
|
|
type=str, |
56
|
|
|
help='output file format and extension (ini/xml/yml/json). Default is json.', |
57
|
|
|
default='json') |
58
|
|
|
parser.add_argument( |
59
|
|
|
'-n', |
60
|
|
|
'--namespace', |
61
|
|
|
metavar='namespace', |
62
|
|
|
type=str, |
63
|
|
|
help='namespace like notation in version file, use dot separator to segment namespaces e.g.: \'foo.bar.git\'. Default is \'app_version\' for XML and INI and no namespace for JSON and YAML.', |
|
|
|
|
64
|
|
|
default='') |
65
|
|
|
|
66
|
|
|
args = parser.parse_args() |
67
|
|
|
|
68
|
|
|
try: |
69
|
|
|
vcs = Git() |
70
|
|
|
|
71
|
|
|
if not vcs.is_git_repo(args.repository): |
72
|
|
|
raise Exception( |
73
|
|
|
'The directory \'' + |
74
|
|
|
args.repository + |
75
|
|
|
'\' is not a git repository.') |
76
|
|
|
|
77
|
|
|
data = vcs.get_infos(commit=args.commit, cwd=args.repository) |
78
|
|
|
|
79
|
|
|
if args.verbose > 1 and not args.quiet: |
80
|
|
|
print('Git commit :') |
81
|
|
|
keys = sorted(data.keys()) |
82
|
|
|
for key in keys: |
83
|
|
|
try: |
84
|
|
|
print (key + ' = ' + data[key]) |
|
|
|
|
85
|
|
|
except TypeError: |
86
|
|
|
print (key + ' = ' + ' '.join(data[key])) |
|
|
|
|
87
|
|
|
|
88
|
|
|
dumper = FileDumper() |
89
|
|
|
dest = dumper.dump( |
90
|
|
|
data=data, |
91
|
|
|
fileformat=args.format, |
92
|
|
|
target=args.output, |
93
|
|
|
cwd=args.repository, |
94
|
|
|
namespace=args.namespace) |
95
|
|
|
if not args.quiet: |
96
|
|
|
print("Git commit informations stored in " + dest) |
97
|
|
|
|
98
|
|
|
except Exception as exc: |
|
|
|
|
99
|
|
|
print("Error Writing version config file : ", exc) |
100
|
|
|
exit(1) |
101
|
|
|
|
102
|
|
|
if __name__ == '__main__': |
103
|
|
|
main() |
104
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.