Passed
Pull Request — master (#249)
by
unknown
01:32
created

gvmtools.version.main()   B

Complexity

Conditions 5

Size

Total Lines 43
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 31
nop 0
dl 0
loc 43
rs 8.6693
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2020 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
import argparse
20
import sys
21
22
from pathlib import Path
23
24
from gvm.version import (
25
    print_version,
26
    get_version_from_pyproject_toml,
27
    is_version_pep440_compliant,
28
    strip_version,
29
    safe_version,
30
    update_pyproject_version,
31
    update_version_file,
32
    versions_equal,
33
)
34
35
from gvmtools import get_version
36
37
38
def _update_gvm_tools_version(
39
    new_version: str, pyproject_toml_path: Path, *, force: bool = False
40
):
41
    if not pyproject_toml_path.exists():
42
        sys.exit(
43
            'Could not find pyproject.toml file in the current working dir.'
44
        )
45
46
    cwd_path = Path.cwd()
47
    pyproject_version = get_version_from_pyproject_toml(
48
        pyproject_toml_path=pyproject_toml_path
49
    )
50
    version_file_path = cwd_path / 'gvmtools' / '__version__.py'
51
52
    if not version_file_path.exists():
53
        version_file_path.touch()
54
    elif not force and versions_equal(new_version, get_version()):
55
        print('Version is already up-to-date.')
56
        sys.exit(0)
57
58
    update_pyproject_version(
59
        new_version=new_version, pyproject_toml_path=pyproject_toml_path
60
    )
61
62
    update_version_file(
63
        new_version=new_version, version_file_path=version_file_path,
64
    )
65
66
    print(
67
        'Updated version from {} to {}'.format(
68
            pyproject_version, safe_version(new_version)
69
        )
70
    )
71
72
73
def _verify_version(version: str, pyproject_toml_path: Path) -> None:
74
    gvmtools_version = get_version()
75
    pyproject_version = get_version_from_pyproject_toml(
76
        pyproject_toml_path=pyproject_toml_path
77
    )
78
    if not is_version_pep440_compliant(gvmtools_version):
79
        sys.exit(
80
            "The version in gvmtools/__version__.py is not PEP 440 compliant."
81
        )
82
83
    if pyproject_version != gvmtools_version:
84
        sys.exit(
85
            "The version set in the pyproject.toml file \"{}\" doesn't "
86
            "match the gvm-tools version \"{}\"".format(
87
                pyproject_version, gvmtools_version
88
            )
89
        )
90
91
    if version != 'current':
92
        provided_version = strip_version(version)
93
        if provided_version != gvmtools_version:
94
            sys.exit(
95
                "Provided version \"{}\" does not match the python-gvm "
96
                "version \"{}\"".format(provided_version, gvmtools_version)
97
            )
98
99
    print('OK')
100
101
102
def main():
103
    parser = argparse.ArgumentParser(
104
        description='Version handling utilities for gvm-tools.', prog='version'
105
    )
106
107
    subparsers = parser.add_subparsers(
108
        title='subcommands',
109
        description='valid subcommands',
110
        help='additional help',
111
        dest='command',
112
    )
113
114
    verify_parser = subparsers.add_parser('verify')
115
    verify_parser.add_argument('version', help='version string to compare')
116
117
    subparsers.add_parser('show')
118
119
    update_parser = subparsers.add_parser('update')
120
    update_parser.add_argument('version', help='version string to use')
121
    update_parser.add_argument(
122
        '--force',
123
        help="don't check if version is already set",
124
        action="store_true",
125
    )
126
127
    args = parser.parse_args()
128
129
    if not getattr(args, 'command', None):
130
        parser.print_usage()
131
        sys.exit(0)
132
133
    pyproject_toml_path = Path.cwd() / 'pyproject.toml'
134
135
    if args.command == 'update':
136
        _update_gvm_tools_version(
137
            args.version,
138
            pyproject_toml_path=pyproject_toml_path,
139
            force=args.force,
140
        )
141
    elif args.command == 'show':
142
        print_version(pyproject_toml_path=pyproject_toml_path)
143
    elif args.command == 'verify':
144
        _verify_version(args.version, pyproject_toml_path=pyproject_toml_path)
145
146
147
if __name__ == '__main__':
148
    main()
149