From 554bee32ceb3e839334ac966dcc4a1e2389ff73b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B0=D0=BC=D0=BE=D1=83=D0=BA=D0=B8=D0=BD=20=D0=90?= =?UTF-8?q?=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9?= Date: Fri, 4 Sep 2009 18:29:33 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=20?= =?UTF-8?q?=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20=D0=B7=D0=B0=D0=BF=D1=83=D1=81?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=B2=D0=BD=D0=B5=D1=88=D0=BD=D0=B8=D1=85=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BC=D0=B0=D0=BD=D0=B4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pym/cl_base.py | 12 +++++++++--- pym/cl_fill.py | 45 +++++++++++++++++++++++++++++++-------------- pym/cl_utils.py | 21 ++++++++++++--------- pym/cl_utils2.py | 1 - 4 files changed, 52 insertions(+), 27 deletions(-) diff --git a/pym/cl_base.py b/pym/cl_base.py index 4298108..42e2bd5 100644 --- a/pym/cl_base.py +++ b/pym/cl_base.py @@ -1025,8 +1025,14 @@ class DataVars(object): ############################################################################## class glob_attr: """Глобальные аттрибуты для методов заполнения переменных""" - path_env = cl_utils.getpathenv() - def _runos(self,cmd,ret_first=None): + def _runos(self,cmd, ret_first=None, env={}): """Вернуть результат выполнения команды ОС""" - return cl_utils.runOsCommand(cmd, None, ret_first) + if not env: + envDict = {} + env.update(os.environ.items() + [("PATH",cl_utils.getpathenv())] +\ + env.items()) + retCode, programOut = cl_utils.runOsCommand(cmd, None, ret_first, env) + if not retCode: + return programOut + return False diff --git a/pym/cl_fill.py b/pym/cl_fill.py index 20be1c1..bf6969e 100644 --- a/pym/cl_fill.py +++ b/pym/cl_fill.py @@ -24,7 +24,7 @@ class fillVars(object, cl_base.glob_attr): def get_os_net_domain(self): ''' Определим домен''' - domain=self._runos("%s hostname -d 2>&1"%self.path_env) + domain=self._runos("hostname -d 2>&1") if not domain: print _("Error:") + " " +_("Not found domain name") print _("Command 'hostname -d' returns an empty value") @@ -104,19 +104,20 @@ class fillVars(object, cl_base.glob_attr): systemVersion = os.readlink(gentooLink).rpartition("/")[2] flagGentoo = True if not flagGentoo: - kernelVersion=self._runos("%s uname -r"%self.path_env) + kernelVersion=self._runos("uname -r") if kernelVersion: systemVersion = kernelVersion.partition("-")[0] return systemVersion def get_os_net_hostname(self): '''Считать имя компьютера net_host''' - hostname=self._runos("""%s hostname -s 2>&1"""%self.path_env) - #Set('net_host',hostname, True) - #упрощенный вариант, следует выполнять только если не указан домен - #в системе + hostname=self._runos("hostname -s 2>&1") + if not hostname: + return "" if re.search("^hostname: ",hostname): - hostname=self._runos("""%s hostname 2>&1"""%self.path_env) + hostname=self._runos("hostname 2>&1") + if not hostname: + return "" if re.search("^hostname: ",hostname): return self.Get('os_linux_shortname') else: @@ -134,8 +135,12 @@ class fillVars(object, cl_base.glob_attr): '255.255.255.255':''} networks=[] netInterfaces=cl_utils.getdirlist("/sys/class/net/") + flagError = False for i in netInterfaces: - res=self._runos("/sbin/ifconfig %s"%i) + 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: @@ -157,6 +162,8 @@ class fillVars(object, cl_base.glob_attr): net[netmask]) elif netmask=='255.255.0.0': networks.append(ip[0]+"."+ip[1]+".0.0"+net[netmask]) + if flagError: + return "" return ",".join(networks) def get_os_locale_locale(self): @@ -247,7 +254,9 @@ class fillVars(object, cl_base.glob_attr): def get_os_arch_machine(self): """архитектура процессора""" - march = self._runos("""%s uname -m"""%self.path_env) + march = self._runos("uname -m") + if not march: + return "" return march def get_os_root_dev(self): @@ -257,7 +266,9 @@ class fillVars(object, cl_base.glob_attr): if re_res: return re_res.group(1) else: - mountLunes = self._runos("""%s mount"""%self.path_env) + mountLunes = self._runos("mount") + if not mountLunes: + return "" if type(mountLunes) == types.ListType: root_dev = mountLunes[0].split("on / type")[0].strip() if root_dev: @@ -266,7 +277,9 @@ class fillVars(object, cl_base.glob_attr): def get_os_root_type(self): """тип носителя (ram, hdd, livecd)""" - mountLunes = self._runos("""%s mount"""%self.path_env) + mountLunes = self._runos("mount") + if not mountLunes: + return "" rootType = "hdd" if type(mountLunes) == types.ListType: flagCD = False @@ -287,8 +300,10 @@ class fillVars(object, cl_base.glob_attr): slpRootDev = rootDev.split("/dev/") if len(slpRootDev) == 2: rDev = slpRootDev[1] - devLines = self._runos("%s LANG=C ls -la /dev/disk/by-id/"\ - %self.path_env) + devLines = self._runos("ls -la /dev/disk/by-id/", None, + {"LANG":"C"}) + if not devLines: + return "" if type(devLines) == types.ListType: for line in devLines: if rDev in line and "usb-" in line: @@ -302,7 +317,9 @@ class fillVars(object, cl_base.glob_attr): def get_hr_virtual(self): """Название виртуальной машины (virtualbox, vmware, qemu)""" - pciLines = self._runos("""%s /usr/sbin/lspci"""%self.path_env) + pciLines = self._runos("/usr/sbin/lspci") + if not pciLines: + return False virtSysDict = {'VirtualBox':'virtualbox', 'VMware':'vmware', 'Qumranet':'qemu'} diff --git a/pym/cl_utils.py b/pym/cl_utils.py index a82481b..938d0d8 100644 --- a/pym/cl_utils.py +++ b/pym/cl_utils.py @@ -264,19 +264,22 @@ def justify(s,width): # вернуть строку в utf8 если она пришла в utf8 return s.encode('utf-8') -def runOsCommand(cmd, inStr=None, ret_first=None): +def runOsCommand(cmd, inStr=None, ret_first=None, env_dict=None): """Выполняет внешнюю программу Параметры: cmd внешняя программа inStr данные передаваемые программе на страндартный вход. ret_first вернуть только первую строку + env_dict словарь переменных окружения Возвращаемые параметры: строка/строки которую выведет внешняя программа + Возвращает код возврата, stdout+stderr """ pipe = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + env=env_dict, close_fds=True, shell=True) fout, fin, ferr = (pipe.stdout, pipe.stdin, pipe.stderr) @@ -288,15 +291,15 @@ def runOsCommand(cmd, inStr=None, ret_first=None): fout.close() res += ferr.readlines() ferr.close() + # Код возврата + retcode = pipe.wait() if res: - if len(res) > 1: - if ret_first: - return res[0] - else: - return res + if len(res) == 1 or ret_first: + return retcode, res[0].strip() else: - return res[0].strip() - + return retcode, res + return retcode, None + def genpassword(passlen=9): '''Вернуть случайный пассворд указанной длины @@ -326,7 +329,7 @@ def getpathenv(): if os.path.exists(dirname) and dirname not in lpath: npath.append(dirname) lpath=npath+lpath - return "PATH="+":".join(lpath)+" && " + return ":".join(lpath) #класс для работы с установленными пакетами class pakages: diff --git a/pym/cl_utils2.py b/pym/cl_utils2.py index 6e7bc3c..4ab28b1 100644 --- a/pym/cl_utils2.py +++ b/pym/cl_utils2.py @@ -15,7 +15,6 @@ # limitations under the License. import sys -sys.path.append('/var/calculate2/calculate-lib/trunk/pym') import struct import termios import fcntl