From 5f563167177be3f50a2bbe3172898e7db7cdcf08 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: Wed, 3 Feb 2010 11:59:35 +0300 Subject: [PATCH] Added cl_lib and cl_libserver modules --- pym/cl_lib.py | 39 +++++++++++++++++++++ pym/cl_libserver.py | 85 +++++++++++++++++++++++++++++++++++++++++++++ pym/cl_template.py | 36 +++---------------- pym/cl_utils.py | 32 ++++++++++++++++- 4 files changed, 159 insertions(+), 33 deletions(-) create mode 100644 pym/cl_lib.py create mode 100644 pym/cl_libserver.py diff --git a/pym/cl_lib.py b/pym/cl_lib.py new file mode 100644 index 0000000..0d0c1e8 --- /dev/null +++ b/pym/cl_lib.py @@ -0,0 +1,39 @@ +#-*- coding: utf-8 -*- + +# Copyright 2008-2010 Mir 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. + +from cl_utils import removeDir +from cl_print import color_print + +import cl_lang +tr = cl_lang.lang() +tr.setLocalDomain('cl_lib') +tr.setLanguage(sys.modules[__name__]) + +class shareFile(color_print): + """Общие методы для работы с файлами""" + + def removeDir(self, rmDir): + """Рекурсивное удаление директории + + входной параметр директория или результат сканирования файлов (объект) + """ + try: + removeDir(rmDir) + except (OSError, Exception), e: + print e + self.printERROR(_("Can not delete dir: " ) + rmDir) + return False + return True \ No newline at end of file diff --git a/pym/cl_libserver.py b/pym/cl_libserver.py new file mode 100644 index 0000000..60c2a3d --- /dev/null +++ b/pym/cl_libserver.py @@ -0,0 +1,85 @@ +#-*- coding: utf-8 -*- + +# Copyright 2008-2010 Mir 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 +import hashlib +import crypt +import string +from random import choice +from base64 import urlsafe_b64encode as b64encode + +from cl_print import color_print +import cl_lang + +tr = cl_lang.lang() +tr.setLocalDomain('cl_lib') +tr.setLanguage(sys.modules[__name__]) + +class shareServer(color_print): + """Класс хранения общих методов используемых для настройки сервисов""" + + def __GenCryptSalt__(self): + """Генерация соли для хеширования пароля (CRYPT)""" + chars = string.letters + string.digits+ "./" + salt = "" + for i in range(2): + salt = salt + choice(chars) + return salt + + def getHashPasswd(self, password, crypt): + """Генерация хеша пароля, + + Поддерживаемые алгоритмы шифрования пароля: + plain, md5, smd5, crypt, sha, ssha + """ + if not password: + self.printERROR(_("ERROR") + " getHashPasswd: " +\ + _("password empty")) + return False + + hashPwd = "" + if crypt == "plain": + hashPwd = password + + elif crypt == "md5": + h = hashlib.md5(password) + hashPwd = "{MD5}" + b64encode(h.digest()) + + elif crypt == "smd5": + salt = os.urandom(4) + h = hashlib.md5(password) + h.update(salt) + hashPwd = "{SMD5}" + b64encode(h.digest() + salt) + + elif crypt == "crypt": + salt = self.__GenCryptSalt__() + hashPwd = "{CRYPT}" + crypt.crypt(password, salt) + + elif crypt == "sha": + h = hashlib.sha1(password) + hashPwd = "{SHA}" + b64encode(h.digest()) + + elif crypt == "ssha": + salt = os.urandom(4) + h = hashlib.sha1(password) + h.update(salt) + hashPwd = "{SSHA}" + b64encode(h.digest() + salt) + + else: + self.printERROR(_("ERROR") + " getHashPasswd: " +\ + _("Can not support '%s' crypto algoritm")%crypt) + return False + return hashPwd diff --git a/pym/cl_template.py b/pym/cl_template.py index 46ee7a8..e1d9ac6 100644 --- a/pym/cl_template.py +++ b/pym/cl_template.py @@ -24,7 +24,7 @@ import subprocess import types import random import string -from cl_utils import _error, scan, _toUNICODE +from cl_utils import _error, scan, _toUNICODE, removeDir from cl_overriding import exit import cl_lang @@ -2544,35 +2544,6 @@ class template(_file, _terms, xmlShare, _shareTemplate): self.importFormats[formatTemplate] = classFormat return classFormat(textTemplate) - def removeDir(self, rmDir): - """Рекурсивное удаление директории - - входной параметр директория - """ - if not os.path.exists(rmDir): - self.printERROR(_("Not found remove dir %s") %rmDir) - return False - fileObj = _file() - # Сканируем директорию - scanObjs = fileObj.scanDirs([rmDir]) - for socketRm in scanObjs[0].sockets: - # Удаляем сокеты - if os.path.exists(socketRm): - os.remove(socketRm) - for linkRm in scanObjs[0].links: - # Удаляем ссылки - os.unlink(linkRm[1]) - for fileRm in scanObjs[0].files: - # Удаляем файлы - os.remove(fileRm) - scanObjs[0].dirs.sort(lambda x, y: cmp(len(y), len(x))) - for dirRm in scanObjs[0].dirs: - # Удаляем директории - os.rmdir(dirRm) - if rmDir: - os.rmdir(rmDir) - return True - def createConfFile(self, pathTemplate, path=False): """Создает конфигурационный файл если его нет""" @@ -3155,8 +3126,9 @@ class template(_file, _terms, xmlShare, _shareTemplate): if os.path.isdir(newDirMv): # удаляем директорию try: - self.removeDir(newDirMv) - except: + removeDir(newDirMv) + except (OSError, Exception), e: + print e self.setError(_("Can not delete dir: " ) +\ newDirMv) return (applyDir, False, createDirs) diff --git a/pym/cl_utils.py b/pym/cl_utils.py index 5a29f48..4e69b58 100644 --- a/pym/cl_utils.py +++ b/pym/cl_utils.py @@ -211,4 +211,34 @@ class scan: return [] dirP.baseDir = scanDir dirs.append(dirP) - return dirs \ No newline at end of file + return dirs + +def removeDir(rmDir): + """Рекурсивное удаление директории + + входной параметр директория для удаления + + Ошибки по except + """ + if not os.path.exists(rmDir): + raise Exception(_("Not found remove dir %s") %rmDir) + fileObj = scan() + # Сканируем директорию + scanObjs = fileObj.scanDirs([rmDir]) + for socketRm in scanObjs[0].sockets: + # Удаляем сокеты + if os.path.exists(socketRm): + os.remove(socketRm) + for linkRm in scanObjs[0].links: + # Удаляем ссылки + os.unlink(linkRm[1]) + for fileRm in scanObjs[0].files: + # Удаляем файлы + os.remove(fileRm) + scanObjs[0].dirs.sort(lambda x, y: cmp(len(y), len(x))) + for dirRm in scanObjs[0].dirs: + # Удаляем директории + os.rmdir(dirRm) + if os.path.exists(rmDir): + os.rmdir(rmDir) + return True