From e2c6c69c0a133f6e910069f012f3fdcb211e91de Mon Sep 17 00:00:00 2001 From: Alberto Planas Date: Oct 12 2021 13:10:33 +0000 Subject: metadata: support workspaces for non-virtual crates --- diff --git a/rust2rpm/inspector.py b/rust2rpm/inspector.py index 489dcc8..b29d443 100644 --- a/rust2rpm/inspector.py +++ b/rust2rpm/inspector.py @@ -71,59 +71,57 @@ def main(): for f in args.features.split(","): features.add(f or None) - def print_deps(deps): - if len(deps) > 0: - print("\n".join(sorted(normalize_deps(deps)))) - def process_metadata(md): + data = [] if args.name: - print(md.name) + data.append(md.name) if args.version: - print(md._version) + data.append(md._version) if args.rpm_version: - print(md.version) + data.append(md.version) if args.target_kinds: - print("\n".join(set(tgt.kind for tgt in md.targets))) + data.extend('\n'.join(set(tgt.kind for tgt in md.targets))) if args.list_features: - for f in sorted(f for f in md.dependencies if f is not None): - print(f) + data.extend(f for f in md.dependencies if f is not None) if args.provides: - for f in features: - print(md.provides(f)) + data.extend(md.provides(f) for f in features) if args.requires: # Someone should own /usr/share/cargo/registry - print("cargo") + data.append('cargo') if args.all_features: - print_deps(md.all_dependencies) + data.extend(md.all_dependencies) else: for f in features: - print_deps(md.requires(f)) + data.extend(md.requires(f)) if args.build_requires: - print("rust-packaging") + data.append("rust-packaging") if args.all_features: - print_deps(md.all_dependencies) + data.extend(md.all_dependencies) else: for f in features: - print_deps(md.requires(f, resolve=True)) + data.extend(md.requires(f, resolve=True)) if args.test_requires: - print_deps(md.dev_dependencies) + data.extend(md.dev_dependencies) if args.provides_vendor: # Print the vendoring providers only if the 'vendor' # directory is present if args.force or os.path.isdir('vendor'): - print_deps(md.resolved_dependencies()) + data.extend(md.resolved_dependencies()) + return data for f in files: # We need to change directory, so cargo can find any local # configuration file _go_to_top_cargo_toml(args.path, f) + data = [] mds = Metadata.from_file(f) - if isinstance(mds, list): - for md in mds: - process_metadata(md) - else: - process_metadata(mds) + for md in mds: + data.extend(process_metadata(md)) + # Converting a data element to string can generate a multiple + # line string. We flatten the lines via a double interation. + for line in sorted({d for dat in data for d in str(dat).splitlines()}): + print(line) if __name__ == "__main__": diff --git a/rust2rpm/metadata.py b/rust2rpm/metadata.py index 5627289..4a0b68c 100644 --- a/rust2rpm/metadata.py +++ b/rust2rpm/metadata.py @@ -204,7 +204,8 @@ class Target: class Dependency: - def __init__(self, name, req=None, features=(), optional=False, bundled=False): + def __init__(self, name, req=None, features=(), optional=False, + bundled=False): self.name = name self.req = req self.features = features @@ -370,22 +371,15 @@ class Metadata: @classmethod def from_file(cls, path): - try: - instance = cls.from_json(Metadata.manifest(path)) - instance._path = path - return instance - except subprocess.CalledProcessError: - # If fails, we still check that is a workspace - members = Metadata.members(path) - if members: - instances = [ - cls.from_json(Metadata.manifest(m)) for m in members - ] - for instance in instances: - instance._path = path - return instances - # Is not a workspace? re-raise the exception - raise + instances = [] + members = Metadata.members(path) + if not members: + members = [path] + for member in members: + instance = cls.from_json(Metadata.manifest(member)) + instance._path = member + instances.append(instance) + return instances @staticmethod def manifest(path, check=True): @@ -465,7 +459,7 @@ class Metadata: @staticmethod def _closure(dependencies, metadata): - # Is not very clear how to decide, for a workspace, what + # It is not very clear how to decide, for a workspace, what # features are enabled for a package after resolving all the # dependencies. We can trace back from the initial set of # dependencies / features, until the final set of packages