From 2f1eae8c0d5ed0f3d6c8cce39be5e0a5c4e3e0a5 Mon Sep 17 00:00:00 2001 From: Todd Gamblin Date: Mon, 23 Dec 2013 16:12:02 -0800 Subject: [PATCH] spack checksum works. --- lib/spack/spack/cmd/checksum.py | 48 ++++++++++++++++++++++-------- lib/spack/spack/util/executable.py | 23 +++++++++----- lib/spack/spack/version.py | 10 ++++--- 3 files changed, 56 insertions(+), 25 deletions(-) diff --git a/lib/spack/spack/cmd/checksum.py b/lib/spack/spack/cmd/checksum.py index 30c2f8b32f..70670390a8 100644 --- a/lib/spack/spack/cmd/checksum.py +++ b/lib/spack/spack/cmd/checksum.py @@ -7,12 +7,13 @@ import spack.tty as tty import spack.packages as packages -from spack.stage import Stage +import spack.util.crypto +from spack.stage import Stage, FailedDownloadError from spack.colify import colify -from spack.util.crypto import checksum from spack.version import * -group='foo' +default_number_to_fetch = 10 + description ="Checksum available versions of a package, print out checksums for addition to a package file." def setup_parser(subparser): @@ -20,9 +21,6 @@ def setup_parser(subparser): 'package', metavar='PACKAGE', help='Package to list versions for') subparser.add_argument( 'versions', nargs=argparse.REMAINDER, help='Versions to generate checksums for') - subparser.add_argument( - '-n', '--number', dest='number', type=int, - default=10, help='Number of versions to list') def checksum(parser, args): @@ -30,7 +28,6 @@ def checksum(parser, args): pkg = packages.get(args.package) # If the user asked for specific versions, use those. - # Otherwise get the latest n, where n is from the -n/--number param versions = [ver(v) for v in args.versions] if not all(type(v) == Version for v in versions): @@ -38,23 +35,48 @@ def checksum(parser, args): "version ranges. Use unambiguous versions.") if not versions: - versions = pkg.fetch_available_versions()[:args.number] + versions = pkg.fetch_available_versions() if not versions: tty.die("Could not fetch any available versions for %s." % pkg.name) - versions.sort() - versions.reverse() + versions = list(reversed(versions)) urls = [pkg.url_for_version(v) for v in versions] - tty.msg("Found %s versions to checksum." % len(urls)) - tty.msg("Downloading...") + version_listings = ["%-10s%s" % (v,u) for v, u in zip(versions, urls)] + tty.msg("Found %s versions to checksum." % len(urls), + *version_listings) + print + while True: + ans = raw_input("How many would you like to checksum? (default 10, 0 to abort) ") + try: + if not ans: + to_download = default_number_to_fetch + else: + to_download = int(ans) + break + except ValueError: + tty.msg("Please enter a valid number.") + pass + + if not to_download: + tty.msg("Aborted.") + return + else: + urls = urls[:to_download] + + tty.msg("Downloading...") hashes = [] for url, version in zip(urls, versions): stage = Stage(url) try: stage.fetch() - hashes.append(checksum(hashlib.md5, stage.archive_file)) + hashes.append(spack.util.crypto.checksum( + hashlib.md5, stage.archive_file)) + except FailedDownloadError, e: + tty.msg("Failed to fetch %s" % url) + continue + finally: stage.destroy() diff --git a/lib/spack/spack/util/executable.py b/lib/spack/spack/util/executable.py index ad13695405..f29eaa357d 100644 --- a/lib/spack/spack/util/executable.py +++ b/lib/spack/spack/util/executable.py @@ -1,4 +1,5 @@ import os +import sys import re import subprocess import spack.tty as tty @@ -26,20 +27,26 @@ def __call__(self, *args, **kwargs): quoted_args = [arg for arg in args if re.search(r'^"|^\'|"$|\'$', arg)] if quoted_args: - tty.warn("Quotes in package command arguments can confuse shell scripts like configure.", + tty.warn("Quotes in command arguments can confuse scripts like configure.", "The following arguments may cause problems when executed:", str("\n".join([" "+arg for arg in quoted_args])), - "Quotes aren't needed because spack doesn't use a shell. Consider removing them") + "Quotes aren't needed because spack doesn't use a shell.", + "Consider removing them") cmd = self.exe + list(args) tty.verbose(" ".join(cmd)) - if return_output: - return subprocess.check_output(cmd) - elif fail_on_error: - return subprocess.check_call(cmd) - else: - return subprocess.call(cmd) + try: + proc = subprocess.Popen( + cmd, + stderr=sys.stderr, + stdout=subprocess.PIPE if return_output else sys.stdout) + out, err = proc.communicate() + if return_output: + return out + + except CalledProcessError, e: + if fail_on_error: raise def __repr__(self): return "" % self.exe diff --git a/lib/spack/spack/version.py b/lib/spack/spack/version.py index 2f4c80a85d..66ec870021 100644 --- a/lib/spack/spack/version.py +++ b/lib/spack/spack/version.py @@ -144,8 +144,7 @@ def a_or_n(seg): def __iter__(self): - for v in self.version: - yield v + return iter(self.version) def __getitem__(self, idx): @@ -486,8 +485,11 @@ def __getitem__(self, index): def __iter__(self): - for v in self.versions: - yield v + return iter(self.versions) + + + def __reversed__(self): + return reversed(self.versions) def __len__(self):