def write_spec(self, spec, path): """Write a spec out to a file.""" _check_concrete(spec) with open(path, 'w') as f: # The hash the the projection is the DAG hash but we write out the # full provenance by full hash so it's availabe if we want it later spec.to_yaml(f, hash=ht.full_hash)
def spec(parser, args): name_fmt = '{namespace}.{name}' if args.namespaces else '{name}' fmt = '{@version}{%compiler}{compiler_flags}{variants}{arch=architecture}' install_status_fn = spack.spec.Spec.install_status kwargs = { 'cover': args.cover, 'format': name_fmt + fmt, 'hashlen': None if args.very_long else 7, 'show_types': args.types, 'status_fn': install_status_fn if args.install_status else None } if not args.specs: tty.die("spack spec requires at least one spec") for spec in spack.cmd.parse_specs(args.specs): # With -y, just print YAML to output. if args.yaml: if spec.name in spack.repo.path or spec.virtual: spec.concretize() # use write because to_yaml already has a newline. sys.stdout.write(spec.to_yaml()) continue kwargs['hashes'] = False # Always False for input spec print("Input spec") print("--------------------------------") print(spec.tree(**kwargs)) kwargs['hashes'] = args.long or args.very_long print("Concretized") print("--------------------------------") spec.concretize() print(spec.tree(**kwargs))
def test_yaml_subdag(config, mock_packages): spec = Spec('mpileaks^mpich+debug') spec.concretize() yaml_spec = Spec.from_yaml(spec.to_yaml()) for dep in ('callpath', 'mpich', 'dyninst', 'libdwarf', 'libelf'): assert spec[dep].eq_dag(yaml_spec[dep])
def spec(parser, args): name_fmt = '{namespace}.{name}' if args.namespaces else '{name}' fmt = '{@version}{%compiler}{compiler_flags}{variants}{arch=architecture}' install_status_fn = spack.spec.Spec.install_status tree_kwargs = { 'cover': args.cover, 'format': name_fmt + fmt, 'hashlen': None if args.very_long else 7, 'show_types': args.types, 'status_fn': install_status_fn if args.install_status else None } # use a read transaction if we are getting install status for every # spec in the DAG. This avoids repeatedly querying the DB. tree_context = nullcontext if args.install_status: tree_context = spack.store.db.read_transaction if not args.specs: tty.die("spack spec requires at least one spec") concretize_kwargs = {'reuse': args.reuse} for spec in spack.cmd.parse_specs(args.specs): # With -y, just print YAML to output. if args.format: if spec.name in spack.repo.path or spec.virtual: spec.concretize(**concretize_kwargs) # The user can specify the hash type to use hash_type = getattr(ht, args.hash_type) if args.format == 'yaml': # use write because to_yaml already has a newline. sys.stdout.write(spec.to_yaml(hash=hash_type)) else: print(spec.to_json(hash=hash_type)) continue with tree_context(): tree_kwargs['hashes'] = False # Always False for input spec print("Input spec") print("--------------------------------") print(spec.tree(**tree_kwargs)) tree_kwargs['hashes'] = args.long or args.very_long print("Concretized") print("--------------------------------") spec.concretize(**concretize_kwargs) print(spec.tree(**tree_kwargs))
def write_spec(self, spec, path): """Write a spec out to a file.""" _check_concrete(spec) with open(path, 'w') as f: spec.to_yaml(f)
def check_yaml_round_trip(spec): yaml_text = spec.to_yaml() spec_from_yaml = Spec.from_yaml(yaml_text) assert spec.eq_dag(spec_from_yaml)
def test_ordered_read_not_required_for_consistent_dag_hash( hash_type, config, mock_packages): """Make sure ordered serialization isn't required to preserve hashes. For consistent hashes, we require that YAML and json documents have their keys serialized in a deterministic order. However, we don't want to require them to be serialized in order. This ensures that is not required. """ specs = ['mpileaks ^zmpi', 'dttop', 'dtuse'] for spec in specs: spec = Spec(spec) spec.concretize() # # Dict & corresponding YAML & JSON from the original spec. # spec_dict = spec.to_dict(hash=hash_type) spec_yaml = spec.to_yaml(hash=hash_type) spec_json = spec.to_json(hash=hash_type) # # Make a spec with reversed OrderedDicts for every # OrderedDict in the original. # reversed_spec_dict = reverse_all_dicts(spec.to_dict(hash=hash_type)) # # Dump to YAML and JSON # yaml_string = syaml.dump(spec_dict, default_flow_style=False) reversed_yaml_string = syaml.dump(reversed_spec_dict, default_flow_style=False) json_string = sjson.dump(spec_dict) reversed_json_string = sjson.dump(reversed_spec_dict) # # Do many consistency checks # # spec yaml is ordered like the spec dict assert yaml_string == spec_yaml assert json_string == spec_json # reversed string is different from the original, so it # *would* generate a different hash assert yaml_string != reversed_yaml_string assert json_string != reversed_json_string # build specs from the "wrongly" ordered data round_trip_yaml_spec = Spec.from_yaml(yaml_string) round_trip_json_spec = Spec.from_json(json_string) round_trip_reversed_yaml_spec = Spec.from_yaml(reversed_yaml_string) round_trip_reversed_json_spec = Spec.from_yaml(reversed_json_string) # Strip spec if we stripped the yaml spec = spec.copy(deps=hash_type.deptype) # specs are equal to the original assert spec == round_trip_yaml_spec assert spec == round_trip_json_spec assert spec == round_trip_reversed_yaml_spec assert spec == round_trip_reversed_json_spec assert round_trip_yaml_spec == round_trip_reversed_yaml_spec assert round_trip_json_spec == round_trip_reversed_json_spec # dag_hashes are equal assert spec.dag_hash() == round_trip_yaml_spec.dag_hash() assert spec.dag_hash() == round_trip_json_spec.dag_hash() assert spec.dag_hash() == round_trip_reversed_yaml_spec.dag_hash() assert spec.dag_hash() == round_trip_reversed_json_spec.dag_hash() # full_hashes are equal if we round-tripped by build_hash or full_hash if hash_type in (ht.build_hash, ht.full_hash): spec.concretize() round_trip_yaml_spec.concretize() round_trip_json_spec.concretize() round_trip_reversed_yaml_spec.concretize() round_trip_reversed_json_spec.concretize() assert spec.full_hash() == round_trip_yaml_spec.full_hash() assert spec.full_hash() == round_trip_json_spec.full_hash() assert spec.full_hash() == round_trip_reversed_yaml_spec.full_hash( ) assert spec.full_hash() == round_trip_reversed_json_spec.full_hash( )