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

Make reuse init reuse info present in pyproject.toml #345

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Jinja2==2.11.3
license-expression==1.2
python-debian==0.1.38
requests==2.25.1
toml==0.10.2

setuptools==52.0.0
setuptools-scm==5.0.1
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"Jinja2",
# Exactly what it says.
"binaryornot",
# For reusing info from pyproject.toml
"toml",
]

test_requirements = ["pytest"]
Expand Down
85 changes: 63 additions & 22 deletions src/reuse/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
from gettext import gettext as _
from inspect import cleandoc
from pathlib import Path
from typing import List
from typing import List, Mapping, IO, Union

import requests
import toml

from ._licenses import ALL_NON_DEPRECATED_MAP
from ._util import PathType, print_incorrect_spdx_identifier
Expand Down Expand Up @@ -60,6 +61,39 @@ def add_arguments(parser):
)


def read_pyproject_toml(
path: Path, out: IO
) -> Mapping[str, Union[str, List[str]]]:
"""Parse information from pyproject.toml"""
out.write(_("Using information from pyproject.toml."))
out.write("\n")
doc = toml.load(path)
project = doc.get("project")
config = {}

if "license" in project:
license = project["license"].get("text")
if license not in ALL_NON_DEPRECATED_MAP:
print_incorrect_spdx_identifier(license, out)
out.write("\n")
else:
config["licenses"] = [license]

if "name" in project:
config["project_name"] = project["name"]

homepage = project.get("urls", {}).get("homepage")
if homepage:
config["project_url"] = homepage

authors = project.get("authors", [])
if authors:
config["contact_name"] = authors[0]["name"]
config["contact_address"] = authors[0]["email"]

return config


def run(args, project: Project, out=sys.stdout):
"""List all non-compliant files."""
# pylint: disable=too-many-statements,unused-argument
Expand All @@ -75,34 +109,41 @@ def run(args, project: Project, out=sys.stdout):
out.write("\n")
return 1

if (root / "pyproject.toml").exists():
config = read_pyproject_toml(root / "pyproject.toml", out)

out.write(_("Initializing project for REUSE."))
out.write("\n\n")

licenses = prompt_licenses(out=out)

out.write(_("What is the name of the project?"))
out.write("\n")
project_name = input()

out.write("\n")

out.write(_("What is the internet address of the project?"))
out.write("\n")
project_url = input()

out.write("\n")
licenses = config.get("licenses") or prompt_licenses(out=out)

out.write(_("What is the name of the maintainer?"))
out.write("\n")
contact_name = input()
project_name = config.get("project_name")
if not project_name:
out.write(_("What is the name of the project?"))
out.write("\n")
project_name = config.get("project_name") or input()
out.write("\n")

out.write("\n")
project_url = config.get("project_url") or input()
if not project_url:
out.write(_("What is the internet address of the project?"))
out.write("\n")
project_url = input()
out.write("\n")

out.write(_("What is the e-mail address of the maintainer?"))
out.write("\n")
contact_address = input()
contact_name = config.get("contact_name")
if not contact_name:
out.write(_("What is the name of the maintainer?"))
out.write("\n")
contact_name = input()
out.write("\n")

out.write("\n")
contact_address = config.get("contact_address") or input()
if not contact_address:
out.write(_("What is the e-mail address of the maintainer?"))
out.write("\n")
contact_address = input()
out.write("\n")

out.write(_("All done! Initializing now."))
out.write("\n\n")
Expand Down
53 changes: 53 additions & 0 deletions tests/test_main_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# SPDX-FileCopyrightText: 2021 Chris Wesseling <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later

"""Tests for reuse._main: init"""

from inspect import cleandoc

from reuse._main import main


def test_init_complete_pyproject_toml(empty_directory, stringio):
"""reuse init should use info from pyproject.toml"""
repo = empty_directory
(repo / "pyproject.toml").write_text(
cleandoc(
"""
[project]
name = "fake-repo"
discription = "Fake Repository"
authors = [
{name = "Mary Sue", email = "[email protected]"}
]
dynamic = ["classifiers"]
license = {text = "GPL-3.0-or-later"}
requires-python = ">=3.8"
[project.urls]
homepage = "https://example.com"
"""
)
)

result = main(["init"], out=stringio)
assert result == 0
assert "What" not in stringio
assert (
(repo / ".reuse/dep5").read_text()
== cleandoc(
"""
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: fake-repo
Upstream-Contact: Mary Sue <[email protected]>
Source: https://example.com

# Sample paragraph, commented out:
#
# Files: src/*
# Copyright: $YEAR $NAME <$CONTACT>
# License: ...
"""
)
+ "\n"
)