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

added thoth-license-solver #2647

Merged
merged 3 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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 Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ sqlalchemy-utils = "*"
alembic = "*"
thoth-ssdeep = "*"
packaging = "*"
thoth-license-solver = ">=0.1.4"

[dev-packages]
pytest = "*"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""added license-solver

Revision ID: 2a4632197512
Revises: 2b787ddad4a4
Create Date: 2022-06-09 07:26:05.576715+00:00

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "2a4632197512"
down_revision = "2b787ddad4a4"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"python_package_license",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("license_name", sa.Text(), nullable=False),
sa.Column("license_identifier", sa.Text(), nullable=False),
sa.Column("license_version", sa.Text(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)

op.add_column("python_package_version", sa.Column("package_license", sa.Integer(), nullable=True))
op.add_column("python_package_version", sa.Column("package_license_warning", sa.Boolean(), nullable=True))
op.create_foreign_key(None, "python_package_version", "python_package_license", ["package_license"], ["id"])
meile18 marked this conversation as resolved.
Show resolved Hide resolved
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, "python_package_version", type_="foreignkey")
op.drop_column("python_package_version", "package_license_warning")
op.drop_column("python_package_version", "package_license")
op.drop_table("python_package_license")
# ### end Alembic commands ###
20 changes: 20 additions & 0 deletions thoth/storages/graph/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class PythonPackageVersion(Base, BaseExtension):
)
is_missing = Column(Boolean, nullable=False, default=False)
provides_source_distro = Column(Boolean, nullable=False, default=True)
package_license = Column(Integer, ForeignKey("python_package_license.id")) # nullable=False
package_license_warning = Column(Boolean) # nullable=False

# Relations
dependencies = relationship("DependsOn", back_populates="version")
solvers = relationship("Solved", back_populates="version")
Expand All @@ -82,6 +85,7 @@ class PythonPackageVersion(Base, BaseExtension):
si_aggregated = relationship("SIAggregated", back_populates="python_package_version")
python_software_stacks = relationship("HasPythonRequirementsLock", back_populates="python_package_version")
import_packages = relationship("FoundImportPackage", back_populates="python_package_version")
licenses = relationship("PythonPackageLicense", back_populates="python_package_version")

__table_args__ = tuple(
get_python_package_version_index_combinations()
Expand All @@ -94,6 +98,21 @@ class PythonPackageVersion(Base, BaseExtension):
)


class PythonPackageLicense(Base, BaseExtension):
"""Representation of a Licenses."""
meile18 marked this conversation as resolved.
Show resolved Hide resolved

__tablename__ = "python_package_license"

id = Column(Integer, primary_key=True, autoincrement=True, nullable=False)

license_name = Column(Text, nullable=False)
license_identifier = Column(Text, nullable=False)
license_version = Column(Text, nullable=False)

# Relations
python_package_version = relationship("PythonPackageVersion", back_populates="licenses")


class HasArtifact(Base, BaseExtension):
"""The given package has the given artifact."""

Expand Down Expand Up @@ -1761,6 +1780,7 @@ class FoundImportPackage(Base, BaseExtension):
PythonPackageRequirement,
PythonPackageVersion,
PythonPackageVersionEntity,
PythonPackageLicense,
RPMPackageVersion,
RPMRequirement,
SecurityIndicatorAggregatedRun,
Expand Down
19 changes: 18 additions & 1 deletion thoth/storages/graph/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
from .models import SecurityIndicatorAggregatedRun
from .models import SoftwareEnvironment
from .models import VersionedSymbol
from .models import PythonPackageLicense

from .models import Advised
from .models import DebDepends
Expand Down Expand Up @@ -4734,6 +4735,8 @@ def _create_python_package_version(
python_version: Union[str, None],
python_package_metadata_id: int = None,
sync_only_entity: bool = False,
package_license: int = None,
package_license_warning: bool = None,
) -> Union[PythonPackageVersion, PythonPackageVersionEntity]:
"""Create a Python package version.

Expand Down Expand Up @@ -4768,6 +4771,8 @@ def _create_python_package_version(
os_version=os_version,
python_version=python_version,
entity_id=entity.id,
package_license=package_license,
package_license_warning=package_license_warning,
)

# including this value in "get_or_create" will cause errors because it is not part of a unique entry
Expand Down Expand Up @@ -5989,11 +5994,13 @@ def sync_solver_result(self, document: dict, *, force: bool = False) -> None:
package_version = python_package_info["package_version_requested"]
index_url = python_package_info["index_url"]
importlib_metadata = python_package_info["importlib_metadata"]["metadata"]
package_license = python_package_info["package_license"]

_LOGGER.info(
"Syncing solver result of package %r in version %r from %r solved by %r",
"Syncing solver result of package %r in version %r license %r from %r solved by %r",
package_name,
package_version,
package_license,
index_url,
solver_info,
)
Expand Down Expand Up @@ -6032,6 +6039,14 @@ def sync_solver_result(self, document: dict, *, force: bool = False) -> None:
f"No related columns for {list(importlib_metadata.keys())!r} "
"found in PythonPackageMetadata table, the error is not fatal"
)

license_metadata, _ = PythonPackageLicense.get_or_create(
session,
license_name=package_license["license"].get("full_name"),
license_identifier=package_license["license"].get("identifier_spdx"),
license_version=package_license["license_version"],
)

try:
python_package_version = self._create_python_package_version(
session,
Expand All @@ -6042,6 +6057,8 @@ def sync_solver_result(self, document: dict, *, force: bool = False) -> None:
python_version=ecosystem_solver.python_version,
index_url=index_url,
python_package_metadata_id=package_metadata.id,
package_license=license_metadata.id,
package_license_warning=package_license["warning"],
)
except NoResultFound:
if not force:
Expand Down