Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Version formatting properties and functions to return Version objects #4834

Merged
32 changes: 27 additions & 5 deletions lib/spack/spack/test/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,13 +426,35 @@ def test_satisfaction_with_lists():


def test_formatted_strings():
versions = '1.2.3', '1_2_3', '1-2-3'
versions = '1.2.3b', '1_2_3b', '1-2-3b'
for item in versions:
v = Version(item)
assert v.dotted == '1.2.3'
assert v.dashed == '1-2-3'
assert v.underscored == '1_2_3'
assert v.joined == '123'
assert v.dotted == '1.2.3b'
assert v.dashed == '1-2-3b'
assert v.underscored == '1_2_3b'
assert v.joined == '123b'


def test_up_to():
version = Version('1.23-4_5b')

assert version.up_to(1) == Version('1')
assert version.up_to(2) == Version('1.23')
assert version.up_to(3) == Version('1.23-4')
assert version.up_to(4) == Version('1.23-4_5')
assert version.up_to(5) == Version('1.23-4_5b')

assert version.up_to(-1) == Version('1.23-4_5')
assert version.up_to(-2) == Version('1.23-4')
assert version.up_to(-3) == Version('1.23')
assert version.up_to(-4) == Version('1')

assert version.up_to(2).dotted == '1.23'
assert version.up_to(2).dashed == '1-23'
assert version.up_to(2).underscored == '1_23'
assert version.up_to(2).joined == '123'

assert version.up_to(2).up_to(1) == Version('1')


def test_repr_and_str():
Expand Down
80 changes: 68 additions & 12 deletions lib/spack/spack/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,30 +130,88 @@ def __init__(self, string):
self.version = tuple(int_if_int(seg) for seg in segments)

# Store the separators from the original version string as well.
# last element of separators is ''
self.separators = tuple(re.split(segment_regex, string)[1:-1])
self.separators = tuple(re.split(segment_regex, string)[1:])

@property
def dotted(self):
return '.'.join(str(x) for x in self.version)
"""The dotted representation of the version.

Example:
>>> version = Version('1-2-3b')
>>> version.dotted
'1.2.3b'

Returns:
str: The elements of the version, separated by dots
"""
return self.string.replace('-', '.').replace('_', '.')

@property
def underscored(self):
return '_'.join(str(x) for x in self.version)
"""The underscored representation of the version.

Example:
>>> version = Version('1.2.3b')
>>> version.underscored
'1_2_3b'

Returns:
str: The elements of the version, separated by underscores
"""
return self.string.replace('.', '_').replace('-', '_')

@property
def dashed(self):
return '-'.join(str(x) for x in self.version)
"""The dashed representation of the version.

Example:
>>> version = Version('1.2.3b')
>>> version.dashed
'1-2-3b'

Returns:
str: The elements of the version, separated by dashes
"""
return self.string.replace('.', '-').replace('_', '-')

@property
def joined(self):
return ''.join(str(x) for x in self.version)
"""The joined representation of the version.

Example:
>>> version = Version('1.2.3b')
>>> version.joined
'123b'

Returns:
str: The version with no separator characters
"""
return self.string.replace('.', '').replace('-', '').replace('_', '')

def up_to(self, index):
"""Return a version string up to the specified component, exclusive.
e.g., if this is 10.8.2, self.up_to(2) will return '10.8'.
"""The version up to the specified component.

Examples:
>>> version = Version('1.23-4b')
>>> version.up_to(1)
Version('1')
>>> version.up_to(2)
Version('1.23')
>>> version.up_to(3)
Version('1.23-4')
>>> version.up_to(4)
Version('1.23-4b')
>>> version.up_to(-1)
Version('1.23-4')
>>> version.up_to(-2)
Version('1.23')
>>> version.up_to(-3)
Version('1')

Returns:
Version: The first index components of the version
"""
return '.'.join(str(x) for x in self[:index])
return self[:index]

def lowest(self):
return self
Expand Down Expand Up @@ -204,11 +262,9 @@ def __getitem__(self, idx):
return self.version[idx]

elif isinstance(idx, slice):
# Currently len(self.separators) == len(self.version) - 1
extendend_separators = self.separators + ('',)
string_arg = []

pairs = zip(self.version[idx], extendend_separators[idx])
pairs = zip(self.version[idx], self.separators[idx])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tgamblin This seems to be the only place in Spack that uses version.separators. I changed the implementation above to make this simpler. Let me know if you want me to undo this change.

for token, sep in pairs:
string_arg.append(str(token))
string_arg.append(str(sep))
Expand Down
4 changes: 2 additions & 2 deletions var/spack/repos/builtin/packages/lua/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ def setup_environment(self, spack_env, run_env):

@property
def lua_lib_dir(self):
return os.path.join('lib', 'lua', self.version.up_to(2))
return os.path.join('lib', 'lua', str(self.version.up_to(2)))

@property
def lua_share_dir(self):
return os.path.join('share', 'lua', self.version.up_to(2))
return os.path.join('share', 'lua', str(self.version.up_to(2)))

def setup_dependent_package(self, module, dependent_spec):
"""
Expand Down
2 changes: 1 addition & 1 deletion var/spack/repos/builtin/packages/magics/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def install(self, spec, prefix):
if '+metview' in spec:
if '+qt' in spec:
options.append('-DENABLE_METVIEW=ON')
if spec['qt'].version.up_to(1) == '5':
if int(spec['qt'].version.up_to(1)) == 5:
options.append('-DENABLE_QT5=ON')
else:
options.append('-DENABLE_METVIEW_NO_QT=ON')
Expand Down
4 changes: 2 additions & 2 deletions var/spack/repos/builtin/packages/qt/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ def url_for_version(self, version):
url = self.list_url

if version >= Version('4.0'):
url += version.up_to(2) + '/'
url += str(version.up_to(2)) + '/'
else:
url += version.up_to(1) + '/'
url += str(version.up_to(1)) + '/'

if version >= Version('4.8'):
url += str(version) + '/'
Expand Down
2 changes: 1 addition & 1 deletion var/spack/repos/builtin/packages/sublime-text/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class SublimeText(Package):
depends_on('libxau', type='run')

def url_for_version(self, version):
if version.up_to(1) == '2':
if int(version.up_to(1)) == 2:
return "https://download.sublimetext.com/Sublime%20Text%20{0}%20x64.tar.bz2".format(version)
else:
return "https://download.sublimetext.com/sublime_text_3_build_{0}_x64.tar.bz2".format(version)
Expand Down