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

[conan-center] Validate license with upstream repository #401

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
44 changes: 43 additions & 1 deletion hooks/conan-center.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import os
import re
import sys
import requests
from urllib.parse import urlparse
from logging import WARNING, ERROR, INFO, DEBUG, NOTSET

import yaml
Expand Down Expand Up @@ -74,11 +76,13 @@
"KB-H065": "NO REQUIRED_CONAN_VERSION",
"KB-H066": "SHORT_PATHS USAGE",
"KB-H068": "TEST_TYPE MANAGEMENT",
"KB-H070": "LICENSE VALIDATION",
}


this = sys.modules[__name__]


class _HooksOutputErrorCollector(object):

def __init__(self, output, kb_id=None):
Expand Down Expand Up @@ -673,6 +677,7 @@ def test(out):
if any(line.endswith(b'\r\n') for line in lines):
out.error("The file '{}' uses CRLF. Please, replace by LF."
.format(filename))

@run_test("KB-H061", output)
def test(out):
Location = collections.namedtuple("Location", ("line", "column", "line_end", "column_end"))
Expand Down Expand Up @@ -727,7 +732,6 @@ def visit_Attribute(self, node):
out.error("{}:{} Build system dependent functions detected. (Use of {} is forbidden in {})".format(
dut_conanfile_path, build_info.loc.line, build_info.what, build_info.func))


@run_test("KB-H062", output)
def test(out):
def _check_content(content, path):
Expand Down Expand Up @@ -793,6 +797,44 @@ def test(out):
except Exception as e:
out.warn("Invalid conanfile: {}".format(e))

@run_test("KB-H070", output)
def test(out):
def get_project_license_from_repo():
try:
conandata_path = os.path.join(os.path.dirname(conanfile_path), "conandata.yml")
conandata_yml = load_yml(conandata_path)
if not conandata_yml:
return False
sources = conandata_yml["sources"]
first_url = sources[sources.keys()[0]]
url = urlparse(first_url)
if url.netloc != "github.com":
return False
repository = "{}/{}".format(url.path.split("/")[1], url.path.split("/")[2])
result = requests.get(f"https://api.github.com/repos/{repository}/license")
if not result.ok:
return False
return result.json()["license"]["spdx_id"]
except:
return False

def get_recipe_license():
license_attr = getattr(conanfile, "license")
if license_attr:
return license_attr if isinstance(license_attr, (list, tuple)) else [license_attr]

upstream_license = get_project_license_from_repo()
recipe_license = get_recipe_license()
if not upstream_license or not recipe_license:
return
if upstream_license.lower() not in [it.lower() for it in recipe_license]:
out.error(f"License '{upstream_license}' is listed on upstream repository,"
" but not listed on 'license' attribute.")
else:
for it in recipe_license:
if upstream_license.lower() == it.lower() and upstream_license != it:
out.error(f"License '{it}' uses incorrect case. Use '{upstream_license}' instead.")


@raise_if_error_output
def post_export(output, conanfile, conanfile_path, reference, **kwargs):
Expand Down
32 changes: 32 additions & 0 deletions tests/test_hooks/conan-center/test_license_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os
import textwrap

from conans import tools

from tests.utils.test_cases.conan_client import ConanClientTestCase


class TestLicenseValidation(ConanClientTestCase):
conanfile = textwrap.dedent("""\
from conans import ConanFile
class AConan(ConanFile):
license = "{}"
""")
conandata = textwrap.dedent("""\
sources:
8.9.2:
- url: https://github.com/approvals/ApprovalTests.cpp/releases/download/v.8.9.2/ApprovalTests.v.8.9.2.hpp
sha256: e743f1b83afb045cb9bdee310e438a3ed610a549240e4b4c4c995e548c773da5
""")

def _get_environ(self, **kwargs):
kwargs = super(TestLicenseValidation, self)._get_environ(**kwargs)
kwargs.update({'CONAN_HOOKS': os.path.join(os.path.dirname(__file__), '..', '..', '..',
'hooks', 'conan-center')})
return kwargs

def test_expected_license(self):
tools.save('conanfile.py', content=self.conanfile)
tools.save('conandata.yml', content=self.conandata)
output = self.conan(['export', '.', 'name/version@user/channel'])
self.assertIn("[LICENSE VALIDATION (KB-H070)] OK", output)