diff --git a/pym/cl_fill.py b/pym/cl_fill.py index 94a3bdb..975c9ed 100644 --- a/pym/cl_fill.py +++ b/pym/cl_fill.py @@ -25,6 +25,7 @@ from os.path import exists as pathexists from os import path from cl_utils import isMount, genpassword, getAvailableX11Drivers, \ listDirectory,isPkgInstalled +from utils import ip from encrypt import getHash class fillVars(varsShare): @@ -145,15 +146,9 @@ class fillVars(varsShare): IPs = [] netInterfaces=self.Get("os_net_interfaces") for i in netInterfaces: - res = self._runos("/sbin/ifconfig %s"%i) - if not res: - break - for line in res: - searchIP = re.search('addr:([0-9\.]+).+Bcast:', line) - if searchIP: - # ip адрес - ip = searchIP.groups()[0] - IPs.append(ip) + ipaddr, mask = ip.receiveIpAndMask(i) + if ipaddr: + IPs.append(ipaddr) return ",".join(IPs) def get_os_net_interfaces(self): @@ -162,39 +157,14 @@ class fillVars(varsShare): def get_os_net_allow(self): """Allowed networks""" - - def getNet(ip, mask): - """By ip and mask get network""" - octetsMult = (0x1, 0x100, 0x10000, 0x1000000) - octetsIp = map(lambda x: int(x), ip.split(".")) - octetsMask = map(lambda x: int(x), mask.split(".")) - ipNumb = 0 - for i in octetsMult: - ipNumb += octetsIp.pop()*i - maskNumb = 0 - for i in octetsMult: - maskNumb += octetsMask.pop()*i - startIpNumber = maskNumb&ipNumb - x = startIpNumber - nMask = lambda y: len(filter(lambda x: y >> x &1 ,range(32))) - return "%s.%s.%s.%s/%s"\ - %(x>>24, x>>16&255, x>>8&255, x&255, nMask(maskNumb)) - networks=[] netInterfaces=self.Get("os_net_interfaces") - flagError = False for i in netInterfaces: - res = self._runos("/sbin/ifconfig %s"%i) - if not res: - flagError = True - break - for j in res: - s_ip=re.search('addr:([0-9\.]+).+Bcast:.+Mask:([0-9\.]+)' ,j) - if s_ip: - ip, mask = s_ip.groups() - networks.append(getNet(ip, mask)) - if flagError: - return "" + ipaddr, mask = ip.receiveIpAndMask(i) + if ipaddr and mask: + networks.append(ip.getIpNet(ipaddr, mask)) + else: + networks.append("") return ",".join(networks) def get_os_arch_machine(self): diff --git a/pym/cl_utils.py b/pym/cl_utils.py index e2c46cf..24760c9 100644 --- a/pym/cl_utils.py +++ b/pym/cl_utils.py @@ -805,6 +805,27 @@ def listDirectory(directory,fullPath=False): pass return [] +def readLinesFile(filename): + """Read file by line""" + try: + if path.exists(filename): + for line in open(filename,'r'): + yield line.rstrip('\n') + except (OSError,IOError): + pass + finally: + raise StopIteration + +def readFile(filename): + """Read whole file or return empty string""" + try: + if path.exists(filename): + return open(filename,'r').read() + except (OSError,IOError): + pass + finally: + return "" + def getUUIDDict(revers=False): """Get dict UUID -> dev""" devuuid = '/dev/disk/by-uuid' @@ -850,9 +871,26 @@ def getProgPath(progname): else: return False +def checkUtils(*utils): + """Check utils and exit if it not found""" + retval = [] + for util in utils: + utilPath = getProgPath(util) + if not utilPath: + cl_overriding.printERROR(_("Command not found '%s'")% + path.basename(util)) + cl_overriding.exit(1) + retval.append(utilPath) + if len(retval) == 1: + return retval[0] + else: + return retval + reVerSplit = re.compile(r"^(.*?)-(([^-]+?)(?:-(r\d+))?)(?:.(tbz2))?$",re.S) def reVerSplitToPV(x): """Convert match from reVerSplit to PV hash""" + if type(x) == str: + x = reVerSplit.search(x) if x: match = x.groups() return {'PN':match[0], @@ -900,12 +938,14 @@ def isPkgInstalled(pkg,prefix='/'): if "/" in pkg: category,op,pkg = pkg.partition('/') return bool( - filter(lambda x:x.startswith(pkg), - listDirectory(path.join(pkgDir,category)))) + filter(lambda x:x['PN'] == pkg, + map(reVerSplitToPV, + listDirectory(path.join(pkgDir,category))))) else: return bool( - filter(lambda x: filter(lambda y:y.startswith(pkg), - listDirectory(x)), + filter(lambda x: filter(lambda y:y['PN'] == pkg, + map(reVerSplitToPV, + listDirectory(x))), listDirectory(pkgDir,fullPath=True))) def getPkgActiveUses(fullpkg): @@ -937,3 +977,33 @@ def getAvailableX11Drivers(prefix="/"): map(lambda x: x[:-7], filter(lambda x:x.endswith('_drv.so'), listDirectory(xorg_modules_dir))))) + +def lspci(filtername=None,shortInfo=False): + """Get hash of lspci, filtred by filtername. If shortInfo, then + type,vendor and name get only first word + + pcidata(domain,bus,slot,func) + 'type' + 'vendor' + 'name'""" + reData = re.compile(r'(\S+)\s"([^"]+)"\s+"([^"]+)"\s+"([^"]+)"',re.S) + if filtername: + filterfunc = lambda x: filtername in x + else: + filterfunc = lambda x:x + if shortInfo: + sfunc = lambda x:x.partition(" ")[0] + else: + sfunc = lambda x:x + lspciProg = checkUtils('/usr/sbin/lspci') + processLsPci = process(lspciProg,"-m") + retData = {} + for device in map(lambda x:x.groups(), + filter(lambda x:x, + map(reData.search, + filter(filterfunc, + processLsPci)))): + retData[device[0]] = {'type':sfunc(device[1]),\ + 'vendor':sfunc(device[2]),\ + 'name':sfunc(device[3])} + return retData diff --git a/pym/cl_vars_share.py b/pym/cl_vars_share.py index e225d38..66e1567 100644 --- a/pym/cl_vars_share.py +++ b/pym/cl_vars_share.py @@ -235,14 +235,6 @@ class clLocale: class varsShare: """Share methods and attributs for fill vars methods""" - ipdig = "[%s-9]|(?:1[0-9]|[1-9])[0-9]|2[0-4][0-9]|25[0-%s]" - ipnet = "[1-9]|[12][0-9]|3[012]" - ipdigs = { 'dig1_254' : ipdig % (1,4), 'dig0_254' : ipdig % (0,4), - 'dig0_255' : ipdig % (0,5), } - ipaddr = "(%(dig1_254)s)\.(%(dig0_254)s)\.(%(dig0_254)s)\.(%(dig1_254)s)"%\ - ipdigs - ipaddr_net = "%(ipaddr)s/(%(ipnet)s)"%{'ipaddr':ipaddr,'ipnet':ipnet} - dictNetworkManagers = {"openrc":"sys-apps/openrc", "networkmanager":"net-misc/networkmanager"} diff --git a/pym/utils/__init__.py b/pym/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pym/utils/ip.py b/pym/utils/ip.py new file mode 100644 index 0000000..0f6b190 --- /dev/null +++ b/pym/utils/ip.py @@ -0,0 +1,167 @@ +#-*- coding: utf-8 -*- + +# Copyright 2008-2010 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 math +from cl_utils import process, checkUtils, readFile, listDirectory +import sys +import re +from os import path + +import cl_lang +tr = cl_lang.lang() +tr.setLocalDomain('cl_lib') +tr.setLanguage(sys.modules[__name__]) + +# ip digit from 0|1-255|254 (template) +IP_DIG = "[%s-9]|(?:1[0-9]|[1-9])[0-9]|2[0-4][0-9]|25[0-%s]" +# ip net 0-32 +IP_NET_SUFFIX = "[0-9]|[12][0-9]|3[012]" +# ip digs 1-254,0-254,0-255 +IP_DIGS = { 'dig1_254' : IP_DIG % (1,4), 'dig0_254' : IP_DIG % (0,4), + 'dig0_255' : IP_DIG % (0,5), } +# ip addr 10.0.0.12 +IP_ADDR = "(%(dig1_254)s)\.(%(dig0_254)s)\.(%(dig0_254)s)\.(%(dig1_254)s)"%\ + IP_DIGS +IP_MASK = "(%(dig0_255)s)\.(%(dig0_255)s)\.(%(dig0_255)s)\.(%(dig0_255)s)"%\ + IP_DIGS +# ip addr for net 10.0.0.0 +IP_NET = "(%(dig1_254)s)\.(%(dig0_254)s)\.(%(dig0_254)s)\.(%(dig0_254)s)"%\ + IP_DIGS +# ip and net 192.168.0.0/16 +IP_ADDR_NET = "(%(ipaddr)s)/((%(ipnet)s))"%{'ipaddr':IP_NET, + 'ipnet':IP_NET_SUFFIX} + +reIp = re.compile("^{0}$".format(IP_ADDR)) +reNetSuffix = re.compile("^{0}$".format(IP_NET_SUFFIX)) +reNet = re.compile("^{0}$".format(IP_ADDR_NET)) +reMask = re.compile("^{}$".format(IP_MASK)) + +def checkIp(ip): + """Check ip""" + return reIp.match(ip) + +def checkNetSuffix(netSuffix): + """Check net suffix""" + return reNetSuffix.match(netSuffix) + +def checkNet(net): + """Check net""" + if not reNet.match(net): + return False + mask = strIpToIntIp(netToMask(int(net))) + ip,op,net = net.partition('/') + return (strIpToIntIp(ip)&mask) == (strIpToIntIp(ip)) + +maskDigs = map(lambda x:str(x),(0b10000000,0b11000000,0b11100000,0b11110000, + 0b11111000,0b11111100,0b11111110,0b11111111)) + +def checkMask(mask): + """Check net""" + if mask.count('.') != 3: + return False + zero = False + for dig in mask.split('.'): + if zero or not dig in maskDigs: + if dig == "0": + zero = True + else: + return False + return True + +def getIpAndMask(interface="eth0"): + """Get ip and mask from interface""" + ifconfig = process('/sbin/ifconfig',interface) + res = re.search(r"inet addr:(\S+)\s.*Mask:(\S+)",ifconfig.read(),re.S) + if res: + return res.groups() + else: + return ("","") + +def strIpToIntIp(addr): + """Convert ip specified by string to integer""" + addr = addr.split('.') + return ((int(addr[0])<<24)| + (int(addr[1])<<16)| + (int(addr[2])<<8)| + (int(addr[3]))) + return reduce(lambda x,y:x+(int(y[1])<<(y[0]*8)), + enumerate(reversed(addr.split("."))),0) + +def intIpToStrIp(addr): + """Convert ip specified by integer to string""" + return "{0}.{1}.{2}.{3}".format( + addr>>24,(addr>>16)&0xff,(addr>>8)&0xff,addr&0xff) + +def maskToNet(mask): + """Convert mask specified by str to net""" + mask = strIpToIntIp(mask) + return 32-int(math.log(((~mask) & 0xffffffff)+1,2)) + +def netToMask(net): + """Convert net to mask specified by str""" + return intIpToStrIp((2**net-1)<<(32-net)) + +def getIpNet(ip,mask): + """Get net (xx.xx.xx.xx/xx) by ip address and mask""" + ip = strIpToIntIp(ip) + net = maskToNet(mask) + mask = strIpToIntIp(mask) + return "{ip}/{net}".format(ip=intIpToStrIp(ip&mask), + net=net) + +def isIpInNet(checkip,ipnet): + """Check is ip in specified net""" + ip,op,net = ipnet.partition('/') + mask = strIpToIntIp(netToMask(int(net))) + return (strIpToIntIp(checkip)&mask) == (strIpToIntIp(ip)&mask) + +def receiveMac(interface="eth0"): + """Get MAC from interface""" + ipconfigProg = checkUtils('/sbin/ifconfig') + ifconfig = process(ipconfigProg,interface) + res = re.search(r"HWaddr\s(\S+)",ifconfig.read(),re.S) + if res: + return res.group(1) + else: + return "00:00:00:00:00:00" + +def receiveIpAndMask(interface="eth0"): + """Get ip and mask from interface""" + ipconfigProg = checkUtils('/sbin/ifconfig') + ifconfig = process(ipconfigProg,interface) + res = re.search(r"inet addr:(\S+)\s.*Mask:(\S+)",ifconfig.read(),re.S) + if res: + return res.groups() + else: + return ("","") + +def isDhcpIp(interface="eth0"): + """Get ip by dhcp or static""" + # dhclient + fdhcpLeases = "/var/lib/dhcp/dhclient.leases" + if interface in readFile(fdhcpLeases): + return True + # dhcpcd + fdhcpInfo = "/var/lib/dhcpcd/dhcpcd-%s.info"%interface + fdhcpLease = "/var/lib/dhcpcd/dhcpcd-%s.lease"%interface + if path.exists(fdhcpInfo) or path.exists(fdhcpLease): + return True + return False + +def getInterfaces(): + """Get available interfaces""" + return filter(lambda x:x != "lo", + listDirectory('/sys/class/net')) diff --git a/setup.py b/setup.py index 53d31d9..4e07382 100755 --- a/setup.py +++ b/setup.py @@ -69,6 +69,7 @@ setup( 'calculate-lib.pym.server', 'calculate-lib.pym.client', 'calculate-lib.pym.update_config', + 'calculate-lib.pym.utils', 'calculate-lib.pym.mod'], data_files = [("/usr/share/calculate/i18n",['i18n/cl_lib_ru.mo']), ("/etc/calculate", []),