do-partial-upgrade 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/python3
  2. # -*- Mode: Python; indent-tabs-mode: nil; tab-width: 4; coding: utf-8 -*-
  3. #
  4. # Copyright (c) 2004-2012 Canonical
  5. # 2004-2008 Michael Vogt
  6. # 2004 Michiel Sikkes
  7. #
  8. # Author: Michiel Sikkes <michiel@eyesopened.nl>
  9. # Michael Vogt <mvo@debian.org>
  10. #
  11. # This program is free software; you can redistribute it and/or
  12. # modify it under the terms of the GNU General Public License as
  13. # published by the Free Software Foundation; either version 2 of the
  14. # License, or (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License
  22. # along with this program; if not, write to the Free Software
  23. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  24. # USA
  25. from __future__ import print_function
  26. import os
  27. import subprocess
  28. import sys
  29. from DistUpgrade.DistUpgradeVersion import VERSION
  30. from DistUpgrade.DistUpgradeController import DistUpgradeController
  31. from DistUpgrade.DistUpgradeConfigParser import DistUpgradeConfig
  32. from DistUpgrade.DistUpgradeMain import (
  33. setup_logging,
  34. setup_view,
  35. )
  36. import locale
  37. import gettext
  38. from optparse import OptionParser
  39. if __name__ == "__main__":
  40. #FIXME: Workaround a bug in optparser which doesn't handle unicode/str
  41. # correctly, see http://bugs.python.org/issue4391
  42. # Should be resolved by Python3
  43. gettext.bindtextdomain("ubuntu-release-upgrader", "/usr/share/locale")
  44. gettext.textdomain("ubuntu-release-upgrader")
  45. translation = gettext.translation("ubuntu-release-upgrader", fallback=True)
  46. if sys.version >= '3':
  47. _ = translation.gettext
  48. else:
  49. _ = translation.ugettext
  50. try:
  51. locale.setlocale(locale.LC_ALL, "")
  52. except:
  53. pass
  54. # gtk2 used to throw a exception when it failed to init the display,
  55. # so back then it was safe to try to import the frontend and fallback
  56. # to text if the import failed. this is no longer the case so we need
  57. # do figure it out here :/
  58. if "DISPLAY" in os.environ:
  59. default_frontend = "DistUpgradeViewGtk3"
  60. else:
  61. default_frontend = "DistUpgradeViewText"
  62. # Begin parsing of options
  63. parser = OptionParser()
  64. parser.add_option ("-V", "--version", action="store_true",
  65. dest="show_version", default=False,
  66. help=_("Show version and exit"))
  67. parser.add_option ("--data-dir", "", dest="datadir",
  68. default="/usr/share/ubuntu-release-upgrader/",
  69. help=_("Directory that contains the data files"))
  70. parser.add_option ("-f", "--frontend", default=default_frontend,
  71. dest="frontend",
  72. help=_("Run the specified frontend"))
  73. (options, args) = parser.parse_args()
  74. datadir = os.path.normpath(options.datadir)+"/"
  75. if options.show_version:
  76. print("%s: version %s" % (os.path.basename(sys.argv[0]), VERSION))
  77. sys.exit(0)
  78. # raise privileges when not started as root
  79. if os.getuid() != 0:
  80. # apply workaround for Wayland
  81. if ((options.frontend in {"DistUpgradeViewGtk3"} and
  82. 'WAYLAND_DISPLAY' in os.environ)):
  83. subprocess.run(['xhost', '+si:localuser:root'])
  84. os.execv("/usr/bin/pkexec", ["pkexec"] + sys.argv)
  85. # we are by definition in partial upgrade mode
  86. options.partial = True
  87. config = DistUpgradeConfig(options.datadir)
  88. logdir = setup_logging(options, config)
  89. view = setup_view(options, config, logdir)
  90. if options.frontend == "DistUpgradeViewGtk3":
  91. from gi.repository import Gtk
  92. import gi
  93. gi.require_version("Gtk", "3.0")
  94. Gtk.init_check(sys.argv)
  95. Gtk.Window.set_default_icon_name("system-software-update")
  96. view.label_title.set_markup("<b><big>%s</big></b>" %
  97. _("Running partial upgrade"))
  98. controller = DistUpgradeController(view, datadir=datadir)
  99. controller.doPartialUpgrade()