Browse Source

Refactoring.

develop
Mike Hiretsky 11 years ago
parent
commit
25b87520fb
  1. 6
      pym/cl_datavars.py
  2. 147
      pym/cl_utils.py

6
pym/cl_datavars.py

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

147
pym/cl_utils.py

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

Loading…
Cancel
Save