Browse Source

Handle binary and .license files

tags/v0.5.0
carmenbianca 3 months ago
parent
commit
d04fec06a4
Signed by: Carmen Bianca Bakker <carmenbianca@fsfe.org> GPG Key ID: 76EBA6F6B6EA2EC9
7 changed files with 116 additions and 7 deletions
  1. 4
    0
      CHANGELOG.md
  2. 2
    0
      requirements.txt
  3. 2
    0
      setup.py
  4. 1
    0
      src/reuse/_comment.py
  5. 23
    7
      src/reuse/header.py
  6. 6
    0
      tests/conftest.py
  7. 78
    0
      tests/test_main.py

+ 4
- 0
CHANGELOG.md View File

@@ -29,6 +29,8 @@ The versions follow [semantic versioning](https://semver.org).

- Added `--template` to `reuse addheader`.

- `binaryornot` added as new dependency.

### Changed

- `reuse addheader` now automatically adds the current year to the copyright
@@ -37,6 +39,8 @@ The versions follow [semantic versioning](https://semver.org).
- `reuse addheader` preserves the original header below the new header if it did
not contain any SPDX information.

- `reuse addheader` now correctly handles `.license` files.

## 0.4.1 - 2019-08-07

### Added

+ 2
- 0
requirements.txt View File

@@ -5,7 +5,9 @@
python-debian==0.1.34
requests==2.21.0
license-expression==0.99
boolean.py==3.6
Jinja2==2.10.1
binaryornot==0.4.4

sphinx==1.7.6
sphinx_rtd_theme==0.4.1

+ 2
- 0
setup.py View File

@@ -27,6 +27,8 @@ requirements = [
"boolean.py",
# For templates of headers.
"Jinja2",
# Exactly what it says.
"binaryornot",
]

test_requirements = ["pytest"]

+ 1
- 0
src/reuse/_comment.py View File

@@ -346,6 +346,7 @@ COMMENT_STYLE_MAP = {
".js": CCommentStyle,
".l": LispCommentStyle,
".latex": TexCommentStyle,
".license": EmptyCommentStyle,
".lisp": LispCommentStyle,
".lsp": LispCommentStyle,
".lua": HaskellCommentStyle,

+ 23
- 7
src/reuse/header.py View File

@@ -12,6 +12,7 @@ from os import PathLike
from pathlib import Path
from typing import Optional

from binaryornot.check import is_binary
from boolean.boolean import ParseError
from jinja2 import Environment, FileSystemLoader, PackageLoader, Template
from jinja2.exceptions import TemplateNotFound
@@ -28,6 +29,7 @@ from ._comment import (
)
from ._util import (
PathType,
_determine_license_path,
extract_spdx_info,
make_copyright_line,
spdx_identifier,
@@ -212,12 +214,14 @@ def _verify_paths_supported(paths, parser):
try:
COMMENT_STYLE_MAP[path.suffix]
except KeyError:
parser.error(
_(
"'{}' does not have a recognised file extension, "
"please use --style".format(path)
# TODO: This check is duplicated.
if not is_binary(str(path)):
parser.error(
_(
"'{}' does not have a recognised file extension, "
"please use --style".format(path)
)
)
)


def _find_template(project: Project, name: str) -> Template:
@@ -352,9 +356,11 @@ def run(args, out=sys.stdout) -> int:
_("option --exclude-year and --year are mutually exclusive")
)

paths = [_determine_license_path(path) for path in args.path]

# First loop to verify before proceeding
if args.style is None:
_verify_paths_supported(args.path, args.parser)
_verify_paths_supported(paths, args.parser)

project = create_project()
template = None
@@ -389,7 +395,17 @@ def run(args, out=sys.stdout) -> int:
spdx_info = SpdxInfo(expressions, copyright_lines)

result = 0
for path in args.path:
for path in paths:
if is_binary(str(path)):
new_path = f"{path}.license"
_LOGGER.info(
_(
"'{path}' is a binary, therefore using '{new_path}' for "
"the header"
).format(path=path, new_path=new_path)
)
path = Path(new_path)
path.touch()
result += _add_header_to_file(
path, spdx_info, template, commented, args.style, out
)

+ 6
- 0
tests/conftest.py View File

@@ -140,6 +140,12 @@ def stringio():
return StringIO()


@pytest.fixture()
def binary_string():
"""Create a binary string."""
return bytes(range(256))


@pytest.fixture()
def template_simple_source():
"""Source code of simple Jinja2 template."""

+ 78
- 0
tests/test_main.py View File

@@ -572,3 +572,81 @@ def test_addheader_template_without_extension(
"""
).replace("spdx", "SPDX")
)


def test_addheader_binary(
fake_repository, stringio, mock_date_today, binary_string
):
"""Add a header to a .license file if the file is a binary."""
# pylint: disable=unused-argument
binary_file = fake_repository / "foo.png"
binary_file.write_bytes(binary_string)

result = main(
[
"addheader",
"--license",
"GPL-3.0-or-later",
"--copyright",
"Mary Sue",
"foo.png",
],
out=stringio,
)

assert result == 0
assert (
binary_file.with_name(f"{binary_file.name}.license")
.read_text()
.strip()
== cleandoc(
"""
spdx-FileCopyrightText: 2018 Mary Sue

spdx-License-Identifier: GPL-3.0-or-later
"""
).replace("spdx", "SPDX")
)


def test_addheader_license_file(fake_repository, stringio, mock_date_today):
"""Add a header to a .license file if it exists."""
# pylint: disable=unused-argument
simple_file = fake_repository / "foo.py"
simple_file.touch()
license_file = fake_repository / "foo.py.license"
license_file.write_text(
cleandoc(
"""
spdx-FileCopyrightText: 2016 Jane Doe

Hello
"""
).replace("spdx", "SPDX")
)

result = main(
[
"addheader",
"--license",
"GPL-3.0-or-later",
"--copyright",
"Mary Sue",
"foo.py",
],
out=stringio,
)

assert result == 0
assert (
license_file.read_text()
== cleandoc(
"""
spdx-FileCopyrightText: 2016 Jane Doe
spdx-FileCopyrightText: 2018 Mary Sue

spdx-License-Identifier: GPL-3.0-or-later
"""
).replace("spdx", "SPDX")
)
assert not simple_file.read_text()

Loading…
Cancel
Save