-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcli.py
121 lines (108 loc) · 4.13 KB
/
cli.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
"""Command Line Interface functionality. """
import os.path
import sys
import click
import yaml
from totem.main import LocalCheck, PRCheck, PreCommitLocalCheck
from totem.reporting.console import Color
def run_checks(
pr_url: str,
config_file: str = None,
details_url: str = None,
checks: str = '',
arguments: list = None,
):
"""Run all checks described in `config_file` for the PR on the given URL.
:param str pr_url: the URL of the pull request as retrieved from the CI
:param str config_file: the path of the configuration file,
formatted in YAML, as found in contrib/config/sample.yml
:param str details_url: the URL to visit for more details about the results
:param str checks: comma separated list of check IDs to run
:param list arguments: a list of optional arguments; if the list is empty,
all commits of the current branch will be checked; otherwise,
only the pending commit will be checked (as in a pre-commit fashion)
"""
if not config_file:
if os.path.isfile('.totem.yml'):
config_file = '.totem.yml'
else:
package_root = os.path.split(__file__)[0]
config_file = os.path.join(package_root, 'contrib/config/default.yml')
try:
with open(config_file, 'r') as f:
try:
config = yaml.load(f)
except Exception as e:
print(
Color.format(
'[error]Error parsing config file "{}" as a YAML document: '
'{}[end]'.format(config_file, e)
)
)
sys.exit(1)
except Exception as e:
print(Color.format('[error]Error opening config file: {}[end]'.format(e)))
sys.exit(1)
print(
'Running with arguments:\n'
' - PR URL: {pr_url}\n'
' - Config file path: {config_file}\n'
' - Checks: {checks}\n'
' - Details URL: {details_url}'.format(
pr_url=pr_url if pr_url is None else '"{}"'.format(pr_url),
config_file=(
config_file if config_file is None else '"{}"'.format(config_file)
),
checks=checks,
details_url=(
details_url if details_url is None else '"{}"'.format(details_url)
),
)
)
if pr_url:
print('Running in PRCheck mode')
check = PRCheck(config_dict=config, pr_url=pr_url, details_url=details_url)
else:
if not arguments:
print('Running in LocalCheck mode')
check = LocalCheck(config_dict=config)
else:
print('Running in PreCommitLocalCheck mode')
check = PreCommitLocalCheck(config_dict=config)
check_ids = checks.split(',') if checks else None
results = check.run(checks=check_ids)
if results.errors:
sys.exit(1)
@click.command()
@click.option('-p', '--pr-url', required=False, type=str)
@click.option('-c', '--config-file', required=False, type=str)
@click.option('--details-url', required=False, type=str)
@click.option('--checks', required=False, type=str)
@click.argument('args', nargs=-1)
def main(
pr_url: str,
config_file: str = None,
details_url: str = None,
checks: str = None,
args: list = None,
):
"""Run all checks described in `config_file`.
If a URL of a pull request is given, it performs all checks on it.
If no such URL is given, the checks run locally.
If no `config_file` is given, it attempts to use `.totem.yml`.
If that is not found, it defaults to `contrib/config/default.yml`.
A command line function.
:param str pr_url: the URL of the pull request
:param str config_file: the path of the configuration file,
formatted in YAML, as found in contrib/config/sample.yml
:param str details_url: the URL to visit for more details about the results
:param str checks: comma separated list of check IDs to run
:param list args: necessary for pre-commit support
"""
run_checks(
pr_url=pr_url,
config_file=config_file,
details_url=details_url,
checks=checks,
arguments=args,
)