1#!/usr/bin/env python
2
3import gi, argparse, os, logging, sys
4
5gi.require_version("AccountsService", "1.0")
6from gi.repository import AccountsService, GLib
7from ordered_set import OrderedSet
8
9
10def get_session_file(session):
11 system_data_dirs = GLib.get_system_data_dirs()
12
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"}
17 )
18
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"))
23 )
24
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)
31 )
32 else:
33 session_files = OrderedSet(
34 session for session in session_files if is_session_xsession(session)
35 )
36
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]
42
43
44def is_session_xsession(session_file):
45 return "/xsessions/" in session_file
46
47
48def is_session_wayland(session_file):
49 return "/wayland-sessions/" in session_file
50
51
52def main():
53 parser = argparse.ArgumentParser(
54 description="Set session type for all normal users."
55 )
56 parser.add_argument("session", help="Name of session to set.")
57
58 args = parser.parse_args()
59
60 session = getattr(args, "session")
61 session_file = get_session_file(session)
62
63 user_manager = AccountsService.UserManager.get_default()
64 users = user_manager.list_users()
65
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}"
73 )
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}"
79 )
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)
86
87
88if __name__ == "__main__":
89 main()