Completed
Push — master ( 419779...7515a9 )
by Charles
02:09
created

main()   D

Complexity

Conditions 8

Size

Total Lines 88

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
dl 0
loc 88
ccs 0
cts 31
cp 0
crap 72
rs 4.3858
c 2
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
# -*- coding: utf-8 -*-
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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():
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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.',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (198/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
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])
0 ignored issues
show
Coding Style introduced by
No space allowed before bracket
print (key + ' = ' + data[key])
^
Loading history...
85
                except TypeError:
86
                    print (key + ' = ' + ' '.join(data[key]))
0 ignored issues
show
Coding Style introduced by
No space allowed before bracket
print (key + ' = ' + ' '.join(data[key]))
^
Loading history...
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:
0 ignored issues
show
Best Practice introduced by
Catching very general exceptions such as Exception is usually not recommended.

Generally, you would want to handle very specific errors in the exception handler. This ensure that you do not hide other types of errors which should be fixed.

So, unless you specifically plan to handle any error, consider adding a more specific exception.

Loading history...
99
        print("Error Writing version config file : ", exc)
100
        exit(1)
101
102
if __name__ == '__main__':
103
    main()
104