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.

1757 lines
71 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 Mir Calculate. 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. from functools import wraps
  16. import random
  17. import sys
  18. from os import path
  19. import os
  20. import time
  21. from calculate.core.server.gen_pid import search_worked_process
  22. from calculate.core.setup_cache import Cache as SetupCache
  23. from calculate.core.server.func import MethodsInterface
  24. from calculate.lib.cl_template import SystemIni, LayeredIni
  25. from calculate.lib.datavars import DataVarsError, VariableError, Variable
  26. from calculate.lib.utils.tools import AddonError
  27. from calculate.lib.utils.colortext.palette import TextState
  28. from calculate.lib.utils.colortext import get_color_print
  29. from calculate.update.emerge_parser import RevdepPercentBlock
  30. from calculate.update.datavars import DataVarsUpdate
  31. from calculate.update.update_info import UpdateInfo
  32. from calculate.lib.utils.binhosts import (Binhosts, BinhostSignError,
  33. BinhostError, PackagesIndex, DAYS)
  34. from calculate.lib.utils.gpg import GPG, GPGError
  35. from calculate.lib.utils.common import get_fastlogin_domain_path
  36. from calculate.lib.cl_log import log
  37. import hashlib
  38. import re
  39. import shutil
  40. from collections import MutableSet
  41. from contextlib import contextmanager
  42. import tempfile
  43. from fnmatch import fnmatch
  44. from calculate.lib.utils.vardb import Vardb
  45. from calculate.lib.utils.git import Git, GitError, MTimeKeeper, NotGitError
  46. from calculate.lib.utils.portage import (ReposConf, EmergeLog,
  47. EmergeLogNamedTask,
  48. VDB_PATH,
  49. PackageInformation,
  50. PortageState,
  51. get_packages_files_directory,
  52. get_manifest_files_directory,
  53. get_remove_list)
  54. from calculate.lib.utils.text import _u8, _u
  55. Colors = TextState.Colors
  56. from calculate.lib.utils.files import (getProgPath, STDOUT, removeDir,
  57. getParentPids,
  58. PercentProgress, process, getRunCommands,
  59. readFile, listDirectory, pathJoin,
  60. find, FindFileType,quite_unlink,
  61. writeFile, makeDirectory)
  62. import emerge_parser
  63. import logging
  64. from emerge_parser import (EmergeParser, EmergeCommand, EmergeError,
  65. EmergeCache, Chroot)
  66. from calculate.lib.cl_lang import (setLocalTranslate, getLazyLocalTranslate,
  67. RegexpLocalization, _)
  68. setLocalTranslate('cl_update3', sys.modules[__name__])
  69. __ = getLazyLocalTranslate(_)
  70. class UpdateError(AddonError):
  71. """Update Error"""
  72. class OverlayOwnCache(MutableSet):
  73. """
  74. Сет оверлеев с интегрированным кэшем
  75. """
  76. def __init__(self, dv=None):
  77. self.dv = dv
  78. def __get_overlays(self):
  79. own_cache_value = SystemIni(self.dv).getVar('system', 'own_cache') or ""
  80. return [x.strip() for x in own_cache_value.split(',') if x.strip()]
  81. def __write_overlays(self, overlays):
  82. if not overlays:
  83. SystemIni(self.dv).delVar('system', 'own_cache')
  84. else:
  85. SystemIni(self.dv).setVar('system',
  86. {'own_cache': ",".join(overlays)})
  87. def __contains__(self, item):
  88. return item in self.__get_overlays()
  89. def __iter__(self):
  90. return iter(self.__get_overlays())
  91. def __len__(self):
  92. return len(self.__get_overlays())
  93. def __append_value(self, overlays, value):
  94. if value not in overlays:
  95. overlays.append(value)
  96. self.__write_overlays(overlays)
  97. def add(self, value):
  98. overlays = self.__get_overlays()
  99. self.__append_value(overlays, value)
  100. def discard(self, value):
  101. overlays = self.__get_overlays()
  102. if value in overlays:
  103. overlays.remove(value)
  104. self.__write_overlays(overlays)
  105. def variable_module(var_env):
  106. def variable_module_decor(f):
  107. @wraps(f)
  108. def wrapper(self, *args, **kw):
  109. old_env = self.clVars.defaultModule
  110. try:
  111. self.clVars.defaultModule = var_env
  112. return f(self, *args, **kw)
  113. finally:
  114. self.clVars.defaultModule = old_env
  115. return wrapper
  116. return variable_module_decor
  117. class Update(MethodsInterface):
  118. """Основной объект для выполнения действий связанных с обновлением системы
  119. """
  120. def init(self):
  121. commandLog = path.join(self.clVars.Get('core.cl_log_path'),
  122. 'lastcommand.log')
  123. emerge_parser.CommandExecutor.logfile = commandLog
  124. self.color_print = get_color_print()
  125. self.emerge_cache = EmergeCache()
  126. if self.clVars.Get('cl_env_debug_set') == 'off':
  127. EmergeCache.logger.logger.setLevel(logging.WARNING)
  128. self.emerge_cache.check_list = (
  129. self.emerge_cache.check_list +
  130. map(lambda x:emerge_parser.GitCheckvalue(x, self.getGit()),
  131. self.clVars.Get('update.cl_update_rep_path')))
  132. self.update_map = {}
  133. self.refresh_binhost = False
  134. self.pkgnum = None
  135. self.pkgnummax = None
  136. self.gpgdata_md5 = []
  137. self.gpg_changed = False
  138. def get_prog_path(self, program_name):
  139. return getProgPath(program_name)
  140. def getGit(self):
  141. return self.clVars.Get('cl_update_git')
  142. @contextmanager
  143. def private_repo(self, rpath, url):
  144. if Git.is_private_url(url):
  145. try:
  146. if not path.exists(rpath):
  147. makeDirectory(rpath)
  148. os.chmod(rpath, 0700)
  149. yield
  150. finally:
  151. try:
  152. for dn in (Git._gitDir(rpath), path.join(rpath, "profiles/templates")):
  153. if path.exists(dn):
  154. os.chmod(dn, 0700)
  155. for fn in find(path.join(rpath, "profiles"), True, FindFileType.RegularFile,
  156. True, None, downfilter=lambda x: not x.endswith("/templates")):
  157. if fn.endswith("calculate.env") or fn.endswith("ini.env"):
  158. os.chmod(fn, 0600)
  159. if path.exists(rpath):
  160. os.chmod(rpath, 0755)
  161. except OSError:
  162. pass
  163. else:
  164. yield
  165. def _syncRepository(self, name, url, rpath, revision,
  166. cb_progress=None, clean=False, notask=False):
  167. """
  168. Синхронизировать репозитори
  169. """
  170. dv = self.clVars
  171. git = self.getGit()
  172. info_outdated = False
  173. old_dir = "%s.old" % git._gitDir(rpath)
  174. if path.exists(old_dir):
  175. clean = True
  176. try:
  177. self.stash_cache(rpath, name)
  178. if not git.checkExistsRep(rpath):
  179. if not notask:
  180. self.startTask(_("Syncing the {rep} repository").format(
  181. rep=name.capitalize()))
  182. self.addProgress()
  183. with self.private_repo(rpath, url):
  184. git.cloneTagRepository(url, rpath, revision,
  185. cb_progress=cb_progress)
  186. info_outdated = True
  187. else:
  188. cr = ""
  189. try:
  190. need_update = False
  191. tag_cr = git.getCommit(rpath, revision)
  192. cr = git.getCurrentCommit(rpath)
  193. ref_type = git.reference_type(rpath, revision)
  194. if tag_cr != cr or ref_type == Git.Reference.Branch:
  195. need_update = True
  196. elif clean:
  197. status = git.getStatusInfo(rpath)
  198. if not status or status['files']:
  199. need_update = True
  200. except GitError as e:
  201. need_update = True
  202. if need_update:
  203. if not notask:
  204. self.startTask(_("Syncing the {rep} repository").format(
  205. rep=name.capitalize()))
  206. self.addProgress()
  207. with self.private_repo(rpath, url):
  208. git.updateTagRepository(url, rpath, revision,
  209. cb_progress=cb_progress,
  210. clean=clean)
  211. new_cr = git.getCurrentCommit(rpath)
  212. if new_cr != cr:
  213. info_outdated = True
  214. if info_outdated:
  215. self.raiseOutdate()
  216. dv.Set('cl_update_outdate_set', 'on', force=True)
  217. finally:
  218. self.unstash_cache(rpath, name)
  219. # TODO: debug1
  220. #dv.Set('cl_update_outdate_set', 'on', force=True)
  221. return True
  222. def raiseOutdate(self):
  223. self.clVars.Set('cl_update_outdate_set', 'on', force=True)
  224. def setAutocheckParams(self, status, interval, update_other, cleanpkg):
  225. """
  226. Настроить параметры автопроверки обновлений
  227. """
  228. onoff = lambda x: "on" if x else "off"
  229. self.clVars.Write('cl_update_autocheck_set', onoff(status), True)
  230. self.clVars.Write('cl_update_autocheck_interval', interval, True)
  231. self.clVars.Write('cl_update_other_set', onoff(update_other), True)
  232. self.clVars.Write('cl_update_cleanpkg_set', onoff(cleanpkg), True)
  233. if not status:
  234. UpdateInfo.set_update_ready(False)
  235. return True
  236. def checkSchedule(self, interval, status):
  237. """
  238. Проверить по расписанию необходимость запуска команды
  239. """
  240. if not status:
  241. self.printWARNING(_("Updates autocheck is not enabled"))
  242. return False
  243. last_check = SystemIni(self.clVars).getVar('system', 'last_check') or ""
  244. re_interval = re.compile("^(\d+)\s*(hours?|days?|weeks?)?", re.I)
  245. interval_match = re_interval.search(interval)
  246. MINUTE = 60
  247. HOUR = MINUTE * 60
  248. DAY = HOUR * 24
  249. WEEK = DAY * 7
  250. if interval_match:
  251. if interval_match.group(2):
  252. suffix_map = {'h': HOUR, 'd': DAY, 'w': WEEK}
  253. k = suffix_map.get(interval_match.group(2).lower()[0], HOUR)
  254. else:
  255. k = HOUR
  256. est = int(interval_match.group(1)) * k
  257. else:
  258. est = 3 * HOUR
  259. if last_check:
  260. if last_check.isdigit():
  261. if (time.time() - int(last_check)) < (est - 10 * MINUTE):
  262. self.printWARNING(_("Please wait for the update time"))
  263. return False
  264. self.mark_schedule()
  265. return True
  266. def checkRun(self, wait_update):
  267. """
  268. Проверить повторный запуск
  269. """
  270. update_running = lambda: any(os.getpid() != x
  271. for x in
  272. search_worked_process('update', dv))
  273. dv = self.clVars
  274. if update_running():
  275. if not wait_update:
  276. raise UpdateError(_("Update is already running. "
  277. "Try to run later."))
  278. else:
  279. self.startTask(_("Waiting for another update to be complete"))
  280. while update_running():
  281. self.pauseProcess()
  282. while update_running():
  283. time.sleep(0.3)
  284. self.resumeProcess()
  285. time.sleep(random.random() * 3)
  286. self.endTask()
  287. if self.clVars.Get('cl_chroot_status') == 'off':
  288. parents_pids = set(getParentPids(os.getpid()))
  289. emerge_running = lambda : (
  290. any((fnmatch(x[1], "*python-exec/*/emerge*") and
  291. x[0] not in parents_pids)
  292. for x in getRunCommands(True, withpid=True)))
  293. if emerge_running():
  294. if not wait_update:
  295. raise UpdateError(_("Emerge is running. "
  296. "Try to run later."))
  297. else:
  298. self.startTask(_("Waiting for emerge to be complete"))
  299. while emerge_running():
  300. time.sleep(1)
  301. self.endTask()
  302. return True
  303. @variable_module("update")
  304. def trimRepositories(self, repname):
  305. """
  306. Синхронизировать репозитории
  307. """
  308. dv = self.clVars
  309. rpath = \
  310. dv.select('cl_update_rep_path', cl_update_rep_name=repname, limit=1)
  311. git = self.getGit()
  312. self.addProgress()
  313. git.trimRepository(rpath, cb_progress=self.setProgress)
  314. return True
  315. def migrateLayman(self, reposdir, laymandir, repname, rpath_orig):
  316. if rpath_orig.startswith("/var/db/repos/"):
  317. dn_name = os.path.basename(rpath_orig)
  318. repos_fullname = pathJoin(reposdir, dn_name)
  319. layman_fullname = pathJoin(laymandir, dn_name)
  320. if (not os.path.islink(layman_fullname) and
  321. os.path.isdir(layman_fullname) and
  322. not os.path.exists(repos_fullname)):
  323. self.startTask(_("Move {repname} from {laymandir} to {reposdir}").format(
  324. repname=repname.capitalize(),
  325. laymandir="/var/lib/layman",
  326. reposdir="/var/db/repos"))
  327. symlink_target = os.path.relpath(repos_fullname, laymandir)
  328. if not os.path.exists(reposdir):
  329. makeDirectory(reposdir)
  330. os.rename(layman_fullname, repos_fullname)
  331. os.symlink(symlink_target, layman_fullname)
  332. #print "MYDEBUG", reposdir, laymandir, rpath_orig
  333. self.endTask(True)
  334. return True
  335. @variable_module("update")
  336. def syncRepositories(self, repname, fallback_sync=False,
  337. clean_on_error=True):
  338. """
  339. Синхронизировать репозитории
  340. """
  341. dv = self.clVars
  342. check_status = dv.GetBool('update.cl_update_check_rep_set')
  343. url, rpath, revision = (
  344. dv.Select(["cl_update_rep_url", "cl_update_rep_path",
  345. "cl_update_rep_rev"],
  346. where="cl_update_rep_name", eq=repname, limit=1))
  347. if not url or not rpath:
  348. raise UpdateError(_("Configuration variables for repositories "
  349. "are not setup"))
  350. git = self.getGit()
  351. if not git.checkUrl(url):
  352. raise UpdateError(_("Git %s is unavailable") % url)
  353. chroot_path = path.normpath(self.clVars.Get('cl_chroot_path'))
  354. if chroot_path == '/':
  355. rpath_orig = rpath
  356. else:
  357. rpath_orig = rpath[len(chroot_path):]
  358. mtime = MTimeKeeper(path.join(rpath, "profiles/updates"))
  359. mtime.save()
  360. self.migrateLayman(dv.Get('cl_update_repos_storage'),
  361. dv.Get('cl_update_layman_storage'), repname, rpath_orig)
  362. try:
  363. if clean_on_error:
  364. try:
  365. repconf = ReposConf(dv.Get('cl_update_reposconf'),
  366. dv.Get('cl_update_reposconf_dir'),
  367. prefix=chroot_path)
  368. repconf.add(repname, url, rpath_orig)
  369. if not self._syncRepository(repname, url, rpath, revision,
  370. cb_progress=self.setProgress,
  371. clean=check_status,
  372. notask=fallback_sync):
  373. return "skip"
  374. return True
  375. except GitError as e:
  376. if not isinstance(e, NotGitError):
  377. if e.addon:
  378. self.printWARNING(str(e.addon))
  379. self.printWARNING(str(e))
  380. self.endTask(False)
  381. self.startTask(
  382. _("Re-fetching the {name} repository").format(
  383. name=repname))
  384. self.addProgress()
  385. rpath_new = "%s_new" % rpath
  386. try:
  387. self._syncRepository(repname, url, rpath_new, revision,
  388. cb_progress=self.setProgress,
  389. clean=check_status,
  390. notask=fallback_sync)
  391. removeDir(rpath)
  392. shutil.move(rpath_new, rpath)
  393. except OSError as e:
  394. raise UpdateError(_("Failed to modify the "
  395. "{repname} repository").format(
  396. repname=repname) + _(": ") + str(e))
  397. finally:
  398. if path.exists(rpath_new):
  399. removeDir(rpath_new)
  400. else:
  401. if not self._syncRepository(repname, url, rpath, revision,
  402. clean=check_status):
  403. return "skip"
  404. repconf = ReposConf(dv.Get('cl_update_reposconf'),
  405. dv.Get('cl_update_reposconf_dir'),
  406. prefix=chroot_path)
  407. repconf.add(repname, url, rpath_orig)
  408. finally:
  409. mtime.restore()
  410. return True
  411. metadata_cache_names = ("metadata/md5-cache", "metadata/cache")
  412. def stash_cache(self, rpath, name):
  413. """
  414. Спрятать кэш
  415. """
  416. if name in ("portage", "gentoo"):
  417. return
  418. if not name in OverlayOwnCache(self.clVars):
  419. for cachename in self.metadata_cache_names:
  420. cachedir = path.join(rpath, cachename)
  421. if path.exists(cachedir):
  422. try:
  423. cachedir_s = path.join(path.dirname(rpath),
  424. path.basename(
  425. cachename) + ".stash")
  426. if path.exists(cachedir_s):
  427. removeDir(cachedir_s)
  428. shutil.move(cachedir, cachedir_s)
  429. except BaseException as e:
  430. pass
  431. def unstash_cache(self, rpath, name):
  432. """
  433. Извлеч кэш
  434. """
  435. if name in ("portage", "gentoo"):
  436. return
  437. cachenames = self.metadata_cache_names
  438. if not name in OverlayOwnCache(self.clVars):
  439. if any(path.exists(path.join(rpath, x)) for x in cachenames):
  440. for cachename in cachenames:
  441. cachedir_s = path.join(path.dirname(rpath),
  442. path.basename(cachename) + ".stash")
  443. if path.exists(cachedir_s):
  444. try:
  445. removeDir(cachedir_s)
  446. except BaseException as e:
  447. pass
  448. OverlayOwnCache(self.clVars).add(name)
  449. else:
  450. for cachename in cachenames:
  451. cachedir = path.join(rpath, cachename)
  452. cachedir_s = path.join(path.dirname(rpath),
  453. path.basename(cachename) + ".stash")
  454. if path.exists(cachedir_s):
  455. try:
  456. shutil.move(cachedir_s, cachedir)
  457. except BaseException as e:
  458. pass
  459. else:
  460. if all(not path.exists(path.join(rpath, x)) for x in cachenames):
  461. OverlayOwnCache(self.clVars).discard(name)
  462. def syncOtherRepository(self, repname):
  463. """
  464. Обновить репозиторий через emerge --sync
  465. """
  466. emerge = self.get_prog_path('/usr/bin/emerge')
  467. if not emerge:
  468. raise UpdateError(_("The Emerge is not found"))
  469. rpath = self.clVars.Select('cl_update_other_rep_path',
  470. where='cl_update_other_rep_name', eq=repname,
  471. limit=1)
  472. repdirname = path.basename(rpath)
  473. self.stash_cache(rpath, repdirname)
  474. try:
  475. if Git.is_git(rpath):
  476. self.addProgress()
  477. p = PercentProgress(emerge, "--sync", repname, part=1, atty=True)
  478. for perc in p.progress():
  479. self.setProgress(perc)
  480. else:
  481. p = process(emerge, "--sync", repname, stderr=STDOUT)
  482. if p.failed():
  483. raise UpdateError(
  484. _("Failed to update the {rname} repository").format(
  485. rname=repname),
  486. addon=p.read())
  487. finally:
  488. self.unstash_cache(rpath, repdirname)
  489. return True
  490. def _regenCache_process(self, progname, repname, cpu_num):
  491. return process(progname, "--repo=%s" % repname, "--update",
  492. "--jobs=%s" % cpu_num, stderr=STDOUT)
  493. def regenCache(self, repname):
  494. """
  495. Обновить кэш метаданных репозитория
  496. """
  497. egenCache = self.get_prog_path('/usr/bin/egencache')
  498. if not egenCache:
  499. raise UpdateError(_("The Portage tool is not found"))
  500. if repname in self.clVars.Get('cl_update_rep_name'):
  501. path_rep = self.clVars.Select('cl_update_rep_path',
  502. where='cl_update_rep_name',
  503. eq=repname, limit=1)
  504. repo_name = readFile(
  505. path.join(path_rep, "profiles/repo_name")).strip()
  506. if repo_name != repname:
  507. self.printWARNING(
  508. _("Repository '{repo_name}' called '{repname}'"
  509. " in cl_update_rep_name").format(
  510. repo_name=repo_name, repname=repname))
  511. raise UpdateError(_("Failed to update the cache of the {rname} "
  512. "repository").format(rname=repname))
  513. cpu_num = self.clVars.Get('hr_cpu_num')
  514. if repname in OverlayOwnCache(self.clVars):
  515. self.printWARNING(
  516. _("Repository %s has its own cache") % repname.capitalize())
  517. else:
  518. self.startTask(_("Updating the %s repository cache") %
  519. repname.capitalize())
  520. p = self._regenCache_process(egenCache, repname, cpu_num)
  521. if p.failed():
  522. raise UpdateError(_("Failed to update the cache of the {rname} "
  523. "repository").format(rname=repname),
  524. addon=p.read())
  525. return True
  526. def emergeMetadata(self):
  527. """
  528. Выполнить egencache и emerge --metadata
  529. """
  530. emerge = self.get_prog_path("/usr/bin/emerge")
  531. if not emerge:
  532. raise UpdateError(_("The Emerge tool is not found"))
  533. self.addProgress()
  534. p = PercentProgress(emerge, "--ask=n", "--metadata", part=1, atty=True)
  535. for perc in p.progress():
  536. self.setProgress(perc)
  537. if p.failed():
  538. data = p.read()
  539. with open('/var/log/calculate/failed-metadata-%d.log' % time.time(),
  540. 'w') as f:
  541. f.write(data + p.alldata)
  542. raise UpdateError(_("Failed to update metadata"), addon=data)
  543. return True
  544. def _eixUpdateCommand(self, eix_cmd, countRep):
  545. return PercentProgress(eix_cmd, "-F", part=countRep or 1, atty=True)
  546. def _copyPreviousEix(self):
  547. chroot_path = self.clVars.Get("cl_chroot_path")
  548. portage_eix = pathJoin(chroot_path, "var/cache/eix/portage.eix")
  549. previous_eix = pathJoin(chroot_path, "var/cache/eix/previous.eix")
  550. if path.exists(portage_eix):
  551. if path.exists(previous_eix):
  552. os.unlink(previous_eix)
  553. try:
  554. with writeFile(previous_eix) as f:
  555. f.write(readFile(portage_eix))
  556. portage_eix_stat = os.stat(portage_eix)
  557. os.chmod(previous_eix, portage_eix_stat.st_mode)
  558. os.chown(previous_eix, portage_eix_stat.st_uid,
  559. portage_eix_stat.st_gid)
  560. except (OSError,IOError):
  561. self.printWARNING(_("Failed to create previous eix"))
  562. return True
  563. def eixUpdate(self, repositories):
  564. """
  565. Выполенине eix-update для репозиторием
  566. eix-update выполнятется только для тех репозиториев, которые
  567. обновлялись, если cl_update_eixsync_force==auto, либо
  568. все, если cl_update_eixupdate_force==force
  569. """
  570. self._copyPreviousEix()
  571. eixupdate = self.get_prog_path("/usr/bin/eix-update")
  572. if not eixupdate:
  573. raise UpdateError(_("The Eix tool is not found"))
  574. self.addProgress()
  575. countRep = len(repositories)
  576. p = self._eixUpdateCommand(eixupdate, countRep)
  577. for perc in p.progress():
  578. self.setProgress(perc)
  579. if p.failed():
  580. raise UpdateError(_("Failed to update eix cache"), addon=p.read())
  581. return True
  582. def is_binary_pkg(self, pkg, binary=None):
  583. """
  584. Является ли пакет бинарным
  585. """
  586. if binary:
  587. return True
  588. if 'PN' in pkg and pkg['PN'].endswith('-bin'):
  589. return True
  590. if binary is not None:
  591. return binary
  592. if "binary" in pkg and pkg['binary']:
  593. return True
  594. return False
  595. def _printEmergePackage(self, pkg, binary=False, num=1, max_num=1):
  596. """
  597. Вывод сообщения сборки пакета
  598. """
  599. self.endTask()
  600. _print = self.color_print
  601. if self.pkgnum is not None:
  602. num = self.pkgnum
  603. if self.pkgnummax is not None:
  604. max_num = self.pkgnummax
  605. one = _print("{0}", num)
  606. two = _print("{0}", max_num)
  607. part = _("({current} of {maximum})").format(current=one, maximum=two)
  608. _print = _print.foreground(Colors.DEFAULT)
  609. if self.is_binary_pkg(pkg, binary):
  610. _colorprint = _print.foreground(Colors.PURPLE)
  611. else:
  612. _colorprint = _print.foreground(Colors.GREEN)
  613. PackageInformation.add_info(pkg)
  614. name = ""
  615. if pkg.info['DESCRIPTION']:
  616. name = _(pkg.info['DESCRIPTION'])
  617. name = name[:1].upper() + name[1:]
  618. if not name.strip():
  619. name = str(pkg)
  620. self.printSUCCESS(
  621. _u(_("{part} {package}")).format(part=_u(part), package=_u(name)))
  622. self.startTask(
  623. _u(_("Emerging {package}")).format(package=_u(_colorprint(str(pkg)))))
  624. def _printInstallPackage(self, pkg, binary=False):
  625. """
  626. Вывод сообщения установки пакета
  627. """
  628. self.endTask()
  629. _print = self.color_print
  630. if self.is_binary_pkg(pkg, binary):
  631. _print = _print.foreground(Colors.PURPLE)
  632. else:
  633. _print = _print.foreground(Colors.GREEN)
  634. pkg_key = "{CATEGORY}/{PF}".format(**pkg)
  635. if pkg_key in self.update_map:
  636. self.startTask(_("Installing {pkg} [{oldver}]").format(
  637. pkg=_print(str(pkg)), oldver=self.update_map[pkg_key]))
  638. self.update_map.pop(pkg_key)
  639. else:
  640. self.startTask(_("Installing %s") % (_print(str(pkg))))
  641. def _printFetching(self, fn):
  642. """
  643. Вывод сообщения о скачивании
  644. """
  645. self.endTask()
  646. self.startTask(_("Fetching binary packages"))
  647. def _printUninstallPackage(self, pkg, num=1, max_num=1):
  648. """
  649. Вывод сообщения удаления пакета
  650. """
  651. self.endTask()
  652. _print = self.color_print
  653. if num and max_num:
  654. one = _print("{0}", num)
  655. two = _print("{0}", max_num)
  656. part = _(" ({current} of {maximum})").format(current=one,
  657. maximum=two)
  658. else:
  659. part = ""
  660. _print = _print.foreground(Colors.LIGHT_RED)
  661. self.startTask(
  662. _("Unmerging{part} {package}").format(part=part,
  663. package=_print(str(pkg))))
  664. def emergelike(self, cmd, *params):
  665. """
  666. Запуск команды, которая подразумевает выполнение emerge
  667. """
  668. cmd_path = self.get_prog_path(cmd)
  669. if not cmd_path:
  670. raise UpdateError(_("Failed to find the %s command") % cmd)
  671. with EmergeParser(
  672. emerge_parser.CommandExecutor(cmd_path, params)) as emerge:
  673. self._startEmerging(emerge)
  674. return True
  675. def revdep_rebuild(self, cmd, *params):
  676. """
  677. Запуск revdep-rebulid
  678. """
  679. cmd_path = self.get_prog_path(cmd)
  680. if not cmd_path:
  681. raise UpdateError(_("Failed to find the %s command") % cmd)
  682. with EmergeParser(
  683. emerge_parser.CommandExecutor(cmd_path, params)) as emerge:
  684. revdep = RevdepPercentBlock(emerge)
  685. self.addProgress()
  686. revdep.add_observer(self.setProgress)
  687. revdep.action = lambda x: (
  688. self.endTask(), self.startTask(_("Assigning files to packages"))
  689. if "Assign" in revdep else None)
  690. self._startEmerging(emerge)
  691. return True
  692. def _display_pretty_package_list(self, pkglist, remove_list=False):
  693. """
  694. Отобразить список пакетов в "удобочитаемом" виде
  695. """
  696. _print = self.color_print
  697. ebuild_color = TextState.Colors.GREEN
  698. binary_color = TextState.Colors.PURPLE
  699. remove_color = TextState.Colors.LIGHT_RED
  700. flag_map = {"updating":
  701. _print.foreground(TextState.Colors.LIGHT_CYAN)("U"),
  702. "reinstall":
  703. _print.foreground(TextState.Colors.YELLOW)("rR"),
  704. "new":
  705. _print.foreground(TextState.Colors.LIGHT_GREEN)("N"),
  706. "newslot":
  707. _print.foreground(TextState.Colors.LIGHT_GREEN)("NS"),
  708. "downgrading": (
  709. _print.foreground(TextState.Colors.LIGHT_CYAN)("U") +
  710. _print.foreground(TextState.Colors.LIGHT_BLUE)("D"))}
  711. for pkg in sorted([PackageInformation.add_info(x) for x in
  712. pkglist],
  713. key=lambda y: y['CATEGORY/PN']):
  714. install_flag = ""
  715. if remove_list:
  716. pkgcolor = _print.foreground(remove_color)
  717. else:
  718. for flag in flag_map:
  719. if pkg[flag]:
  720. install_flag = "(%s) " % flag_map[flag]
  721. break
  722. if self.is_binary_pkg(pkg):
  723. pkgcolor = _print.foreground(binary_color)
  724. else:
  725. pkgcolor = _print.foreground(ebuild_color)
  726. if pkg.info['DESCRIPTION']:
  727. fullname = "%s " % _(pkg.info['DESCRIPTION'])
  728. fullname = fullname[:1].upper() + fullname[1:]
  729. else:
  730. fullname = ""
  731. shortname = pkgcolor("%s-%s" % (pkg["CATEGORY/PN"], pkg["PVR"]))
  732. if "SIZE" in pkg and pkg['SIZE'] and pkg["SIZE"] != "0 kB":
  733. size = " (%s)" % pkg["SIZE"]
  734. else:
  735. size = ""
  736. mult = _print.bold("*")
  737. self.printDefault(
  738. _u8("&nbsp;{mult} {fullname}{flag}{shortname}{size}").format(
  739. mult=_u8(mult), fullname=_u8(fullname), shortname=_u8(shortname),
  740. size=_u8(size),
  741. flag=_u8(install_flag)))
  742. def _display_install_package(self, emerge, emergelike=False):
  743. """
  744. Отобразить список устанавливаемых пакетов
  745. """
  746. # подробный список пакетов
  747. _print = self.color_print
  748. if emergelike:
  749. self.printPre(str(emerge.install_packages))
  750. else:
  751. pkglist = emerge.install_packages.list
  752. self.printSUCCESS(_print(
  753. _("Listing packages for installation")))
  754. self._display_pretty_package_list(pkglist)
  755. if emerge.install_packages.remove_list:
  756. self.printSUCCESS(_print(
  757. _("Listing packages for removal")))
  758. self._display_pretty_package_list(
  759. emerge.install_packages.remove_list, remove_list=True)
  760. if len(emerge.install_packages.list) > 0:
  761. install_mess = (_("{count} packages will be installed").format(
  762. count=len(emerge.install_packages.list)) + ", ")
  763. else:
  764. install_mess = ""
  765. if str(emerge.download_size) != "0 kB":
  766. self.printSUCCESS(_("{install}{size} will be downloaded").format(
  767. install=install_mess,
  768. size=str(emerge.download_size)))
  769. def _display_remove_list(self, emerge):
  770. """
  771. Отобразить список удаляемых пакетов
  772. """
  773. # подробный список пакетов
  774. if self.clVars.Get('update.cl_update_emergelist_set') == 'on':
  775. self.printPre(self._emerge_translate(
  776. emerge.uninstall_packages.verbose_result))
  777. else:
  778. _print = self.color_print
  779. pkglist = emerge.uninstall_packages.list
  780. self.printSUCCESS(_print.bold(
  781. _("Listing packages for removal")))
  782. self._display_pretty_package_list(pkglist, remove_list=True)
  783. def mark_schedule(self):
  784. """
  785. Установить отметку о запуске запланированной проверки
  786. """
  787. SystemIni(self.clVars).setVar('system', {'last_check': str(int(time.time()))})
  788. def get_default_emerge_opts(self, depclean=False):
  789. bdeps_val = self.clVars.Get('cl_update_with_bdeps_set')
  790. if bdeps_val == "auto":
  791. bdeps = " --with-bdeps-auto=y"
  792. elif bdeps_val == "on":
  793. bdeps = " --with-bdeps=y"
  794. else:
  795. bdeps = " --with-bdeps=n"
  796. return self.clVars.Get('cl_emerge_default_opts') + bdeps
  797. def premerge(self, param, *packages):
  798. """
  799. Вывести информацию об обновлении
  800. """
  801. deo = self.get_default_emerge_opts()
  802. param = [param, "-pv"]
  803. with EmergeParser(EmergeCommand(list(packages), emerge_default_opts=deo,
  804. extra_params=param)) as emerge:
  805. try:
  806. emerge.run()
  807. if not emerge.install_packages.list:
  808. self.printSUCCESS(_("The system is up to date"))
  809. self.set_need_update(False)
  810. return True
  811. emergelike = self.clVars.Get('cl_update_emergelist_set') == 'on'
  812. self._display_install_package(emerge, emergelike)
  813. if str(emerge.skipped_packages):
  814. self._display_error(emerge.skipped_packages)
  815. except EmergeError:
  816. self.set_need_update(False)
  817. self._display_install_package(emerge, emergelike=True)
  818. self._display_error(emerge.prepare_error)
  819. raise
  820. if self.clVars.Get('cl_update_pretend_set') == 'on':
  821. # установить кэш: есть обновления
  822. self.set_need_update()
  823. return True
  824. self.set_need_update(False)
  825. answer = self.askConfirm(
  826. _("Would you like to merge these packages?"), "yes")
  827. if answer == "no":
  828. raise KeyboardInterrupt
  829. return "yes"
  830. return True
  831. def set_need_update(self, val=True):
  832. """
  833. Установить флаг: есть обновления
  834. """
  835. if self.clVars.Get('update.cl_update_autocheck_set') == 'off':
  836. val = False
  837. UpdateInfo.set_update_ready(val)
  838. return True
  839. def _emerge_translate(self, s):
  840. """
  841. Перевести текст из emerge
  842. """
  843. return RegexpLocalization('cl_emerge').translate(str(s))
  844. def setUpToDateCache(self):
  845. """
  846. Установить кэш - "нет пакетов для обновления"
  847. """
  848. #self.updateCache(PackageList([]))
  849. self.set_need_update(False)
  850. return True
  851. def _startEmerging(self, emerge):
  852. """
  853. Настроить и выполнить emerge
  854. """
  855. if emerge.install_packages and emerge.install_packages.list:
  856. for pkg in emerge.install_packages.list:
  857. rv = pkg.get('REPLACING_VERSIONS', '')
  858. if rv:
  859. self.update_map["{CATEGORY}/{PF}".format(**pkg)] = \
  860. rv.partition(":")[0]
  861. emerge.command.send("yes\n")
  862. emerge.emerging.add_observer(self._printEmergePackage)
  863. emerge.installing.add_observer(self._printInstallPackage)
  864. emerge.uninstalling.add_observer(self._printUninstallPackage)
  865. emerge.fetching.add_observer(self._printFetching)
  866. def cancel_observing_fetch(fn):
  867. emerge.fetching.clear_observers()
  868. emerge.fetching.add_observer(cancel_observing_fetch)
  869. try:
  870. emerge.run()
  871. except EmergeError:
  872. if emerge.emerging_error:
  873. self._display_error(emerge.emerging_error.log)
  874. else:
  875. self._display_error(emerge.prepare_error)
  876. raise
  877. def _display_error(self, error):
  878. lines_num = int(self.clVars.Get('update.cl_update_lines_limit'))
  879. error = "<br/>".join(str(error).split('<br/>')[-lines_num:])
  880. self.printPre(self._emerge_translate(error))
  881. def emerge(self, use, param, *packages):
  882. """
  883. Выполнить сборку пакета
  884. """
  885. deo = self.get_default_emerge_opts()
  886. if not packages:
  887. packages = [param]
  888. extra_params = None
  889. else:
  890. extra_params = [param]
  891. command = EmergeCommand(list(packages), emerge_default_opts=deo,
  892. extra_params=extra_params,
  893. use=use)
  894. if self.clVars.Get('cl_chroot_path') != '/':
  895. command = Chroot(self.clVars.Get('cl_chroot_path'), command)
  896. with EmergeParser(command) as emerge:
  897. try:
  898. emerge.question.action = lambda x: False
  899. emerge.run()
  900. if not emerge.install_packages.list:
  901. return True
  902. except EmergeError:
  903. self._display_error(emerge.prepare_error)
  904. raise
  905. self._startEmerging(emerge)
  906. return True
  907. def emerge_ask(self, pretend, *params):
  908. """
  909. Вывести информацию об обновлении
  910. """
  911. deo = self.get_default_emerge_opts()
  912. param = [x for x in params if x.startswith("-")]
  913. packages = [x for x in params if not x.startswith("-")]
  914. command = EmergeCommand(list(packages), emerge_default_opts=deo,
  915. extra_params=param)
  916. with EmergeParser(command) as emerge:
  917. try:
  918. emerge.question.action = lambda x: False
  919. emerge.run()
  920. if emerge.install_packages.list:
  921. emergelike = self.clVars.Get(
  922. 'update.cl_update_emergelist_set') == 'on'
  923. self._display_install_package(emerge, emergelike)
  924. if emerge.skipped_packages:
  925. self._display_error(emerge.skipped_packages)
  926. if not pretend:
  927. answer = self.askConfirm(
  928. _("Would you like to merge these packages?"), "yes")
  929. if answer == "no":
  930. emerge.command.send("no\n")
  931. raise KeyboardInterrupt
  932. else:
  933. return True
  934. else:
  935. self.set_need_update(False)
  936. self.printSUCCESS(_("The system is up to date"))
  937. except EmergeError:
  938. self.set_need_update(False)
  939. self._display_install_package(emerge, emergelike=True)
  940. self._display_error(emerge.prepare_error)
  941. raise
  942. self._startEmerging(emerge)
  943. return True
  944. def depclean(self):
  945. """
  946. Выполнить очистку системы от лишних пакетов
  947. """
  948. deo = self.get_default_emerge_opts(depclean=True)
  949. emerge = None
  950. try:
  951. emerge = EmergeParser(EmergeCommand(["--depclean",
  952. "--dynamic-deps=n"],
  953. emerge_default_opts=deo))
  954. outdated_kernel = False
  955. try:
  956. emerge.question.action = lambda x: False
  957. emerge.run()
  958. if not emerge.uninstall_packages.list:
  959. UpdateInfo(self.clVars).outdated_kernel = False
  960. return True
  961. kernel_pkg = self.clVars.Get('cl_update_kernel_pkg')
  962. if any(("%s-%s" % (x['CATEGORY/PN'], x['PVR'])) == kernel_pkg
  963. for x in emerge.uninstall_packages.list):
  964. pkglist = [
  965. "=%s-%s" % (x['CATEGORY/PN'], x['PVR']) for x in
  966. emerge.uninstall_packages.list
  967. if ("%s-%s" % (x['CATEGORY/PN'],
  968. x['PVR'])) != kernel_pkg]
  969. emerge.command.send('n\n')
  970. emerge.close()
  971. emerge = None
  972. if not pkglist:
  973. UpdateInfo(self.clVars).outdated_kernel = True
  974. return True
  975. emerge = EmergeParser(
  976. EmergeCommand(pkglist,
  977. extra_params=["--unmerge", '--ask=y'],
  978. emerge_default_opts=deo))
  979. emerge.question.action = lambda x: False
  980. emerge.run()
  981. outdated_kernel = True
  982. else:
  983. outdated_kernel = False
  984. self._display_remove_list(emerge)
  985. except EmergeError:
  986. self._display_error(emerge.prepare_error)
  987. raise
  988. if (self.askConfirm(
  989. _("Would you like to unmerge these unused packages "
  990. "(recommended)?")) != 'yes'):
  991. return True
  992. UpdateInfo(self.clVars).outdated_kernel = outdated_kernel
  993. self._startEmerging(emerge)
  994. finally:
  995. if emerge:
  996. emerge.close()
  997. return True
  998. def update_task(self, task_name):
  999. """
  1000. Декоратор для добавления меток запуска и останова задачи
  1001. """
  1002. def decor(f):
  1003. def wrapper(*args, **kwargs):
  1004. logger = EmergeLog(EmergeLogNamedTask(task_name))
  1005. logger.mark_begin_task()
  1006. ret = f(*args, **kwargs)
  1007. if ret:
  1008. logger.mark_end_task()
  1009. return ret
  1010. return wrapper
  1011. return decor
  1012. def migrateCacheRepository(self, url, branch, storage):
  1013. """
  1014. Перенести репозиторий из кэша в локальный
  1015. """
  1016. rep = storage.get_repository(url, branch)
  1017. if rep:
  1018. rep.storage = storage.storages[0]
  1019. self.clVars.Invalidate('cl_update_profile_storage')
  1020. return True
  1021. def reconfigureProfileVars(self, profile_dv, chroot):
  1022. """
  1023. Синхронизировать репозитории
  1024. Настройка переменных для выполнения синхронизации репозиториев
  1025. """
  1026. dv = self.clVars
  1027. try:
  1028. if not profile_dv:
  1029. raise UpdateError(
  1030. _("Failed to use the new profile. Try again."))
  1031. for var_name in ('cl_update_rep_path',
  1032. 'cl_update_rep_url',
  1033. 'cl_update_rep_name',
  1034. 'cl_update_branch',
  1035. 'cl_update_binhost_list',
  1036. 'cl_update_binhost_unstable_list',
  1037. 'cl_update_binhost_stable_set',
  1038. 'cl_update_binhost_stable_opt_set',
  1039. 'cl_update_branch_name',
  1040. 'cl_profile_system',
  1041. 'cl_update_rep'):
  1042. dv.Set(var_name, profile_dv.Get(var_name), force=True)
  1043. dv.Set('cl_chroot_path', chroot, force=True)
  1044. except DataVarsError as e:
  1045. error = UpdateError(_("Wrong profile"))
  1046. error.addon = e
  1047. raise error
  1048. return True
  1049. def setProfile(self, profile_shortname):
  1050. profile = self.clVars.Select('cl_update_profile_path',
  1051. where='cl_update_profile_shortname',
  1052. eq=profile_shortname, limit=1)
  1053. if not profile:
  1054. raise UpdateError(_("Failed to determine profile %s") %
  1055. self.clVars.Get('cl_update_profile_system'))
  1056. profile_path = path.relpath(profile, '/etc/portage')
  1057. try:
  1058. profile_file = '/etc/portage/make.profile'
  1059. if not path.exists(
  1060. path.join(path.dirname(profile_file), profile_path)):
  1061. raise UpdateError(
  1062. _("Failed to set the profile: %s") % _("Profile not found"))
  1063. for rm_fn in filter(path.lexists,
  1064. ('/etc/make.profile',
  1065. '/etc/portage/make.profile')):
  1066. os.unlink(rm_fn)
  1067. os.symlink(profile_path, profile_file)
  1068. except (OSError, IOError) as e:
  1069. raise UpdateError(_("Failed to set the profile: %s") % str(e))
  1070. return True
  1071. def applyProfileTemplates(self, useClt=None, cltFilter=False,
  1072. useDispatch=True, action="merge"):
  1073. """
  1074. Наложить шаблоны из профиля
  1075. """
  1076. from calculate.lib.cl_template import TemplatesError, ProgressTemplate
  1077. dv = DataVarsUpdate()
  1078. try:
  1079. dv.importUpdate()
  1080. dv.flIniFile()
  1081. dv.Set('cl_action', action, force=True)
  1082. try:
  1083. dv.Set('cl_templates_locate',
  1084. self.clVars.Get('cl_update_templates_locate'))
  1085. except VariableError:
  1086. self.printERROR(_("Failed to apply profiles templates"))
  1087. return True
  1088. dv.Set("cl_chroot_path", '/', True)
  1089. dv.Set("cl_root_path", '/', True)
  1090. for copyvar in ("cl_dispatch_conf", "cl_verbose_set",
  1091. "update.cl_update_world"):
  1092. dv.Set(copyvar, self.clVars.Get(copyvar), True)
  1093. # определение каталогов содержащих шаблоны
  1094. useClt = useClt in ("on", True)
  1095. self.addProgress()
  1096. nullProgress = lambda *args, **kw: None
  1097. dispatch = self.dispatchConf if useDispatch else None
  1098. clTempl = ProgressTemplate(nullProgress, dv, cltObj=useClt,
  1099. cltFilter=cltFilter,
  1100. printSUCCESS=self.printSUCCESS,
  1101. printWARNING=self.printWARNING,
  1102. askConfirm=self.askConfirm,
  1103. dispatchConf=dispatch,
  1104. printERROR=self.printERROR)
  1105. try:
  1106. clTempl.applyTemplates()
  1107. if clTempl.hasError():
  1108. if clTempl.getError():
  1109. raise TemplatesError(clTempl.getError())
  1110. finally:
  1111. if clTempl:
  1112. if clTempl.cltObj:
  1113. clTempl.cltObj.closeFiles()
  1114. clTempl.closeFiles()
  1115. finally:
  1116. dv.close()
  1117. return True
  1118. def cleanpkg(self):
  1119. """
  1120. Очистить PKGDIR и DISTFILES в текущей системе
  1121. """
  1122. portdirs = ([self.clVars.Get('cl_portdir')] +
  1123. self.clVars.Get('cl_portdir_overlay'))
  1124. pkgfiles = get_packages_files_directory(*portdirs)
  1125. distdirfiles = get_manifest_files_directory(*portdirs)
  1126. distdir = self.clVars.Get('install.cl_distfiles_path')
  1127. pkgdir = self.clVars.Get('cl_pkgdir')
  1128. logger = log("update_cleanpkg.log",
  1129. filename="/var/log/calculate/update_cleanpkg.log",
  1130. formatter="%(asctime)s - %(clean)s - %(message)s")
  1131. return self._cleanpkg(
  1132. distdir, pkgdir, distdirfiles, pkgfiles, logger)
  1133. def _update_binhost_packages(self):
  1134. os.system('/usr/sbin/emaint binhost -f &>/dev/null')
  1135. def _cleanpkg(self, distdir, pkgdir, distdirfiles, pkgfiles, logger):
  1136. """
  1137. Общий алгоритм очистки distfiles и pkgdir от устаревших пакетов
  1138. """
  1139. skip_files = ["/metadata.dtd", "/Packages"]
  1140. try:
  1141. if self.clVars.Get('client.os_remote_auth'):
  1142. skip_files += ['portage_lockfile']
  1143. except DataVarsError:
  1144. pass
  1145. for cleantype, filelist in (
  1146. ("packages",
  1147. get_remove_list(pkgdir, list(pkgfiles), depth=4)),
  1148. ("distfiles",
  1149. get_remove_list(distdir, list(distdirfiles), depth=1))):
  1150. removelist = []
  1151. for fn in filelist:
  1152. try:
  1153. if not any(fn.endswith(x) for x in skip_files):
  1154. os.unlink(fn)
  1155. removelist.append(path.basename(fn))
  1156. except OSError:
  1157. pass
  1158. removelist_str = ",".join(removelist)
  1159. if removelist_str:
  1160. logger.info(removelist_str, extra={'clean': cleantype})
  1161. if cleantype == "packages":
  1162. try:
  1163. self._update_binhost_packages()
  1164. for dn in listDirectory(pkgdir, fullPath=True):
  1165. if path.isdir(dn) and not listDirectory(dn):
  1166. os.rmdir(dn)
  1167. except OSError:
  1168. pass
  1169. return True
  1170. def updateSetupCache(self):
  1171. cache = SetupCache(self.clVars)
  1172. cache.update(force=True)
  1173. return True
  1174. def get_bin_cache_filename(self):
  1175. return pathJoin(self.clVars.Get('cl_chroot_path'),
  1176. LayeredIni.IniPath.Grp)
  1177. def update_local_info_binhost(self, write_binhost=True):
  1178. """
  1179. Проверить, что доступен хотя бы один из binhost'ов
  1180. :return:
  1181. """
  1182. hosts = self.clVars.Get("update.cl_update_binhost_host")
  1183. datas = self.clVars.Get("update.cl_update_binhost_revisions")
  1184. if not hosts:
  1185. self.delete_binhost()
  1186. raise UpdateError(_("Failed to find the server with "
  1187. "appropriate updates"))
  1188. else:
  1189. with writeFile(self.get_bin_cache_filename()) as f:
  1190. f.write(datas[0].strip()+"\n")
  1191. if write_binhost:
  1192. if hosts[0] != self.clVars.Get('update.cl_update_binhost'):
  1193. self.refresh_binhost = True
  1194. self.clVars.Set('cl_update_package_cache_set', 'on')
  1195. self.clVars.Write('cl_update_binhost', hosts[0], location="system")
  1196. new_ts = self.clVars.Get("update.cl_update_binhost_timestamp")
  1197. if new_ts:
  1198. new_ts = new_ts[0]
  1199. if new_ts.isdigit():
  1200. ini = SystemIni(self.clVars)
  1201. ini.setVar('system', {'last_update': new_ts})
  1202. if self.is_update_action(self.clVars.Get("cl_action")):
  1203. value = self.clVars.GetBool('update.cl_update_binhost_stable_set')
  1204. new_value = self.clVars.GetBool('update.cl_update_binhost_stable_opt_set')
  1205. if value != new_value:
  1206. self.clVars.Write(
  1207. 'cl_update_binhost_stable_set',
  1208. self.clVars.Get('update.cl_update_binhost_stable_opt_set'),
  1209. location="system")
  1210. return True
  1211. def is_update_action(self, action):
  1212. return action == 'sync'
  1213. def modify_binary_depends(self, bdeps, prefix="/"):
  1214. """
  1215. Включить/выключить сборочные зависимости для бинарных пакетов
  1216. """
  1217. hide = bdeps == "auto"
  1218. self.startTask(_("Updating binary build dependences"))
  1219. vardb = Vardb(pathJoin(prefix,VDB_PATH))
  1220. if hide:
  1221. vardb.hide_binary_depends()
  1222. else:
  1223. vardb.unhide_binary_depends()
  1224. self.endTask()
  1225. return True
  1226. def save_with_bdeps(self):
  1227. oldval = self.clVars.Get('cl_update_with_bdeps_set')
  1228. newval = self.clVars.Get('cl_update_with_bdeps_opt_set')
  1229. if oldval != newval:
  1230. self.modify_binary_depends(newval)
  1231. self.clVars.Write('cl_update_with_bdeps_set', newval,
  1232. location="system")
  1233. self.clVars.Set('cl_update_force_depclean_set', 'on')
  1234. return True
  1235. def message_binhost_changed(self):
  1236. if self.refresh_binhost:
  1237. self.printWARNING(_("Update server was changed to %s") %
  1238. self.clVars.Get('update.cl_update_binhost'))
  1239. self.clVars.Set("update.cl_update_package_cache_set",
  1240. Variable.On, force=True)
  1241. else:
  1242. self.printSUCCESS(_("Update server %s") %
  1243. self.clVars.Get('update.cl_update_binhost'))
  1244. return True
  1245. def delete_binhost(self):
  1246. self.clVars.Delete('cl_update_binhost', location="system")
  1247. try:
  1248. bin_cache_fn = self.get_bin_cache_filename()
  1249. if path.exists(bin_cache_fn):
  1250. os.unlink(bin_cache_fn)
  1251. except OSError:
  1252. raise UpdateError(
  1253. _("Failed to remove cached ini.env of binary repository"))
  1254. try:
  1255. for varname in ('update.cl_update_package_cache',
  1256. 'update.cl_update_package_cache_sign'):
  1257. fn = self.clVars.Get(varname)
  1258. if path.exists(fn):
  1259. os.unlink(fn)
  1260. except OSError:
  1261. raise UpdateError(
  1262. _("Failed to remove cached Package index"))
  1263. # удалить binhost
  1264. binhost_fn = self.inchroot(
  1265. self.clVars.Get("update.cl_update_portage_binhost_path"))
  1266. if path.exists(binhost_fn):
  1267. os.unlink(binhost_fn)
  1268. return False
  1269. def update_binhost_list(self, dv=None):
  1270. """
  1271. Обновить список binhost'ов после обновления до master веток
  1272. :return:
  1273. """
  1274. changes = False
  1275. try:
  1276. if dv is None:
  1277. dv = DataVarsUpdate()
  1278. dv.importUpdate()
  1279. dv.flIniFile()
  1280. for varname in ('update.cl_update_binhost_list',
  1281. 'update.cl_update_binhost_unstable_list',
  1282. 'update.cl_update_binhost_timestamp_path',
  1283. 'update.cl_update_binhost_revision_path'):
  1284. new_value = dv.Get(varname)
  1285. old_value = self.clVars.Get(varname)
  1286. if new_value != old_value:
  1287. changes = True
  1288. self.clVars.Set(varname, new_value, force=True)
  1289. except DataVarsError as e:
  1290. raise UpdateError(_("Failed to get values for binhost search"))
  1291. if not changes:
  1292. return False
  1293. self.create_binhost_data()
  1294. return True
  1295. def drop_binhosts(self, dv):
  1296. """
  1297. Обновление до master веток
  1298. """
  1299. branch = dv.Get('update.cl_update_branch')
  1300. revs = [
  1301. branch for x in dv.Get('update.cl_update_rep_name')
  1302. ]
  1303. dv.Set('update.cl_update_branch_name', revs)
  1304. dv.Invalidate('update.cl_update_rep_rev')
  1305. return True
  1306. def inchroot(self, fn):
  1307. return pathJoin(self.clVars.Get("cl_chroot_path"), fn)
  1308. def prepare_gpg(self):
  1309. """
  1310. Получить объект для проверки подписи, либо получить заглушку
  1311. """
  1312. gpg_force = self.clVars.Get('cl_update_gpg_force')
  1313. gpg_keys = [self.inchroot(x)
  1314. for x in self.clVars.Get('cl_update_gpg_keys')]
  1315. if gpg_force == "skip":
  1316. return True
  1317. gpgtmpdir = "/var/calculate/tmp/update"
  1318. if not path.exists(gpgtmpdir):
  1319. makeDirectory(gpgtmpdir)
  1320. gpg = GPG(tempfile.mkdtemp(dir=gpgtmpdir,
  1321. prefix="gpg-"))
  1322. for keyfn in gpg_keys:
  1323. if path.exists(keyfn):
  1324. try:
  1325. key = readFile(keyfn)
  1326. gpg.import_key(key)
  1327. except GPGError as e:
  1328. self.printWARNING(_("Failed to load public keys from '%s' "
  1329. "for signature checking") % keyfn)
  1330. if not gpg.count_public():
  1331. if gpg_force == "force":
  1332. raise UpdateError(_("Public keys for Packages signature checking not found"))
  1333. else:
  1334. return True
  1335. self.clVars.Set('update.cl_update_gpg', gpg, force=True)
  1336. return True
  1337. def download_packages(self, url_binhost, packages_fn, packages_sign_fn, gpg):
  1338. quite_unlink(packages_fn)
  1339. orig_packages = Binhosts.fetch_packages(url_binhost)
  1340. try:
  1341. with writeFile(packages_fn) as f:
  1342. pi = PackagesIndex(orig_packages)
  1343. pi["TTL"] = str(30 * DAYS)
  1344. pi["DOWNLOAD_TIMESTAMP"] = str(int(time.time()))
  1345. pi.write(f)
  1346. except (OSError, IOError):
  1347. raise UpdateError(_("Failed to save Packages"))
  1348. self.endTask(True)
  1349. self.startTask(_("Check packages index signature"))
  1350. if not gpg:
  1351. self.endTask("skip")
  1352. self.clVars.Set('cl_update_package_cache_set', Variable.Off, force=True)
  1353. return True
  1354. try:
  1355. Binhosts.check_packages_signature(
  1356. url_binhost, orig_packages, gpg)
  1357. with writeFile(packages_sign_fn) as f:
  1358. f.write(Binhosts.fetch_packages_sign(url_binhost))
  1359. except BinhostSignError:
  1360. for fn in (packages_fn, packages_sign_fn):
  1361. if path.exists(fn):
  1362. try:
  1363. os.unlink(fn)
  1364. except OSError:
  1365. pass
  1366. self.clVars.Set("update.cl_update_bad_sign_set", Variable.On)
  1367. self.clVars.Set('update.cl_update_binhost_recheck_set', Variable.On)
  1368. self.clVars.Set('cl_update_package_cache_set', Variable.Off, force=True)
  1369. raise
  1370. return True
  1371. class Reason(object):
  1372. Success = 0
  1373. BadSign = 1
  1374. Outdated = 2
  1375. Skip = 3
  1376. Updating = 4
  1377. WrongBinhost = 5
  1378. BadEnv = 6
  1379. EnvNotFound = 7
  1380. SkipSlower = 8
  1381. UnknownError = 9
  1382. @staticmethod
  1383. def humanReadable(reason):
  1384. return {
  1385. Update.Reason.WrongBinhost: "FAILED (Wrong binhost)",
  1386. Update.Reason.Outdated: "OUTDATED",
  1387. Update.Reason.Updating: "UPDATING",
  1388. Update.Reason.BadEnv: "FAILED (Bad env)",
  1389. Update.Reason.EnvNotFound: "FAILED (Env not found)",
  1390. Update.Reason.UnknownError: "FAILED (Unknown error)",
  1391. Update.Reason.BadSign: "FAILED (Bad sign)",
  1392. Update.Reason.Skip: "SKIP",
  1393. Update.Reason.SkipSlower: "",
  1394. Update.Reason.Success: ""
  1395. }.get(reason,reason)
  1396. def _get_binhost_logger(self):
  1397. return log("binhost-scan.log",
  1398. filename=pathJoin(
  1399. self.clVars.Get('cl_chroot_path'),
  1400. "/var/log/calculate/binhost-scan.log"),
  1401. formatter="%(message)s")
  1402. def get_arch_machine(self):
  1403. return self.clVars.Get('os_arch_machine')
  1404. @variable_module("update")
  1405. def create_binhost_data(self):
  1406. dv = self.clVars
  1407. last_ts = dv.Get('cl_update_last_timestamp')
  1408. if dv.GetBool('cl_update_binhost_stable_opt_set'):
  1409. binhost_list = dv.Get('cl_update_binhost_list')
  1410. else:
  1411. binhost_list = dv.Get('cl_update_binhost_unstable_list')
  1412. self.binhosts_data = Binhosts(
  1413. # значение малозначимо, поэтому берётся из собирающей системы
  1414. dv.GetInteger('cl_update_binhost_timeout'),
  1415. dv.Get('cl_update_binhost_revision_path'),
  1416. dv.Get('cl_update_binhost_timestamp_path'),
  1417. last_ts, binhost_list,
  1418. self.get_arch_machine(),
  1419. gpg=dv.Get('update.cl_update_gpg'))
  1420. return True
  1421. @variable_module("update")
  1422. def _search_best_binhost(self, binhosts_data, stabilization):
  1423. if not self.clVars.Get('cl_ebuild_phase'):
  1424. logger = self._get_binhost_logger()
  1425. if logger:
  1426. logger.info(
  1427. "Started scan on: {date}, current timestamp: {ts}".format(
  1428. date=time.ctime(), ts=binhosts_data.last_ts))
  1429. retval = []
  1430. skip_check_status = False
  1431. actual_reason = self.Reason.UnknownError
  1432. for binhost in sorted(binhosts_data.get_binhosts(), reverse=True):
  1433. host = binhost.host
  1434. if not binhost.valid:
  1435. reason = self.Reason.WrongBinhost
  1436. elif binhost.outdated:
  1437. reason = self.Reason.Outdated
  1438. elif not skip_check_status:
  1439. status = binhost.status
  1440. if status is not binhosts_data.BinhostStatus.Success:
  1441. errors = {
  1442. binhosts_data.BinhostStatus.Updating: self.Reason.Updating,
  1443. binhosts_data.BinhostStatus.BadEnv: self.Reason.BadEnv,
  1444. binhosts_data.BinhostStatus.EnvNotFound: self.Reason.EnvNotFound
  1445. }
  1446. reason = errors.get(status, self.Reason.UnknownError)
  1447. elif binhost.bad_sign:
  1448. reason = self.Reason.BadSign
  1449. else:
  1450. # SUCCESS
  1451. if not binhost.downgraded or stabilization:
  1452. host = "-> %s" % host
  1453. reason = self.Reason.Success
  1454. else:
  1455. reason = self.Reason.Skip
  1456. elif binhost.downgraded:
  1457. reason = self.Reason.Skip
  1458. else:
  1459. reason = self.Reason.SkipSlower
  1460. if reason == self.Reason.Success:
  1461. retval.append([binhost.host, binhost.data,
  1462. str(binhost.timestamp),
  1463. str(binhost.duration)])
  1464. skip_check_status = True
  1465. if reason < actual_reason:
  1466. actual_reason = reason
  1467. logger.info("{host:<60} {speed:<7} {timestamp:<10} {reason}".format(
  1468. host=host, speed=float(binhost.duration) / 1000.0,
  1469. timestamp=binhost.timestamp,
  1470. reason=Update.Reason.humanReadable(reason)))
  1471. if not retval:
  1472. if actual_reason is self.Reason.BadSign:
  1473. raise UpdateError(_("Failed to find the reliable server with appropriate updates"))
  1474. elif actual_reason in (self.Reason.Outdated,
  1475. self.Reason.Skip,
  1476. self.Reason.Updating):
  1477. raise UpdateError(_("Failed to find the server with appropriate updates"))
  1478. else:
  1479. raise UpdateError(_("Failed to find the working server with updates"))
  1480. return retval
  1481. def check_current_binhost(self, binhost_url):
  1482. """
  1483. Проверка текущего сервера обновлений на валидность
  1484. """
  1485. if not binhost_url in self.binhosts_data.binhost_list:
  1486. raise UpdateError(_("Current binhost is absent in list of update servers"))
  1487. binhost = self.binhosts_data.get_binhost(binhost_url)
  1488. if binhost.valid and not binhost.outdated and not binhost.downgraded:
  1489. if binhost.status == self.binhosts_data.BinhostStatus.Success:
  1490. self.clVars.Set('update.cl_update_binhost_data',
  1491. [[binhost.host, binhost.data,
  1492. str(binhost.timestamp),
  1493. str(binhost.duration)]],
  1494. force=True)
  1495. self.endTask()
  1496. else:
  1497. if not binhost.valid:
  1498. raise UpdateError(
  1499. _("Current binhost {} is not valid").format(binhost_url))
  1500. elif binhost.outdated:
  1501. raise UpdateError(
  1502. _("Current binhost {} is outdated").format(binhost_url))
  1503. elif binhost.downgraded:
  1504. raise UpdateError(
  1505. _("Binary packages on the current binhost {} "
  1506. "are older than local").format(binhost_url))
  1507. if self.binhosts_data.gpg:
  1508. packages_fn = self.clVars.Get('update.cl_update_package_cache')
  1509. packages_sign_fn = self.clVars.Get('update.cl_update_package_cache_sign')
  1510. if path.exists(packages_fn) and path.exists(packages_sign_fn):
  1511. packages_sign = readFile(packages_sign_fn)
  1512. pi = PackagesIndex(readFile(packages_fn))
  1513. pi.clean()
  1514. try:
  1515. Binhosts.check_packages_signature(
  1516. None, pi.get_value(), self.binhosts_data.gpg,
  1517. sign=packages_sign)
  1518. return True
  1519. except BinhostSignError:
  1520. for fn in (packages_fn, packages_sign_fn):
  1521. if path.exists(fn):
  1522. try:
  1523. os.unlink(fn)
  1524. except OSError:
  1525. pass
  1526. if binhost.bad_sign:
  1527. raise UpdateError(
  1528. _("Current binhost {} has wrong signature").format(
  1529. binhost_url))
  1530. return True
  1531. @variable_module("update")
  1532. def detect_best_binhost(self):
  1533. # выполняется переход с серверов unstable обновлней на stable
  1534. # в этом случае не важно, что бинари могут старее текущих
  1535. if (self.clVars.GetBool('cl_update_binhost_stable_opt_set') and
  1536. not self.clVars.GetBool('cl_update_binhost_stable_set')):
  1537. stabilization = True
  1538. else:
  1539. stabilization = False
  1540. self.startTask(_("Searching new binhost"))
  1541. retval = self._search_best_binhost(self.binhosts_data, stabilization)
  1542. self.clVars.Set('update.cl_update_binhost_data',
  1543. retval or Variable.EmptyTable, force=True)
  1544. self.endTask()
  1545. return True
  1546. def update_rep_list(self):
  1547. """
  1548. Обновить список доступных репозиториев
  1549. :param builder_path:
  1550. :return:
  1551. """
  1552. cmd = "/usr/bin/eselect"
  1553. cmd_path = self.get_prog_path(cmd)
  1554. if not cmd_path:
  1555. raise UpdateError(_("Failed to find the %s command") % cmd)
  1556. repsync = emerge_parser.CommandExecutor(cmd_path, ["repository", "list"])
  1557. repsync.execute()
  1558. return repsync.success()
  1559. def rename_custom_files(self):
  1560. """
  1561. Переименовать все custom файлы: keywords, use, sets и т.д. в связи
  1562. с изменением профиля
  1563. """
  1564. newdv = self.clVars.Get("cl_update_profile_datavars")
  1565. cur_short = self.clVars.Get("os_linux_shortname").lower()
  1566. new_short = newdv["os_linux_shortname"].lower()
  1567. if cur_short != new_short:
  1568. for fn in ("/etc/portage/package.keywords/custom.{}",
  1569. "/etc/portage/package.use/custom.{}",
  1570. "/etc/portage/package.mask/custom.{}",
  1571. "/etc/portage/package.unmask/custom.{}",
  1572. "/etc/portage/sets/custom.{}",
  1573. "/etc/portage/make.conf/custom.{}"):
  1574. fn_source = fn.format(cur_short)
  1575. fn_target = fn.format(new_short)
  1576. try:
  1577. if path.exists(fn_source) and not path.exists(fn_target):
  1578. os.rename(fn_source, fn_target)
  1579. except (OSError, IOError) as e:
  1580. self.printWARNING(str(e))
  1581. world_sets_fn = "/var/lib/portage/world_sets"
  1582. if path.exists(world_sets_fn):
  1583. worlds_sets = readFile(world_sets_fn)
  1584. new_worlds_sets = re.sub("^@custom.{}$".format(cur_short),
  1585. "@custom.{}".format(new_short),
  1586. worlds_sets, flags=re.M)
  1587. try:
  1588. with open(world_sets_fn, 'w') as fd:
  1589. fd.write(new_worlds_sets)
  1590. except IOError as e:
  1591. self.printWARNING(str(e))
  1592. return True
  1593. def save_portage_state_hash(self):
  1594. """
  1595. Сохранить состояние
  1596. """
  1597. ini = SystemIni(self.clVars)
  1598. ps = PortageState()
  1599. new_portage_state_hash = ps.get_state()
  1600. ini.setVar('system', {'portage_hash': new_portage_state_hash})
  1601. return True
  1602. def update_fastlogin_domain_path(self):
  1603. try:
  1604. if not self.clVars.Get('client.cl_remote_host'):
  1605. return True
  1606. except:
  1607. return True
  1608. fn = '/var/lib/calculate/calculate-desktop/fastlogin-domain'
  1609. try:
  1610. with writeFile(fn) as f:
  1611. f.write("{}\n".format(
  1612. "\n".join(get_fastlogin_domain_path(self.clVars))))
  1613. except Exception:
  1614. if os.path.exists(fn):
  1615. try:
  1616. os.unlink(fn)
  1617. except Exception:
  1618. pass
  1619. return True