diff options
Diffstat (limited to 'scripts/lib/mic/3rdparty/pykickstart/version.py')
| -rw-r--r-- | scripts/lib/mic/3rdparty/pykickstart/version.py | 197 |
1 files changed, 197 insertions, 0 deletions
diff --git a/scripts/lib/mic/3rdparty/pykickstart/version.py b/scripts/lib/mic/3rdparty/pykickstart/version.py new file mode 100644 index 0000000000..102cc37d80 --- /dev/null +++ b/scripts/lib/mic/3rdparty/pykickstart/version.py | |||
| @@ -0,0 +1,197 @@ | |||
| 1 | # | ||
| 2 | # Chris Lumens <clumens@redhat.com> | ||
| 3 | # | ||
| 4 | # Copyright 2006, 2007, 2008, 2009, 2010 Red Hat, Inc. | ||
| 5 | # | ||
| 6 | # This copyrighted material is made available to anyone wishing to use, modify, | ||
| 7 | # copy, or redistribute it subject to the terms and conditions of the GNU | ||
| 8 | # General Public License v.2. This program is distributed in the hope that it | ||
| 9 | # will be useful, but WITHOUT ANY WARRANTY expressed or implied, including the | ||
| 10 | # implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| 11 | # See the GNU General Public License for more details. | ||
| 12 | # | ||
| 13 | # You should have received a copy of the GNU General Public License along with | ||
| 14 | # this program; if not, write to the Free Software Foundation, Inc., 51 | ||
| 15 | # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat | ||
| 16 | # trademarks that are incorporated in the source code or documentation are not | ||
| 17 | # subject to the GNU General Public License and may only be used or replicated | ||
| 18 | # with the express permission of Red Hat, Inc. | ||
| 19 | # | ||
| 20 | """ | ||
| 21 | Methods for working with kickstart versions. | ||
| 22 | |||
| 23 | This module defines several symbolic constants that specify kickstart syntax | ||
| 24 | versions. Each version corresponds roughly to one release of Red Hat Linux, | ||
| 25 | Red Hat Enterprise Linux, or Fedora Core as these are where most syntax | ||
| 26 | changes take place. | ||
| 27 | |||
| 28 | This module also exports several functions: | ||
| 29 | |||
| 30 | makeVersion - Given a version number, return an instance of the | ||
| 31 | matching handler class. | ||
| 32 | |||
| 33 | returnClassForVersion - Given a version number, return the matching | ||
| 34 | handler class. This does not return an | ||
| 35 | instance of that class, however. | ||
| 36 | |||
| 37 | stringToVersion - Convert a string representation of a version number | ||
| 38 | into the symbolic constant. | ||
| 39 | |||
| 40 | versionToString - Perform the reverse mapping. | ||
| 41 | |||
| 42 | versionFromFile - Read a kickstart file and determine the version of | ||
| 43 | syntax it uses. This requires the kickstart file to | ||
| 44 | have a version= comment in it. | ||
| 45 | """ | ||
| 46 | import imputil, re, sys | ||
| 47 | from urlgrabber import urlopen | ||
| 48 | |||
| 49 | import gettext | ||
| 50 | _ = lambda x: gettext.ldgettext("pykickstart", x) | ||
| 51 | |||
| 52 | from pykickstart.errors import KickstartVersionError | ||
| 53 | |||
| 54 | # Symbolic names for internal version numbers. | ||
| 55 | RHEL3 = 900 | ||
| 56 | FC3 = 1000 | ||
| 57 | RHEL4 = 1100 | ||
| 58 | FC4 = 2000 | ||
| 59 | FC5 = 3000 | ||
| 60 | FC6 = 4000 | ||
| 61 | RHEL5 = 4100 | ||
| 62 | F7 = 5000 | ||
| 63 | F8 = 6000 | ||
| 64 | F9 = 7000 | ||
| 65 | F10 = 8000 | ||
| 66 | F11 = 9000 | ||
| 67 | F12 = 10000 | ||
| 68 | F13 = 11000 | ||
| 69 | RHEL6 = 11100 | ||
| 70 | F14 = 12000 | ||
| 71 | F15 = 13000 | ||
| 72 | F16 = 14000 | ||
| 73 | |||
| 74 | # This always points at the latest version and is the default. | ||
| 75 | DEVEL = F16 | ||
| 76 | |||
| 77 | # A one-to-one mapping from string representations to version numbers. | ||
| 78 | versionMap = { | ||
| 79 | "DEVEL": DEVEL, | ||
| 80 | "FC3": FC3, "FC4": FC4, "FC5": FC5, "FC6": FC6, "F7": F7, "F8": F8, | ||
| 81 | "F9": F9, "F10": F10, "F11": F11, "F12": F12, "F13": F13, | ||
| 82 | "F14": F14, "F15": F15, "F16": F16, | ||
| 83 | "RHEL3": RHEL3, "RHEL4": RHEL4, "RHEL5": RHEL5, "RHEL6": RHEL6 | ||
| 84 | } | ||
| 85 | |||
| 86 | def stringToVersion(s): | ||
| 87 | """Convert string into one of the provided version constants. Raises | ||
| 88 | KickstartVersionError if string does not match anything. | ||
| 89 | """ | ||
| 90 | # First try these short forms. | ||
| 91 | try: | ||
| 92 | return versionMap[s.upper()] | ||
| 93 | except KeyError: | ||
| 94 | pass | ||
| 95 | |||
| 96 | # Now try the Fedora versions. | ||
| 97 | m = re.match("^fedora.* (\d+)$", s, re.I) | ||
| 98 | |||
| 99 | if m and m.group(1): | ||
| 100 | if versionMap.has_key("FC" + m.group(1)): | ||
| 101 | return versionMap["FC" + m.group(1)] | ||
| 102 | elif versionMap.has_key("F" + m.group(1)): | ||
| 103 | return versionMap["F" + m.group(1)] | ||
| 104 | else: | ||
| 105 | raise KickstartVersionError(_("Unsupported version specified: %s") % s) | ||
| 106 | |||
| 107 | # Now try the RHEL versions. | ||
| 108 | m = re.match("^red hat enterprise linux.* (\d+)([\.\d]*)$", s, re.I) | ||
| 109 | |||
| 110 | if m and m.group(1): | ||
| 111 | if versionMap.has_key("RHEL" + m.group(1)): | ||
| 112 | return versionMap["RHEL" + m.group(1)] | ||
| 113 | else: | ||
| 114 | raise KickstartVersionError(_("Unsupported version specified: %s") % s) | ||
| 115 | |||
| 116 | # If nothing else worked, we're out of options. | ||
| 117 | raise KickstartVersionError(_("Unsupported version specified: %s") % s) | ||
| 118 | |||
| 119 | def versionToString(version, skipDevel=False): | ||
| 120 | """Convert version into a string representation of the version number. | ||
| 121 | This is the reverse operation of stringToVersion. Raises | ||
| 122 | KickstartVersionError if version does not match anything. | ||
| 123 | """ | ||
| 124 | if not skipDevel and version == versionMap["DEVEL"]: | ||
| 125 | return "DEVEL" | ||
| 126 | |||
| 127 | for (key, val) in versionMap.iteritems(): | ||
| 128 | if key == "DEVEL": | ||
| 129 | continue | ||
| 130 | elif val == version: | ||
| 131 | return key | ||
| 132 | |||
| 133 | raise KickstartVersionError(_("Unsupported version specified: %s") % version) | ||
| 134 | |||
| 135 | def versionFromFile(f): | ||
| 136 | """Given a file or URL, look for a line starting with #version= and | ||
| 137 | return the version number. If no version is found, return DEVEL. | ||
| 138 | """ | ||
| 139 | v = DEVEL | ||
| 140 | |||
| 141 | fh = urlopen(f) | ||
| 142 | |||
| 143 | while True: | ||
| 144 | try: | ||
| 145 | l = fh.readline() | ||
| 146 | except StopIteration: | ||
| 147 | break | ||
| 148 | |||
| 149 | # At the end of the file? | ||
| 150 | if l == "": | ||
| 151 | break | ||
| 152 | |||
| 153 | if l.isspace() or l.strip() == "": | ||
| 154 | continue | ||
| 155 | |||
| 156 | if l[:9] == "#version=": | ||
| 157 | v = stringToVersion(l[9:].rstrip()) | ||
| 158 | break | ||
| 159 | |||
| 160 | fh.close() | ||
| 161 | return v | ||
| 162 | |||
| 163 | def returnClassForVersion(version=DEVEL): | ||
| 164 | """Return the class of the syntax handler for version. version can be | ||
| 165 | either a string or the matching constant. Raises KickstartValueError | ||
| 166 | if version does not match anything. | ||
| 167 | """ | ||
| 168 | try: | ||
| 169 | version = int(version) | ||
| 170 | module = "%s" % versionToString(version, skipDevel=True) | ||
| 171 | except ValueError: | ||
| 172 | module = "%s" % version | ||
| 173 | version = stringToVersion(version) | ||
| 174 | |||
| 175 | module = module.lower() | ||
| 176 | |||
| 177 | try: | ||
| 178 | import pykickstart.handlers | ||
| 179 | sys.path.extend(pykickstart.handlers.__path__) | ||
| 180 | found = imputil.imp.find_module(module) | ||
| 181 | loaded = imputil.imp.load_module(module, found[0], found[1], found[2]) | ||
| 182 | |||
| 183 | for (k, v) in loaded.__dict__.iteritems(): | ||
| 184 | if k.lower().endswith("%shandler" % module): | ||
| 185 | return v | ||
| 186 | except: | ||
| 187 | raise KickstartVersionError(_("Unsupported version specified: %s") % version) | ||
| 188 | |||
| 189 | def makeVersion(version=DEVEL): | ||
| 190 | """Return a new instance of the syntax handler for version. version can be | ||
| 191 | either a string or the matching constant. This function is useful for | ||
| 192 | standalone programs which just need to handle a specific version of | ||
| 193 | kickstart syntax (as provided by a command line argument, for example) | ||
| 194 | and need to instantiate the correct object. | ||
| 195 | """ | ||
| 196 | cl = returnClassForVersion(version) | ||
| 197 | return cl() | ||
