diff options
author | Scott Wittenburg <scott.wittenburg@kitware.com> | 2022-03-10 15:12:19 -0700 |
---|---|---|
committer | Todd Gamblin <tgamblin@llnl.gov> | 2022-05-13 10:45:12 -0700 |
commit | be0e3f44588145a3067f4289f9d16c4fc67f2d0a (patch) | |
tree | 67da4120ca1f56f33c2cc667c182ef2bc1ddda8a /lib | |
parent | fd3bb5177b325a6c8b49420a5440feb99073263a (diff) | |
download | spack-be0e3f44588145a3067f4289f9d16c4fc67f2d0a.tar.gz spack-be0e3f44588145a3067f4289f9d16c4fc67f2d0a.tar.bz2 spack-be0e3f44588145a3067f4289f9d16c4fc67f2d0a.tar.xz spack-be0e3f44588145a3067f4289f9d16c4fc67f2d0a.zip |
binary_distribution: Refactor index generation into smaller methods
Diffstat (limited to 'lib')
-rw-r--r-- | lib/spack/spack/binary_distribution.py | 104 |
1 files changed, 57 insertions, 47 deletions
diff --git a/lib/spack/spack/binary_distribution.py b/lib/spack/spack/binary_distribution.py index 3c3d7f9c5c..ee96dfa6ee 100644 --- a/lib/spack/spack/binary_distribution.py +++ b/lib/spack/spack/binary_distribution.py @@ -762,6 +762,62 @@ def sign_tarball(key, force, specfile_path): spack.util.gpg.sign(key, specfile_path, '%s.asc' % specfile_path) +def _fetch_spec_from_mirror(spec_url): + s = None + tty.debug('fetching {0}'.format(spec_url)) + _, _, spec_file = web_util.read_from_url(spec_url) + spec_file_contents = codecs.getreader('utf-8')(spec_file).read() + # Need full spec.json name or this gets confused with index.json. + if spec_url.endswith('.json'): + s = Spec.from_json(spec_file_contents) + elif spec_url.endswith('.yaml'): + s = Spec.from_yaml(spec_file_contents) + return s + + +def _read_specs_and_push_index(file_list, cache_prefix, db, db_root_dir): + for file_path in file_list: + try: + s = _fetch_spec_from_mirror(url_util.join(cache_prefix, file_path)) + except (URLError, web_util.SpackWebError) as url_err: + tty.error('Error reading specfile: {0}'.format(file_path)) + tty.error(url_err) + + if s: + db.add(s, None) + db.mark(s, 'in_buildcache', True) + + # Now generate the index, compute its hash, and push the two files to + # the mirror. + index_json_path = os.path.join(db_root_dir, 'index.json') + with open(index_json_path, 'w') as f: + db._write_to_file(f) + + # Read the index back in and compute its hash + with open(index_json_path) as f: + index_string = f.read() + index_hash = compute_hash(index_string) + + # Write the hash out to a local file + index_hash_path = os.path.join(db_root_dir, 'index.json.hash') + with open(index_hash_path, 'w') as f: + f.write(index_hash) + + # Push the index itself + web_util.push_to_url( + index_json_path, + url_util.join(cache_prefix, 'index.json'), + keep_original=False, + extra_args={'ContentType': 'application/json'}) + + # Push the hash + web_util.push_to_url( + index_hash_path, + url_util.join(cache_prefix, 'index.json.hash'), + keep_original=False, + extra_args={'ContentType': 'text/plain'}) + + def generate_package_index(cache_prefix): """Create the build cache index page. @@ -797,53 +853,7 @@ def generate_package_index(cache_prefix): record_fields=['spec', 'ref_count', 'in_buildcache']) try: - for file_path in file_list: - try: - spec_url = url_util.join(cache_prefix, file_path) - tty.debug('fetching {0}'.format(spec_url)) - _, _, spec_file = web_util.read_from_url(spec_url) - spec_file_contents = codecs.getreader('utf-8')(spec_file).read() - # Need full spec.json name or this gets confused with index.json. - if spec_url.endswith('.json'): - s = Spec.from_json(spec_file_contents) - elif spec_url.endswith('.yaml'): - s = Spec.from_yaml(spec_file_contents) - if s: - db.add(s, None) - db.mark(s, 'in_buildcache', True) - except (URLError, web_util.SpackWebError) as url_err: - tty.error('Error reading specfile: {0}'.format(file_path)) - tty.error(url_err) - - # Now generate the index, compute its hash, and push the two files to - # the mirror. - index_json_path = os.path.join(db_root_dir, 'index.json') - with open(index_json_path, 'w') as f: - db._write_to_file(f) - - # Read the index back in and compute its hash - with open(index_json_path) as f: - index_string = f.read() - index_hash = compute_hash(index_string) - - # Write the hash out to a local file - index_hash_path = os.path.join(db_root_dir, 'index.json.hash') - with open(index_hash_path, 'w') as f: - f.write(index_hash) - - # Push the index itself - web_util.push_to_url( - index_json_path, - url_util.join(cache_prefix, 'index.json'), - keep_original=False, - extra_args={'ContentType': 'application/json'}) - - # Push the hash - web_util.push_to_url( - index_hash_path, - url_util.join(cache_prefix, 'index.json.hash'), - keep_original=False, - extra_args={'ContentType': 'text/plain'}) + _read_specs_and_push_index(file_list, cache_prefix, db, db_root_dir) except Exception as err: msg = 'Encountered problem pushing package index to {0}: {1}'.format( cache_prefix, err) |