2 # Chris Lumens <clumens@redhat.com>
4 # Copyright 2006-2014 Red Hat, Inc.
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.
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.
21 Methods for working with kickstart versions.
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
28 This module also exports several functions:
30 makeVersion - Given a version number, return an instance of the
31 matching handler class.
33 returnClassForVersion - Given a version number, return the matching
34 handler class. This does not return an
35 instance of that class, however.
37 stringToVersion - Convert a string representation of a version number
38 into the symbolic constant.
40 versionToString - Perform the reverse mapping.
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.
46 import imputil
, re
, sys
47 from urlgrabber
import urlopen
50 _
= lambda x
: gettext
.ldgettext("pykickstart", x
)
52 from pykickstart
.errors
import KickstartVersionError
54 # Symbolic names for internal version numbers.
81 # This always points at the latest version and is the default.
84 # A one-to-one mapping from string representations to version numbers.
87 "FC3": FC3
, "FC4": FC4
, "FC5": FC5
, "FC6": FC6
, "F7": F7
, "F8": F8
,
88 "F9": F9
, "F10": F10
, "F11": F11
, "F12": F12
, "F13": F13
,
89 "F14": F14
, "F15": F15
, "F16": F16
, "F17": F17
, "F18": F18
,
90 "F19": F19
, "F20": F20
, "F21": F21
, "F22": F22
,
91 "RHEL3": RHEL3
, "RHEL4": RHEL4
, "RHEL5": RHEL5
, "RHEL6": RHEL6
,
95 def stringToVersion(s
):
96 """Convert string into one of the provided version constants. Raises
97 KickstartVersionError if string does not match anything.
99 # First try these short forms.
101 return versionMap
[s
.upper()]
105 # Now try the Fedora versions.
106 m
= re
.match(r
"^fedora.* (\d+)$", s
, re
.I
)
109 if "FC" + m
.group(1) in versionMap
:
110 return versionMap
["FC" + m
.group(1)]
111 elif "F" + m
.group(1) in versionMap
:
112 return versionMap
["F" + m
.group(1)]
114 raise KickstartVersionError(_("Unsupported version specified: %s") % s
)
116 # Now try the RHEL versions.
117 m
= re
.match(r
"^red hat enterprise linux.* (\d+)([\.\d]*)$", s
, re
.I
)
120 if "RHEL" + m
.group(1) in versionMap
:
121 return versionMap
["RHEL" + m
.group(1)]
123 raise KickstartVersionError(_("Unsupported version specified: %s") % s
)
125 # If nothing else worked, we're out of options.
126 raise KickstartVersionError(_("Unsupported version specified: %s") % s
)
128 def versionToString(version
, skipDevel
=False):
129 """Convert version into a string representation of the version number.
130 This is the reverse operation of stringToVersion. Raises
131 KickstartVersionError if version does not match anything.
133 if not skipDevel
and version
== versionMap
["DEVEL"]:
136 for (key
, val
) in list(versionMap
.items()):
142 raise KickstartVersionError(_("Unsupported version specified: %s") % version
)
144 def versionFromFile(f
):
145 """Given a file or URL, look for a line starting with #version= and
146 return the version number. If no version is found, return DEVEL.
155 except StopIteration:
158 # At the end of the file?
162 if l
.isspace() or l
.strip() == "":
165 if l
[:9] == "#version=":
166 v
= stringToVersion(l
[9:].rstrip())
172 def returnClassForVersion(version
=DEVEL
):
173 """Return the class of the syntax handler for version. version can be
174 either a string or the matching constant. Raises KickstartValueError
175 if version does not match anything.
178 version
= int(version
)
179 module
= "%s" % versionToString(version
, skipDevel
=True)
181 module
= "%s" % version
182 version
= stringToVersion(version
)
184 module
= module
.lower()
187 import pykickstart
.handlers
188 sys
.path
.extend(pykickstart
.handlers
.__path
__)
189 found
= imputil
.imp
.find_module(module
)
190 loaded
= imputil
.imp
.load_module(module
, found
[0], found
[1], found
[2])
192 for (k
, v
) in list(loaded
.__dict
__.items()):
193 if k
.lower().endswith("%shandler" % module
):
196 raise KickstartVersionError(_("Unsupported version specified: %s") % version
)
198 def makeVersion(version
=DEVEL
):
199 """Return a new instance of the syntax handler for version. version can be
200 either a string or the matching constant. This function is useful for
201 standalone programs which just need to handle a specific version of
202 kickstart syntax (as provided by a command line argument, for example)
203 and need to instantiate the correct object.
205 cl
= returnClassForVersion(version
)