Skip to content

Commit

Permalink
Create .license file when running against unrecognised file paths
Browse files Browse the repository at this point in the history
Signed-off-by: Carmen Bianca BAKKER <carmenbianca@fsfe.org>
  • Loading branch information
carmenbianca committed Oct 24, 2023
1 parent a5573d0 commit a9873d8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 69 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,17 @@ CLI command and its behaviour. There are no guarantees of stability for the
"[repositories] that were cloned independently and later added as a submodule
or old setups", which "have the submodule's git directory inside the submodule
instead of embedded into the superproject's git directory". (#687)

- When running `annotate` on a file with an unrecognised file path,
automatically create a `.license` file. (#851)
- No longer scan binary or uncommentable files for their contents in search of
REUSE information. (#825)

### Deprecated

### Removed

- Removed deprecated `--explicit-license`. (#TODO)
- Removed deprecated `addheader`. (#TODO)
- Removed deprecated `--explicit-license`. (#851)
- Removed deprecated `addheader`. (#851)

### Fixed

Expand Down
47 changes: 19 additions & 28 deletions src/reuse/_annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@
make_copyright_line,
spdx_identifier,
)
from .comment import NAME_STYLE_MAP, CommentCreateError, CommentStyle
from .comment import (
NAME_STYLE_MAP,
CommentCreateError,
CommentStyle,
EmptyCommentStyle,
)
from .header import MissingReuseInfo, add_new_header, find_and_replace_header
from .project import Project

Expand Down Expand Up @@ -77,29 +82,6 @@ def verify_paths_line_handling(
)


def verify_paths_comment_style(
paths: Iterable[Path], parser: ArgumentParser
) -> None:
"""Raise parser.error if a style could not be found for one of the paths."""
unrecognised_files = []

for path in paths:
if not _has_style(path):
unrecognised_files.append(path)

if unrecognised_files:
parser.error(
"{}\n{}".format(
_(
"The following files do not have a recognised file"
" extension. Please use --style, --force-dot-license or"
" --skip-unrecognised:"
),
"\n".join(str(path) for path in unrecognised_files),
)
)


def find_template(project: Project, name: str) -> Template:
"""Find a template given a name.
Expand Down Expand Up @@ -133,6 +115,7 @@ def add_header_to_file(
style: Optional[str],
force_multi: bool = False,
skip_existing: bool = False,
skip_unrecognised: bool = False,
merge_copyrights: bool = False,
replace: bool = True,
out: IO[str] = sys.stdout,
Expand All @@ -145,9 +128,18 @@ def add_header_to_file(
else:
comment_style = _get_comment_style(path)
if comment_style is None:
out.write(_("Skipped unrecognised file {path}").format(path=path))
if skip_unrecognised:
out.write(_("Skipped unrecognised file {path}").format(path=path))
out.write("\n")
return result
out.write(
_("{path} is not recognised; creating {path}.license").format(
path=path
)
)
out.write("\n")
return result
path = _determine_license_path(path)
comment_style = EmptyCommentStyle

with open(path, "r", encoding="utf-8", newline="") as fp:
text = fp.read()
Expand Down Expand Up @@ -468,8 +460,6 @@ def run(args: Namespace, project: Project, out: IO[str] = sys.stdout) -> int:
force_single=args.single_line,
force_multi=args.multi_line,
)
if not args.skip_unrecognised:
verify_paths_comment_style(paths, args.parser)

template, commented = get_template(args, project)

Expand Down Expand Up @@ -504,6 +494,7 @@ def run(args: Namespace, project: Project, out: IO[str] = sys.stdout) -> int:
style=args.style,
force_multi=args.multi_line,
skip_existing=args.skip_existing,
skip_unrecognised=args.skip_unrecognised,
merge_copyrights=args.merge_copyrights,
replace=not args.no_replace,
out=out,
Expand Down
56 changes: 18 additions & 38 deletions tests/test_main_annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,22 +399,28 @@ def test_annotate_implicit_style_filename(
assert simple_file.read_text() == expected


def test_annotate_unrecognised_style(fake_repository):
def test_annotate_unrecognised_style(fake_repository, stringio):
"""Add a header to a file that has an unrecognised extension."""
simple_file = fake_repository / "foo.foo"
simple_file.write_text("pass")

with pytest.raises(SystemExit):
main(
[
"annotate",
"--license",
"GPL-3.0-or-later",
"--copyright",
"Jane Doe",
"foo.foo",
]
)
result = main(
[
"annotate",
"--license",
"GPL-3.0-or-later",
"--copyright",
"Jane Doe",
"foo.foo",
],
out=stringio,
)

assert result == 0
assert (
"foo.foo is not recognised; creating foo.foo.license"
in stringio.getvalue()
)


def test_annotate_skip_unrecognised(fake_repository, stringio):
Expand Down Expand Up @@ -1267,30 +1273,4 @@ def test_annotate_recursive_on_file(fake_repository, stringio, mock_date_today):
assert result == 0


def test_annotate_recursive_contains_unrecognised(
fake_repository, stringio, mock_date_today
):
"""Expect error and no edited files if at least one file has not been
recognised."""
(fake_repository / "baz").mkdir(parents=True)
(fake_repository / "baz/foo.py").write_text("foo")
(fake_repository / "baz/bar.unknown").write_text("bar")
(fake_repository / "baz/baz.sh").write_text("baz")

with pytest.raises(SystemExit):
main(
[
"annotate",
"--license",
"Apache-2.0",
"--copyright",
"Jane Doe",
"--recursive",
"baz/",
]
)

assert "Jane Doe" not in (fake_repository / "baz/foo.py").read_text()


# REUSE-IgnoreEnd

0 comments on commit a9873d8

Please sign in to comment.