Skip to content

Commit

Permalink
Uniformize use of LicenseRef- regex when checking for that license
Browse files Browse the repository at this point in the history
  • Loading branch information
sgaist committed Jul 12, 2023
1 parent 326393e commit c845aa4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/reuse/_licenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import json
import os
from typing import Dict, List, Tuple
import re

_BASE_DIR = os.path.dirname(__file__)
_RESOURCES_DIR = os.path.join(_BASE_DIR, "resources")
Expand Down Expand Up @@ -57,3 +58,5 @@ def _load_exception_list(file_name: str) -> Tuple[List[int], Dict[str, Dict]]:
for identifier, contents in ALL_MAP.items()
if not contents["isDeprecatedLicenseId"]
}

REF_RE = re.compile("LicenseRef-[a-zA-Z0-9-.]+$")
4 changes: 1 addition & 3 deletions src/reuse/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from urllib.error import URLError
from urllib.parse import urljoin

from ._licenses import ALL_NON_DEPRECATED_MAP
from ._licenses import ALL_NON_DEPRECATED_MAP, REF_RE
from ._util import (
PathType,
StrPath,
Expand All @@ -35,8 +35,6 @@
"https://raw.githubusercontent.com/spdx/license-list-data/master/text/"
)

REF_RE = re.compile("LicenseRef-[a-zA-Z0-9-.]+$")


def download_license(spdx_identifier: str) -> str:
"""Download the license text from the SPDX repository.
Expand Down
11 changes: 5 additions & 6 deletions src/reuse/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import logging
import os
import warnings
import re

from gettext import gettext as _
from pathlib import Path
from typing import Dict, Iterator, List, Optional, Union, cast
Expand All @@ -29,7 +31,7 @@
ReuseInfo,
SourceType,
)
from ._licenses import EXCEPTION_MAP, LICENSE_MAP
from ._licenses import EXCEPTION_MAP, LICENSE_MAP, REF_RE
from ._util import (
_HEADER_BYTES,
GIT_EXE,
Expand Down Expand Up @@ -288,7 +290,7 @@ def _identifier_of_license(self, path: Path) -> str:
raise IdentifierNotFound(f"{path} has no file extension")
if path.stem in self.license_map:
return path.stem
if path.stem.startswith("LicenseRef-"):
if re.match(REF_RE, path.stem):
return path.stem

raise IdentifierNotFound(
Expand Down Expand Up @@ -379,10 +381,7 @@ def _licenses(self) -> Dict[str, Path]:
raise RuntimeError(f"Multiple licenses resolve to {identifier}")
# Add the identifiers
license_files[identifier] = path
if (
identifier.startswith("LicenseRef-")
and "Unknown" not in identifier
):
if re.match(REF_RE, identifier) and "Unknown" not in identifier:
self.license_map[identifier] = {
"reference": str(path),
"isDeprecatedLicenseId": False,
Expand Down
4 changes: 3 additions & 1 deletion src/reuse/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import logging
import multiprocessing as mp
import random
import re
from gettext import gettext as _
from hashlib import md5
from io import StringIO
Expand All @@ -20,6 +21,7 @@
from uuid import uuid4

from . import __REUSE_version__, __version__
from ._licenses import REF_RE
from ._util import _LICENSING, StrPath, _checksum
from .project import Project, ReuseInfo

Expand Down Expand Up @@ -217,7 +219,7 @@ def bill_of_materials(

# Licenses
for lic, path in sorted(self.licenses.items()):
if lic.startswith("LicenseRef-"):
if re.match(REF_RE, lic):
out.write("\n")
out.write(f"LicenseID: {lic}\n")
out.write("LicenseName: NOASSERTION\n")
Expand Down

0 comments on commit c845aa4

Please sign in to comment.