You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1303 lines
44 KiB

11 years ago
12 years ago
12 years ago
11 years ago
11 years ago
12 years ago
11 years ago
11 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. #-*- coding: utf-8 -*-
  2. # Copyright 2008-2010 Calculate Ltd. http://www.calculate-linux.org
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import string
  16. from random import choice
  17. import os
  18. from os import path
  19. import types
  20. import subprocess
  21. from subprocess import Popen,PIPE,STDOUT
  22. import stat
  23. from shutil import copytree, rmtree
  24. import cl_overriding
  25. import re
  26. import sys
  27. import getpass
  28. from types import StringType
  29. try:
  30. from magic import open as type_file, MAGIC_NONE as MAGIC_NONE
  31. except ImportError:
  32. try:
  33. from magic import open as type_file, NONE as MAGIC_NONE
  34. except:
  35. type_file = None
  36. MAGIC_NONE = None
  37. import cl_lang
  38. tr = cl_lang.lang()
  39. tr.setLocalDomain('cl_lib')
  40. tr.setLanguage(sys.modules[__name__])
  41. class _error:
  42. # Здесь ошибки, если они есть
  43. error = []
  44. def getError(self):
  45. """Выдать ошибки"""
  46. if not self.error:
  47. return False
  48. error = ""
  49. for e in self.error:
  50. error += e + "\n"
  51. return error
  52. def setError(self, error):
  53. """Установка ошибки"""
  54. self.error.append(error)
  55. return True
  56. class _warning:
  57. # Здесь предупреждения
  58. warning = []
  59. def getWarning(self):
  60. """Выдать ошибки"""
  61. if not self.warning:
  62. return False
  63. warning = ""
  64. for w in self.warning:
  65. warning += w + "\n"
  66. return warning
  67. def setWarning(self, warning):
  68. """Установка ошибки"""
  69. self.warning.append(warning)
  70. return True
  71. class proxy_type_file:
  72. def __init__(self,flags):
  73. self.flags = flags
  74. def load(self):
  75. pass
  76. def setflags(self,flags):
  77. self.flags = flags
  78. def close(self):
  79. pass
  80. def file(self,filename):
  81. if path.exists(filename):
  82. if self.flags == 0x410:
  83. processFile = process("file","-bi",filename)
  84. if processFile.success():
  85. return processFile.read().rstrip()
  86. else:
  87. processFile = process("file","-bz",filename)
  88. if processFile.success():
  89. return processFile.read().rstrip()
  90. return None
  91. class typeFile:
  92. """Получение типа файла"""
  93. def __init__(self, magic=0x410):
  94. if type_file is None:
  95. self.magicObject = proxy_type_file(MAGIC_NONE)
  96. else:
  97. self.magicObject = type_file(MAGIC_NONE)
  98. self.magicObject.load()
  99. self.magicObject.setflags(magic)
  100. def __del__(self):
  101. """Закрываем magic"""
  102. self.magicObject.close()
  103. def getMType(self, filename):
  104. """Информация о типе файла"""
  105. return self.magicObject.file(filename)
  106. def isBinary(self, filename):
  107. """является ли файл бинарным"""
  108. mime = self.getMType(filename)
  109. # В случае ошибки
  110. if mime.count("`"):
  111. return mime
  112. elif mime.count("binary"):
  113. return True
  114. return False
  115. class scanDirectory:
  116. """Класс для cканирования директории"""
  117. def processingFile(self, pathname, prefix):
  118. """Обработка в случае файла"""
  119. return True
  120. def processingDirectory(self, pathname, prefix):
  121. """Обработка в случае директории если возвращаем None то пропуск дир."""
  122. return True
  123. def scanningDirectory(self, scanDir, skipFile=[], skipDir=[],
  124. prefix=None, flagDir=False):
  125. """Сканирование и обработка шаблонов в директории scanDir"""
  126. ret = True
  127. if not prefix:
  128. prefix = path.join(scanDir,"")[:-1]
  129. if not flagDir:
  130. # проверка корневой директории
  131. retDir = self.processingDirectory(scanDir, scanDir)
  132. if retDir is None:
  133. return None
  134. elif retDir is False:
  135. return False
  136. if flagDir or stat.S_ISDIR(os.lstat(scanDir)[stat.ST_MODE]):
  137. for absPath in sorted(listDirectory(scanDir,fullPath=True)):
  138. relPath = absPath.split(prefix)[1]
  139. stInfo = os.lstat(absPath)
  140. statInfo = stInfo[stat.ST_MODE]
  141. if stat.S_ISREG(statInfo):
  142. # Обработка файла
  143. if relPath in skipFile:
  144. continue
  145. if not self.processingFile(absPath, prefix):
  146. return False
  147. elif stat.S_ISDIR(statInfo):
  148. # Обработка директории
  149. if relPath in skipDir:
  150. continue
  151. retDir = self.processingDirectory(absPath, prefix)
  152. if retDir is None:
  153. continue
  154. elif retDir is False:
  155. return False
  156. ret = self.scanningDirectory(absPath, skipFile,
  157. skipDir, prefix, True)
  158. if ret is False:
  159. return False
  160. return ret
  161. class process:
  162. """Execute system command by Popen
  163. Examples:
  164. execute program and get result:
  165. if process("/bin/gzip","/boot/somefile").success():
  166. print "Gzip success"
  167. unzip and process unzip data by cpio (list files):
  168. processGzip = process("/bin/gzip","-dc","/boot/initrd")
  169. processCpio = process("/bin/cpio","-tf",stdin=processGzip)
  170. filelist = processCpio.readlines()
  171. execute command and send data:
  172. processGrub = process("/sbin/grub")
  173. processGrub.write("root (hd0,0)\n")
  174. processGrub.write("setup (hd0)\n")
  175. processGrub.write("quit\n")
  176. isok = processGrub.success()
  177. union stdout and stderr:
  178. process("/bin/ls","/",stderr=STDOUT)
  179. result to stdout:
  180. process("/bin/ls",stdout=None)
  181. get data from keyboard:
  182. process("/bin/cat",stdin=None)
  183. """
  184. def __init__(self,command,*params,**kwarg):
  185. if not "stdin" in kwarg:
  186. stdin=self._defaultStdin
  187. else:
  188. if kwarg["stdin"] == None:
  189. stdin = self._keyboardStdin
  190. else:
  191. stdin=kwarg["stdin"].getStdout
  192. self.stdout = kwarg.get("stdout",PIPE)
  193. self.envdict = kwarg.get("envdict",{})
  194. if "lang" in kwarg:
  195. self.envdict["LANG"] = kwarg.get('lang')
  196. self.langc = "langc" in kwarg
  197. self.stderr = kwarg.get("stderr",PIPE)
  198. self.command = [command] + list(params)
  199. self.stdin = stdin
  200. self.iter = None
  201. self.pipe = None
  202. self.cacheresult = None
  203. self.communicated = False
  204. def _open(self):
  205. """Open pipe if it not open"""
  206. if not self.pipe:
  207. self.pipe = Popen(self.command,
  208. stdout=self.stdout,
  209. stdin=self.stdin(),
  210. stderr=self.stderr,
  211. env=self.envdict)
  212. def _defaultStdin(self):
  213. """Return default stdin"""
  214. return PIPE
  215. def _keyboardStdin(self):
  216. """Return keyboard stdin"""
  217. return None
  218. def getStdout(self):
  219. """Get current stdout"""
  220. self.close()
  221. self._open()
  222. return self.pipe.stdout
  223. def write(self,data):
  224. """Write to process stdin"""
  225. self._open()
  226. self.pipe.stdin.write(data)
  227. self.pipe.stdin.flush()
  228. def close(self):
  229. """Close stdin"""
  230. if self.pipe:
  231. self.pipe.stdin.close()
  232. def read(self):
  233. """Read all data"""
  234. try:
  235. self._open()
  236. if self.cacheresult is None:
  237. self.cacheresult = self.pipe.communicate()[0]
  238. return self.cacheresult
  239. except KeyboardInterrupt:
  240. self.kill()
  241. raise KeyboardInterrupt
  242. def readlines(self):
  243. """Read lines"""
  244. return self.read().split('\n')
  245. def __iter__(self):
  246. """Get iterator"""
  247. if not self.iter:
  248. self.iter = iter(self.readlines())
  249. return self.iter
  250. def kill(self):
  251. """Kill this process"""
  252. if self.pipe:
  253. self.pipe.kill()
  254. def next(self):
  255. """Next string from stdout"""
  256. return self.__iter__().next()
  257. def returncode(self):
  258. """Get return code"""
  259. self.read()
  260. return self.pipe.returncode
  261. def success(self):
  262. """Success or not"""
  263. return self.returncode() == 0
  264. def failed(self):
  265. """Failed or not"""
  266. return self.returncode() != 0
  267. class processProgress(process):
  268. """Execute system command by Popen for parse stdout."""
  269. def __init__(self,command,*params,**kwarg):
  270. process.__init__(self,command,*params,**kwarg)
  271. self.readsize = kwarg.get("readsize",10)
  272. self.init(**kwarg)
  273. def init(self,**kwarg):
  274. pass
  275. def read(self):
  276. """Read data with parsing ability"""
  277. try:
  278. self.processInit()
  279. self._open()
  280. if self.cacheresult is None:
  281. self.cacheresult = []
  282. self.buf = ""
  283. part = self.pipe.stdout.read(1)
  284. while part:
  285. if self.buf:
  286. self.buf += part
  287. else:
  288. self.buf = part
  289. if self.processStdout():
  290. self.processDraw()
  291. self.cacheresult.append(part)
  292. part = self.pipe.stdout.read(self.readsize)
  293. self.pipe.poll()
  294. self.processEnd(self.success())
  295. except KeyboardInterrupt:
  296. self.cacheresult = "".join(self.cacheresult)
  297. self.pipe.kill()
  298. self.processEnd(False)
  299. raise KeyboardInterrupt()
  300. self.cacheresult = "".join(self.cacheresult)
  301. return self.cacheresult
  302. def processInit(self):
  303. """Called when read first byte"""
  304. pass
  305. def processDraw(self):
  306. """Called when processStdout return True"""
  307. pass
  308. def processStdout(self):
  309. """Called when read readsize byte from stdout"""
  310. return True
  311. def processEnd(self,res=True):
  312. """Called when process end"""
  313. pass
  314. def runOsCommand(cmd,in_str=None, env_dict=None):
  315. """Run system command
  316. Параметры:
  317. cmd внешняя программа
  318. in_str данные передаваемые программе на страндартный вход.
  319. env_dict словарь переменных окружения
  320. Возвращает (код возврата, список stdout+stderr)
  321. """
  322. pipe = subprocess.Popen(cmd, stdin=subprocess.PIPE,
  323. stdout=subprocess.PIPE,
  324. stderr=subprocess.PIPE,
  325. env=env_dict,
  326. close_fds=True,
  327. shell=True)
  328. fout, fin, ferr = (pipe.stdout, pipe.stdin, pipe.stderr)
  329. # если есть данные на вход, передать их
  330. if in_str:
  331. fin.write(in_str)
  332. fin.close()
  333. res = map(lambda x: x.rstrip(), fout.readlines())
  334. fout.close()
  335. res += map(lambda x: x.rstrip(), ferr.readlines())
  336. ferr.close()
  337. # Код возврата
  338. retcode = pipe.wait()
  339. return retcode, res
  340. def genpassword(passlen=9):
  341. """Return random charset specified lenght (passlen)"""
  342. return ''.join(map(lambda x:choice(string.ascii_letters+string.digits),
  343. xrange(0,passlen)))
  344. def getpathenv():
  345. """Return path for run utilities"""
  346. bindir=set(filter(path.exists,
  347. ['/sbin','/bin','/usr/sbin','/usr/bin']))
  348. env=os.environ
  349. envPath = set(env.get('PATH','').split(":")) | bindir
  350. return ":".join(envPath)
  351. class MultiReplace:
  352. """MultiReplace function object
  353. Usage:
  354. replacer = MultiReplace({'str':'efg','in':'asd'})
  355. s = replacer("string")
  356. """
  357. def __init__(self, repl_dict):
  358. # string to string mapping; use a regular expression
  359. keys = repl_dict.keys()
  360. keys.sort(reverse=True) # lexical order
  361. pattern = u"|".join([re.escape(key) for key in keys])
  362. self.pattern = re.compile(pattern)
  363. self.dict = repl_dict
  364. def replace(self, s):
  365. # apply replacement dictionary to string
  366. def repl(match, get=self.dict.get):
  367. item = match.group(0)
  368. return get(item, item)
  369. return self.pattern.sub(repl, s)
  370. __call__ = replace
  371. def str2dict(s):
  372. """Convert string to dictionary:
  373. String format:
  374. {'key1':'value1','key2':'val\'ue2'}
  375. """
  376. value = r'(?:\\\\|\\\'|[^\'])'
  377. pair = r"""
  378. \s*' # begin key
  379. (%(v)s*)
  380. ' # end key
  381. \s*:\s* # delimeter key/value
  382. ' # begin value
  383. (%(v)s*)
  384. '\s* # end value
  385. """ % {"v":value}
  386. reDict = re.compile(pair, re.X)
  387. reMatchDict = re.compile("""
  388. ^{ # begin dict
  389. ((%(v)s,)* # many pair with comma at end
  390. %(v)s)? # pair without comma
  391. }$ # end dict
  392. """ % {'v':pair}, re.X)
  393. if reMatchDict.match(s.strip()):
  394. d = dict(reDict.findall(s))
  395. replaceSlash = MultiReplace({'\\\\':'\\','\\\'':'\''})
  396. for i in d.keys():
  397. d[i] = replaceSlash(d[i])
  398. return d
  399. else:
  400. cl_overriding.printERROR(_("wrong dict value: %s"%s))
  401. cl_overriding.exit(1)
  402. def str2list(s):
  403. """Convert string to list:
  404. String format:
  405. ['value1','val\'ue2']
  406. """
  407. value = r'(?:\\\\|\\\'|[^\'])'
  408. element = r"""
  409. \s*' # begin value
  410. (%(v)s*)
  411. '\s* # end value
  412. """ % {"v":value}
  413. reList = re.compile(element, re.X)
  414. reMatchList = re.compile("""
  415. ^\[ # begin dict
  416. ((%(v)s,)* # many elements with comma at end
  417. %(v)s)? # element without comma
  418. \]$ # end dict
  419. """ % {'v':element}, re.X)
  420. if reMatchList.match(s.strip()):
  421. replaceSlash = MultiReplace({'\\\\':'\\','\\\'':'\''})
  422. return [replaceSlash(i) for i in reList.findall(s)]
  423. else:
  424. cl_overriding.printERROR(_("wrong list value: %s"%s))
  425. cl_overriding.exit(1)
  426. def list2str(list):
  427. """Convert list to string
  428. Return string with escaped \ and '.
  429. """
  430. replaceSlash = MultiReplace({'\\':'\\\\','\'':'\\\''})
  431. return "[%s]" % ','.join(["'%s'"%replaceSlash(str(i))
  432. for i in list ])
  433. def dict2str(dict):
  434. """Convert dictionry to string
  435. Return string with escaped \ and '.
  436. """
  437. replaceSlash = MultiReplace({'\\':'\\\\','\'':'\\\''})
  438. return '{%s}' % ','.join(["'%s':'%s'" % (str(k),replaceSlash(str(v))) \
  439. for (k,v) in dict.items()])
  440. def convertStrListDict(val):
  441. """Convert data between string, list, dict"""
  442. # if val is list
  443. if type(val) == types.ListType:
  444. return list2str(val)
  445. # if val is dictionary
  446. elif type(val) == types.DictType:
  447. return dict2str(val)
  448. # else it is string
  449. else:
  450. # detect dictionary
  451. if re.match("^{.*}$",val):
  452. return str2dict(val)
  453. # detect list
  454. elif re.match("^\[.*\]$",val):
  455. return str2list(val)
  456. # else is simple string
  457. else:
  458. return val
  459. def _toUNICODE(val):
  460. """Convert text to unicode"""
  461. if type(val) == types.UnicodeType:
  462. return val
  463. else:
  464. return str(val).decode('UTF-8')
  465. def getModeFile(nameFile, mode="all"):
  466. """Выдает информацию о файле
  467. mode=="all"
  468. права файла, владелец, группа файла
  469. mode=="mode"
  470. права файла
  471. mode=="owner"
  472. владелец, группа файла
  473. """
  474. fst = os.lstat(nameFile)
  475. if mode == "all":
  476. return (stat.S_IMODE(fst.st_mode), fst.st_uid, fst.st_gid)
  477. if mode == "mode":
  478. return stat.S_IMODE(fst.st_mode)
  479. if mode == "owner":
  480. return fst.st_uid, fst.st_gid
  481. def chownR(directory, uid, gid):
  482. """Recusive chown"""
  483. def chownPaths(rootPath, listPath, uid, gid):
  484. for chPath in listPath:
  485. chownPath = path.join(rootPath, chPath)
  486. statInfo = os.lstat(chownPath)[stat.ST_MODE]
  487. if stat.S_ISLNK(statInfo):
  488. os.lchown(chownPath, uid, gid)
  489. else:
  490. os.chown(chownPath, uid, gid)
  491. for root, dirs, files in os.walk(directory):
  492. # меняем владельца директории
  493. os.chown(root, uid, gid)
  494. # Меняем владельца директорий
  495. chownPaths(root, dirs, uid, gid)
  496. # Меняем владельца файлов
  497. chownPaths(root, files, uid, gid)
  498. return True
  499. def copyDir(srcDir, destDir):
  500. """Копируем директорию srcDir в destDir
  501. При копировании сохраняются владелец, группа, права
  502. """
  503. def ignoreFile(pathname, names):
  504. """Игнорирование сокетов при копировании"""
  505. ignore = []
  506. for name in names:
  507. if stat.S_ISSOCK(os.lstat(path.join(pathname, name))[stat.ST_MODE]):
  508. ignore.append(name)
  509. return ignore
  510. copytree(srcDir, destDir, ignore=ignoreFile)
  511. return True
  512. def removeDir(rmDir):
  513. """Рекурсивное удаление директории"""
  514. rmtree(rmDir)
  515. return True
  516. def getRunCommands():
  517. """List run program"""
  518. def getCmd(procNum):
  519. cmdLineFile = '/proc/%s/cmdline'%procNum
  520. try:
  521. if path.exists(cmdLineFile):
  522. return open(cmdLineFile,'r').read().strip()
  523. except:
  524. pass
  525. return ""
  526. if not os.access('/proc',os.R_OK):
  527. return []
  528. return map(getCmd,
  529. filter(lambda x:x.isdigit(),
  530. listDirectory('/proc')))
  531. def isFstabMount(pathname,mapDevUuid={},listFstab=[]):
  532. """Get mount point or device from fstab"""
  533. def removeQuotes(s):
  534. return s.replace('"','').replace("'","")
  535. if pathname == "swap":
  536. absPath = "swap"
  537. else:
  538. absPath = path.abspath(pathname)
  539. if not mapDevUuid:
  540. mapDevUuid.update(getUUIDDict())
  541. # convert fstab to
  542. # [['/dev/sda3', '/', '', 'reiserfs', 'noatime', '', '', '0', '2\n'],
  543. # ['/dev/sda5', '/var/calculate', 'reiserfs', 'noatime', '0', '0\n']]
  544. #if not listFstab:
  545. if not listFstab:
  546. listFstab.extend(
  547. map(lambda x: [mapDevUuid.get(removeQuotes(x[0]),x[0]),
  548. x[1] if x[2] != "swap" else "swap"],
  549. filter(lambda x: len(x) >= 4,
  550. map(lambda x: filter(lambda x: x,
  551. x.replace('\t',' ').split(' ')),
  552. filter(lambda x: not x.startswith('#') and x.strip(),
  553. open("/etc/fstab"))))))
  554. # get mount point or device or dir
  555. return filter(lambda x: x!=absPath,
  556. reduce(lambda x,y: y,
  557. filter(lambda x: absPath in x and x[1] != "none",
  558. listFstab),[""]))[0]
  559. class SingletonParam(type):
  560. def __init__(cls, name, bases, dict):
  561. super(SingletonParam, cls).__init__(name, bases, dict)
  562. cls.instance = {}
  563. def __call__(cls,*args,**kw):
  564. keyarg = args[0] if args else ""
  565. if not keyarg in cls.instance:
  566. cls.instance[keyarg] = super(SingletonParam, cls).__call__(*args, **kw)
  567. return cls.instance[keyarg]
  568. class FStab(object):
  569. """Data reader for fstab"""
  570. __metaclass__ = SingletonParam
  571. fstab_file = '/etc/fstab'
  572. NAME, DIR, TYPE, OPTS, FREQ, PASSNO = range(0,6)
  573. def __init__(self,fstab_file=None):
  574. if fstab_file:
  575. self.fstab_file = fstab_file
  576. self.cache = []
  577. self.rotateCache = []
  578. self.dictUUID = getUUIDDict()
  579. self.rebuildCache()
  580. def rebuildCache(self):
  581. """Rebuild cache from fstab file"""
  582. self.cache = \
  583. map(lambda x:map(lambda y:y.strip(),x.split()),
  584. filter(lambda x:x and not x.lstrip().startswith("#"),
  585. open(self.fstab_file,'r').read().split('\n')))
  586. for data in self.cache:
  587. convertDev = lambda x: path.realpath(x) if x.startswith('/') else x
  588. data[0] = getUdevDeviceInfo(
  589. name=convertDev(self.dictUUID.get(data[0],data[0]))
  590. ).get('DEVNAME','')
  591. data[1] = data[1] if data[2] != "swap" else "swap"
  592. self.rotateCache = zip(*self.cache)
  593. def getBy(self,what=DIR,where=NAME,eq=None,noteq=None,allentry=False):
  594. """Get data from fstab"""
  595. if not eq is None:
  596. filterfunc = lambda x: x[where] == eq
  597. else:
  598. filterfunc = lambda x: x[where] != noteq
  599. res = map(lambda x:x[what],filter(filterfunc,self.cache))
  600. if allentry:
  601. return res
  602. else:
  603. return "" if not res else res[-1]
  604. def getFields(self,*fields):
  605. """Get all data by specifie fields"""
  606. return zip(*reduce(lambda x,y:x+[self.rotateCache[y]],fields,[]))
  607. def isExists(self,what=DIR,eq=None,noteq=None):
  608. """Field with condition exist in fstab"""
  609. if not eq is None:
  610. filterfunc = lambda x: x[what] == eq
  611. else:
  612. filterfunc = lambda x: x[what] != noteq
  613. return bool(filter(filterfunc,self.cache))
  614. def isMount(pathname):
  615. """В случае монтирования директории выдает другой примонтированный путь"""
  616. absPath = path.abspath(pathname)
  617. mtabFile = '/etc/mtab'
  618. if not os.access(mtabFile,os.R_OK):
  619. return ""
  620. return filter(lambda x: x!=absPath,
  621. reduce(lambda x,y: y,
  622. filter(lambda x: absPath in x,
  623. map(lambda x: [x[0], x[1]],
  624. map(lambda x: x.split(" "),
  625. open(mtabFile)))), [""]))[0]
  626. def commonPath(*paths):
  627. """Return common path from list of paths"""
  628. paths = map(lambda x:path.normpath(x).split('/'),paths)
  629. res = map(lambda x:x[0],
  630. filter(lambda x:filter(lambda y:x[0]==y,x[1:]),zip(*paths)))
  631. return "/".join(res)
  632. def childMounts(pathname):
  633. """Get all mount points which contain path"""
  634. if pathname != "none":
  635. absPath = path.abspath(pathname)
  636. else:
  637. absPath = pathname
  638. mtabFile = '/etc/mtab'
  639. if not os.access(mtabFile,os.R_OK):
  640. return ""
  641. return reduce(lambda x,y: x + [y],
  642. filter(lambda x: commonPath(absPath,x[0])==absPath or \
  643. commonPath(absPath,x[1])==absPath,
  644. map(lambda x: [x[0], x[1]],
  645. map(lambda x: x.split(" "),
  646. open(mtabFile)))),
  647. [])
  648. def pathJoin(*paths):
  649. """Складывает пути, в отличии от os.path.join, складывает абсолютные пути"""
  650. if len(paths)==1:
  651. return paths[0]
  652. return reduce(path.join,
  653. filter(lambda x:x and x != "/",
  654. map(lambda x: x.startswith("/") and x[1:] or x,
  655. paths[1:])),paths[0])
  656. def getUserPassword(flag="dialog", pwDialog=False):
  657. """Получить пароль у пользователя
  658. flag - опция "dalog" или "stdin" - откуда получаем пароль
  659. pwDialog - структура для вывода приглашения в режиме диалога
  660. """
  661. userPwd = ""
  662. if flag == "dialog":
  663. if not pwDialog:
  664. pwDialog = [_("New password"),
  665. _("Retype new password")]
  666. pwdA = getpass.getpass(pwDialog[0]+":")
  667. pwdB = getpass.getpass(pwDialog[1]+":")
  668. elif flag == "stdin":
  669. pwdA = sys.stdin.readline().rstrip()
  670. pwdB = sys.stdin.readline().rstrip()
  671. else:
  672. cl_overriding.printERROR(_("ERROR in function getUserPassword, \
  673. incorrect option 'flag=%s'")%flag)
  674. return False
  675. if not pwdA or not (pwdA == pwdB):
  676. cl_overriding.printERROR(_("ERROR") + ": " +\
  677. _("password incorrect")+ ": " + _("try again"))
  678. return False
  679. userPwd = pwdA
  680. return userPwd
  681. def cmpVersion(v1,v2):
  682. """Compare versions specified by tuple or string"""
  683. if isinstance(v1,StringType):
  684. v1 = getTupleVersion(v1)
  685. if isinstance(v2,StringType):
  686. v2 = getTupleVersion(v2)
  687. return cmp((v1[0]+[0,]*(len(v2[0])-len(v1[0])),v1[1]),
  688. (v2[0]+[0,]*(len(v1[0])-len(v2[0])),v2[1]))
  689. def getTupleVersion(ver):
  690. """Get version specified by string as list:
  691. Example:
  692. 2.6.30 [(2,6,30),('r',0)]
  693. 2.6.31-r1 [(2,6,31),('r',1)]
  694. """
  695. suffix_value = {"pre": -2, "p": 0, "alpha": -4, "beta": -3,
  696. "rc": -1}
  697. def toTuple(v):
  698. return map(lambda x: suffix_value[x] if x in suffix_value else x,
  699. map(lambda x: int(x) if x.isdigit() else x,
  700. re.findall("r\d+$|\d+|[a-zA-Z+]+",
  701. v.replace('-SNAPSHOT',''))))
  702. vers, revision = re.search("(^.*?)(-r\d+)?$",ver,re.S).groups()
  703. vers = toTuple(vers)
  704. revision = toTuple(revision or "r0")
  705. return [vers,revision]
  706. def appendProgramToEnvFile(nameProg, objVar):
  707. """Append name program to variable cl-merges and
  708. save /etc/calculate/calculate.env """
  709. if not objVar.AppendToList("cl_merges", nameProg, force=True):
  710. return False
  711. if not objVar.WriteList("cl_merges", force=True):
  712. return False
  713. return True
  714. def removeProgramToEnvFile(nameProg, objVar):
  715. """Remove name program from variable cl-merges and save
  716. /etc/calculate/calculate.env"""
  717. if not objVar.RemoveToList("cl_merges", nameProg, force=True):
  718. return False
  719. if not objVar.WriteList("cl_merges", force=True):
  720. return False
  721. return True
  722. def checkDigestFile(digestfile):
  723. """Check digest by digestfile"""
  724. reEntry = re.compile(r"# (\S+) HASH\n(\S+) (\S+)",re.S)
  725. result = []
  726. for alg,hashdata,filename in \
  727. reEntry.findall(open(digestfile,'r').read()):
  728. if hasattr(hashlib,alg.lower()):
  729. hashobj = getattr(hashlib,alg.lower())
  730. filename = path.join(path.dirname(digestfile),filename)
  731. if os.path.exists(filename):
  732. digest = hashobj(open(filename,'r').read())
  733. result.append((alg,
  734. digest.hexdigest().upper() == hashdata.upper()))
  735. return result
  736. def getFilesCount(directory):
  737. """Get files count from directory"""
  738. if path.exists(directory):
  739. return len(reduce(lambda x,y:x+y,map(lambda x:x[1]+x[2],
  740. os.walk(directory)),[]))
  741. return 0
  742. def listDirectory(directory,fullPath=False):
  743. """Get files from directory, if it exists"""
  744. if not path.exists(directory):
  745. return []
  746. try:
  747. if fullPath:
  748. return map(lambda x:path.join(directory,x),
  749. os.listdir(directory))
  750. else:
  751. return os.listdir(directory)
  752. except OSError:
  753. pass
  754. return []
  755. def getInstalledVideo(prefix="/"):
  756. """Get installed video drivers"""
  757. x11Drivers = path.join(prefix,"usr/lib/xorg/modules/drivers")
  758. return map(lambda x:x[:-7],
  759. filter(lambda x:x.endswith('_drv.so'),
  760. listDirectory(x11Drivers)))
  761. def getDistfilesVideo(prefix="/"):
  762. """Get video drivers from distfiles"""
  763. distFiles = path.join(prefix,"usr/portage/distfiles")
  764. return list(set(
  765. map(lambda x:'fglrx' if x.startswith('ati-driver') else "nvidia",
  766. filter(lambda x:x.startswith('ati-driver-installer') or
  767. x.startswith('NVIDIA-Linux'),
  768. listDirectory(distFiles)))))
  769. def getAvailableVideo(prefix="/"):
  770. """Get available video drivers (installed and maybe installed)"""
  771. return list(set(getInstalledVideo(prefix=prefix) + \
  772. getDistfilesVideo(prefix=prefix)))
  773. def readLinesFile(filename):
  774. """Read file by line"""
  775. try:
  776. if path.exists(filename):
  777. for line in open(filename,'r'):
  778. yield line.rstrip('\n')
  779. except (OSError,IOError):
  780. pass
  781. finally:
  782. raise StopIteration
  783. def readFile(filename):
  784. """Read whole file or return empty string"""
  785. try:
  786. if path.exists(filename):
  787. return open(filename,'r').read()
  788. except (OSError,IOError):
  789. pass
  790. return ""
  791. def getUUIDDict(revers=False):
  792. """Get dict UUID -> dev"""
  793. blkidProcess = process("/sbin/blkid","-s","UUID","-c","/dev/null")
  794. if revers:
  795. datafunc = lambda x,y: (y,x)
  796. else:
  797. datafunc = lambda x,y: (x,y)
  798. DEV,UUID = 0,1
  799. reSplit = re.compile('^([^:]+):.*UUID="([^"]+)"',re.S)
  800. return dict(
  801. map(lambda x:datafunc("UUID=%s"%x[UUID],
  802. getUdevDeviceInfo(name=x[DEV]).get('DEVNAME',x[DEV])),
  803. map(lambda x:x.groups(),
  804. filter(lambda x:x,
  805. map(reSplit.search,
  806. blkidProcess)))))
  807. def detectDeviceForPartition(dev):
  808. """Detect parent device for partition by udev and return property"""
  809. prop = getUdevDeviceInfo(name=dev)
  810. if prop.get('DEVTYPE','') != 'partition':
  811. return ''
  812. parentpath = path.dirname(prop.get('DEVPATH',''))
  813. if parentpath:
  814. devProp = getUdevDeviceInfo(path=parentpath)
  815. return devProp.get('DEVNAME','')
  816. return None
  817. def getProgPath(progname):
  818. """Get full path of program or False"""
  819. baseprogname = path.basename(progname)
  820. env = {"LANG":"C"}
  821. env.update(os.environ.items() + [("PATH",getpathenv())] +\
  822. env.items())
  823. res = runOsCommand("which %s"%progname,env_dict=env)
  824. if res[0] == 0:
  825. return res[1][0].strip()
  826. elif path.isabs(progname) and path.exists(progname):
  827. return progname
  828. else:
  829. return False
  830. def checkUtils(*utils):
  831. """Check utils, exit if it not found and return fullpath"""
  832. retval = []
  833. for util in utils:
  834. utilPath = getProgPath(util)
  835. if not utilPath:
  836. cl_overriding.printERROR(_("Command not found '%s'")%
  837. path.basename(util))
  838. cl_overriding.exit(1)
  839. retval.append(utilPath)
  840. if len(retval) == 1:
  841. return retval[0]
  842. else:
  843. return retval
  844. reVerSplit = re.compile(r"^(.*?)-(([^-]+?)(?:-(r\d+))?)(?:.(tbz2))?$",re.S)
  845. def reVerSplitToPV(x):
  846. """Convert match from reVerSplit to PV hash"""
  847. if type(x) == str:
  848. x = reVerSplit.search(x)
  849. if x:
  850. match = x.groups()
  851. return {'PN':match[0],
  852. 'PF':"%s-%s"%(match[0],match[1]),
  853. 'P':"%s-%s"%(match[0],match[2]),
  854. 'PV':match[2],
  855. 'PR':match[3] or "r0",
  856. 'PVR':match[1]}
  857. return {'PN':"",
  858. 'PF':"",
  859. 'P':"",
  860. 'PV':"",
  861. 'PR':"",
  862. 'PVR':""}.copy()
  863. def getPkgUses(fullpkg):
  864. """Get USE and IUSE from package"""
  865. category,slash,pkg = fullpkg.partition('/')
  866. pkgCategory = '/var/db/pkg/{0}'.format(category)
  867. packages = filter(lambda x:x['PN'] == pkg,
  868. map(reVerSplitToPV,
  869. filter(lambda x:x,
  870. map(lambda x:reVerSplit.search(x),
  871. listDirectory(pkgCategory)))))
  872. if not packages:
  873. return None
  874. usePath = path.join(pkgCategory,packages[-1]['PF'],"USE")
  875. iusePath = path.join(pkgCategory,packages[-1]['PF'],"IUSE")
  876. iuse = open(iusePath,'r').read().strip().split() \
  877. if path.exists(iusePath) else \
  878. []
  879. use = open(usePath,'r').read().strip().split() \
  880. if path.exists(usePath) else \
  881. []
  882. return (map(lambda x:x[1:] if x.startswith("+") else x,
  883. filter(lambda x:x,
  884. use)),
  885. map(lambda x:x[1:] if x.startswith("+") else x,
  886. filter(lambda x:x,
  887. iuse)))
  888. def isPkgInstalled(pkg,prefix='/'):
  889. """Check is package installed"""
  890. pkgDir = path.join(prefix,'var/db/pkg')
  891. if "/" in pkg:
  892. category,op,pkg = pkg.partition('/')
  893. return bool(
  894. filter(lambda x:x['PN'] == pkg,
  895. map(reVerSplitToPV,
  896. listDirectory(path.join(pkgDir,category)))))
  897. else:
  898. return bool(
  899. filter(lambda x: filter(lambda y:y['PN'] == pkg,
  900. map(reVerSplitToPV,
  901. listDirectory(x))),
  902. listDirectory(pkgDir,fullPath=True)))
  903. def getPkgActiveUses(fullpkg):
  904. """Get active uses from package"""
  905. res = getPkgUses(fullpkg)
  906. if not res:
  907. return None
  908. return list(set(res[0]) & set(res[1]))
  909. def getSquashList():
  910. """Get supprted squashfs compressions method"""
  911. wantMethod = set(["lzo","lzma","xz","gzip"])
  912. usesSquashFs = getPkgActiveUses("sys-fs/squashfs-tools")
  913. if not usesSquashFs:
  914. return ["gzip"]
  915. else:
  916. return map(lambda x:{"lzma":"xz"}.get(x,x),
  917. list(set(usesSquashFs) & wantMethod))
  918. def countPartitions(devname):
  919. """Count partition for specified device"""
  920. syspath = getUdevDeviceInfo(name=devname).get('DEVPATH','')
  921. if not syspath:
  922. return 0
  923. deviceName = path.basename(syspath)
  924. if not syspath.startswith("/sys"):
  925. syspath = pathJoin("/sys",syspath)
  926. return len(filter(lambda x:x.startswith(deviceName),
  927. listDirectory(syspath)))
  928. def getLvmGroups():
  929. """Get LVM groups"""
  930. pvdisplayCmd = getProgPath('/sbin/pvdisplay')
  931. pvdata = process(pvdisplayCmd,"-C","-o", "vg_name","--noh")
  932. return filter(lambda x:x,pvdata.read().split())
  933. def getLvmPartitions(vg_name,lv_name,cache=[]):
  934. """Get lvm partitions"""
  935. if not cache:
  936. pvdisplayCmd = getProgPath('/sbin/pvdisplay')
  937. pvdata = process(pvdisplayCmd,"-C","-o",
  938. "vg_name,lv_name,pv_name","--noh")
  939. if pvdata.success():
  940. cache.extend(
  941. filter(lambda x:x and len(x)==3,
  942. map(lambda x:x.split(),
  943. pvdata.read().split('\n'))))
  944. if cache:
  945. res = map(lambda x:x[2],
  946. filter(lambda x:x[0]==vg_name and x[1]==lv_name,cache))
  947. if res:
  948. return res
  949. return []
  950. def getPartitionDevice(syspath):
  951. """Get real parent device by partition,lvm,mdraid"""
  952. prop = getUdevDeviceInfo(path=syspath)
  953. # real device
  954. if prop.get('ID_TYPE',"") == "disk" and \
  955. prop.get('DEVTYPE',"") == "disk":
  956. return prop.get('DEVNAME',"")
  957. # partition
  958. if prop.get('DEVTYPE') == "partition":
  959. return getPartitionDevice(path.dirname(syspath))
  960. # md raid
  961. if prop.get('MD_LEVEL',"").startswith("raid"):
  962. if not syspath.startswith('/sys'):
  963. syspath = pathJoin('/sys',syspath)
  964. syspath = pathJoin(syspath,"md")
  965. for rd in filter(lambda x:path.basename(x).startswith('rd'),
  966. listDirectory(syspath,fullPath=True)):
  967. rdBlockPath = path.join(rd,"block")
  968. if path.exists(rdBlockPath):
  969. return getPartitionDevice(path.realpath(rdBlockPath))
  970. else:
  971. return ""
  972. # lvm
  973. if prop.get('DM_LV_NAME',"") != "":
  974. parts = getLvmPartitions(prop.get('DM_VG_NAME',''),
  975. prop.get('DM_LV_NAME',''))
  976. if parts:
  977. propPartLvm = getUdevDeviceInfo(name=parts[0])
  978. if 'DEVPATH' in propPartLvm:
  979. return getPartitionDevice(propPartLvm['DEVPATH'])
  980. return ""
  981. def getAvailableX11Drivers(prefix="/"):
  982. """Get available x11 drivers (Depricated Function)"""
  983. xorg_modules_dir = path.join(prefix,'usr/lib/xorg/modules/drivers')
  984. return map(lambda x: x[:-7],
  985. filter(lambda x:x.endswith('_drv.so'),
  986. listDirectory(xorg_modules_dir)))
  987. def lspci(filtername=None,shortInfo=False):
  988. """Get hash of lspci, filtred by filtername. If shortInfo, then
  989. type,vendor and name get only first word
  990. pcidata(domain,bus,slot,func)
  991. 'type'
  992. 'vendor'
  993. 'name'"""
  994. reData = re.compile(r'(\S+)\s"([^"]+)"\s+"([^"]+)"\s+"([^"]+)"',re.S)
  995. if filtername:
  996. filterfunc = lambda x: filtername in x
  997. else:
  998. filterfunc = lambda x:x
  999. if shortInfo:
  1000. sfunc = lambda x:x.partition(" ")[0]
  1001. else:
  1002. sfunc = lambda x:x
  1003. lspciProg = checkUtils('/usr/sbin/lspci')
  1004. processLsPci = process(lspciProg,"-m")
  1005. retData = {}
  1006. for device in map(lambda x:x.groups(),
  1007. filter(lambda x:x,
  1008. map(reData.search,
  1009. filter(filterfunc,
  1010. processLsPci)))):
  1011. retData[device[0]] = {'type':sfunc(device[1]),\
  1012. 'vendor':sfunc(device[2]),\
  1013. 'name':sfunc(device[3])}
  1014. return retData
  1015. def getUdevDeviceInfo(path="",name=""):
  1016. """Get device info by syspath of name"""
  1017. udevadmCmd = getProgPath('/sbin/udevadm')
  1018. typeQuery = "--path" if path else "--name"
  1019. value = path if path else name
  1020. return dict(
  1021. filter(lambda x:x[0],
  1022. map(lambda x:x.partition("=")[0::2],
  1023. process(udevadmCmd,"info","--query","property",
  1024. typeQuery,value).read().split("\n"))))
  1025. def getPartitionSize(dev):
  1026. """Get partition size"""
  1027. SECTORSIZE=512
  1028. sizeFile = pathJoin(dev,"size")
  1029. if path.exists(sizeFile):
  1030. size = int(open(sizeFile,'r').read().strip())*SECTORSIZE
  1031. suffix = (((1024**0),"",False),
  1032. ((1024**1),"K",False),
  1033. ((1024**2),"M",False),
  1034. ((1024**3),"G",True),
  1035. ((1024**4),"T",True),
  1036. ((1024**5),"P",True))
  1037. suffix = filter(lambda x:size >x[0],suffix)
  1038. if suffix:
  1039. suffix = suffix[-1]
  1040. printSize = size / (suffix[0]/10)
  1041. printSizeTail = printSize % 10
  1042. printSize = printSize / 10
  1043. if suffix[2] and printSizeTail:
  1044. return "%d.%d%s"%(printSize,printSizeTail,suffix[1])
  1045. else:
  1046. return "%d%s"%(printSize,suffix[1])
  1047. return ""
  1048. def getDeviceType(syspath):
  1049. """Get device type (disk,partition,lvm,raid)"""
  1050. prop = getUdevDeviceInfo(path=syspath)
  1051. # real device
  1052. if prop.get('ID_CDROM',""):
  1053. return "cdrom"
  1054. if prop.get('ID_TYPE',"") == "disk" and \
  1055. prop.get('DEVTYPE',"") == "disk":
  1056. return "disk"
  1057. # partition
  1058. if prop.get('DEVTYPE') == "partition":
  1059. return getDeviceType(path.dirname(syspath))+"-partition"
  1060. # md raid
  1061. if prop.get('MD_LEVEL',"").startswith("raid"):
  1062. if not syspath.startswith('/sys'):
  1063. syspath = pathJoin('/sys',syspath)
  1064. syspath = pathJoin(syspath,"md")
  1065. for rd in filter(lambda x:path.basename(x).startswith('rd'),
  1066. listDirectory(syspath,fullPath=True)):
  1067. rdBlockPath = path.join(rd,"block")
  1068. if path.exists(rdBlockPath):
  1069. return getDeviceType(path.realpath(rdBlockPath))+"-raid"
  1070. else:
  1071. return "loop"
  1072. # lvm
  1073. if prop.get('DM_LV_NAME',"") != "":
  1074. parts = getLvmPartitions(prop.get('DM_VG_NAME',''),
  1075. prop.get('DM_LV_NAME',''))
  1076. if parts:
  1077. propPartLvm = getUdevDeviceInfo(name=parts[0])
  1078. if 'DEVPATH' in propPartLvm:
  1079. return getDeviceType(propPartLvm['DEVPATH'])+"-lvm"
  1080. return "loop"
  1081. def getRaidPartitions(raidpath):
  1082. """Get raid partitions"""
  1083. prop = getUdevDeviceInfo(path=raidpath)
  1084. raidParts = []
  1085. if prop.get('MD_LEVEL',"").startswith("raid"):
  1086. if not raidpath.startswith('/sys'):
  1087. raidpath = pathJoin('/sys',raidpath)
  1088. raidpath = pathJoin(raidpath,"md")
  1089. for rd in filter(lambda x:path.basename(x).startswith('rd'),
  1090. listDirectory(raidpath,fullPath=True)):
  1091. rdpath = path.join(raidpath,rd,"block")
  1092. if path.exists(rdpath):
  1093. raidParts.append(
  1094. getUdevDeviceInfo(path=path.realpath(rdpath)).get(
  1095. "DEVNAME",''))
  1096. return filter(lambda x:x,raidParts)
  1097. def getPartitionType(prop):
  1098. """Get type of dos part table (primary,extended or logical)"""
  1099. if prop.get('ID_PART_TABLE_TYPE') == 'dos':
  1100. partId = prop.get('ID_PART_ENTRY_TYPE','')
  1101. partNumber = prop.get('ID_PART_ENTRY_NUMBER','')
  1102. if partId and partNumber:
  1103. if partId == "0x5":
  1104. return "extended"
  1105. elif int(partNumber)>4:
  1106. return "logical"
  1107. else:
  1108. return "primary"
  1109. return prop.get('ID_PART_TABLE_TYPE','')
  1110. def detectBuild(pathname,dictInfo):
  1111. """Detect build by root passwd 'root'"""
  1112. shadowPath = pathJoin(pathname,'/etc/shadow')
  1113. if r"root:$1$JMvNh5xg$VnV1DyJdTcwuZ0hp5YiJG0:14349:0:::::" in \
  1114. readFile(shadowPath):
  1115. dictInfo['type'] = ' assemble'
  1116. elif path.exists(pathJoin(pathname,"delta")) and \
  1117. path.exists(pathJoin(pathname,"workspace")):
  1118. dictInfo['type'] = " builder"
  1119. issue = readFile(pathJoin(pathname,'etc/gentoo-release'))
  1120. if "Server" in issue:
  1121. if "Scratch" in issue:
  1122. dictInfo['name'] = "CSS"
  1123. else:
  1124. dictInfo['name'] = "CDS"
  1125. elif "Desktop" in issue:
  1126. if "XFCE" in issue:
  1127. dictInfo['name'] = "CLDX"
  1128. elif "KDE" in issue:
  1129. dictInfo['name'] = "CLD"
  1130. elif "GNOME" in issue:
  1131. dictInfo['name'] = "CLDG"
  1132. elif "Scratch" in issue:
  1133. dictInfo['name'] = "CLS"
  1134. else:
  1135. dictInfo['type'] = ''
  1136. return dictInfo
  1137. def getOsProberHash(getContentFunc=None):
  1138. """Get partition content by os-prober"""
  1139. os_prober = getProgPath('/usr/bin/os-prober')
  1140. if os_prober:
  1141. DEV,LONG,SHORT,TYPE = 0,1,2,3
  1142. osProberList = \
  1143. map(lambda x:[getUdevDeviceInfo(name=x[DEV]).get('DEVNAME',''),
  1144. x[LONG],x[SHORT],x[TYPE]],
  1145. filter(lambda x:len(x)>=4,
  1146. map(lambda x:x.split(":"),
  1147. process(os_prober))))
  1148. for osRecord in osProberList:
  1149. if "Gentoo" in osRecord[SHORT] and getContentFunc:
  1150. osDescr = getContentFunc(osRecord[DEV],addFunc=detectBuild)
  1151. if "name" in osDescr and "march" in osDescr and \
  1152. "build" in osDescr and "ver" in osDescr and \
  1153. (osDescr["ver"] != "0" or osDescr["build"]):
  1154. if osDescr['build']:
  1155. osDescr['build'] = "-%s"%osDescr['build']
  1156. else:
  1157. osDescr['build'] = "-%s"%osDescr['ver']
  1158. osRecord[SHORT] = \
  1159. "{name}-{march}{build}{type}".format(**osDescr)
  1160. else:
  1161. osRecord[SHORT] = "Gentoo"
  1162. elif "Gentoo" in osRecord[SHORT] and "Calculate" in osRecord[LONG]:
  1163. osRecord[SHORT] = "Calculate"
  1164. osProberHash = \
  1165. dict(
  1166. map(lambda x:(x[DEV],x[SHORT]),
  1167. osProberList))
  1168. else:
  1169. osProberHash = {}
  1170. return osProberHash