* pykickstart/commands/logvol.py (FC4_LogVol._setClassData): Fix
[pykickstart.git] / pykickstart / version.py
blob7fd9429c213176341179a52aeb31f4a770bea5e6
2 # Chris Lumens <clumens@redhat.com>
4 # Copyright 2006, 2007 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.
41 """
42 import re
44 from rhpl.translate import _
45 from pykickstart.errors import KickstartVersionError
47 # Symbolic names for internal version numbers.
48 RHEL3 = 900
49 FC3 = 1000
50 RHEL4 = 1100
51 FC4 = 2000
52 FC5 = 3000
53 FC6 = 4000
54 RHEL5 = 4100
55 F7 = 5000
56 F8 = 6000
57 F9 = 7000
59 # This always points at the latest version and is the default.
60 DEVEL = F9
62 """A one-to-one mapping from string representations to version numbers."""
63 versionMap = {
64 "DEVEL": DEVEL,
65 "FC3": FC3, "FC4": FC4, "FC5": FC5, "FC6": FC6, "F7": F7, "F8": F8,
66 "F9": F9,
67 "RHEL3": RHEL3, "RHEL4": RHEL4, "RHEL5": RHEL5
70 def stringToVersion(s):
71 """Convert string into one of the provided version constants. Raises
72 KickstartVersionError if string does not match anything.
73 """
74 # First try these short forms.
75 try:
76 return versionMap[s.upper()]
77 except KeyError:
78 pass
80 # Now try the Fedora versions.
81 m = re.match("^fedora.* (\d)+$", s, re.I)
83 if m and m.group(1):
84 if versionMap.has_key("FC" + m.group(1)):
85 return versionMap["FC" + m.group(1)]
86 elif versionMap.has_key("F" + m.group(1)):
87 return versionMap["F" + m.group(1)]
88 else:
89 raise KickstartVersionError(_("Unsupported version specified: %s") % s)
91 # Now try the RHEL versions.
92 m = re.match("^red hat enterprise linux.* (\d)+$", s, re.I)
94 if m and m.group(1):
95 if versionMap.has_key("RHEL" + m.group(1)):
96 return versionMap["RHEL" + m.group(1)]
97 else:
98 raise KickstartVersionError(_("Unsupported version specified: %s") % s)
100 # If nothing else worked, we're out of options.
101 raise KickstartVersionError(_("Unsupported version specified: %s") % s)
103 def versionToString(version):
104 """Convert version into a string representation of the version number.
105 This is the reverse operation of stringToVersion. Raises
106 KickstartVersionError if version does not match anything.
108 for (key, val) in versionMap.iteritems():
109 if val == version:
110 return key
112 raise KickstartVersionError(_("Unsupported version specified: %s") % version)
114 def returnClassForVersion(version=DEVEL):
115 """Return the class of the syntax handler for version. version can be
116 either a string or the matching constant. Raises KickstartValueError
117 if version does not match anything.
119 try:
120 version = int(version)
121 except ValueError:
122 version = stringToVersion(version)
124 if version == FC3:
125 from pykickstart.handlers.fc3 import FC3Handler
126 return FC3Handler
127 elif version == FC4:
128 from pykickstart.handlers.fc4 import FC4Handler
129 return FC4Handler
130 elif version == FC5:
131 from pykickstart.handlers.fc5 import FC5Handler
132 return FC5Handler
133 elif version == FC6:
134 from pykickstart.handlers.fc6 import FC6Handler
135 return FC6Handler
136 elif version == F7:
137 from pykickstart.handlers.f7 import F7Handler
138 return F7Handler
139 elif version == F8:
140 from pykickstart.handlers.f8 import F8Handler
141 return F8Handler
142 elif version == F9:
143 from pykickstart.handlers.f9 import F9Handler
144 return F9Handler
145 elif version == RHEL3:
146 from pykickstart.handlers.rhel3 import RHEL3Handler
147 return RHEL3Handler
148 elif version == RHEL4:
149 from pykickstart.handlers.rhel4 import RHEL4Handler
150 return RHEL4Handler
151 elif version == RHEL5:
152 from pykickstart.handlers.rhel5 import RHEL5Handler
153 return RHEL5Handler
154 else:
155 raise KickstartVersionError(_("Unsupported version specified: %s") % version)
157 def makeVersion(version=DEVEL):
158 """Return a new instance of the syntax handler for version. version can be
159 either a string or the matching constant. This function is useful for
160 standalone programs which just need to handle a specific version of
161 kickstart syntax (as provided by a command line argument, for example)
162 and need to instantiate the correct object.
164 cl = returnClassForVersion(version)
165 return cl()