autoupdate.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # @Base: Miro - an RSS based video player application
  2. # Copyright (C) 2017
  3. # Jesus E.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. #
  19. # In addition, as a special exception, the copyright holders give
  20. # permission to link the code of portions of this program with the OpenSSL
  21. # library.
  22. #
  23. # You must obey the GNU General Public License in all respects for all of
  24. # the code used other than OpenSSL. If you modify file(s) with this
  25. # exception, you may extend this exception to your version of the file(s),
  26. # but you are not obligated to do so. If you do not wish to do so, delete
  27. # this exception statement from your version. If you delete this exception
  28. # statement from all source files in the program, then also delete it here.
  29. """Autoupdate functionality """
  30. import ctypes
  31. import _winreg as winreg
  32. import logging
  33. winsparkle = ctypes.cdll.WinSparkle
  34. APPCAST_URL = 'http://miro-updates.participatoryculture.org/lvc-appcast.xml'
  35. def startup():
  36. enable_automatic_checks()
  37. winsparkle.win_sparkle_set_appcast_url(APPCAST_URL)
  38. winsparkle.win_sparkle_init()
  39. def shutdown():
  40. winsparkle.win_sparkle_cleanup()
  41. def enable_automatic_checks():
  42. # We should be able to use win_sparkle_set_automatic_check_for_updates,
  43. # but that's only available after version 0.4 and the current release
  44. # version is 0.4
  45. with open_winsparkle_key() as winsparkle_key:
  46. if not check_for_updates_set(winsparkle_key):
  47. set_default_check_for_updates(winsparkle_key)
  48. def open_winsparkle_key():
  49. """Open the MVC WinSparkle registry key
  50. If any components are not created yet, we will try to create them
  51. """
  52. with open_or_create_key(winreg.HKEY_CURRENT_USER, "Software") as software:
  53. with open_or_create_key(software,
  54. "Participatory Culture Foundation") as pcf:
  55. with open_or_create_key(pcf, "Libre Video Converter") as lvc:
  56. return open_or_create_key(lvc, "WinSparkle",
  57. write_access=True)
  58. def open_or_create_key(key, subkey, write_access=False):
  59. if write_access:
  60. sam = winreg.KEY_READ | winreg.KEY_WRITE
  61. else:
  62. sam = winreg.KEY_READ
  63. try:
  64. return winreg.OpenKey(key, subkey, 0, sam)
  65. except OSError as e:
  66. if e.errno == 2:
  67. # Not Found error. We should create the key
  68. return winreg.CreateKey(key, subkey)
  69. else:
  70. raise
  71. def check_for_updates_set(winsparkle_key):
  72. try:
  73. winreg.QueryValueEx(winsparkle_key, "CheckForUpdates")
  74. except OSError as e:
  75. if e.errno == 2:
  76. # not found error.
  77. return False
  78. else:
  79. raise
  80. else:
  81. return True
  82. def set_default_check_for_updates(winsparkle_key):
  83. """Initialize the WinSparkle regstry values with our defaults.
  84. :param lvc_key winreg.HKey object for to the MVC registry
  85. """
  86. logging.info("Writing WinSparkle keys")
  87. winreg.SetValueEx(winsparkle_key, "CheckForUpdates", 0, winreg.REG_SZ, "1")