Advertisement
Guest User

Untitled

a guest
Feb 26th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. #!/usr/bin/python
  2. # Copyright (c) 2009 Canonical Ltd
  3. #
  4. # AUTHOR:
  5. # Michael Vogt <mvo@ubuntu.com>
  6. #
  7. # unattended-upgrade-shutdown - helper that checks if a
  8. # unattended-upgrade is in progress and waits until it exists
  9. #
  10. # This file is part of unattended-upgrades
  11. #
  12. # unattended-upgrades is free software; you can redistribute it and/or
  13. # modify it under the terms of the GNU General Public License as published
  14. # by the Free Software Foundation; either version 2 of the License, or (at
  15. # your option) any later version.
  16. #
  17. # unattended-upgrades is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. # General Public License for more details.
  21. #
  22. # You should have received a copy of the GNU General Public License
  23. # along with unattended-upgrades; if not, write to the Free Software
  24. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. #
  26.  
  27. import apt_pkg
  28. import time
  29. import sys
  30. import logging
  31. import logging.handlers
  32. import gettext
  33. import subprocess
  34. import os.path
  35.  
  36. from optparse import OptionParser
  37. from gettext import gettext as _
  38.  
  39. def do_usplash(msg):
  40. if os.path.exists("/sbin/usplash_write"):
  41. logging.debug("Running usplash_write")
  42. subprocess.call(["/sbin/usplash_write","TEXT", msg])
  43. subprocess.call(["/sbin/usplash_write","PULSATE"])
  44.  
  45. def do_plymouth(msg):
  46. if os.path.exists("/bin/plymouth"):
  47. logging.debug("Running plymouth --text")
  48. subprocess.call(["/bin/plymouth","message", "--text", msg])
  49.  
  50. if __name__ == "__main__":
  51. # setup gettext
  52. localesApp="unattended-upgrades"
  53. localesDir="/usr/share/locale"
  54. gettext.bindtextdomain(localesApp, localesDir)
  55. gettext.textdomain(localesApp)
  56.  
  57. parser = OptionParser()
  58. parser.add_option("", "--debug",
  59. action="store_true", dest="debug", default=False,
  60. help="print debug messages")
  61. parser.add_option("", "--delay", default=10,
  62. help="delay in minutes to wait for unattended-upgrades")
  63. parser.add_option("", "--lock-file",
  64. default="/var/run/unattended-upgrades.lock",
  65. help="lock file location")
  66. (options, args) = parser.parse_args()
  67.  
  68. # setup logging
  69. level = logging.INFO
  70. if options.debug:
  71. level = logging.DEBUG
  72. logging.basicConfig(level=level, format="%(levelname)s - %(message)s")
  73. logger = logging.getLogger()
  74. try:
  75. logger.addHandler(logging.handlers.SysLogHandler(
  76. "/dev/log", logging.handlers.SysLogHandler.LOG_DAEMON))
  77. except Exception, e:
  78. logging.debug("failed to setup syslog logger: %s" % e)
  79.  
  80. # run
  81. start_time = time.time()
  82. while True:
  83. res = apt_pkg.GetLock(options.lock_file)
  84. logging.debug("GetLock returned %i" % res)
  85. if res > 0:
  86. logging.debug("Lock not taken")
  87. sys.exit(0)
  88. # wait a some seconds and try again
  89. msg = _("Unattended-upgrade in progress during shutdown, "
  90. "sleeping for 5s")
  91. logging.warning(msg)
  92. do_plymouth(msg)
  93. do_usplash(msg)
  94. time.sleep(5)
  95. if (time.time() - start_time) > options.delay*60:
  96. logging.warning(_("Giving up on lockfile after %s delay") % options.delay)
  97. sys.exit(1)
  98. sys.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement