Refactoring.

develop
Mike Hiretsky 13 years ago
parent 1b09e5effa
commit 25b87520fb

@ -16,7 +16,7 @@
import os import os
import sys import sys
from cl_utils import convertStrListDict, fillstr, getpathenv, runOsCommand,\ from cl_utils import convertStrListDict, getpathenv, runOsCommand,\
pathJoin, _toUNICODE pathJoin, _toUNICODE
from cl_vars_share import varsShare as glob_attr from cl_vars_share import varsShare as glob_attr
import re import re
@ -659,8 +659,8 @@ storage of variables templates")%location)
mlen_name, mlen_type, mlen_mode = getLenElements(varsDict) mlen_name, mlen_type, mlen_mode = getLenElements(varsDict)
plist=varsDict.keys() plist=varsDict.keys()
plist.sort() plist.sort()
br = fillstr("-",mlen_name) + " " +\ br = "-"*mlen_name + " " +\
fillstr("-",mlen_mode) + " " + fillstr("-",10) "-"*mlen_mode + " " + "-"*10
cl_overriding.printSUCCESS(title) cl_overriding.printSUCCESS(title)
cl_overriding.printSUCCESS(br) cl_overriding.printSUCCESS(br)
cl_overriding.printSUCCESS(\ cl_overriding.printSUCCESS(\

@ -18,6 +18,7 @@
import string import string
from random import choice from random import choice
import os import os
from os import path
import types import types
import subprocess import subprocess
from subprocess import Popen,PIPE,STDOUT from subprocess import Popen,PIPE,STDOUT
@ -91,7 +92,7 @@ class proxy_type_file:
pass pass
def file(self,filename): def file(self,filename):
if os.path.exists(filename): if path.exists(filename):
if self.flags == 0x410: if self.flags == 0x410:
processFile = process("file","-bi",filename) processFile = process("file","-bi",filename)
if processFile.success(): if processFile.success():
@ -134,11 +135,11 @@ class typeFile:
class scanDirectory: class scanDirectory:
"""Класс для cканирования директории""" """Класс для cканирования директории"""
def processingFile(self, path, prefix): def processingFile(self, pathname, prefix):
"""Обработка в случае файла""" """Обработка в случае файла"""
return True return True
def processingDirectory(self, path, prefix): def processingDirectory(self, pathname, prefix):
"""Обработка в случае директории если возвращаем None то пропуск дир.""" """Обработка в случае директории если возвращаем None то пропуск дир."""
return True return True
@ -147,7 +148,7 @@ class scanDirectory:
"""Сканирование и обработка шаблонов в директории scanDir""" """Сканирование и обработка шаблонов в директории scanDir"""
ret = True ret = True
if not prefix: if not prefix:
prefix = os.path.join(scanDir,"")[:-1] prefix = path.join(scanDir,"")[:-1]
if not flagDir: if not flagDir:
# проверка корневой директории # проверка корневой директории
retDir = self.processingDirectory(scanDir, scanDir) retDir = self.processingDirectory(scanDir, scanDir)
@ -156,8 +157,7 @@ class scanDirectory:
elif retDir is False: elif retDir is False:
return False return False
if flagDir or stat.S_ISDIR(os.lstat(scanDir)[stat.ST_MODE]): if flagDir or stat.S_ISDIR(os.lstat(scanDir)[stat.ST_MODE]):
for fileOrDir in sorted(os.listdir(scanDir)): for absPath in sorted(listDirectory(scanDir,fullPath=True)):
absPath = os.path.join(scanDir,fileOrDir)
relPath = absPath.split(prefix)[1] relPath = absPath.split(prefix)[1]
stInfo = os.lstat(absPath) stInfo = os.lstat(absPath)
statInfo = stInfo[stat.ST_MODE] statInfo = stInfo[stat.ST_MODE]
@ -166,8 +166,7 @@ class scanDirectory:
if relPath in skipFile: if relPath in skipFile:
continue continue
if not self.processingFile(absPath, prefix): if not self.processingFile(absPath, prefix):
ret = False return False
break
elif stat.S_ISDIR(statInfo): elif stat.S_ISDIR(statInfo):
# Обработка директории # Обработка директории
if relPath in skipDir: if relPath in skipDir:
@ -176,12 +175,11 @@ class scanDirectory:
if retDir is None: if retDir is None:
continue continue
elif retDir is False: elif retDir is False:
ret = False return False
break
ret = self.scanningDirectory(absPath, skipFile, ret = self.scanningDirectory(absPath, skipFile,
skipDir, prefix, True) skipDir, prefix, True)
if ret is False: if ret is False:
break return False
return ret return ret
class process: class process:
@ -406,10 +404,6 @@ def genpassword(passlen=9):
for i in xrange(passlen)]) for i in xrange(passlen)])
return res return res
def fillstr(char, width):
'''Заполнить строку указанным числом символов. Псеводоним символ*кол-во'''
return str(char) * width
def getpathenv(): def getpathenv():
"""Вернуть пути для запуска утилит""" """Вернуть пути для запуска утилит"""
bindir=['/sbin','/bin','/usr/sbin','/usr/bin'] bindir=['/sbin','/bin','/usr/sbin','/usr/bin']
@ -418,7 +412,7 @@ def getpathenv():
lpath=env['PATH'].split(":") lpath=env['PATH'].split(":")
npath=[] npath=[]
for dirname in bindir: for dirname in bindir:
if os.path.exists(dirname) and dirname not in lpath: if path.exists(dirname) and dirname not in lpath:
npath.append(dirname) npath.append(dirname)
lpath=npath+lpath lpath=npath+lpath
return ":".join(lpath) return ":".join(lpath)
@ -550,7 +544,6 @@ def _toUNICODE(val):
else: else:
return str(val).decode('UTF-8') return str(val).decode('UTF-8')
def getModeFile(nameFile, mode="all"): def getModeFile(nameFile, mode="all"):
"""Выдает информацию о файле """Выдает информацию о файле
mode=="all" mode=="all"
@ -570,13 +563,10 @@ def getModeFile(nameFile, mode="all"):
def chownR(directory, uid, gid): def chownR(directory, uid, gid):
"""изменяет владельца и группу """Recusive chown"""
для всех файлов и директорий внутри directory
"""
def chownPaths(rootPath, listPath, uid, gid): def chownPaths(rootPath, listPath, uid, gid):
for chPath in listPath: for chPath in listPath:
chownPath = os.path.join(rootPath, chPath) chownPath = path.join(rootPath, chPath)
statInfo = os.lstat(chownPath)[stat.ST_MODE] statInfo = os.lstat(chownPath)[stat.ST_MODE]
if stat.S_ISLNK(statInfo): if stat.S_ISLNK(statInfo):
os.lchown(chownPath, uid, gid) os.lchown(chownPath, uid, gid)
@ -597,11 +587,11 @@ def copyDir(srcDir, destDir):
При копировании сохраняются владелец, группа, права При копировании сохраняются владелец, группа, права
""" """
def ignoreFile(path, names): def ignoreFile(pathname, names):
"""Игнорирование сокетов при копировании""" """Игнорирование сокетов при копировании"""
ignore = [] ignore = []
for name in names: for name in names:
if stat.S_ISSOCK(os.lstat(os.path.join(path, name))[stat.ST_MODE]): if stat.S_ISSOCK(os.lstat(path.join(pathname, name))[stat.ST_MODE]):
ignore.append(name) ignore.append(name)
return ignore return ignore
@ -618,91 +608,92 @@ def getRunCommands():
def getCmd(procNum): def getCmd(procNum):
cmdLineFile = '/proc/%s/cmdline'%procNum cmdLineFile = '/proc/%s/cmdline'%procNum
try: try:
if os.path.exists(cmdLineFile): if path.exists(cmdLineFile):
return open(cmdLineFile,'r').read().strip() return open(cmdLineFile,'r').read().strip()
except: except:
pass pass
return "" return ""
if not os.access('/proc',os.R_OK): if not os.access('/proc',os.R_OK):
return [] return []
return map(getCmd,filter(lambda x:x.isdigit(), return map(getCmd,
os.listdir('/proc'))) filter(lambda x:x.isdigit(),
listDirectory('/proc')))
def isFstabMount(pathname,mapDevUuid={}): def isFstabMount(pathname,mapDevUuid={},listFstab=[]):
"""Get mount point or device from fstab""" """Get mount point or device from fstab"""
def removeQuotes(s): def removeQuotes(s):
return s.replace('"','').replace("'","") return s.replace('"','').replace("'","")
if pathname == "swap": if pathname == "swap":
absPath = "swap" absPath = "swap"
else: else:
absPath = os.path.abspath(pathname) absPath = path.abspath(pathname)
devuuid = '/dev/disk/by-uuid' devuuid = '/dev/disk/by-uuid'
if not mapDevUuid and os.path.exists(devuuid): if not mapDevUuid:
mapDevUuid.update( mapDevUuid.update(getUUIDDict())
map(lambda x:("UUID=%s"%os.path.basename(x),
os.path.normpath(os.path.join(devuuid,os.readlink(x)))),
filter(os.path.islink,
map(lambda x:os.path.join(devuuid,x),
os.listdir(devuuid)))))
# convert fstab to # convert fstab to
# [['/dev/sda3', '/', '', 'reiserfs', 'noatime', '', '', '0', '2\n'], # [['/dev/sda3', '/', '', 'reiserfs', 'noatime', '', '', '0', '2\n'],
# ['/dev/sda5', '/var/calculate', 'reiserfs', 'noatime', '0', '0\n']] # ['/dev/sda5', '/var/calculate', 'reiserfs', 'noatime', '0', '0\n']]
listFstab = filter(lambda x: len(x) >= 4, #if not listFstab:
if not listFstab:
listFstab.extend(
map(lambda x: [mapDevUuid.get(removeQuotes(x[0]),x[0]),
x[1] if x[2] != "swap" else "swap"],
filter(lambda x: len(x) >= 4,
map(lambda x: filter(lambda x: x, map(lambda x: filter(lambda x: x,
x.replace('\t',' ').split(' ')), x.replace('\t',' ').split(' ')),
filter(lambda x: not x.startswith('#') and x.strip(), filter(lambda x: not x.startswith('#') and x.strip(),
open("/etc/fstab")))) open("/etc/fstab"))))))
# get mount point or device or dir # get mount point or device or dir
return filter(lambda x: x!=absPath, return filter(lambda x: x!=absPath,
reduce(lambda x,y: y, reduce(lambda x,y: y,
filter(lambda x: absPath in x and x[1] != "none", filter(lambda x: absPath in x and x[1] != "none",
map(lambda x: [mapDevUuid.get(removeQuotes(x[0]),x[0]), listFstab),[""]))[0]
x[1] if x[2] != "swap" else "swap"],
listFstab)),[""]))[0]
def isMount(path): def isMount(pathname):
"""В случае монтирования директории выдает другой примонтированный путь""" """В случае монтирования директории выдает другой примонтированный путь"""
absPath = os.path.abspath(path) absPath = path.abspath(pathname)
mtabFile = '/etc/mtab' mtabFile = '/etc/mtab'
if not os.access(mtabFile,os.R_OK): if not os.access(mtabFile,os.R_OK):
return "" return ""
return filter(lambda x: x!=absPath, return filter(lambda x: x!=absPath,
reduce(lambda x,y: y, filter(lambda x: absPath in x, reduce(lambda x,y: y,
map(lambda x: [x[0], x[1]], map(lambda x: x.split(" "), filter(lambda x: absPath in x,
open(mtabFile)))),[""]))[0] map(lambda x: [x[0], x[1]],
map(lambda x: x.split(" "),
open(mtabFile)))), [""]))[0]
def commonPath(*paths): def commonPath(*paths):
"""Return common path from list of paths""" """Return common path from list of paths"""
paths = map(lambda x:os.path.normpath(x).split('/'),paths) paths = map(lambda x:path.normpath(x).split('/'),paths)
res = map(lambda x:x[0], res = map(lambda x:x[0],
filter(lambda x:filter(lambda y:x[0]==y,x[1:]),zip(*paths))) filter(lambda x:filter(lambda y:x[0]==y,x[1:]),zip(*paths)))
return "/".join(res) return "/".join(res)
def childMounts(path): def childMounts(pathname):
"""Get all mount points which contain path""" """Get all mount points which contain path"""
if path != "none": if pathname != "none":
absPath = os.path.abspath(path) absPath = path.abspath(pathname)
else: else:
absPath = path absPath = pathname
mtabFile = '/etc/mtab' mtabFile = '/etc/mtab'
if not os.access(mtabFile,os.R_OK): if not os.access(mtabFile,os.R_OK):
return "" return ""
return reduce(lambda x,y: x + [y], return reduce(lambda x,y: x + [y],
filter(lambda x: commonPath(absPath,x[0])==absPath or \ filter(lambda x: commonPath(absPath,x[0])==absPath or \
commonPath(absPath,x[1])==absPath, commonPath(absPath,x[1])==absPath,
map(lambda x: [x[0], x[1]], map(lambda x: [x[0], x[1]],
map(lambda x: x.split(" "), map(lambda x: x.split(" "),
open(mtabFile)))), open(mtabFile)))),
[]) [])
def pathJoin(*paths): def pathJoin(*paths):
"""Складывает пути, в отличии от os.path.join, складывает абсолютные пути""" """Складывает пути, в отличии от os.path.join, складывает абсолютные пути"""
if len(paths)==1: if len(paths)==1:
return paths[0] return paths[0]
return reduce(os.path.join, return reduce(path.join,
filter(lambda x:x and x != "/", filter(lambda x:x and x != "/",
map(lambda x: x.startswith("/") and x[1:] or x, map(lambda x: x.startswith("/") and x[1:] or x,
paths[1:])),paths[0]) paths[1:])),paths[0])
def getUserPassword(flag="dialog", pwDialog=False): def getUserPassword(flag="dialog", pwDialog=False):
"""Получить пароль у пользователя """Получить пароль у пользователя
@ -786,7 +777,7 @@ def checkDigestFile(digestfile):
reEntry.findall(open(digestfile,'r').read()): reEntry.findall(open(digestfile,'r').read()):
if hasattr(hashlib,alg.lower()): if hasattr(hashlib,alg.lower()):
hashobj = getattr(hashlib,alg.lower()) hashobj = getattr(hashlib,alg.lower())
filename = os.path.join(os.path.dirname(digestfile),filename) filename = path.join(path.dirname(digestfile),filename)
if os.path.exists(filename): if os.path.exists(filename):
digest = hashobj(open(filename,'r').read()) digest = hashobj(open(filename,'r').read())
result.append((alg, result.append((alg,
@ -795,19 +786,37 @@ def checkDigestFile(digestfile):
def getFilesCount(directory): def getFilesCount(directory):
"""Get files count from directory""" """Get files count from directory"""
if os.path.exists(directory): if path.exists(directory):
return len(reduce(lambda x,y:x+y,map(lambda x:x[1]+x[2], return len(reduce(lambda x,y:x+y,map(lambda x:x[1]+x[2],
os.walk(directory)),[])) os.walk(directory)),[]))
return 0 return 0
def listDirectory(directory): def listDirectory(directory,fullPath=False):
"""Get files from directory, if it exists""" """Get files from directory, if it exists"""
if not path.exists(directory):
return []
try: try:
return os.listdir(directory) if fullPath:
return map(lambda x:path.join(directory,x),
os.listdir(directory))
else:
return os.listdir(directory)
except OSError: except OSError:
pass pass
return [] return []
def getUUIDDict(revers=False):
"""Get dict UUID -> dev"""
devuuid = '/dev/disk/by-uuid'
datafunc = lambda x,y: (x,y)
if revers:
datafunc = lambda x,y: (y,x)
return dict(
map(lambda x:datafunc("UUID=%s"%path.basename(x),
path.normpath(path.join(devuuid,os.readlink(x)))),
filter(path.islink,
listDirectory(devuuid,fullPath=True))))
def detectDeviceForPartition(dev): def detectDeviceForPartition(dev):
"""Detect parent device for partition by /sys/block (sysfs)""" """Detect parent device for partition by /sys/block (sysfs)"""
reDeviceSplit = re.compile("^(.*/)?(.*?)(\d+)$") reDeviceSplit = re.compile("^(.*/)?(.*?)(\d+)$")
@ -817,9 +826,9 @@ def detectDeviceForPartition(dev):
if device: if device:
device = device[0] device = device[0]
parentdevices = \ parentdevices = \
filter(lambda x: os.path.split(dev)[-1] in \ filter(lambda x: path.split(dev)[-1] in \
reduce(lambda y,z:y+z[1], reduce(lambda y,z:y+z[1],
os.walk(os.path.join('/sys/block',x)),[]), device) os.walk(path.join('/sys/block',x)),[]), device)
if parentdevices: if parentdevices:
return parentdevices[0] return parentdevices[0]
res = reDeviceSplit.search(dev) res = reDeviceSplit.search(dev)
@ -829,14 +838,14 @@ def detectDeviceForPartition(dev):
def getProgPath(progname): def getProgPath(progname):
"""Get full path of program or False""" """Get full path of program or False"""
baseprogname = os.path.basename(progname) baseprogname = path.basename(progname)
env = {"LANG":"C"} env = {"LANG":"C"}
env.update(os.environ.items() + [("PATH",getpathenv())] +\ env.update(os.environ.items() + [("PATH",getpathenv())] +\
env.items()) env.items())
res = runOsCommand("which %s"%progname,env_dict=env) res = runOsCommand("which %s"%progname,env_dict=env)
if res[0] == 0: if res[0] == 0:
return res[1][0].strip() return res[1][0].strip()
elif os.path.isabs(progname) and os.path.exists(progname): elif path.isabs(progname) and path.exists(progname):
return progname return progname
else: else:
return False return False
@ -870,13 +879,13 @@ def getPkgUses(fullpkg):
listDirectory(pkgCategory))))) listDirectory(pkgCategory)))))
if not packages: if not packages:
return None return None
usePath = os.path.join(pkgCategory,packages[-1]['PF'],"USE") usePath = path.join(pkgCategory,packages[-1]['PF'],"USE")
iusePath = os.path.join(pkgCategory,packages[-1]['PF'],"IUSE") iusePath = path.join(pkgCategory,packages[-1]['PF'],"IUSE")
iuse = open(iusePath,'r').read().strip().split() \ iuse = open(iusePath,'r').read().strip().split() \
if os.path.exists(iusePath) else \ if path.exists(iusePath) else \
[] []
use = open(usePath,'r').read().strip().split() \ use = open(usePath,'r').read().strip().split() \
if os.path.exists(usePath) else \ if path.exists(usePath) else \
[] []
return (map(lambda x:x[1:] if x.startswith("+") else x, return (map(lambda x:x[1:] if x.startswith("+") else x,
filter(lambda x:x, filter(lambda x:x,

Loading…
Cancel
Save