Browse Source

Reorganize python code

main
Sindre Stephansen 2 years ago
parent
commit
0fe75c46ce
Signed by: sindre <sindre@sindrestephansen.com> GPG Key ID: B06FC67D17A46ADE
3 changed files with 57 additions and 49 deletions
  1. +1
    -0
      sync/src/maven/__init__.py
  2. +3
    -49
      sync/src/maven/fetch.py
  3. +53
    -0
      sync/src/maven/packages.py

+ 1
- 0
sync/src/maven/__init__.py View File

@@ -0,0 +1 @@
from .packages import get_effective_packages

sync/src/maven.py → sync/src/maven/fetch.py View File

@@ -1,9 +1,9 @@
import asyncio
import logging
from typing import Optional, Iterable
import asyncio
from aiohttp import ClientSession
from typing import Optional, Iterable

from pom import PropertyMissing, PackagePOM, Properties
from pom import PackagePOM

logger = logging.getLogger(__name__)

@@ -12,52 +12,6 @@ class TooManyRequestsException(Exception):
pass


async def get_effective_packages(package: str, mirrors: Iterable[str]) -> list[str]:
"""
Get a list of packages that is required for Gradle to fetch this package.

For most packages, this just returns the package name. However, for BOMs, the full list of packages included
in the BOM is returned (including the BOM itself). This is because Gradle doesn't fetch all packages of a BOM.

This requires querying Maven and parsing the POM to check if the package is a BOM.
"""
packages = []

if pom := await fetch_pom(package, mirrors):
packages.append(package)

if pom.is_bom:
try:
packages.extend(pom.dependency_management)
except PropertyMissing as e1:
parent_props = await get_parent_props(pom.parent, mirrors)
if parent_props:
pom.set_properties(parent_props)
try:
packages.extend(pom.dependency_management)
except PropertyMissing as e2:
logger.warning(
f'{package}: Could not resolve property {e2.prop}, which is necessary for resolving dependencies'
)
else:
logger.warning(
f'{package}: Could not resolve property {e1.prop}, and could not get properties from parent'
)

return packages


async def get_parent_props(parent: Optional[str], mirrors: Iterable[str]) -> Properties:
if parent:
if pom := await fetch_pom(parent, mirrors):
pom.set_properties(await get_parent_props(pom.parent, mirrors))
return pom.properties
else:
logger.warning(f'{parent}: Error fetching pom')

return {}


async def fetch_pom(package: str, mirrors: Iterable[str]) -> Optional[PackagePOM]:
pom = await fetch_maven_file(package, 'pom', mirrors)
return PackagePOM(package, pom) if pom else None

+ 53
- 0
sync/src/maven/packages.py View File

@@ -0,0 +1,53 @@
import logging
from typing import Optional, Iterable

from maven.fetch import fetch_pom
from pom import PropertyMissing, Properties

logger = logging.getLogger(__name__)


async def get_effective_packages(package: str, mirrors: Iterable[str]) -> list[str]:
"""
Get a list of packages that is required for Gradle to fetch this package.

For most packages, this just returns the package name. However, for BOMs, the full list of packages included
in the BOM is returned (including the BOM itself). This is because Gradle doesn't fetch all packages of a BOM.

This requires querying Maven and parsing the POM to check if the package is a BOM.
"""
packages = []

if pom := await fetch_pom(package, mirrors):
packages.append(package)

if pom.is_bom:
try:
packages.extend(pom.dependency_management)
except PropertyMissing as e1:
parent_props = await get_parent_props(pom.parent, mirrors)
if parent_props:
pom.set_properties(parent_props)
try:
packages.extend(pom.dependency_management)
except PropertyMissing as e2:
logger.warning(
f'{package}: Could not resolve property {e2.prop}, which is necessary for resolving dependencies'
)
else:
logger.warning(
f'{package}: Could not resolve property {e1.prop}, and could not get properties from parent'
)

return packages


async def get_parent_props(parent: Optional[str], mirrors: Iterable[str]) -> Properties:
if parent:
if pom := await fetch_pom(parent, mirrors):
pom.set_properties(await get_parent_props(pom.parent, mirrors))
return pom.properties
else:
logger.warning(f'{parent}: Error fetching pom')

return {}

Loading…
Cancel
Save