qubes-session-autostart 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2015-2019 Marek Marczykowski-Górecki
  7. # <marmarek@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License
  11. # as published by the Free Software Foundation; either version 2
  12. # of the License, or (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. #
  23. #
  24. import sys
  25. from xdg.DesktopEntry import DesktopEntry
  26. from qubesagent.xdg import launch, find_dropins, \
  27. load_desktop_entry_with_dropins
  28. import xdg.BaseDirectory
  29. import os
  30. QUBES_XDG_CONFIG_DROPINS = '/etc/qubes/autostart'
  31. def entry_should_be_started(entry, environments):
  32. """
  33. :type entry: DesktopEntry
  34. """
  35. if entry.getHidden():
  36. return False
  37. if entry.getOnlyShowIn():
  38. return bool(set(entry.getOnlyShowIn()).intersection(environments))
  39. if entry.getNotShowIn():
  40. return not bool(set(entry.getNotShowIn()).intersection(environments))
  41. return True
  42. def process_autostart(environments):
  43. # handle only "most important" entry
  44. processed_entries = {}
  45. for path in xdg.BaseDirectory.load_config_paths('autostart'):
  46. try:
  47. entries = os.listdir(path)
  48. except Exception as e:
  49. print(
  50. "Failed to process path '{}': {}".format(path, str(e)),
  51. file=sys.stderr
  52. )
  53. continue
  54. for entry_name in entries:
  55. if entry_name in processed_entries:
  56. continue
  57. # make the entry as processed, even if not actually started
  58. processed_entries[entry_name] = True
  59. try:
  60. entry_path = os.path.join(path, entry_name)
  61. # files in $HOME have higher priority than dropins
  62. if not path.startswith(xdg.BaseDirectory.xdg_config_home):
  63. dropins = find_dropins(
  64. entry_path, QUBES_XDG_CONFIG_DROPINS)
  65. entry = load_desktop_entry_with_dropins(
  66. entry_path, dropins)
  67. else:
  68. entry = DesktopEntry(entry_path)
  69. if entry_should_be_started(entry, environments):
  70. launch(entry_path, wait=False)
  71. except Exception as e:
  72. print("Failed to process '{}': {}".format(
  73. entry_name, str(e)),
  74. file=sys.stderr
  75. )
  76. def main():
  77. process_autostart(sys.argv[1:])
  78. if __name__ == '__main__':
  79. main()