Debian Codegraph @

bleachbit.py

raw
1 #!/usr/bin/env python3
2
3 # SPDX-License-Identifier: GPL-3.0-or-later
4 # Copyright (c) 2008-2026 Andrew Ziem.
5 #
6 # This work is licensed under the terms of the GNU GPL, version 3 or
7 # later. See the COPYING file in the top-level directory.
8
9 """
10 Launcher
11 """
12
13 import os
14 import sys
15
16
17 def _add_posix_share_to_path():
18 """Setup path on POSIX systems for AppImage"""
19 if os.name != 'posix':
20 return
21 # Add the appropriate share directory to sys.path so we can import bleachbit.
22 # This must happen before importing from bleachbit.Bootstrap.
23 #
24 _launcher_dir = os.path.dirname(os.path.abspath(__file__))
25 if not (os.path.isfile(os.path.join(_launcher_dir, 'bleachbit', '__init__.py')) and
26 os.path.isfile(os.path.join(_launcher_dir, 'bleachbit', 'Unix.py'))):
27 _appdir = os.environ.get('APPDIR')
28 if _appdir:
29 _share = os.path.normpath(os.path.join(_appdir, 'usr/share/'))
30 if os.path.isfile(os.path.join(_share, 'bleachbit', '__init__.py')):
31 sys.path.insert(0, _share)
32 elif os.path.isfile('/usr/share/bleachbit/__init__.py'):
33 sys.path.append('/usr/share/')
34
35
36 def main():
37 """Main entry point"""
38 _add_posix_share_to_path()
39 from bleachbit.Bootstrap import bootstrap, check_wayland_and_root
40 bootstrap()
41
42 # Check for GUI only when needed: this avoids a Gtk warning when
43 # a display is not available.
44 if len(sys.argv) == 1 or '--gui' in sys.argv:
45 from bleachbit.GtkShim import is_gtk_available
46 have_gui = is_gtk_available()
47 else:
48 have_gui = False
49
50 # If no arguments provided and display is available, then
51 # assume the user wants to start the GUI.
52 # If Wayland is a problem and no explicit CLI args were provided,
53 # then fall back to CLI.
54 if 1 == len(sys.argv) and have_gui and not check_wayland_and_root():
55 # Import GUI inside the condition for Linux packagers to
56 # separate GUI into another package.
57 import bleachbit.GuiApplication # pylint: disable=import-outside-toplevel
58 app = bleachbit.GuiApplication.Bleachbit()
59 sys.exit(app.run(sys.argv))
60
61 # Either CLI args were provided or no display is available
62 import bleachbit.CLI # pylint: disable=import-outside-toplevel
63 # If no args and defaulting to CLI, print usage information
64 if 1 == len(sys.argv) and not have_gui:
65 sys.argv.append('--help')
66 try:
67 bleachbit.CLI.process_cmd_line()
68 except BrokenPipeError:
69 # The downstream pipe consumer (e.g., `less`) closed before we
70 # finished writing. Redirect stdout to devnull to suppress the
71 # secondary BrokenPipeError raised by the interpreter while
72 # flushing stdout at shutdown, then exit with the conventional
73 # SIGPIPE status (128 + 13).
74 devnull = os.open(os.devnull, os.O_WRONLY)
75 os.dup2(devnull, sys.stdout.fileno())
76 os.close(devnull)
77 sys.exit(141)
78
79
80 if __name__ == '__main__':
81 main()