diff options
author | Zac Medico <zmedico@gentoo.org> | 2010-02-25 07:33:32 +0000 |
---|---|---|
committer | Zac Medico <zmedico@gentoo.org> | 2010-02-25 07:33:32 +0000 |
commit | 6cbca9379b83273a3f4999b445d7031e58081a83 (patch) | |
tree | 34696b6a0bf62ffe4bef0a0071a11efc752ecf6b /pym/portage/util/ExtractKernelVersion.py | |
parent | e3c8fdd6e9b5bcb39163e5b8c0413bd85ef35753 (diff) | |
download | portage-6cbca9379b83273a3f4999b445d7031e58081a83.tar.gz portage-6cbca9379b83273a3f4999b445d7031e58081a83.tar.bz2 portage-6cbca9379b83273a3f4999b445d7031e58081a83.zip |
Move ExtractKernelVersion portage.util.ExtractKernelVersion.
svn path=/main/trunk/; revision=15453
Diffstat (limited to 'pym/portage/util/ExtractKernelVersion.py')
-rw-r--r-- | pym/portage/util/ExtractKernelVersion.py | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/pym/portage/util/ExtractKernelVersion.py b/pym/portage/util/ExtractKernelVersion.py new file mode 100644 index 000000000..9e6433b28 --- /dev/null +++ b/pym/portage/util/ExtractKernelVersion.py @@ -0,0 +1,77 @@ +# Copyright 2010 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Id$ + +__all__ = ['ExtractKernelVersion'] + +import codecs + +from portage import os, _encodings, _unicode_encode +from portage.util import getconfig, grabfile + +def ExtractKernelVersion(base_dir): + """ + Try to figure out what kernel version we are running + @param base_dir: Path to sources (usually /usr/src/linux) + @type base_dir: string + @rtype: tuple( version[string], error[string]) + @returns: + 1. tuple( version[string], error[string]) + Either version or error is populated (but never both) + + """ + lines = [] + pathname = os.path.join(base_dir, 'Makefile') + try: + f = codecs.open(_unicode_encode(pathname, + encoding=_encodings['fs'], errors='strict'), mode='r', + encoding=_encodings['content'], errors='replace') + except OSError as details: + return (None, str(details)) + except IOError as details: + return (None, str(details)) + + try: + for i in range(4): + lines.append(f.readline()) + except OSError as details: + return (None, str(details)) + except IOError as details: + return (None, str(details)) + + lines = [l.strip() for l in lines] + + version = '' + + #XXX: The following code relies on the ordering of vars within the Makefile + for line in lines: + # split on the '=' then remove annoying whitespace + items = line.split("=") + items = [i.strip() for i in items] + if items[0] == 'VERSION' or \ + items[0] == 'PATCHLEVEL': + version += items[1] + version += "." + elif items[0] == 'SUBLEVEL': + version += items[1] + elif items[0] == 'EXTRAVERSION' and \ + items[-1] != items[0]: + version += items[1] + + # Grab a list of files named localversion* and sort them + localversions = os.listdir(base_dir) + for x in range(len(localversions)-1,-1,-1): + if localversions[x][:12] != "localversion": + del localversions[x] + localversions.sort() + + # Append the contents of each to the version string, stripping ALL whitespace + for lv in localversions: + version += "".join( " ".join( grabfile( base_dir+ "/" + lv ) ).split() ) + + # Check the .config for a CONFIG_LOCALVERSION and append that too, also stripping whitespace + kernelconfig = getconfig(base_dir+"/.config") + if kernelconfig and "CONFIG_LOCALVERSION" in kernelconfig: + version += "".join(kernelconfig["CONFIG_LOCALVERSION"].split()) + + return (version,None) |