Add new RHEL7 logvol objects to master
[pykickstart.git] / pykickstart / version.py
blob85562d4fcd19198ffb1d5e3a3e20ff2f665de8d6
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.
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
78 F21 = 19000
79 F22 = 20000
81 # This always points at the latest version and is the default.
82 DEVEL = F22
84 # A one-to-one mapping from string representations to version numbers.
85 versionMap = {
86 "DEVEL": DEVEL,
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,
92 "RHEL7": RHEL7
95 def stringToVersion(s):
96 """Convert string into one of the provided version constants. Raises
97 KickstartVersionError if string does not match anything.
98 """
99 # First try these short forms.
100 try:
101 return versionMap[s.upper()]
102 except KeyError:
103 pass
105 # Now try the Fedora versions.
106 m = re.match(r"^fedora.* (\d+)$", s, re.I)
108 if m and m.group(1):
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)]
113 else:
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)
119 if m and m.group(1):
120 if "RHEL" + m.group(1) in versionMap:
121 return versionMap["RHEL" + m.group(1)]
122 else:
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"]:
134 return "DEVEL"
136 for (key, val) in list(versionMap.items()):
137 if key == "DEVEL":
138 continue
139 elif val == version:
140 return key
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.
148 v = DEVEL
150 fh = urlopen(f)
152 while True:
153 try:
154 l = fh.readline()
155 except StopIteration:
156 break
158 # At the end of the file?
159 if l == "":
160 break
162 if l.isspace() or l.strip() == "":
163 continue
165 if l[:9] == "#version=":
166 v = stringToVersion(l[9:].rstrip())
167 break
169 fh.close()
170 return v
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.
177 try:
178 version = int(version)
179 module = "%s" % versionToString(version, skipDevel=True)
180 except ValueError:
181 module = "%s" % version
182 version = stringToVersion(version)
184 module = module.lower()
186 try:
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):
194 return v
195 except:
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)
206 return cl()