Skip to content

Commit

Permalink
Add support for using LicenseRef- when initialising
Browse files Browse the repository at this point in the history
Signed-off-by: Samuel Gaist <samuel.gaist@idiap.ch>
  • Loading branch information
sgaist authored and mxmehl committed Apr 6, 2023
1 parent c89563c commit 194b337
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
27 changes: 22 additions & 5 deletions src/reuse/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

import errno
import logging
import re
import shutil
import sys
import urllib.request
from gettext import gettext as _
from os import PathLike
from pathlib import Path
from typing import TextIO
from urllib.error import URLError
from urllib.parse import urljoin

Expand All @@ -30,6 +33,8 @@
"https://raw.githubusercontent.com/spdx/license-list-data/master/text/"
)

REF_RE = re.compile("LicenseRef-.+")


def download_license(spdx_identifier: str) -> str:
"""Download the license text from the SPDX repository.
Expand All @@ -52,7 +57,9 @@ def _path_to_license_file(spdx_identifier: str, root: PathLike) -> Path:
return licenses_path / "".join((spdx_identifier, ".txt"))


def put_license_in_file(spdx_identifier: str, destination: PathLike) -> None:
def put_license_in_file(
spdx_identifier: str, destination: PathLike, out: TextIO = None
) -> None:
"""Download a license and put it in the destination file.
This function exists solely for convenience.
Expand All @@ -69,10 +76,20 @@ def put_license_in_file(spdx_identifier: str, destination: PathLike) -> None:
if destination.exists():
raise FileExistsError(errno.EEXIST, "File exists", str(destination))

text = download_license(spdx_identifier)
with destination.open("w", encoding="utf-8") as fp:
fp.write(header)
fp.write(text)
if re.match(REF_RE, spdx_identifier):
if out is not None:
out.write(_("Enter path to custom license file"))
out.write("\n")
result = input()
out.write("\n")
if result:
source = Path(result) / "".join((spdx_identifier, ".txt"))
shutil.copyfile(source, destination)
else:
text = download_license(spdx_identifier)
with destination.open("w", encoding="utf-8") as fp:
fp.write(header)
fp.write(text)


def add_arguments(parser) -> None:
Expand Down
15 changes: 11 additions & 4 deletions src/reuse/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

"""Functions for REUSE-ifying a project."""

import re
import sys
from gettext import gettext as _
from inspect import cleandoc
Expand All @@ -13,7 +14,7 @@

from ._licenses import ALL_NON_DEPRECATED_MAP
from ._util import PathType, print_incorrect_spdx_identifier
from .download import _path_to_license_file, put_license_in_file
from .download import REF_RE, _path_to_license_file, put_license_in_file
from .project import Project
from .vcs import find_root

Expand Down Expand Up @@ -42,7 +43,9 @@ def prompt_licenses(out=sys.stdout) -> List[str]:
out.write("\n")
if not result:
return licenses
if result not in ALL_NON_DEPRECATED_MAP:
if result not in ALL_NON_DEPRECATED_MAP and not re.match(
REF_RE, result
):
print_incorrect_spdx_identifier(result, out=out)
out.write("\n\n")
else:
Expand Down Expand Up @@ -110,17 +113,21 @@ def run(args, project: Project, out=sys.stdout):

for lic in licenses:
destination = _path_to_license_file(lic, root=root)

try:
out.write(_("Downloading {}").format(lic))
out.write(_("Retrieving {}").format(lic))
out.write("\n")
put_license_in_file(lic, destination=destination)
put_license_in_file(lic, destination=destination, out=out)
# TODO: exceptions
except FileExistsError:
out.write(_("{} already exists").format(destination))
out.write("\n")
except URLError:
out.write(_("Could not download {}").format(lic))
out.write("\n")
except FileNotFoundError:
out.write(_("Could not copy {}").format(lic))
out.write("\n")

out.write("\n")

Expand Down

0 comments on commit 194b337

Please sign in to comment.