-
Notifications
You must be signed in to change notification settings - Fork 134
/
_main.py
279 lines (249 loc) · 8.73 KB
/
_main.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# SPDX-FileCopyrightText: 2017 Free Software Foundation Europe e.V. <https://fsfe.org>
# SPDX-FileCopyrightText: 2022 Florian Snow <florian@familysnow.net>
# SPDX-FileCopyrightText: 2024 Carmen Bianca BAKKER <carmenbianca@fsfe.org>
# SPDX-FileCopyrightText: © 2020 Liferay, Inc. <https://liferay.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""Entry functions for reuse."""
import argparse
import logging
import sys
import warnings
from gettext import gettext as _
from pathlib import Path
from typing import IO, Callable, List, Optional, Type, cast
from . import (
__REUSE_version__,
__version__,
_annotate,
convert_dep5,
download,
lint,
spdx,
supported_licenses,
)
from ._format import INDENT, fill_all, fill_paragraph
from ._util import PathType, setup_logging
from .global_licensing import GlobalLicensingParseError
from .project import GlobalLicensingConflict, GlobalLicensingFound, Project
from .vcs import find_root
_LOGGER = logging.getLogger(__name__)
_DESCRIPTION_LINES = [
_(
"reuse is a tool for compliance with the REUSE"
" recommendations. See <https://reuse.software/> for more"
" information, and <https://reuse.readthedocs.io/> for the online"
" documentation."
),
_(
"This version of reuse is compatible with version {} of the REUSE"
" Specification."
).format(__REUSE_version__),
_("Support the FSFE's work:"),
]
_INDENTED_LINE = _(
"Donations are critical to our strength and autonomy. They enable us to"
" continue working for Free Software wherever necessary. Please consider"
" making a donation at <https://fsfe.org/donate/>."
)
_DESCRIPTION_TEXT = (
fill_all("\n\n".join(_DESCRIPTION_LINES))
+ "\n\n"
+ fill_paragraph(_INDENTED_LINE, indent_width=INDENT)
)
_EPILOG_TEXT = ""
def parser() -> argparse.ArgumentParser:
"""Create the parser and return it."""
# pylint: disable=redefined-outer-name
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter,
description=_DESCRIPTION_TEXT,
epilog=_EPILOG_TEXT,
)
parser.add_argument(
"--debug", action="store_true", help=_("enable debug statements")
)
parser.add_argument(
"--suppress-deprecation",
action="store_true",
help=_("hide deprecation warnings"),
)
parser.add_argument(
"--include-submodules",
action="store_true",
help=_("do not skip over Git submodules"),
)
parser.add_argument(
"--include-meson-subprojects",
action="store_true",
help=_("do not skip over Meson subprojects"),
)
parser.add_argument(
"--no-multiprocessing",
action="store_true",
help=_("do not use multiprocessing"),
)
parser.add_argument(
"--root",
action="store",
metavar="PATH",
type=PathType("r", force_directory=True),
help=_("define root of project"),
)
parser.add_argument(
"--version",
action="store_true",
help=_("show program's version number and exit"),
)
parser.set_defaults(func=lambda *args: parser.print_help())
subparsers = parser.add_subparsers(title=_("subcommands"))
add_command(
subparsers,
"annotate",
_annotate.add_arguments,
_annotate.run,
help=_("add copyright and licensing into the header of files"),
description=fill_all(
_(
"Add copyright and licensing into the header of one or more"
" files.\n"
"\n"
"By using --copyright and --license, you can specify which"
" copyright holders and licenses to add to the headers of the"
" given files.\n"
"\n"
"By using --contributor, you can specify people or entity that"
" contributed but are not copyright holder of the given"
" files."
)
),
)
add_command(
subparsers,
"download",
download.add_arguments,
download.run,
help=_("download a license and place it in the LICENSES/ directory"),
description=fill_all(
_("Download a license and place it in the LICENSES/ directory.")
),
)
add_command(
subparsers,
"lint",
lint.add_arguments,
lint.run,
help=_("list all non-compliant files"),
description=fill_all(
_(
"Lint the project directory for compliance with"
" version {reuse_version} of the REUSE Specification. You can"
" find the latest version of the specification at"
" <https://reuse.software/spec/>.\n"
"\n"
"Specifically, the following criteria are checked:\n"
"\n"
"- Are there any bad (unrecognised, not compliant with SPDX)"
" licenses in the project?\n"
"\n"
"- Are any licenses referred to inside of the project, but"
" not included in the LICENSES/ directory?\n"
"\n"
"- Are any licenses included in the LICENSES/ directory that"
" are not used inside of the project?\n"
"\n"
"- Do all files have valid copyright and licensing"
" information?"
).format(reuse_version=__REUSE_version__)
),
)
add_command(
subparsers,
"spdx",
spdx.add_arguments,
spdx.run,
help=_("print the project's bill of materials in SPDX format"),
)
add_command(
subparsers,
"supported-licenses",
supported_licenses.add_arguments,
supported_licenses.run,
help=_("list all supported SPDX licenses"),
aliases=["supported-licences"],
)
add_command(
subparsers,
"convert-dep5",
convert_dep5.add_arguments,
convert_dep5.run,
help=_("convert .reuse/dep5 to REUSE.toml"),
)
return parser
def add_command( # pylint: disable=too-many-arguments,redefined-builtin
subparsers: argparse._SubParsersAction,
name: str,
add_arguments_func: Callable[[argparse.ArgumentParser], None],
run_func: Callable[[argparse.Namespace, Project, IO[str]], int],
formatter_class: Optional[Type[argparse.HelpFormatter]] = None,
description: Optional[str] = None,
help: Optional[str] = None,
aliases: Optional[List[str]] = None,
) -> None:
"""Add a subparser for a command."""
if formatter_class is None:
formatter_class = argparse.RawTextHelpFormatter
subparser = subparsers.add_parser(
name,
formatter_class=formatter_class,
description=description,
help=help,
aliases=aliases or [],
)
add_arguments_func(subparser)
subparser.set_defaults(func=run_func)
subparser.set_defaults(parser=subparser)
def main(args: Optional[List[str]] = None, out: IO[str] = sys.stdout) -> int:
"""Main entry function."""
if args is None:
args = cast(List[str], sys.argv[1:])
main_parser = parser()
parsed_args = main_parser.parse_args(args)
setup_logging(level=logging.DEBUG if parsed_args.debug else logging.WARNING)
# Show all warnings raised by ourselves.
if not parsed_args.suppress_deprecation:
warnings.filterwarnings("default", module="reuse")
if parsed_args.version:
out.write(f"reuse {__version__}\n")
return 0
root = parsed_args.root
if root is None:
root = find_root()
if root is None:
root = Path.cwd()
try:
project = Project.from_directory(root)
# FileNotFoundError and NotADirectoryError don't need to be caught because
# argparse already made sure of these things.
except UnicodeDecodeError:
found = cast(GlobalLicensingFound, Project.find_global_licensing(root))
main_parser.error(
_("'{path}' could not be decoded as UTF-8.").format(path=found.path)
)
except GlobalLicensingParseError as error:
found = cast(GlobalLicensingFound, Project.find_global_licensing(root))
main_parser.error(
_(
"'{path}' could not be parsed. We received the following error"
" message: {message}"
).format(path=found.path, message=str(error))
)
except GlobalLicensingConflict as error:
main_parser.error(str(error))
except OSError as error:
main_parser.error(str(error))
project.include_submodules = parsed_args.include_submodules
project.include_meson_subprojects = parsed_args.include_meson_subprojects
return parsed_args.func(parsed_args, project, out)
if __name__ == "__main__":
sys.exit(main())