metadata: support virtual manifest

This commit is contained in:
Alberto Planas 2019-12-10 17:22:57 +01:00
parent afbee03f26
commit b746a2ec5c
2 changed files with 47 additions and 6 deletions

View file

@ -32,9 +32,7 @@ def main():
if len(deps) > 0: if len(deps) > 0:
print("\n".join(sorted(normalize_deps(deps)))) print("\n".join(sorted(normalize_deps(deps))))
for f in files: def process_metadata(md):
f = f.rstrip()
md = Metadata.from_file(f)
if args.name: if args.name:
print(md.name) print(md.name)
if args.version: if args.version:
@ -67,5 +65,15 @@ def main():
if args.test_requires: if args.test_requires:
print_deps(md.dev_dependencies) print_deps(md.dev_dependencies)
for f in files:
f = f.rstrip()
mds = Metadata.from_file(f)
if isinstance(mds, list):
for md in mds:
process_metadata(md)
else:
process_metadata(mds)
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View file

@ -3,8 +3,10 @@ __all__ = ["Dependency", "Metadata"]
import collections import collections
import copy import copy
import json import json
import os.path
import re import re
import subprocess import subprocess
from urllib.parse import urlparse
Requirement = collections.namedtuple('Requirement', ('kind', Requirement = collections.namedtuple('Requirement', ('kind',
@ -334,9 +336,40 @@ class Metadata:
@classmethod @classmethod
def from_file(cls, path): def from_file(cls, path):
metadata = subprocess.check_output(["cargo", "read-manifest", try:
f"--manifest-path={path}"]) return cls.from_json(Metadata.manifest(path))
return cls.from_json(json.loads(metadata)) except subprocess.CalledProcessError:
# If fails, we still check that is a workspace
members = Metadata.members(path)
if members:
return [
cls.from_json(Metadata.manifest(m)) for m in members
]
# Is not a workspace? re-raise the exception
raise
@staticmethod
def manifest(path):
return json.loads(
subprocess.check_output(["cargo", "read-manifest",
f"--manifest-path={path}"]))
@staticmethod
def metadata(path, deps=False):
cmd = ["cargo", "metadata", "--format-version=1",
f"--manifest-path={path}"]
if not deps:
cmd.append("--no-deps")
return json.loads(subprocess.check_output(cmd))
@staticmethod
def members(path):
members = []
metadata = Metadata.metadata(path)
for workspace in metadata.get('workspace_members', []):
path = re.search(r'\((.*)\)', workspace).group(1)
members.append(os.path.join(urlparse(path).path, 'Cargo.toml'))
return members
@property @property
def all_dependencies(self): def all_dependencies(self):