Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | |
| 48 | import gettext |
| 49 | _ = lambda x: gettext.ldgettext("pykickstart", x) |
| 50 | |
| 51 | from pykickstart.errors import KickstartVersionError |
| 52 | |
| 53 | # Symbolic names for internal version numbers. |
| 54 | RHEL3 = 900 |
| 55 | FC3 = 1000 |
| 56 | RHEL4 = 1100 |
| 57 | FC4 = 2000 |
| 58 | FC5 = 3000 |
| 59 | FC6 = 4000 |
| 60 | RHEL5 = 4100 |
| 61 | F7 = 5000 |
| 62 | F8 = 6000 |
| 63 | F9 = 7000 |
| 64 | F10 = 8000 |
| 65 | F11 = 9000 |
| 66 | F12 = 10000 |
| 67 | F13 = 11000 |
| 68 | RHEL6 = 11100 |
| 69 | F14 = 12000 |
| 70 | F15 = 13000 |
| 71 | F16 = 14000 |
| 72 | |
| 73 | # This always points at the latest version and is the default. |
| 74 | DEVEL = F16 |
| 75 | |
| 76 | # A one-to-one mapping from string representations to version numbers. |
| 77 | versionMap = { |
| 78 | "DEVEL": DEVEL, |
| 79 | "FC3": FC3, "FC4": FC4, "FC5": FC5, "FC6": FC6, "F7": F7, "F8": F8, |
| 80 | "F9": F9, "F10": F10, "F11": F11, "F12": F12, "F13": F13, |
| 81 | "F14": F14, "F15": F15, "F16": F16, |
| 82 | "RHEL3": RHEL3, "RHEL4": RHEL4, "RHEL5": RHEL5, "RHEL6": RHEL6 |
| 83 | } |
| 84 | |
| 85 | def stringToVersion(s): |
| 86 | """Convert string into one of the provided version constants. Raises |
| 87 | KickstartVersionError if string does not match anything. |
| 88 | """ |
| 89 | # First try these short forms. |
| 90 | try: |
| 91 | return versionMap[s.upper()] |
| 92 | except KeyError: |
| 93 | pass |
| 94 | |
| 95 | # Now try the Fedora versions. |
| 96 | m = re.match("^fedora.* (\d+)$", s, re.I) |
| 97 | |
| 98 | if m and m.group(1): |
| 99 | if versionMap.has_key("FC" + m.group(1)): |
| 100 | return versionMap["FC" + m.group(1)] |
| 101 | elif versionMap.has_key("F" + m.group(1)): |
| 102 | return versionMap["F" + m.group(1)] |
| 103 | else: |
| 104 | raise KickstartVersionError(_("Unsupported version specified: %s") % s) |
| 105 | |
| 106 | # Now try the RHEL versions. |
| 107 | m = re.match("^red hat enterprise linux.* (\d+)([\.\d]*)$", s, re.I) |
| 108 | |
| 109 | if m and m.group(1): |
| 110 | if versionMap.has_key("RHEL" + m.group(1)): |
| 111 | return versionMap["RHEL" + m.group(1)] |
| 112 | else: |
| 113 | raise KickstartVersionError(_("Unsupported version specified: %s") % s) |
| 114 | |
| 115 | # If nothing else worked, we're out of options. |
| 116 | raise KickstartVersionError(_("Unsupported version specified: %s") % s) |
| 117 | |
| 118 | def versionToString(version, skipDevel=False): |
| 119 | """Convert version into a string representation of the version number. |
| 120 | This is the reverse operation of stringToVersion. Raises |
| 121 | KickstartVersionError if version does not match anything. |
| 122 | """ |
| 123 | if not skipDevel and version == versionMap["DEVEL"]: |
| 124 | return "DEVEL" |
| 125 | |
| 126 | for (key, val) in versionMap.iteritems(): |
| 127 | if key == "DEVEL": |
| 128 | continue |
| 129 | elif val == version: |
| 130 | return key |
| 131 | |
| 132 | raise KickstartVersionError(_("Unsupported version specified: %s") % version) |
| 133 | |
| 134 | def returnClassForVersion(version=DEVEL): |
| 135 | """Return the class of the syntax handler for version. version can be |
| 136 | either a string or the matching constant. Raises KickstartValueError |
| 137 | if version does not match anything. |
| 138 | """ |
| 139 | try: |
| 140 | version = int(version) |
| 141 | module = "%s" % versionToString(version, skipDevel=True) |
| 142 | except ValueError: |
| 143 | module = "%s" % version |
| 144 | version = stringToVersion(version) |
| 145 | |
| 146 | module = module.lower() |
| 147 | |
| 148 | try: |
| 149 | import pykickstart.handlers |
| 150 | sys.path.extend(pykickstart.handlers.__path__) |
| 151 | found = imputil.imp.find_module(module) |
| 152 | loaded = imputil.imp.load_module(module, found[0], found[1], found[2]) |
| 153 | |
| 154 | for (k, v) in loaded.__dict__.iteritems(): |
| 155 | if k.lower().endswith("%shandler" % module): |
| 156 | return v |
| 157 | except: |
| 158 | raise KickstartVersionError(_("Unsupported version specified: %s") % version) |
| 159 | |
| 160 | def makeVersion(version=DEVEL): |
| 161 | """Return a new instance of the syntax handler for version. version can be |
| 162 | either a string or the matching constant. This function is useful for |
| 163 | standalone programs which just need to handle a specific version of |
| 164 | kickstart syntax (as provided by a command line argument, for example) |
| 165 | and need to instantiate the correct object. |
| 166 | """ |
| 167 | cl = returnClassForVersion(version) |
| 168 | return cl() |