You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
calculate-utils-3-lib/pym/calculate/lib/variables/linux.py

336 lines
12 KiB

#-*- coding: utf-8 -*-
# Copyright 2008-2013 Calculate Ltd. http://www.calculate-linux.org
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from os import path
import re
import platform
from calculate.lib.datavars import (Variable, SimpleDataVars, ReadonlyVariable,
VariableError)
from calculate.lib.utils.portage import isPkgInstalled, searchProfile, \
RepositorySubstituting
from calculate.lib.utils.files import readFile, pathJoin
from calculate.lib.variables import system
from calculate.lib.variables import env
from calculate.lib.configparser import ConfigParser
import env
class Linux:
dictLinuxName = {"CLD":"Calculate Linux Desktop",
"CLDX":"Calculate Linux Desktop",
"CLDG":"Calculate Linux Desktop",
"CDS":"Calculate Directory Server",
"CLS":"Calculate Linux Scratch",
"CSS":"Calculate Scratch Server",
"CMC":"Calculate Media Center",
"Gentoo":"Gentoo"}
dictLinuxSubName = {"CLD":"KDE", "CLDX":"XFCE", "CLDG":"GNOME"}
dictNameSystem = {'CDS':'server',
'CLD':'desktop',
'CLDG':'desktop',
'CLDX':'desktop',
'CLS':'desktop',
'CMC':'desktop',
'CSS':'server'}
def getFullNameByShort(self,shortname):
"""Get full distributive name by short name"""
subname = self.dictLinuxSubName.get(shortname,'')
if subname: subname = " %s"%subname
return "%s%s"%(self.dictLinuxName.get(shortname,''),subname)
def getShortnameByMakeprofile(self,systemroot):
"""Get shortname by symlink of make.profile"""
makeprofile = self.Get('cl_make_profile')
if path.exists(makeprofile):
link = os.readlink(makeprofile)
reMakeProfileLink = re.compile('/calculate/(desktop|server)/(%s)/'%
"|".join(self.dictLinuxName.keys()),re.S)
shortnameSearch = reMakeProfileLink.search(link)
if shortnameSearch:
return shortnameSearch.groups()[1]
return None
def getShortnameByIni(self,systemroot):
"""Get shortname by calculate.ini file"""
inifile = path.join(systemroot,'etc/calculate/calculate.ini')
if path.exists(inifile):
FD = open(inifile)
data = FD.readlines()
FD.close()
shortNameList = filter(lambda y:y,
map(lambda x:\
len(x.split("="))==2 and\
x.split("=")[0]=="calculate" and\
x.split("=")[1].strip(), data))
if shortNameList:
return shortNameList[0]
def detectOtherShortname(self,systemroot):
"""Detect other system. Now only Gentoo."""
gentooFile = path.join(systemroot, "etc/gentoo-release")
if path.exists(gentooFile):
return "Gentoo"
if all(map(lambda x: path.lexists(path.join(systemroot, x)),
['bin', 'var', 'lib', 'etc'])):
13 years ago
return "Linux"
return None
12 years ago
def getVersionFromOverlay(self,systemroot):
"""
Get version from overlay calculate-release
"""
overlayPath = 'var/lib/layman/calculate'
releaseFile = 'profiles/calculate-release'
return readFile(path.join(systemroot, overlayPath, releaseFile))
12 years ago
def getVersionFromMetapackage(self,systemroot,shortname):
"""Get version from meta package"""
for pkg in ("app-misc/calculate-meta", "app-misc/%s-meta" % shortname):
calcMeta = isPkgInstalled(pkg, systemroot)
13 years ago
if calcMeta:
return calcMeta[0]['PV']
return None
def getVersionFromCalculateIni(self, systemroot):
"""Get version from calculate ini"""
pathname = path.join(systemroot,
'etc/calculate/calculate.ini')
if path.exists(pathname):
FD = open(pathname)
data = FD.readlines()
FD.close()
verList = filter(lambda y:y,
map(lambda x:\
len(x.split("="))==2 and\
x.split("=")[0]=="linuxver" and\
x.split("=")[1].strip(), data))
if verList:
reVer=re.compile("^(\d+\.)*\d$",re.S)
reRes = filter(reVer.search,verList)
if reRes:
return reRes[0]
def getVersionFromGentooFiles(self,systemroot):
"""Get version from gentoo files"""
gentooFile = path.join(systemroot,"etc/gentoo-release")
reVer=re.compile("^(\d+\.)*\d+$",re.S)
if path.exists(gentooFile):
gentooLink = self.Get('cl_make_profile')
if path.islink(gentooLink):
vers = filter(reVer.search,
os.readlink(gentooLink).split('/'))
if vers:
return vers[-1]
def getVersionFromUname(self):
"""Get version from uname"""
reVer=re.search("^(\d+\.)*\d",platform.release(),re.S)
if reVer:
return reVer.group()
12 years ago
class VariableOsLinuxShortname(Variable,Linux):
"""
Short system name (Example:CLD)
"""
systemRoot = "/"
def get(self):
return self.getShortnameByMakeprofile(self.systemRoot) or \
self.getShortnameByIni(self.systemRoot) or \
self.detectOtherShortname(self.systemRoot) or \
"Linux"
12 years ago
class VariableOsLinuxName(Variable,Linux):
"""
Full system name
"""
source_variable = "os_linux_shortname"
def get(self):
linuxShortName = self.Get(self.source_variable)
return self.dictLinuxName.get(linuxShortName,"Linux")
12 years ago
class VariableOsLinuxSubname(Variable,Linux):
"""
Subname of linux (KDE, GNOME, XFCE and etc)
"""
source_variable = "os_linux_shortname"
def get(self):
linuxShortName = self.Get(self.source_variable)
return self.dictLinuxSubName.get(linuxShortName,"")
12 years ago
class VariableOsLinuxSystem(Variable,Linux):
"""
System of linux (desktop or server)
"""
source_variable = "os_linux_shortname"
def get(self):
shortName = self.Get(self.source_variable)
return self.dictNameSystem.get(shortName,"")
12 years ago
class VariableOsLinuxVer(Variable,Linux):
"""
Version of system (get by metapackage,calculate.ini,gentoo files or 0)
"""
systemRoot = "/"
def get(self):
"""Get system version"""
linuxShortName = self.Get("os_linux_shortname")
12 years ago
return self.getVersionFromOverlay(self.systemRoot) or \
self.getVersionFromMetapackage(self.systemRoot,
linuxShortName) or \
self.getVersionFromCalculateIni(self.systemRoot) or \
self.getVersionFromGentooFiles(self.systemRoot) or \
self.getVersionFromUname() or "0"
class VariableOsLinuxBuild(Variable,Linux):
"""
Build of system
"""
12 years ago
class VariableOsLinuxFilesnum(Variable,Linux):
systemRoot = "/"
def get(self):
"""Depricated variable which contains count files.
Get files count. Count files discard because it to long
operation. This value is already count by calculate-builder and
is placed in calculate3.env"""
return str(0)
#return str(countFiles(self.systemRoot))
12 years ago
class VariableOsLinuxFiles(Variable,Linux):
systemRoot = "/"
def get(self):
"""Get files count. Count files discard because it to long
operation. This value is already count by calculate-builder and
is placed in calculate.env"""
return self.Get('os_linux_filesnum')
#return str(countFiles(self.systemRoot))
class VariableOsLinuxPkglist(Variable):
type = "list"
def generate_shortnames(self, make_profile):
repos = RepositorySubstituting(self)
for fn in searchProfile(make_profile, "calculate.env",
repository_sub=repos):
config = ConfigParser(strict=False)
config.read(fn, encoding="utf-8")
value = SimpleDataVars.unserialize("string",
config.get('main', 'os_linux_shortname',raw=True, fallback=''))
yield value.encode('utf-8')
yield "base"
def get(self):
make_profile = self.Get('cl_make_profile')
if path.exists(make_profile):
return list(set(filter(None,
self.generate_shortnames(make_profile))))
else:
return []
class VariableClProfileSystem(ReadonlyVariable):
"""
Профиль системы (симлинк /etc/make.profile')
"""
def get(self):
try:
make_profile = self.Get('cl_make_profile')
return path.normpath(
path.join(path.dirname(make_profile),
os.readlink(make_profile)))
except:
raise VariableError(_("Failed to determine the system profile"))
class VariableClProfileName(ReadonlyVariable):
source_var = "cl_profile_system"
def get(self):
make_profile = self.Get(self.source_var)
distro, o, name = make_profile.partition('/profiles/')
if not o:
return make_profile
distro = path.basename(distro)
return "%s:%s" % (distro, name)
class LinuxDataVars(SimpleDataVars):
class VariableOsArchMachine(ReadonlyVariable):
systemRoot = '/'
def get(self):
if path.exists(path.join(self.systemRoot, 'lib64')):
return 'x86_64'
else:
return 'i686'
def variables(self):
os_arch_machine = self.VariableOsArchMachine(systemRoot=self.systemRoot)
return [env.VariableClMakeProfile(systemRoot=self.systemRoot),
VariableClProfileName(systemRoot=self.systemRoot),
VariableClProfileSystem(systemRoot=self.systemRoot),
env.VariableClChrootPath(),
os_arch_machine,
VariableOsLinuxShortname(systemRoot=self.systemRoot),
VariableOsLinuxName(systemRoot=self.systemRoot),
VariableOsLinuxSubname(systemRoot=self.systemRoot),
VariableOsLinuxFilesnum(systemRoot=self.systemRoot),
env.VariableClEmergeConfig(systemRoot=self.systemRoot),
VariableOsLinuxFiles(systemRoot=self.systemRoot),
VariableOsLinuxSystem(systemRoot=self.systemRoot),
VariableOsLinuxVer(systemRoot=self.systemRoot),
VariableOsLinuxBuild(systemRoot=self.systemRoot)]
def __init__(self, systemRoot="/", cache=None):
self.systemRoot = systemRoot
SimpleDataVars.__init__(self, *self.variables())
self.init_variables()
if cache is None:
makeprofile = self.Get('cl_make_profile')
if os.path.exists(makeprofile):
inifile = path.join(systemRoot, os.path.dirname(makeprofile),
os.readlink(makeprofile))
self.flIniFileFrom(inifile, system_root=systemRoot)
for inifile in ("etc/calculate/calculate.env",
"etc/calculate/calculate3.env",
"etc/calculate/calculate2.env"):
self.flIniFileFrom(path.join(systemRoot, inifile),
system_root=systemRoot)
else:
self.cache = cache
def init_variables(self):
self.cache['cl_chroot_path'] = self.systemRoot
return True
def change_root(self, dn, new_root='/'):
if dn.startswith(self.systemRoot):
return pathJoin(new_root, dn[len(self.systemRoot):])
return dn