| 1 |
import re |
| 2 |
|
| 3 |
|
| 4 |
class version_info(object): |
| 5 |
def __init__(self, verstr): |
| 6 |
"""verstr - version string""" |
| 7 |
m1 = re.match("(.*?)([RBIXSF-])(.*)", verstr) |
| 8 |
self.type = m1.group(2) |
| 9 |
|
| 10 |
self.major = tuple(map(int, m1.group(1).split("."))) # creates tuyple |
| 11 |
after_type = m1.group(3).split(".") |
| 12 |
self.minor = after_type[0] |
| 13 |
|
| 14 |
if "X" == self.type: |
| 15 |
# assumes form similar to "45-D10", so extract the bits from this |
| 16 |
xm = re.match(r"(\d+)-(\w)(\d+)", self.minor) |
| 17 |
if xm is not None: |
| 18 |
self.minor = tuple([int(xm.group(1)), xm.group(2), int(xm.group(3))]) |
| 19 |
if len(after_type) < 2: |
| 20 |
self.build = None |
| 21 |
else: |
| 22 |
try: |
| 23 |
# handling case for EVO format X100-202310100600.0-EVO |
| 24 |
self.build = int(after_type[1]) |
| 25 |
except: |
| 26 |
self.build = None |
| 27 |
|
| 28 |
# X type not hyphen format, perhaps "11.4X12.1", just extract |
| 29 |
# build rev or set None |
| 30 |
else: |
| 31 |
if len(after_type) < 2: |
| 32 |
self.build = None |
| 33 |
else: |
| 34 |
try: |
| 35 |
# handling case for EVO format X50.17-EVO |
| 36 |
self.build = int(after_type[1]) |
| 37 |
except: |
| 38 |
self.build = None |
| 39 |
|
| 40 |
elif ("I" == self.type) or ("-" == self.type): |
| 41 |
self.type = "I" |
| 42 |
try: |
| 43 |
# assumes that we have a build/spin, but not numeric |
| 44 |
self.build = after_type[1] |
| 45 |
except: |
| 46 |
self.build = None |
| 47 |
else: |
| 48 |
try: |
| 49 |
self.build = int(after_type[1]) # assumes numeric build/spin |
| 50 |
except: |
| 51 |
self.build = after_type[0] # non-numeric |
| 52 |
|
| 53 |
self.as_tuple = self.major + tuple([self.type, self.minor, self.build]) |
| 54 |
self.v_dict = { |
| 55 |
"major": self.major, |
| 56 |
"type": self.type, |
| 57 |
"minor": self.minor, |
| 58 |
"build": self.build, |
| 59 |
} |
| 60 |
|
| 61 |
def __iter__(self): |
| 62 |
for key in self.v_dict: |
| 63 |
yield key, self.v_dict[key] |
| 64 |
|
| 65 |
def __repr__(self): |
| 66 |
retstr = ( |
| 67 |
"junos.version_info(major={major}, type={type}," |
| 68 |
" minor={minor}, build={build})".format( |
| 69 |
major=self.major, type=self.type, minor=self.minor, build=self.build |
| 70 |
) |
| 71 |
) |
| 72 |
return retstr |
| 73 |
|
| 74 |
def _cmp_tuple(self, other): |
| 75 |
length = len(self) if len(self) < len(other) else len(other) |
| 76 |
return self.as_tuple[0:length] |
| 77 |
|
| 78 |
def __len__(self): |
| 79 |
length = 0 |
| 80 |
for component in self.as_tuple: |
| 81 |
if component is None: |
| 82 |
return length |
| 83 |
else: |
| 84 |
length += 1 |
| 85 |
return length |
| 86 |
|
| 87 |
def __lt__(self, other): |
| 88 |
return self._cmp_tuple(other) < other |
| 89 |
|
| 90 |
def __le__(self, other): |
| 91 |
return self._cmp_tuple(other) <= other |
| 92 |
|
| 93 |
def __gt__(self, other): |
| 94 |
return self._cmp_tuple(other) > other |
| 95 |
|
| 96 |
def __ge__(self, other): |
| 97 |
return self._cmp_tuple(other) >= other |
| 98 |
|
| 99 |
def __eq__(self, other): |
| 100 |
return self._cmp_tuple(other) == other |
| 101 |
|
| 102 |
def __ne__(self, other): |
| 103 |
return self._cmp_tuple(other) != other |
| 104 |
|
| 105 |
|
| 106 |
def version_yaml_representer(dumper, version): |
| 107 |
return dumper.represent_mapping("tag:yaml.org,2002:map", version.v_dict) |
| 108 |
|
| 109 |
|
| 110 |
def provides_facts(): |
| 111 |
""" |
| 112 |
Doesn't really provide any facts. |
| 113 |
""" |
| 114 |
return {} |
| 115 |
|
| 116 |
|
| 117 |
def get_facts(device): |
| 118 |
""" |
| 119 |
Doesn't get any facts. |
| 120 |
""" |
| 121 |
return {} |