Validate network interface name when parsing the kickstart (#1081982)
[pykickstart.git] / pykickstart / version.py
blob9b731022e5de00806c816e1375b8eb286b3cdd3e
2 # Chris Lumens <clumens@redhat.com>
4 # Copyright 2006, 2007, 2008, 2009, 2010, 2012, 2013 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.
20 """
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
26 changes take place.
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.
45 """
46 import imputil, re, sys
47 from urlgrabber import urlopen
49 import gettext
50 _ = lambda x: gettext.ldgettext("pykickstart", x)
52 from pykickstart.errors import KickstartVersionError
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 F17 = 15000
74 F18 = 16000
75 F19 = 17000
76 RHEL7 = 17100
77 F20 = 18000
79 # This always points at the latest version and is the default.
80 DEVEL = F20
82 # A one-to-one mapping from string representations to version numbers.
83 versionMap = {
84 "DEVEL": DEVEL,
85 "FC3": FC3, "FC4": FC4, "FC5": FC5, "FC6": FC6, "F7": F7, "F8": F8,
86 "F9": F9, "F10": F10, "F11": F11, "F12": F12, "F13": F13,
87 "F14": F14, "F15": F15, "F16": F16, "F17": F17, "F18": F18,
88 "F19": F19, "F20": F20,
89 "RHEL3": RHEL3, "RHEL4": RHEL4, "RHEL5": RHEL5, "RHEL6": RHEL6,
90 "RHEL7": RHEL7
93 def stringToVersion(s):
94 """Convert string into one of the provided version constants. Raises
95 KickstartVersionError if string does not match anything.
96 """
97 # First try these short forms.
98 try:
99 return versionMap[s.upper()]
100 except KeyError:
101 pass
103 # Now try the Fedora versions.
104 m = re.match(r"^fedora.* (\d+)$", s, re.I)
106 if m and m.group(1):
107 if versionMap.has_key("FC" + m.group(1)):
108 return versionMap["FC" + m.group(1)]
109 elif versionMap.has_key("F" + m.group(1)):
110 return versionMap["F" + m.group(1)]
111 else:
112 raise KickstartVersionError(_("Unsupported version specified: %s") % s)
114 # Now try the RHEL versions.
115 m = re.match(r"^red hat enterprise linux.* (\d+)([\.\d]*)$", s, re.I)
117 if m and m.group(1):
118 if versionMap.has_key("RHEL" + m.group(1)):
119 return versionMap["RHEL" + m.group(1)]
120 else:
121 raise KickstartVersionError(_("Unsupported version specified: %s") % s)
123 # If nothing else worked, we're out of options.
124 raise KickstartVersionError(_("Unsupported version specified: %s") % s)
126 def versionToString(version, skipDevel=False):
127 """Convert version into a string representation of the version number.
128 This is the reverse operation of stringToVersion. Raises
129 KickstartVersionError if version does not match anything.
131 if not skipDevel and version == versionMap["DEVEL"]:
132 return "DEVEL"
134 for (key, val) in versionMap.iteritems():
135 if key == "DEVEL":
136 continue
137 elif val == version:
138 return key
140 raise KickstartVersionError(_("Unsupported version specified: %s") % version)
142 def versionFromFile(f):
143 """Given a file or URL, look for a line starting with #version= and
144 return the version number. If no version is found, return DEVEL.
146 v = DEVEL
148 fh = urlopen(f)
150 while True:
151 try:
152 l = fh.readline()
153 except StopIteration:
154 break
156 # At the end of the file?
157 if l == "":
158 break
160 if l.isspace() or l.strip() == "":
161 continue
163 if l[:9] == "#version=":
164 v = stringToVersion(l[9:].rstrip())
165 break
167 fh.close()
168 return v
170 def returnClassForVersion(version=DEVEL):
171 """Return the class of the syntax handler for version. version can be
172 either a string or the matching constant. Raises KickstartValueError
173 if version does not match anything.
175 try:
176 version = int(version)
177 module = "%s" % versionToString(version, skipDevel=True)
178 except ValueError:
179 module = "%s" % version
180 version = stringToVersion(version)
182 module = module.lower()
184 try:
185 import pykickstart.handlers
186 sys.path.extend(pykickstart.handlers.__path__)
187 found = imputil.imp.find_module(module)
188 loaded = imputil.imp.load_module(module, found[0], found[1], found[2])
190 for (k, v) in loaded.__dict__.iteritems():
191 if k.lower().endswith("%shandler" % module):
192 return v
193 except:
194 raise KickstartVersionError(_("Unsupported version specified: %s") % version)
196 def makeVersion(version=DEVEL):
197 """Return a new instance of the syntax handler for version. version can be
198 either a string or the matching constant. This function is useful for
199 standalone programs which just need to handle a specific version of
200 kickstart syntax (as provided by a command line argument, for example)
201 and need to instantiate the correct object.
203 cl = returnClassForVersion(version)
204 return cl()