Skip to content

Commit

Permalink
spack list: latest version (JSON)
Browse files Browse the repository at this point in the history
List the latest version of each package in JSON encoding.
Preparation for consumption for a "spack badge" service.
  • Loading branch information
ax3l committed Jun 7, 2019
1 parent 06cc799 commit ba6a08a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
26 changes: 26 additions & 0 deletions lib/spack/spack/cmd/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import spack.dependency
import spack.repo
import spack.cmd.common.arguments as arguments
from spack.version import VersionList

description = "list and search available packages"
section = "basic"
Expand Down Expand Up @@ -116,6 +117,31 @@ def rows_for_ncols(elts, ncols):
yield row


@formatter
def version_json(pkg_names, out):
"""Print all packages with their latest versions."""
pkgs = [spack.repo.get(name) for name in pkg_names]

out.write('[\n')

# output name and latest version for each package
pkg_latest = ",\n".join([
' {{"name": "{0}",\n'
' "latest_version": "{1}",\n'
' "homepage": "{2}",\n'
' "file": "{3}"'
'}}'.format(
pkg.name,
VersionList(pkg.versions).highest_numeric(True),
pkg.homepage,
github_url(pkg)
) for pkg in pkgs
])
out.write(pkg_latest)
# important: no trailing comma in JSON arrays
out.write('\n]\n')


@formatter
def html(pkg_names, out):
"""Print out information on all packages in Sphinx HTML.
Expand Down
8 changes: 8 additions & 0 deletions lib/spack/spack/test/cmd/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ def test_list_format_name_only():


@pytest.mark.maybeslow
def test_list_format_version_json():
output = list('--format', 'version_json')
assert ' {"name": "cloverleaf3d",' in output
assert ' {"name": "hdf5",' in output
import json
json.loads(output)


def test_list_format_html():
output = list('--format', 'html')
assert '<div class="section" id="cloverleaf3d">' in output
Expand Down
14 changes: 13 additions & 1 deletion lib/spack/spack/test/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""
import pytest

from spack.version import Version, ver
from spack.version import Version, VersionList, ver


def assert_ver_lt(a, b):
Expand Down Expand Up @@ -548,3 +548,15 @@ def test_get_item():
# Raise TypeError on tuples
with pytest.raises(TypeError):
b.__getitem__(1, 2)


def test_list_highest():
vl = VersionList(['master', '1.2.3', 'develop', '3.4.5', 'foobar'])
assert vl.highest() == Version('develop')
assert vl.lowest() == Version('foobar')
assert vl.highest_numeric() == Version('3.4.5')

vl2 = VersionList(['master', 'develop'])
assert vl2.highest_numeric() is None
assert vl2.highest_numeric(True) == Version('develop')
assert vl2.lowest() == Version('master')
13 changes: 13 additions & 0 deletions lib/spack/spack/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,19 @@ def highest(self):
else:
return self[-1].highest()

def highest_numeric(self, fallback_all=False):
"""Get the highest numeric version in the list."""
numeric_versions = list(filter(
lambda v: str(v) not in infinity_versions,
self.versions))
if not any(numeric_versions):
if fallback_all:
return self.highest()
else:
return None
else:
return numeric_versions[-1].highest()

@coerced
def overlaps(self, other):
if not other or not self:
Expand Down

0 comments on commit ba6a08a

Please sign in to comment.