|
1 | 1 | import os
|
| 2 | +import re |
2 | 3 | import subprocess
|
| 4 | +import numbers |
| 5 | +from collections import namedtuple |
| 6 | + |
| 7 | + |
| 8 | +class ROOTVersion(namedtuple('_ROOTVersionBase', |
| 9 | + ['major', 'minor', 'micro'])): |
| 10 | + |
| 11 | + def __new__(cls, *version): |
| 12 | + if len(version) == 1: |
| 13 | + version = version[0] |
| 14 | + |
| 15 | + if isinstance(version, numbers.Integral): |
| 16 | + if version < 1E4: |
| 17 | + raise ValueError( |
| 18 | + "{0:d} is not a valid ROOT version integer".format(version)) |
| 19 | + return super(ROOTVersion, cls).__new__( |
| 20 | + cls, |
| 21 | + int(version / 1E4), |
| 22 | + int((version / 1E2) % 100), |
| 23 | + int(version % 100)) |
| 24 | + |
| 25 | + if isinstance(version, tuple): |
| 26 | + return super(ROOTVersion, cls).__new__(cls, *version) |
| 27 | + |
| 28 | + # parse the string version X.YY/ZZ |
| 29 | + match = re.match( |
| 30 | + r"(?P<major>[\d]+)\.(?P<minor>[\d]+)/(?P<micro>[\d]+)", version) |
| 31 | + if not match: |
| 32 | + raise ValueError( |
| 33 | + "'{0}' is not a valid ROOT version string".format(version)) |
| 34 | + return super(ROOTVersion, cls).__new__( |
| 35 | + cls, |
| 36 | + int(match.group('major')), |
| 37 | + int(match.group('minor')), |
| 38 | + int(match.group('micro'))) |
| 39 | + |
| 40 | + |
| 41 | + def __eq__(self, version): |
| 42 | + if not isinstance(version, tuple): |
| 43 | + version = ROOTVersion(version) |
| 44 | + return super(ROOTVersion, self).__eq__(version) |
| 45 | + |
| 46 | + def __ne__(self, version): |
| 47 | + return not self.__eq__(version) |
| 48 | + |
| 49 | + def __gt__(self, version): |
| 50 | + if not isinstance(version, tuple): |
| 51 | + version = ROOTVersion(version) |
| 52 | + return super(ROOTVersion, self).__gt__(version) |
| 53 | + |
| 54 | + def __ge__(self, version): |
| 55 | + if not isinstance(version, tuple): |
| 56 | + version = ROOTVersion(version) |
| 57 | + return super(ROOTVersion, self).__ge__(version) |
| 58 | + |
| 59 | + def __lt__(self, version): |
| 60 | + if not isinstance(version, tuple): |
| 61 | + version = ROOTVersion(version) |
| 62 | + return super(ROOTVersion, self).__lt__(version) |
| 63 | + |
| 64 | + def __le__(self, version): |
| 65 | + if not isinstance(version, tuple): |
| 66 | + version = ROOTVersion(version) |
| 67 | + return super(ROOTVersion, self).__le__(version) |
| 68 | + |
| 69 | + def __repr__(self): |
| 70 | + return str(self) |
| 71 | + |
| 72 | + def __str__(self): |
| 73 | + return '{0:d}.{1:02d}/{2:02d}'.format(*self) |
3 | 74 |
|
4 | 75 |
|
5 | 76 | def root_flags(root_config='root-config'):
|
@@ -33,12 +104,12 @@ def root_version_installed(root_config='root-config'):
|
33 | 104 | stdout=subprocess.PIPE).communicate()[0].strip()
|
34 | 105 | if sys.version > '3':
|
35 | 106 | root_vers = root_vers.decode('utf-8')
|
36 |
| - return root_vers |
| 107 | + return ROOTVersion(root_vers) |
37 | 108 |
|
38 | 109 |
|
39 | 110 | def root_version_active():
|
40 | 111 | import ROOT
|
41 |
| - return ROOT.gROOT.GetVersion() |
| 112 | + return ROOTVersion(ROOT.gROOT.GetVersionInt()) |
42 | 113 |
|
43 | 114 |
|
44 | 115 | def get_config():
|
|
0 commit comments