New version.
[pykickstart.git] / pykickstart / version.py
blob14e531a7ac04f81f4356aa88d5e86612e8151054
2 # Chris Lumens <clumens@redhat.com>
4 # Copyright 2006, 2007, 2008 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
67 # This always points at the latest version and is the default.
68 DEVEL = F10
70 """A one-to-one mapping from string representations to version numbers."""
71 versionMap = {
72 "DEVEL": DEVEL,
73 "FC3": FC3, "FC4": FC4, "FC5": FC5, "FC6": FC6, "F7": F7, "F8": F8,
74 "F9": F9, "F10": F10,
75 "RHEL3": RHEL3, "RHEL4": RHEL4, "RHEL5": RHEL5
78 def stringToVersion(s):
79 """Convert string into one of the provided version constants. Raises
80 KickstartVersionError if string does not match anything.
81 """
82 # First try these short forms.
83 try:
84 return versionMap[s.upper()]
85 except KeyError:
86 pass
88 # Now try the Fedora versions.
89 m = re.match("^fedora.* (\d)+$", s, re.I)
91 if m and m.group(1):
92 if versionMap.has_key("FC" + m.group(1)):
93 return versionMap["FC" + m.group(1)]
94 elif versionMap.has_key("F" + m.group(1)):
95 return versionMap["F" + m.group(1)]
96 else:
97 raise KickstartVersionError(_("Unsupported version specified: %s") % s)
99 # Now try the RHEL versions.
100 m = re.match("^red hat enterprise linux.* (\d)+$", s, re.I)
102 if m and m.group(1):
103 if versionMap.has_key("RHEL" + m.group(1)):
104 return versionMap["RHEL" + m.group(1)]
105 else:
106 raise KickstartVersionError(_("Unsupported version specified: %s") % s)
108 # If nothing else worked, we're out of options.
109 raise KickstartVersionError(_("Unsupported version specified: %s") % s)
111 def versionToString(version, skipDevel=False):
112 """Convert version into a string representation of the version number.
113 This is the reverse operation of stringToVersion. Raises
114 KickstartVersionError if version does not match anything.
116 for (key, val) in versionMap.iteritems():
117 if version == DEVEL and key == "DEVEL" and skipDevel:
118 continue
120 if val == version:
121 return key
123 raise KickstartVersionError(_("Unsupported version specified: %s") % version)
125 def versionFromFile(f):
126 """Given a file or URL, look for a line starting with #version= and
127 return the version number. If no version is found, return DEVEL.
129 v = DEVEL
131 fh = urlopen(f)
133 while True:
134 try:
135 l = fh.readline()
136 except StopIteration:
137 break
139 # At the end of the file?
140 if l == "":
141 break
143 if l.isspace() or l.strip() == "":
144 continue
146 if l[:9] == "#version=":
147 v = stringToVersion(l[9:].rstrip())
148 break
150 fh.close()
151 return v
153 def returnClassForVersion(version=DEVEL):
154 """Return the class of the syntax handler for version. version can be
155 either a string or the matching constant. Raises KickstartValueError
156 if version does not match anything.
158 try:
159 version = int(version)
160 module = "%s" % versionToString(version, skipDevel=True)
161 except ValueError:
162 module = "%s" % version
163 version = stringToVersion(version)
165 module = module.lower()
167 try:
168 import pykickstart.handlers
169 sys.path.extend(pykickstart.handlers.__path__)
170 found = imputil.imp.find_module(module)
171 loaded = imputil.imp.load_module(module, found[0], found[1], found[2])
173 for (k, v) in loaded.__dict__.iteritems():
174 if k.lower().endswith("%shandler" % module):
175 return v
176 except:
177 raise KickstartVersionError(_("Unsupported version specified: %s") % version)
179 def makeVersion(version=DEVEL):
180 """Return a new instance of the syntax handler for version. version can be
181 either a string or the matching constant. This function is useful for
182 standalone programs which just need to handle a specific version of
183 kickstart syntax (as provided by a command line argument, for example)
184 and need to instantiate the correct object.
186 cl = returnClassForVersion(version)
187 return cl()