Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

annotate: add --fallback-dot-license option #823

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ CLI command and its behaviour. There are no guarantees of stability for the

### Added

- `--fallback-dot-license` option added to `annotate` command. (#823)

### Changed

### Deprecated
Expand Down
28 changes: 20 additions & 8 deletions src/reuse/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,14 @@ def _get_comment_style(path: StrPath) -> Optional[Type[CommentStyle]]:
return style


def _is_uncommentable(path: Path) -> bool:
def _is_uncommentable(path: Path, fallback_dot_license: bool) -> bool:
"""Determines if *path* is uncommentable, e.g., the file is a binary or
registered as an UncommentableCommentStyle.
"""
is_uncommentable = _get_comment_style(path) == UncommentableCommentStyle
style = _get_comment_style(path)
is_uncommentable = style == UncommentableCommentStyle
if style is None and fallback_dot_license:
is_uncommentable = True
return is_uncommentable or is_binary(str(path))


Expand Down Expand Up @@ -425,13 +428,16 @@ def _verify_paths_line_handling(


def _verify_paths_comment_style(
paths: Iterable[Path], parser: ArgumentParser
paths: Iterable[Path],
parser: ArgumentParser,
fallback_dot_license: bool,
) -> None:
unrecognised_files = []

for path in paths:
style = _get_comment_style(path)
not_uncommentable = not _is_uncommentable(path)
not_uncommentable = not _is_uncommentable(path,
fallback_dot_license)

# TODO: This check is duplicated.
if style is None and not_uncommentable:
Expand All @@ -442,8 +448,8 @@ def _verify_paths_comment_style(
"{}\n{}".format(
_(
"The following files do not have a recognised file"
" extension. Please use --style, --force-dot-license or"
" --skip-unrecognised:"
" extension. Please use --style, --force-dot-license,"
" --fallback-dot-license or --skip-unrecognised:"
),
"\n".join(str(path) for path in unrecognised_files),
)
Expand Down Expand Up @@ -613,6 +619,11 @@ def add_arguments(parser: ArgumentParser) -> None:
choices=list(NAME_STYLE_MAP),
help=_("comment style to use, optional"),
)
parser.add_argument(
"--fallback-dot-license",
action="store_true",
help=_("Write a .license file for unrecognised files"),
)
Comment on lines +622 to +626
Copy link
Member

@carmenbianca carmenbianca Aug 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to use ArgumentParser.add_mutually_exclusive_group for --fallback-dot-license, --skip-unrecognised, and --force-dot-license?

Incidentally, 'Write' should be lower-case 'write'.

parser.add_argument(
"--copyright-style",
action="store",
Expand Down Expand Up @@ -717,6 +728,7 @@ def run(args: Namespace, project: Project, out: IO[str] = sys.stdout) -> int:
" --style"
)
)

if args.explicit_license:
_LOGGER.warning(
_(
Expand Down Expand Up @@ -755,7 +767,7 @@ def run(args: Namespace, project: Project, out: IO[str] = sys.stdout) -> int:
force_multi=args.multi_line,
)
if not args.skip_unrecognised:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove a lot of logic elsewhere and change this to

Suggested change
if not args.skip_unrecognised:
if not args.skip_unrecognised or args.fallback_dot_license:

This reduces passing the parcel, and matches where skip_unrecognised is used.

_verify_paths_comment_style(paths, args.parser)
_verify_paths_comment_style(paths, args.parser, args.fallback_dot_license)

template: Optional[Template] = None
commented = False
Expand Down Expand Up @@ -810,7 +822,7 @@ def run(args: Namespace, project: Project, out: IO[str] = sys.stdout) -> int:

result = 0
for path in paths:
uncommentable = _is_uncommentable(path)
uncommentable = _is_uncommentable(path, args.fallback_dot_license)
if uncommentable or args.force_dot_license:
new_path = _determine_license_suffix_path(path)
if uncommentable:
Expand Down
Loading