Skip to content

Commit

Permalink
cargo: Capture git packages manifests
Browse files Browse the repository at this point in the history
  • Loading branch information
gasinvein committed Mar 28, 2023
1 parent 4cabcf5 commit 67fc223
Showing 1 changed file with 45 additions and 12 deletions.
57 changes: 45 additions & 12 deletions cargo/flatpak-cargo-generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import logging
import hashlib
import asyncio
from typing import Any, Dict, List, Optional, Tuple, TypedDict
from typing import Any, Dict, List, NamedTuple, Optional, Tuple, TypedDict

import aiohttp
import toml
Expand Down Expand Up @@ -116,7 +116,13 @@ def fetch_git_repo(git_url: str, commit: str) -> str:
return clone_dir


_GitPackagesType = Dict[str, str]
class _GitPackage(NamedTuple):
path: str
package: _TomlType
workspace: Optional[_TomlType]


_GitPackagesType = Dict[str, _GitPackage]


async def get_git_repo_packages(git_url: str, commit: str) -> _GitPackagesType:
Expand All @@ -133,7 +139,14 @@ async def get_git_repo_packages(git_url: str, commit: str) -> _GitPackagesType:
os.path.dirname(toml_path)))

assert packages, f"No packages found in {git_repo_dir}"
logging.debug('Packages in %s:\n%s', git_url, json.dumps(packages, indent=4))
logging.debug(
'Packages in %s:\n%s',
git_url,
json.dumps(
{k: v.path for k, v in packages.items()},
indent=4,
),
)
return packages


Expand All @@ -142,7 +155,11 @@ async def get_cargo_toml_packages(root_toml: _TomlType, root_dir: str) -> _GitPa
assert 'package' in root_toml or 'workspace' in root_toml
packages: _GitPackagesType = {}

async def get_dep_packages(entry: _TomlType, toml_dir: str):
async def get_dep_packages(
entry: _TomlType,
toml_dir: str,
workspace: Optional[_TomlType] = None,
):
assert not os.path.isabs(toml_dir)
# https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html
if 'dependencies' in entry:
Expand All @@ -157,24 +174,40 @@ async def get_dep_packages(entry: _TomlType, toml_dir: str):
logging.debug("Loading dependency %s from %s", dep_name, dep_dir)
dep_toml = load_toml(os.path.join(dep_dir, 'Cargo.toml'))
assert dep_toml['package']['name'] == dep_name, toml_dir
await get_dep_packages(dep_toml, dep_dir)
packages[dep_name] = dep_dir
await get_dep_packages(dep_toml, dep_dir, workspace)
packages[dep_name] = _GitPackage(
path=dep_dir,
package=dep,
workspace=workspace,
)
if 'target' in entry:
for _, target in entry['target'].items():
await get_dep_packages(target, toml_dir)

if 'package' in root_toml:
await get_dep_packages(root_toml, root_dir)
packages[root_toml['package']['name']] = root_dir
packages[root_toml['package']['name']] = _GitPackage(
path=root_dir,
package=root_toml,
workspace=None,
)

if 'workspace' in root_toml:
for member in root_toml['workspace'].get('members', []):
for subpkg_toml in glob.glob(os.path.join(root_dir, member, 'Cargo.toml')):
subpkg = os.path.normpath(os.path.dirname(subpkg_toml))
logging.debug("Loading workspace member %s in %s", member, root_dir)
logging.debug(
"Loading workspace member %s in %s",
subpkg_toml,
os.path.abspath(root_dir),
)
pkg_toml = load_toml(subpkg_toml)
await get_dep_packages(pkg_toml, subpkg)
packages[pkg_toml['package']['name']] = subpkg
await get_dep_packages(pkg_toml, subpkg, root_toml['workspace'])
packages[pkg_toml['package']['name']] = _GitPackage(
path=subpkg,
package=pkg_toml,
workspace=root_toml['workspace'],
)

return packages

Expand Down Expand Up @@ -251,8 +284,8 @@ async def get_git_package_sources(
cargo_vendored_entry[repo_url]['branch'] = branch[0]

logging.info("Adding package %s from %s", name, repo_url)
pkg_subpath = git_repo['commits'][commit][name]
pkg_repo_dir = os.path.join(GIT_CACHE, git_repo_name(repo_url, commit), pkg_subpath)
git_pkg = git_repo['commits'][commit][name]
pkg_repo_dir = os.path.join(GIT_CACHE, git_repo_name(repo_url, commit), git_pkg.path)
git_sources: List[_FlatpakSourceType] = [
{
'type': 'shell',
Expand Down

0 comments on commit 67fc223

Please sign in to comment.