Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

1449 rader
45 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2013-2015 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 os
  16. import sys
  17. import re
  18. from os import path
  19. from calculate.lib.datavars import (Variable, VariableError,
  20. ReadonlyVariable, ReadonlyTableVariable,
  21. TableVariable, FieldValue,
  22. HumanReadable,
  23. SimpleDataVars, DataVarsError)
  24. from calculate.lib.utils.files import readFile, listDirectory, process, pathJoin
  25. from calculate.lib.configparser import ConfigParser
  26. from calculate.lib.cl_lang import setLocalTranslate
  27. from calculate.lib.utils.text import simplify_profiles
  28. from calculate.lib.utils.git import Git, GitError
  29. from calculate.lib.utils.portage import Layman
  30. from ..profile import (RepositoryStorageSet, DEFAULT_BRANCH,
  31. LocalStorage, ProfileRepository, CacheStorage)
  32. from calculate.lib.variables import linux as lib_linux
  33. from calculate.lib.variables import env
  34. from calculate.update.update_info import UpdateInfo
  35. import urllib2
  36. import time
  37. _ = lambda x: x
  38. setLocalTranslate('cl_update3', sys.modules[__name__])
  39. class VariableAcUpdateSync(ReadonlyVariable):
  40. """
  41. Action variable which has value "up" for package install and
  42. install this package
  43. """
  44. def get(self):
  45. action = self.Get("cl_action")
  46. if action in ("sync",):
  47. if self.Get('cl_update_world'):
  48. return self.Get('cl_update_world')
  49. return ""
  50. class VariableClUpdateWorldDefault(Variable):
  51. """
  52. Ad-hoc
  53. """
  54. value = "update"
  55. class VariableClUpdateWorld(Variable):
  56. type = "choice"
  57. opt = ["--world"]
  58. syntax = "--{choice}-world"
  59. metavalue = "MODE"
  60. def init(self):
  61. self.help = ("'rebuild' - " + _("rebuild the system packages list") +
  62. ",\n'merge' - " + _("append the profile packages list") +
  63. ",\n'update' - " + _("update the system packages list"))
  64. self.label = _("System packages list")
  65. def get(self):
  66. return self.Get('cl_update_world_default')
  67. def choice(self):
  68. return [("rebuild", _("Rebuild")),
  69. ("merge", _("Merge")),
  70. ("update", _("Update"))]
  71. class VariableClRebuildWorldSet(Variable):
  72. """
  73. List of action update world, rebuild world,
  74. """
  75. type = "bool"
  76. opt = ["--rebuild-world"]
  77. untrusted = True
  78. value = "off"
  79. def init(self):
  80. self.help = _("Rebuild world")
  81. self.label = _("Rebuild world")
  82. class VariableClUpdateRevSet(Variable):
  83. """
  84. List of action update world, rebuild world,
  85. """
  86. type = "bool"
  87. opt = ["--update-rev"]
  88. untrusted = True
  89. value = "on"
  90. check_after = ["cl_update_sync_rep",
  91. "cl_update_metadata_force",
  92. "cl_update_other_set",
  93. "cl_update_eixupdate_force"]
  94. def init(self):
  95. self.help = _("make a revision update")
  96. self.label = _("Make a revision update")
  97. class VariableClUpdateRep(Variable):
  98. """
  99. Обновлять репозитории до конкретной ревизии или до последней
  100. """
  101. type = "choice"
  102. value = "rev"
  103. def choice(self):
  104. return ["last", "rev"]
  105. class VariableClUpdateRepData(ReadonlyTableVariable):
  106. """
  107. Информация о репозиториях
  108. """
  109. source = ['cl_update_rep_name',
  110. 'cl_update_rep_url',
  111. 'cl_update_rep_path',
  112. 'cl_update_rep_rev']
  113. class VariableClUpdateRepName(Variable):
  114. """
  115. Список имен используемых репозиториев
  116. """
  117. type = "list"
  118. value = []
  119. class VariableClUpdateRepUrl(Variable):
  120. """
  121. Список путей до репозиториев
  122. """
  123. type = "list"
  124. value = []
  125. class VariableClUpdateLaymanConfig(ReadonlyVariable):
  126. """
  127. Объект конфига layman
  128. """
  129. type = "object"
  130. def get(self):
  131. chroot = self.Get('cl_chroot_path')
  132. cp = ConfigParser()
  133. cp.read(path.join(chroot, 'etc/layman/layman.cfg'))
  134. return cp
  135. class VariableClUpdateLaymanStorage(Variable):
  136. """
  137. Путь к репозиториям layman
  138. """
  139. param_name = "storage"
  140. fallback_value = "var/lib/layman"
  141. def get(self):
  142. cp = self.Get('cl_update_layman_config')
  143. res = cp.get('MAIN', self.param_name, fallback=self.fallback_value)
  144. return pathJoin(self.Get('cl_chroot_path'), res)
  145. class VariableClUpdateRepPath(ReadonlyVariable):
  146. """
  147. Пути до репозиториев
  148. """
  149. type = "list"
  150. mapPath = {'portage': 'usr/portage',
  151. 'gentoo': '/usr/portage'}
  152. def get(self):
  153. repPath = self.Get('cl_update_layman_storage')
  154. chroot_path = self.Get('cl_chroot_path')
  155. def generatePaths(names):
  156. for name in names:
  157. if name in self.mapPath:
  158. yield path.join(chroot_path, self.mapPath[name])
  159. else:
  160. yield path.join(repPath, name)
  161. return list(generatePaths(self.Get('cl_update_rep_name')))
  162. class VariableClUpdateRepRev(Variable):
  163. """
  164. Ревизии до которых необходимо обновить репозитории
  165. """
  166. type = "list"
  167. def get(self):
  168. cp = ConfigParser()
  169. revisions = self.Get('update.cl_update_binhost_revisions')
  170. if revisions:
  171. cp.read_string(unicode(revisions[0]))
  172. branch = self.Get('cl_update_branch')
  173. def generateBranch():
  174. for repname, branchname in zip(
  175. self.Get('cl_update_rep_name'),
  176. self.Get('cl_update_branch_name')):
  177. if branchname == Git.Reference.Tag:
  178. yield cp.get("vcs", repname, fallback=branch)
  179. else:
  180. yield branchname
  181. return list(generateBranch())
  182. class VariableClUpdateBranch(Variable):
  183. """
  184. Ветка на которую будет обновляться репозиторий если -getbinpkg
  185. """
  186. value = Git.Reference.Master
  187. class VariableClUpdateBranchData(TableVariable):
  188. """
  189. Выбор веток репозиториев до которых необходимо обновиться
  190. """
  191. opt = ["--branch"]
  192. metavalue = 'REFS'
  193. untrusted = True
  194. source = ["cl_update_branch_rep",
  195. "cl_update_branch_name"]
  196. def init(self):
  197. self.help = _("set references for repository (REPOSITORY:REF)")
  198. self.label = _("Repositories references")
  199. def raiseReadonlyIndexError(self, fieldname="", variablename="", value=""):
  200. """
  201. Неизвестный оврелей
  202. """
  203. raise VariableError(_("Repository %s not found")%value)
  204. class VariableClUpdateBranchRep(ReadonlyVariable):
  205. """
  206. Список доступных репозиторием
  207. """
  208. type = "list"
  209. def init(self):
  210. self.label = _("Repository")
  211. def get(self):
  212. return self.Get('cl_update_rep_name')
  213. class VariableClUpdateBinhostData(ReadonlyTableVariable):
  214. """
  215. Таблица содержащая
  216. binhost/содержимое файла ревизий/время доступа
  217. """
  218. source = ["cl_update_binhost_host",
  219. "cl_update_binhost_revisions",
  220. "cl_update_binhost_time"]
  221. def check_binhost(self, binhost):
  222. revision_files = [path.join(binhost, x)
  223. for x in self.Get('cl_update_binhost_revision_path')]
  224. timeout = self.GetInteger('cl_update_binhost_timeout')
  225. try:
  226. data = None
  227. for fn in revision_files:
  228. if data is None:
  229. data = urllib2.urlopen(fn, timeout=timeout).read()
  230. elif data != urllib2.urlopen(fn, timeout=timeout).read():
  231. return ""
  232. return data
  233. except urllib2.URLError:
  234. return ""
  235. re_revison = re.compile("\w+=(\w+)")
  236. def binhost_key(self, data):
  237. host, t = data
  238. try:
  239. cp = ConfigParser()
  240. cp.read_string(data.decode('utf-8'))
  241. data = sum(int(x) for x in cp['vcs'].values())
  242. except (TypeError, KeyError) as e:
  243. data = 0
  244. return (1 if int(time) >= 0 else 0,
  245. data,
  246. -int(time))
  247. def get_timestamp(self, binhost):
  248. DAY = 60 * 60 * 24
  249. timeout = self.GetInteger('cl_update_binhost_timeout')
  250. timestamp_file = path.join(binhost,
  251. self.Get('cl_update_binhost_timestamp_path'))
  252. try:
  253. t = time.time()
  254. data = urllib2.urlopen(timestamp_file,
  255. timeout=timeout).read().strip()
  256. if data.isdigit() and t - int(data) < 5 * DAY:
  257. return data, int((time.time() - t)*1000)
  258. except urllib2.URLError as e:
  259. pass
  260. except BaseException as e:
  261. if isinstance(e, KeyboardInterrupt):
  262. raise
  263. pass
  264. return "", -1
  265. def get(self, hr=HumanReadable.No):
  266. binhost = self.Get('cl_update_binhost')
  267. recheck = self.GetBool('cl_update_binhost_recheck_set')
  268. def generate_by_timestamp():
  269. for host in self.Get('cl_update_binhost_list'):
  270. if host:
  271. ts_content, duration = self.get_timestamp(host)
  272. if ts_content:
  273. yield host, ts_content, str(duration)
  274. if not recheck and binhost:
  275. ts, t = self.get_timestamp(binhost)
  276. if ts:
  277. data = self.check_binhost(binhost)
  278. if data:
  279. return [[binhost, data, str(t)]]
  280. for host, ts, t in sorted(generate_by_timestamp(),
  281. key=lambda x: (-int(x[1]), int(x[2]))):
  282. data = self.check_binhost(host)
  283. if data:
  284. return [[host, data, str(t)]]
  285. return [[]]
  286. class VariableClUpdateBinhostRecheckSet(Variable):
  287. """
  288. Принудительно обновить binhost
  289. """
  290. type = "bool"
  291. value = "off"
  292. opt = ["--scan"]
  293. def init(self):
  294. self.help = _("search for the most appropriate update server")
  295. self.label = _("Search for the most appropriate update server")
  296. class VariableClUpdateBinhostHost(FieldValue, ReadonlyVariable):
  297. """
  298. Список имен прочих репозиториев
  299. """
  300. type = "list"
  301. source_variable = "cl_update_binhost_data"
  302. column = 0
  303. class VariableClUpdateBinhostRevisions(FieldValue,ReadonlyVariable):
  304. """
  305. Список имен прочих репозиториев
  306. """
  307. type = "list"
  308. source_variable = "cl_update_binhost_data"
  309. column = 1
  310. class VariableClUpdateBinhostTime(FieldValue,ReadonlyVariable):
  311. """
  312. Список имен прочих репозиториев
  313. """
  314. type = "list"
  315. source_variable = "cl_update_binhost_data"
  316. column = 2
  317. class VariableClUpdateBranchName(Variable):
  318. """
  319. Список доступных репозиторием
  320. """
  321. type = "choiceedit-list"
  322. def init(self):
  323. self.label = _("Reference")
  324. def choice(self):
  325. return [
  326. Git.Reference.Tag,
  327. Git.Reference.Master,
  328. Git.Reference.Develop,
  329. Git.Reference.Update]
  330. def get(self):
  331. if "getbinpkg" in self.Get('cl_features'):
  332. return [Git.Reference.Tag for x in self.Get('cl_update_rep_name')]
  333. else:
  334. branch = self.Get('cl_update_branch')
  335. return [branch for x in self.Get('cl_update_rep_name')]
  336. class VariableClUpdateSyncRep(Variable):
  337. """
  338. Обновляемый репозиторий
  339. """
  340. type = "choice-list"
  341. element = "selecttable"
  342. opt = ["-r", "--repositories"]
  343. metavalue = "REPOSITORIES"
  344. untrusted = True
  345. @property
  346. def rep_name(self):
  347. return self.Get('update.cl_update_rep_name')
  348. def init(self):
  349. self.help = _("synchronized repositories (all by default)")
  350. self.label = _("Synchronized repositories")
  351. def set(self, value):
  352. orderList = self.rep_name
  353. return sorted(value, key=lambda x:
  354. (orderList.index(x) if x in orderList else -1), reverse=True)
  355. def get(self):
  356. return list(reversed(self.rep_name))
  357. def choice(self):
  358. return self.rep_name
  359. class VariableClUpdateSyncOverlayRep(ReadonlyVariable):
  360. """
  361. Обновляемые репозитории (исключая portage)
  362. """
  363. type = "list"
  364. def get(self):
  365. return filter(lambda x: x != "portage", self.Get('cl_update_sync_rep'))
  366. class VariableClUpdateOutdateSet(ReadonlyVariable):
  367. """
  368. Флаг устанавливаемый в ходе обновления репозиториев,
  369. сообщающий что хотя бы один из запланированных репозиториев
  370. обновлен и следует обновляет различные метаданные
  371. Если обновляются прочие оверлеи - данные считаются что устарели
  372. """
  373. type = "bool"
  374. def get(self):
  375. if (self.Get('cl_update_other_set') == 'on' and
  376. self.Get('cl_update_other_rep_name')):
  377. return "on"
  378. return "off"
  379. class VariableClUpdateMetadataForce(Variable):
  380. """
  381. Принудительное действие с метаданными
  382. """
  383. type = "choice"
  384. value = "auto"
  385. opt = ["--update-metadata"]
  386. syntax = "--{choice}-update-metadata"
  387. metavalue = "MODE"
  388. #untrusted = True
  389. def init(self):
  390. self.help = ("'force' - " + _("force the update ebuilds metadata") +
  391. ",\n'skip' - " + _("skip the ebuild metadata update") +
  392. ",\n'auto' - " + _("update metadata if it is outdated"))
  393. self.label = _("Update metadata")
  394. def choice(self):
  395. return [("force", _("Force")),
  396. ("skip", _("Skip")),
  397. ("auto", _("If needed"))]
  398. class VariableClUpdateEgencacheForce(Variable):
  399. """
  400. Принудительное выполнение egencache
  401. """
  402. type = "choice"
  403. value = "auto"
  404. opt = ["--egencache"]
  405. syntax = "--{choice}-egencache"
  406. metavalue = "MODE"
  407. #untrusted = True
  408. def init(self):
  409. self.help = ("'force' - " + _("force the update of the overlays cache") +
  410. ",\n'skip' - " + _("skip the update of the overlays cache") +
  411. ",\n'auto' - " + _("update the overlays cache if outdated"))
  412. self.label = _("Update the overlays cache")
  413. def choice(self):
  414. return [("force", _("Force")),
  415. ("skip", _("Skip")),
  416. ("auto", _("If needed"))]
  417. class VariableClUpdateEixupdateForce(Variable):
  418. """
  419. Принудительное действие с eix-update
  420. """
  421. type = "choice"
  422. value = "auto"
  423. opt = ["--eix-update"]
  424. syntax = "--{choice}-eix-update"
  425. metavalue = "MODE"
  426. #untrusted = True
  427. def init(self):
  428. self.help = ("'force' - " + _("force the eix cache update") +
  429. ",\n'skip' - " + _("skip the eix cache update") +
  430. ",\n'auto' - " + _("update the eix cache if it "
  431. "is outdated"))
  432. self.label = _("Update the eix cache")
  433. def choice(self):
  434. return [("force", _("Force")),
  435. ("skip", _("Skip")),
  436. ("auto", _("If needed"))]
  437. class VariableClUpdateOtherSet(Variable):
  438. """
  439. Обновить остальные оверлеи
  440. """
  441. type = "bool"
  442. value = "off"
  443. opt = ["-o", "--update-other"]
  444. def init(self):
  445. self.help = _("update other overlays")
  446. self.label = _("Update other overlays")
  447. class VariableClUpdateOtherRepData(ReadonlyTableVariable):
  448. """
  449. Информация о прочих репозиториях
  450. """
  451. source = ['cl_update_other_rep_name',
  452. 'cl_update_other_rep_path']
  453. portdir_overlay = "main.cl_portdir_overlay"
  454. def generator(self):
  455. repNames = self.Get('update.cl_update_rep_name')
  456. chroot_path = self.Get('cl_chroot_path')
  457. layman = Layman(self.Get('update.cl_update_layman_installed'),
  458. self.Get('update.cl_update_layman_make'),
  459. self.Get('update.cl_update_layman_conf'),
  460. prefix=chroot_path)
  461. layman_overlays = layman.get_installed()
  462. for rpath in self.Get(self.portdir_overlay):
  463. repo_file = path.join(rpath, "profiles/repo_name")
  464. rname = readFile(repo_file).strip() or path.basename(rpath)
  465. if rname in layman_overlays and rname not in repNames:
  466. yield (rname, rpath)
  467. def get(self, hr=HumanReadable.No):
  468. return list(self.generator())
  469. class VariableClUpdateOtherRepName(FieldValue,ReadonlyVariable):
  470. """
  471. Список имен прочих репозиториев
  472. """
  473. type = "list"
  474. source_variable = "cl_update_other_rep_data"
  475. column = 0
  476. class VariableClUpdateOtherRepPath(FieldValue,ReadonlyVariable):
  477. """
  478. Список путей до прочих репозиториев
  479. """
  480. type = "list"
  481. source_variable = "cl_update_other_rep_data"
  482. column = 1
  483. class VariableClUpdateLaymanInstalled(VariableClUpdateLaymanStorage):
  484. """
  485. Путь до файла layman installed.xml
  486. """
  487. param_name = "installed"
  488. fallback_value = "var/lib/layman/installed.xml"
  489. class VariableClUpdateLaymanMake(VariableClUpdateLaymanStorage):
  490. """
  491. Путь до файла make.conf изменяемого layman`ом
  492. """
  493. param_name = "make_conf"
  494. fallback_value = "var/lib/layman/make.conf"
  495. class VariableClUpdateLaymanConf(VariableClUpdateLaymanStorage):
  496. """
  497. Путь до конфигурационного файла репозиториев для layman
  498. """
  499. param_name = "repos_conf"
  500. fallback_value = "etc/portage/repos.conf/layman.conf"
  501. class VariableClUpdatePretendSet(Variable):
  502. """
  503. Запустить предварительную проверку на обновления
  504. """
  505. type = "bool"
  506. value = "off"
  507. opt = ["-p", "--pretend"]
  508. def init(self):
  509. self.label = _("Pretend a package update")
  510. self.help = _("instead of actually performing the update, "
  511. "simply display the list of packages that "
  512. "will be installed")
  513. class VariableClUpdateSyncOnlySet(Variable):
  514. """
  515. Не выполнять установку/обновление пакетов при обновлении
  516. """
  517. type = "bool"
  518. value = "off"
  519. opt = ["-s", "--sync-only"]
  520. def init(self):
  521. self.label = _("Only synchronize repositories")
  522. self.help = _("do not update packages")
  523. def check(self, value):
  524. if (value == "on" and self.Get('cl_rebuild_world_set') != 'on' and
  525. not self.Get('cl_update_sync_rep') and
  526. self.Get('cl_update_other_set') == 'off' and
  527. self.Get('cl_update_rev_set') == 'off' and
  528. self.Get('cl_update_metadata_force') != "force" and
  529. self.Get('cl_update_eixupdate_force') != "force"):
  530. raise VariableError(_("Select at least one sync repository"))
  531. class VariableClUpdateWaitAnotherSet(Variable):
  532. """
  533. Ждать завершения другого процесса обновления
  534. """
  535. type = "bool"
  536. value = "on"
  537. opt = ["--wait-another-update"]
  538. def init(self):
  539. self.label = _("Wait for another update to be complete")
  540. self.help = _("wait until the running update is finished")
  541. class VariableClUpdateProfileStorage(ReadonlyVariable):
  542. type = "object"
  543. def get(self):
  544. return RepositoryStorageSet(
  545. LocalStorage('/var/lib/layman'),
  546. CacheStorage('/var/calculate/tmp/update'))
  547. class VariableClUpdateRepHost(Variable):
  548. type = "list"
  549. value = ['calculate']
  550. class VariableClUpdateRepHostUrl(Variable):
  551. type = "list"
  552. value = ['git://git.calculate.ru/calculate/%s.git']
  553. class VariableClUpdateProfileDatavars(ReadonlyVariable):
  554. type = "object"
  555. profile = "cl_update_profile_system"
  556. profiles_path = "cl_update_profile_path"
  557. profiles_shortname = "cl_update_profile_shortname"
  558. def get(self):
  559. profile = self.Get(self.profile)
  560. path_profile = self.Select(self.profiles_path,
  561. where=self.profiles_shortname,
  562. eq=profile, limit=1)
  563. if path_profile:
  564. return DataVarsUpdateProfile(path_profile)
  565. return ""
  566. class VariableClUpdateProfileLinuxFullname(ReadonlyVariable):
  567. """
  568. Имя системы в профиле
  569. """
  570. def init(self):
  571. self.label = _("Distribution name")
  572. datavars = "cl_update_profile_datavars"
  573. def get(self):
  574. dv = self.Get(self.datavars)
  575. if dv:
  576. try:
  577. subname = dv.Get('os_linux_subname')
  578. linuxname = dv.Get('os_linux_name')
  579. linuxver = dv.Get('os_linux_ver')
  580. if subname:
  581. return "%s %s %s" % (linuxname, linuxver, subname)
  582. return "%s %s" %(linuxname,linuxver)
  583. except DataVarsError as s:
  584. raise VariableError("Wrong Calculate Linux profile")
  585. return ""
  586. class VariableClUpdateProfileDependData(ReadonlyTableVariable):
  587. """
  588. Зависимые репозитории
  589. """
  590. source = ['cl_update_profile_depend_name',
  591. 'cl_update_profile_depend_url']
  592. datavars = "cl_update_profile_datavars"
  593. def init(self):
  594. self.label = _("Used repositories")
  595. @staticmethod
  596. def url_like(url1, url2):
  597. match1 = VariableClUpdateProfileUrl.re_url.search(url1)
  598. match2 = VariableClUpdateProfileUrl.re_url.search(url2)
  599. if match1 and match2:
  600. return match1.group(2).lower() == match2.group(2).lower()
  601. return False
  602. def get(self, hr=HumanReadable.No):
  603. dv = self.Get(self.datavars)
  604. # TODO: неиспользуемая переменная возможно
  605. # испольуется для инициализации
  606. #url = self.Get('cl_update_profile_url').lower()
  607. if dv:
  608. return reversed(zip(dv.Get('cl_update_rep_name'),
  609. dv.Get('cl_update_rep_url')))
  610. return ""
  611. setValue = Variable.setValue
  612. class VariableClUpdateTemplatesLocate(Variable):
  613. """
  614. Выбранные типы хранилищ шаблонов
  615. """
  616. type = "choice-list"
  617. element = "selecttable"
  618. opt = ["-T","--templates"]
  619. metavalue = "TEMPLATES"
  620. untrusted = True
  621. check_after = ['cl_update_profile_system']
  622. profile_datevars = "update.cl_update_profile_datavars"
  623. descriptionMap = {'overlay': _('Overlay templates'),
  624. 'local': _('Local templates'),
  625. 'calculate': _("Calculate overlay templates"),
  626. 'distros': _('Distribution templates'),
  627. 'distro': _('Distribution templates'),
  628. 'remote': _('Remote templates'),
  629. 'clt': _('clt templates')}
  630. def init(self):
  631. self.label = _("Templates location")
  632. self.help = _("select the location for templates %s") \
  633. %",".join(self.get())
  634. def get(self):
  635. dv = self.Get(self.profile_datevars)
  636. if dv:
  637. return dv.Get('cl_template_location') + ['clt']
  638. else:
  639. return self.Get('cl_templates_locate')
  640. def choice(self):
  641. descr = lambda x: self.descriptionMap.get(x,
  642. _("%s overlay templates" % x))
  643. return map(lambda x: (x, descr(x)), self.get())
  644. class VariableClUpdateProfileDependName(FieldValue, ReadonlyVariable):
  645. type = "list"
  646. source_variable = "cl_update_profile_depend_data"
  647. column = 0
  648. def init(self):
  649. self.label = _("Name")
  650. class VariableClUpdateProfileDependUrl(FieldValue, ReadonlyVariable):
  651. type = "list"
  652. source_variable = "cl_update_profile_depend_data"
  653. column = 1
  654. def init(self):
  655. self.label = _("URL")
  656. class VariableClUpdateProfileRepName(ReadonlyVariable):
  657. type = "list"
  658. def get(self):
  659. dv = self.Get('cl_update_profile_datavars')
  660. if dv:
  661. return dv.Get('cl_update_rep_name')
  662. return []
  663. class VariableClUpdateProfileSyncRep(ReadonlyVariable):
  664. type = "list"
  665. def get(self):
  666. return list(reversed(self.Get('cl_update_profile_rep_name')))
  667. class VariableClUpdateProfileRepUrl(ReadonlyVariable):
  668. type = "list"
  669. def get(self):
  670. dv = self.Get('cl_update_profile_datavars')
  671. if dv:
  672. return dv.Get('cl_update_rep_url')
  673. return []
  674. class VariableClUpdateProfileLinuxVer(ReadonlyVariable):
  675. """
  676. Имя системы в профиле
  677. """
  678. def init(self):
  679. self.label = _("System profile version")
  680. def get(self):
  681. dv = self.Get('cl_update_profile_datavars')
  682. if dv:
  683. return dv.Get('os_linux_ver')
  684. return ""
  685. class VariableClUpdateProfileLinuxName(ReadonlyVariable):
  686. """
  687. Имя системы в профиле
  688. """
  689. def get(self):
  690. dv = self.Get('cl_update_profile_datavars')
  691. if dv:
  692. dv.Get('os_linux_name')
  693. return ""
  694. class VariableClUpdateProfileUrl(Variable):
  695. """
  696. URL текущего репозитория
  697. """
  698. untrusted = True
  699. check_after = ["cl_update_profile_branch"]
  700. opt = ["--url"]
  701. metavalue = "URL"
  702. profile = "cl_profile_system"
  703. branch = "cl_update_profile_branch"
  704. storage = "cl_update_profile_storage"
  705. default_url = "git://git.calculate.ru/calculate/distros.git"
  706. profiles_shortname = 'cl_update_profile_shortname'
  707. @property
  708. def current_root(self):
  709. return '/'
  710. def init(self):
  711. self.label = _("Profile repository")
  712. self.help = _("set the profile repository")
  713. re_url = re.compile(
  714. r"^(?:(%s)://)?(\w[\w\./:-]+?\w)(\.git)?$" % "|".join(
  715. ["http", "https", "git"]))
  716. re_shortname = re.compile('^(?:([\w\.-]+):)?([\w\.-]+)$')
  717. @classmethod
  718. def normalize_url(cls, url):
  719. match = cls.re_url.match(url)
  720. if not match:
  721. raise VariableError(_("Wrong repository URL"))
  722. url = match.group(2)
  723. url = "%s://%s" % (match.group(1) or "git", url)
  724. url = "%s.git" % url
  725. return url
  726. def url_by_shortname(self, value):
  727. match = self.re_shortname.match(value)
  728. if not match.group(1):
  729. template = self.Get('update.cl_update_rep_host_url')
  730. if template:
  731. template = template[0]
  732. else:
  733. template = self.Select('update.cl_update_rep_host_url',
  734. where='update.cl_update_rep_host',
  735. eq=match.group(1), limit=1)
  736. if not template:
  737. raise VariableError(_("Failed to determine the repository host"))
  738. try:
  739. return template % match.group(2)
  740. except TypeError:
  741. raise VariableError(_("Failed to determine the repository host"))
  742. def set(self, value):
  743. if self.re_shortname.match(value):
  744. value = self.url_by_shortname(value)
  745. return self.normalize_url(value)
  746. def check(self, value):
  747. if not value:
  748. raise VariableError("Need to specify profile repository")
  749. try:
  750. branch = self.Get(self.branch)
  751. self.Get(self.storage).get_profiles(value, branch)
  752. except GitError as e:
  753. raise VariableError(str(e))
  754. if not self.Get(self.profiles_shortname):
  755. raise VariableError(_("Repository %s has no profiles")%value)
  756. def get(self):
  757. try:
  758. profile = self.Get(self.profile)
  759. if profile:
  760. while (profile != self.current_root and
  761. ".git" not in listDirectory(profile)):
  762. profile = path.dirname(profile)
  763. if profile == self.current_root:
  764. return ""
  765. git = Git()
  766. return git.get_url(profile, "origin") or ""
  767. except Exception:
  768. pass
  769. return self.default_url
  770. class VariableClUpdateProfileRepoName(ReadonlyVariable):
  771. def init(self):
  772. self.label = _("Repository name")
  773. storage = "cl_update_profile_storage"
  774. url = "cl_update_profile_url"
  775. def get(self):
  776. rep_set = self.Get(self.storage)
  777. url = self.Get(self.url)
  778. rep = rep_set.get_repository(url, branch=None)
  779. if rep:
  780. return rep.repo_name
  781. return ""
  782. class VariableClUpdateProfileBranch(Variable):
  783. """
  784. Текущий репозиторий
  785. """
  786. untrusted = True
  787. type = "edit"
  788. opt = ["--branch"]
  789. metavalue = "BRANCH"
  790. storage = "cl_update_profile_storage"
  791. url = "cl_update_profile_url"
  792. value = DEFAULT_BRANCH
  793. def init(self):
  794. self.label = _("Repository branch")
  795. self.help = _("set the repository branch")
  796. def check(self, value):
  797. pass
  798. ## TODO: проверка ветки
  799. #try:
  800. # url = self.Get('cl_update_profile_url')
  801. # self.Get('cl_update_profile_storage').get_profiles(url, value)
  802. #except GitError as e:
  803. # raise VariableError(str(e))
  804. class VariableClProfileRepository(ReadonlyVariable):
  805. """
  806. Репозиторий из которого будут получены профили
  807. """
  808. type = "object"
  809. def get(self):
  810. try:
  811. profile_dn = self.Get('cl_profile_system')
  812. while profile_dn != '/' and ".git" not in listDirectory(profile_dn):
  813. profile_dn = path.dirname(profile_dn)
  814. if profile_dn == '/':
  815. return ""
  816. ls = LocalStorage(path.dirname(profile_dn))
  817. return ProfileRepository(path.basename(profile_dn), ls)
  818. except Exception:
  819. return ""
  820. class VariableClProfileData(ReadonlyTableVariable):
  821. """
  822. Таблица данных о текущих профилях системы
  823. """
  824. source = []
  825. repository = "cl_profile_repository"
  826. def profile_filter(self, profiles):
  827. arch = self.Get('os_arch_machine_gentoo')
  828. return [x for x in profiles if x.arch == arch]
  829. def get(self, hr=HumanReadable.No):
  830. rep = self.Get(self.repository)
  831. if not rep:
  832. return [[]]
  833. profiles = rep.get_profiles()
  834. if not profiles:
  835. return [[]]
  836. repo_name = profiles[0].repository.repo_name
  837. filtered_profiles = self.profile_filter(profiles)
  838. full_name = [x.profile for x in filtered_profiles]
  839. profile_path = [x.path for x in filtered_profiles]
  840. profile_arch = [x.arch for x in filtered_profiles]
  841. short_name = simplify_profiles(full_name)
  842. full_name = ["%s:%s" % (repo_name, x) for x in full_name]
  843. return zip(full_name,
  844. short_name,
  845. profile_path,
  846. profile_arch)
  847. setValue = Variable.setValue
  848. class VariableClUpdateProfileRepository(ReadonlyVariable):
  849. """
  850. Репозиторий из которого будет извлечён список профилей
  851. """
  852. type = "object"
  853. url = "cl_update_profile_url"
  854. storage = "cl_update_profile_storage"
  855. branch = "cl_update_profile_branch"
  856. sync_set = "update.cl_update_profile_sync_set"
  857. def get(self):
  858. url = self.Get(self.url)
  859. if not url:
  860. return ""
  861. try:
  862. rep_set = self.Get(self.storage)
  863. branch = self.Get(self.branch)
  864. rep = rep_set.get_repository(url, branch)
  865. if rep and self.Get(self.sync_set) == 'on':
  866. rep.sync()
  867. return rep_set.get_repository(url, branch) or ""
  868. except GitError:
  869. return ""
  870. class VariableClUpdateProfileRepositoryName(ReadonlyVariable):
  871. """
  872. Название репозитория, из которого будут извлечены профили
  873. """
  874. def get(self):
  875. rep = self.Get('cl_update_profile_repository')
  876. if rep:
  877. return rep.repo_name
  878. return ""
  879. class VariableClUpdateProfileData(VariableClProfileData):
  880. source = ["cl_update_profile_fullname",
  881. "cl_update_profile_shortname",
  882. "cl_update_profile_path",
  883. "cl_update_profile_arch"]
  884. repository = "cl_update_profile_repository"
  885. class VariableClUpdateProfileFullname(FieldValue, ReadonlyVariable):
  886. """
  887. Полное название профиля
  888. """
  889. type = "list"
  890. source_variable = "cl_update_profile_data"
  891. column = 0
  892. class VariableClUpdateProfileShortname(FieldValue, ReadonlyVariable):
  893. """
  894. Упрощенное название профиля
  895. """
  896. type = "list"
  897. source_variable = "cl_update_profile_data"
  898. column = 1
  899. class VariableClUpdateProfilePath(FieldValue, ReadonlyVariable):
  900. """
  901. Путь от корня до профиля
  902. """
  903. type = "list"
  904. source_variable = "cl_update_profile_data"
  905. column = 2
  906. class VariableClUpdateProfileArch(FieldValue, ReadonlyVariable):
  907. """
  908. Архитектура профиля
  909. """
  910. type = "list"
  911. source_variable = "cl_update_profile_data"
  912. column = 3
  913. class VariableClUpdateProfileSystem(Variable):
  914. """
  915. Профиль системы (симлинк /etc/make.profile')
  916. """
  917. type = "choice"
  918. opt = ["cl_update_profile_system"]
  919. untrusted = True
  920. metavalue = "PROFILE"
  921. profiles_path = "cl_update_profile_path"
  922. profiles_shortname = "cl_update_profile_shortname"
  923. profiles_fullname = "cl_update_profile_fullname"
  924. profiles_arch = "cl_update_profile_arch"
  925. profile = "cl_profile_system"
  926. url = "cl_update_profile_url"
  927. gentoo_arch = 'os_arch_machine_gentoo'
  928. def init(self):
  929. self.label = _("System profile")
  930. self.help = _("set the system profile")
  931. def check(self, profile):
  932. if not profile:
  933. raise VariableError(_("You must specify the profile"))
  934. path_profile = self.Select(self.profiles_path,
  935. where=self.profiles_shortname,
  936. eq=profile, limit=1)
  937. profile_fullname = self.Select(self.profiles_fullname,
  938. where=self.profiles_shortname,
  939. eq=profile, limit=1)
  940. if path_profile:
  941. dv = DataVarsUpdateProfile(path_profile)
  942. if ":" in profile_fullname:
  943. repo_name = profile_fullname.partition(":")[0]
  944. else:
  945. repo_name = ""
  946. try:
  947. if (not dv.Get('cl_update_rep_name') or
  948. not dv.Get('cl_update_rep_url')):
  949. raise VariableError(
  950. _("Repository variables "
  951. "were not configured for the profile"))
  952. if not dv.Get('os_linux_name'):
  953. raise VariableError("")
  954. if repo_name not in list(dv.Get('cl_update_rep_name')):
  955. raise VariableError(
  956. _("Overlay %s is not specified "
  957. "in cl_update_rep_name") % repo_name)
  958. except (DataVarsError,VariableError) as e:
  959. if str(e):
  960. message = ". " + str(e)
  961. else:
  962. message = ""
  963. raise VariableError(_("The selected profile is not Calculate")
  964. + message)
  965. else:
  966. raise VariableError(_("Wrong Calculate profile"))
  967. def get(self):
  968. try:
  969. profile_system = self.Get(self.profile)
  970. profile = self.Select(self.profiles_shortname,
  971. where=self.profiles_path,
  972. eq=profile_system, limit=1)
  973. if profile:
  974. return profile
  975. except VariableError:
  976. pass
  977. shortname = self.Get(self.profiles_shortname)
  978. if len(shortname) == 1:
  979. return shortname[0]
  980. return ""
  981. def choice(self):
  982. url = self.Get(self.url)
  983. if not url:
  984. return []
  985. arch = self.Get(self.gentoo_arch)
  986. profiles = zip(*self.Select([self.profiles_shortname,
  987. self.profiles_fullname],
  988. where=self.profiles_arch, eq=arch))
  989. if profiles:
  990. short_name, full_name = profiles
  991. return zip(short_name, full_name)
  992. return []
  993. class DataVarsUpdateProfile(SimpleDataVars):
  994. """
  995. Упрощенная модель переменных для получения данных с удаленного профиля
  996. """
  997. source = ['cl_update_rep_name',
  998. 'cl_update_rep_url',
  999. 'cl_update_rep_path',
  1000. 'cl_update_rep_rev',
  1001. 'cl_update_branch_name']
  1002. def __init__(self, profile, chroot_path='/', recheck=None):
  1003. SimpleDataVars.__init__(
  1004. self,
  1005. lib_linux.VariableOsLinuxName(),
  1006. lib_linux.VariableOsLinuxShortname(),
  1007. lib_linux.VariableOsLinuxSubname(),
  1008. lib_linux.VariableOsLinuxVer(),
  1009. lib_linux.VariableClProfileSystem(),
  1010. env.VariableClRepositoryData(),
  1011. env.VariableClRepositoryName(),
  1012. env.VariableClRepositoryLocation(),
  1013. env.VariableClChrootPath(),
  1014. env.VariableClTemplateLocation(),
  1015. env.VariableClTemplatePath(),
  1016. env.VariableClEmergeConfig(systemRoot=chroot_path),
  1017. env.VariableClFeatures(),
  1018. VariableClUpdateRepData(section="update"),
  1019. VariableClUpdateRepPath(section="update"),
  1020. VariableClUpdateRepRev(section="update"),
  1021. VariableClUpdateBranch(section="update"),
  1022. VariableClUpdateBranchName(section="update"),
  1023. VariableClUpdateLaymanConfig(section="update"),
  1024. VariableClUpdateLaymanStorage(section="update"),
  1025. VariableClUpdateRepName(section="update"),
  1026. VariableClUpdateRep(section="update"),
  1027. VariableClUpdateRepUrl(section="update"),
  1028. VariableClUpdateBinhost(section="update"),
  1029. VariableClUpdateBinhostData(section="update"),
  1030. VariableClUpdateBinhostHost(section="update"),
  1031. VariableClUpdateBinhostRecheckSet(section="update"),
  1032. VariableClUpdateBinhostRevisions(section="update"),
  1033. VariableClUpdateBinhostTime(section="update"),
  1034. VariableClUpdateBinhostTimeout(section="update"),
  1035. VariableClUpdateBinhostTimestampPath(section="update"),
  1036. VariableClUpdateBinhostList(section="update"),
  1037. VariableClUpdateBinhostRevisionPath(section="update"),
  1038. )
  1039. self['cl_profile_system'] = profile
  1040. self['cl_chroot_path'] = chroot_path
  1041. if recheck is not None:
  1042. self['cl_update_binhost_recheck_set'] = recheck
  1043. self.flIniFileFrom(profile)
  1044. def __repr__(self):
  1045. return "Profile variables"
  1046. class VariableClUpdateProfileSyncSet(Variable):
  1047. """
  1048. Синхронизировать репозиторий перед сменой профиля
  1049. """
  1050. type = "bool"
  1051. value = "off"
  1052. opt = ["-u", "--update-cache"]
  1053. def init(self):
  1054. self.label = _("Update the cache")
  1055. self.help = _("Update the cache")
  1056. class VariableClUpdateAutocheckSet(Variable):
  1057. """
  1058. Выполнять/невыполнять автопроверку обновлений
  1059. """
  1060. type = "bool"
  1061. value = "on"
  1062. opt = ["-a", "--autocheck"]
  1063. def init(self):
  1064. self.label = _("Automatically check updates")
  1065. self.help = _("automatically check updates")
  1066. class VariableClUpdateAutocheckInterval(Variable):
  1067. """
  1068. Интервал выполнения проверки
  1069. """
  1070. type = "choiceedit"
  1071. opt = ["-I", "--autocheck-interval"]
  1072. metavalue = "INTERVAL"
  1073. def init(self):
  1074. self.label = _("Interval for the updates checking")
  1075. self.help = _("set interval for the updates checking")
  1076. def choice(self):
  1077. return [["6h",_("every six hours")],
  1078. ["12h",_("every twelve hours")],
  1079. ["1d",_("daily")]]
  1080. class VariableClUpdateAutocheckScheduleSet(Variable):
  1081. """
  1082. Запустить проверку только если со времени последнего запуска прошло
  1083. необходимое время
  1084. """
  1085. type = "bool"
  1086. value = "off"
  1087. opt = ["--schedule"]
  1088. def init(self):
  1089. self.label = _("Consider the autocheck schedule")
  1090. self.help = _("consider the autocheck schedule")
  1091. class VariableClUpdateEmergelistSet(Variable):
  1092. """
  1093. Вывести список пакетов в формате emerge
  1094. """
  1095. type = "bool"
  1096. value = "off"
  1097. opt = ["-e","--emergelist"]
  1098. def init(self):
  1099. self.label = _("Emerge-like packages list")
  1100. self.help = _("display the packages list in emerge format")
  1101. class VariableClUpdateKernelVersion(ReadonlyVariable):
  1102. """
  1103. Текущая версия ядра
  1104. """
  1105. def get(self):
  1106. return process('/bin/uname','-r').read().strip()
  1107. class VariableClUpdateKernelSrcPath(ReadonlyVariable):
  1108. """
  1109. Каталог содержащий исходный код текущего ядра
  1110. """
  1111. def get(self):
  1112. kernel_ver = self.Get('cl_update_kernel_version')
  1113. for template_path in ("/lib/modules/%s/build",
  1114. "/usr/src/linux-%s"):
  1115. src_path = template_path % kernel_ver
  1116. if path.exists(src_path):
  1117. if path.islink(src_path):
  1118. return os.readlink(src_path)
  1119. else:
  1120. return src_path
  1121. else:
  1122. return ""
  1123. class VariableClUpdateKernelPkg(ReadonlyVariable):
  1124. """
  1125. Пакет текущего ядра
  1126. """
  1127. def get(self):
  1128. src_path = self.Get('cl_update_kernel_src_path')
  1129. if src_path:
  1130. qfile = process('/usr/bin/qfile', '-vC', src_path)
  1131. if qfile.success():
  1132. return qfile.read().partition(" ")[0]
  1133. return ""
  1134. class VariableClUpdateLinesLimit(Variable):
  1135. """
  1136. Количество выводимых строк при ошибке
  1137. """
  1138. type = "int"
  1139. value = "30"
  1140. class VariableClUpdateSkipSetupSet(Variable):
  1141. """
  1142. Пропустить выполнение cl-setup-system в cl-update-profile
  1143. """
  1144. type = "bool"
  1145. value = "off"
  1146. opt = ["--skip-setup-system"]
  1147. def init(self):
  1148. self.label = _("Skip the system setup")
  1149. self.help = _("skip the system setup")
  1150. class VariableClUpdateCleanpkgSet(Variable):
  1151. """
  1152. Пропустить выполнение cl-setup-system в cl-update-profile
  1153. """
  1154. type = "bool"
  1155. value = "off"
  1156. opt = ["--clean-pkg"]
  1157. def init(self):
  1158. self.label = _("Clean obsolete programs archives")
  1159. self.help = _("clean obsolete programs archives")
  1160. class VariableClUpdateOutdatedKernelPath(Variable):
  1161. """
  1162. Файл-флаг наличия устаревшего, неудаленного ядра
  1163. """
  1164. value = "/var/lib/calculate/calculate-update/outdated_kernel"
  1165. class VariableClUpdateSkipRbSet(Variable):
  1166. """
  1167. Пропусить revdep-rebuild
  1168. """
  1169. type = "bool"
  1170. value = "off"
  1171. opt = ["-R", "--skip-revdep-rebuild"]
  1172. def init(self):
  1173. self.label = _("Skip reverse dependencies check")
  1174. self.help = _("skip reverse dependencies check")
  1175. class VariableClUpdateOutdatedKernelSet(ReadonlyVariable):
  1176. """
  1177. Есть наличие устаревшего ядра
  1178. """
  1179. type = "bool"
  1180. def get(self):
  1181. ui = UpdateInfo(self.parent)
  1182. return "on" if ui.outdated_kernel else "off"
  1183. class VariableClUpdateBinhostList(Variable):
  1184. """
  1185. Список хостов с бинарными обновлениями
  1186. """
  1187. type = "list"
  1188. value = ["ftp://ftp.calculate-linux.ru/pub/calculate"]
  1189. class VariableClUpdateBinhost(Variable):
  1190. """
  1191. Хост с бинарными обновлениями
  1192. """
  1193. value = ""
  1194. class VariableClUpdateBinhostRevisionPath(Variable):
  1195. """
  1196. Путь до revisions файлов
  1197. """
  1198. type = "list"
  1199. value = [
  1200. "grp/default/ini.env",
  1201. "grp/kde/ini.env",
  1202. "grp/server/ini.env",
  1203. "grp/x/ini.env"
  1204. ]
  1205. class VariableClUpdateBinhostTimestampPath(Variable):
  1206. """
  1207. Путь до файла timestamp
  1208. """
  1209. value = "timestamp"
  1210. class VariableClUpdateBinhostTimeout(Variable):
  1211. """
  1212. Таймаут на проверку одного binhost
  1213. """
  1214. type = "int"
  1215. value = "5"