def get_specs(allarch=False): """ Get spec.yaml's for build caches available on mirror """ global _cached_specs arch = architecture.Arch(architecture.platform(), 'default_os', 'default_target') if not spack.mirror.MirrorCollection(): tty.debug("No Spack mirrors are currently configured") return {} for mirror in spack.mirror.MirrorCollection().values(): fetch_url_build_cache = url_util.join(mirror.fetch_url, _build_cache_relative_path) tty.debug('Finding buildcaches at {0}'.format( url_util.format(fetch_url_build_cache))) index_url = url_util.join(fetch_url_build_cache, 'index.json') try: _, _, file_stream = web_util.read_from_url(index_url, 'application/json') index_object = codecs.getreader('utf-8')(file_stream).read() except (URLError, web_util.SpackWebError) as url_err: tty.error('Failed to read index {0}'.format(index_url)) tty.debug(url_err) # Continue on to the next mirror continue tmpdir = tempfile.mkdtemp() index_file_path = os.path.join(tmpdir, 'index.json') with open(index_file_path, 'w') as fd: fd.write(index_object) db_root_dir = os.path.join(tmpdir, 'db_root') db = spack_db.Database(None, db_dir=db_root_dir, enable_transaction_locking=False) db._read_from_file(index_file_path) spec_list = db.query_local(installed=False) for indexed_spec in spec_list: spec_arch = architecture.arch_for_spec(indexed_spec.architecture) if (allarch is True or spec_arch == arch): _cached_specs.add(indexed_spec) return _cached_specs
def generate_package_index(cache_prefix): """Create the build cache index page. Creates (or replaces) the "index.json" page at the location given in cache_prefix. This page contains a link for each binary package (*.yaml) and public key (*.key) under cache_prefix. """ tmpdir = tempfile.mkdtemp() db_root_dir = os.path.join(tmpdir, 'db_root') db = spack_db.Database(None, db_dir=db_root_dir, enable_transaction_locking=False, record_fields=['spec', 'ref_count']) file_list = ( entry for entry in web_util.list_url(cache_prefix) if entry.endswith('.yaml')) tty.debug('Retrieving spec.yaml files from {0} to build index'.format( cache_prefix)) for file_path in file_list: try: yaml_url = url_util.join(cache_prefix, file_path) tty.debug('fetching {0}'.format(yaml_url)) _, _, yaml_file = web_util.read_from_url(yaml_url) yaml_contents = codecs.getreader('utf-8')(yaml_file).read() # yaml_obj = syaml.load(yaml_contents) # s = Spec.from_yaml(yaml_obj) s = Spec.from_yaml(yaml_contents) db.add(s, None) except (URLError, web_util.SpackWebError) as url_err: tty.error('Error reading spec.yaml: {0}'.format(file_path)) tty.error(url_err) try: index_json_path = os.path.join(db_root_dir, 'index.json') with open(index_json_path, 'w') as f: db._write_to_file(f) web_util.push_to_url( index_json_path, url_util.join(cache_prefix, 'index.json'), keep_original=False, extra_args={'ContentType': 'application/json'}) finally: shutil.rmtree(tmpdir)