Skip to content

Commit

Permalink
Merge pull request #984 from carmenbianca/convert-dep5-asterisks
Browse files Browse the repository at this point in the history
Make sure single asterisks become double asterisks in convert-dep5
  • Loading branch information
carmenbianca committed May 11, 2024
2 parents 0830a2f + e532866 commit a14d062
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/reuse/convert_dep5.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

"""Logic to convert a .reuse/dep5 file to a REUSE.toml file."""

import re
import sys
from argparse import ArgumentParser, Namespace
from gettext import gettext as _
Expand All @@ -15,6 +16,15 @@
from .global_licensing import REUSE_TOML_VERSION, ReuseDep5
from .project import Project

_SINGLE_ASTERISK_PATTERN = re.compile(r"(?<!\*)\*(?!\*)")


def _convert_asterisk(path: str) -> str:
"""This solves a semantics difference. A singular asterisk is semantically
identical to a double asterisk in REUSE.toml.
"""
return _SINGLE_ASTERISK_PATTERN.sub("**", path)


def toml_from_dep5(dep5: Copyright) -> str:
"""Given a Copyright object, return an equivalent REUSE.toml string."""
Expand All @@ -27,6 +37,7 @@ def toml_from_dep5(dep5: Copyright) -> str:
if len(copyrights) == 1:
copyrights = copyrights[0]
paths: Union[str, List[str]] = list(paragraph.files)
paths = [_convert_asterisk(path) for path in paths]
if len(paths) == 1:
paths = paths[0]
annotations.append(
Expand Down
34 changes: 33 additions & 1 deletion tests/test_convert_dep5.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,38 @@ def test_toml_from_dep5_single_file():
assert toml_from_dep5(Copyright(text)) == expected


def test_toml_from_dep5_asterisks():
"""Single asterisks get converted to double asterisks. Double asterisks get
left alone.
"""
text = StringIO(
cleandoc_nl(
"""
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: test
Upstream-Contact: test
Source: test
Files: */**/***
Copyright: 2018 Jane Doe
License: MIT
"""
)
)
expected = cleandoc_nl(
"""
version = 1
[[annotations]]
path = "**/**/***"
precedence = "aggregate"
SPDX-FileCopyrightText = "2018 Jane Doe"
SPDX-License-Identifier = "MIT"
"""
)
assert toml_from_dep5(Copyright(text)) == expected


def test_toml_from_dep5_multiple_files_in_paragraph():
"""Correctly convert a DEP5 file with a more files in a paragraph."""
text = StringIO(
Expand All @@ -65,7 +97,7 @@ def test_toml_from_dep5_multiple_files_in_paragraph():
version = 1
[[annotations]]
path = ["hello.txt", "foo*.txt"]
path = ["hello.txt", "foo**.txt"]
precedence = "aggregate"
SPDX-FileCopyrightText = "2018 Jane Doe"
SPDX-License-Identifier = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ def test_convert_dep5(fake_repository_dep5, stringio):
version = 1
[[annotations]]
path = "doc/*"
path = "doc/**"
precedence = "aggregate"
SPDX-FileCopyrightText = "2017 Jane Doe"
SPDX-License-Identifier = "CC0-1.0"
Expand Down

0 comments on commit a14d062

Please sign in to comment.