New version.
[pykickstart.git] / pykickstart / version.py
blob06fd8319c9965d5625b2a797c8c9115a18aac31b
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 from rhpl.translate import _
50 from pykickstart.errors import KickstartVersionError
52 # Symbolic names for internal version numbers.
53 RHEL3 = 900
54 FC3 = 1000
55 RHEL4 = 1100
56 FC4 = 2000
57 FC5 = 3000
58 FC6 = 4000
59 RHEL5 = 4100
60 F7 = 5000
61 F8 = 6000
62 F9 = 7000
63 F10 = 8000
65 # This always points at the latest version and is the default.
66 DEVEL = F10
68 """A one-to-one mapping from string representations to version numbers."""
69 versionMap = {
70 "DEVEL": DEVEL,
71 "FC3": FC3, "FC4": FC4, "FC5": FC5, "FC6": FC6, "F7": F7, "F8": F8,
72 "F9": F9, "F10": F10,
73 "RHEL3": RHEL3, "RHEL4": RHEL4, "RHEL5": RHEL5
76 def stringToVersion(s):
77 """Convert string into one of the provided version constants. Raises
78 KickstartVersionError if string does not match anything.
79 """
80 # First try these short forms.
81 try:
82 return versionMap[s.upper()]
83 except KeyError:
84 pass
86 # Now try the Fedora versions.
87 m = re.match("^fedora.* (\d)+$", s, re.I)
89 if m and m.group(1):
90 if versionMap.has_key("FC" + m.group(1)):
91 return versionMap["FC" + m.group(1)]
92 elif versionMap.has_key("F" + m.group(1)):
93 return versionMap["F" + m.group(1)]
94 else:
95 raise KickstartVersionError(_("Unsupported version specified: %s") % s)
97 # Now try the RHEL versions.
98 m = re.match("^red hat enterprise linux.* (\d)+$", s, re.I)
100 if m and m.group(1):
101 if versionMap.has_key("RHEL" + m.group(1)):
102 return versionMap["RHEL" + m.group(1)]
103 else:
104 raise KickstartVersionError(_("Unsupported version specified: %s") % s)
106 # If nothing else worked, we're out of options.
107 raise KickstartVersionError(_("Unsupported version specified: %s") % s)
109 def versionToString(version, skipDevel=False):
110 """Convert version into a string representation of the version number.
111 This is the reverse operation of stringToVersion. Raises
112 KickstartVersionError if version does not match anything.
114 for (key, val) in versionMap.iteritems():
115 if version == DEVEL and key == "DEVEL" and skipDevel:
116 continue
118 if val == version:
119 return key
121 raise KickstartVersionError(_("Unsupported version specified: %s") % version)
123 def versionFromFile(f):
124 """Given a file or URL, look for a line starting with #version= and
125 return the version number. If no version is found, return DEVEL.
127 v = DEVEL
129 fh = urlopen(f)
131 while True:
132 try:
133 l = fh.readline()
134 except StopIteration:
135 break
137 # At the end of the file?
138 if l == "":
139 break
141 if l.isspace() or l.strip() == "":
142 continue
144 if l[:9] == "#version=":
145 v = stringToVersion(l[9:].rstrip())
146 break
148 fh.close()
149 return v
151 def returnClassForVersion(version=DEVEL):
152 """Return the class of the syntax handler for version. version can be
153 either a string or the matching constant. Raises KickstartValueError
154 if version does not match anything.
156 try:
157 version = int(version)
158 module = "%s" % versionToString(version, skipDevel=True)
159 except ValueError:
160 module = "%s" % version
161 version = stringToVersion(version)
163 module = module.lower()
165 try:
166 import pykickstart.handlers
167 sys.path.extend(pykickstart.handlers.__path__)
168 found = imputil.imp.find_module(module)
169 loaded = imputil.imp.load_module(module, found[0], found[1], found[2])
171 for (k, v) in loaded.__dict__.iteritems():
172 if k.find("Handler") != -1:
173 return v
174 except:
175 raise KickstartVersionError(_("Unsupported version specified: %s") % version)
177 def makeVersion(version=DEVEL):
178 """Return a new instance of the syntax handler for version. version can be
179 either a string or the matching constant. This function is useful for
180 standalone programs which just need to handle a specific version of
181 kickstart syntax (as provided by a command line argument, for example)
182 and need to instantiate the correct object.
184 cl = returnClassForVersion(version)
185 return cl()