fluffychat: 1.22.1 -> 1.23.0 (#364091)
[NixPkgs.git] / nixos / modules / services / x11 / display-managers / set-session.py
blob75940efe32b42577150ea95c3ba2c3bc248098f3
1 #!/usr/bin/env python
3 import gi, argparse, os, logging, sys
5 gi.require_version("AccountsService", "1.0")
6 from gi.repository import AccountsService, GLib
7 from ordered_set import OrderedSet
10 def get_session_file(session):
11 system_data_dirs = GLib.get_system_data_dirs()
13 session_dirs = OrderedSet(
14 os.path.join(data_dir, session)
15 for data_dir in system_data_dirs
16 for session in {"wayland-sessions", "xsessions"}
19 session_files = OrderedSet(
20 os.path.join(dir, session + ".desktop")
21 for dir in session_dirs
22 if os.path.exists(os.path.join(dir, session + ".desktop"))
25 # Deal with duplicate wayland-sessions and xsessions.
26 # Needed for the situation in gnome-session, where there's
27 # a xsession named the same as a wayland session.
28 if any(map(is_session_wayland, session_files)):
29 session_files = OrderedSet(
30 session for session in session_files if is_session_wayland(session)
32 else:
33 session_files = OrderedSet(
34 session for session in session_files if is_session_xsession(session)
37 if len(session_files) == 0:
38 logging.warning("No session files are found.")
39 sys.exit(0)
40 else:
41 return session_files[0]
44 def is_session_xsession(session_file):
45 return "/xsessions/" in session_file
48 def is_session_wayland(session_file):
49 return "/wayland-sessions/" in session_file
52 def main():
53 parser = argparse.ArgumentParser(
54 description="Set session type for all normal users."
56 parser.add_argument("session", help="Name of session to set.")
58 args = parser.parse_args()
60 session = getattr(args, "session")
61 session_file = get_session_file(session)
63 user_manager = AccountsService.UserManager.get_default()
64 users = user_manager.list_users()
66 for user in users:
67 if user.is_system_account():
68 continue
69 else:
70 if is_session_wayland(session_file):
71 logging.debug(
72 f"Setting session name: {session}, as we found the existing wayland-session: {session_file}"
74 user.set_session(session)
75 user.set_session_type("wayland")
76 elif is_session_xsession(session_file):
77 logging.debug(
78 f"Setting session name: {session}, as we found the existing xsession: {session_file}"
80 user.set_x_session(session)
81 user.set_session(session)
82 user.set_session_type("x11")
83 else:
84 logging.error(f"Couldn't figure out session type for {session_file}")
85 sys.exit(1)
88 if __name__ == "__main__":
89 main()