]> Dogcows Code - chaz/openbox/blob - tools/xdg-autostart/xdg-autostart
some modifications to xdg-autostart based on suggestions from the PyXDG maintainer
[chaz/openbox] / tools / xdg-autostart / xdg-autostart
1 #!/usr/bin/env python
2
3 # xdg-autostart runs things based on the XDG autostart specification
4 # Copyright (C) 2008 Dana Jansens
5 #
6 # XDG autostart specification can be found here:
7 # http://standards.freedesktop.org/autostart-spec/
8 #
9 #
10 #
11 # LICENSE:
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21
22 ME="xdg-autostart"
23 VERSION="1.0"
24
25 import os, glob, sys
26 try:
27 from xdg import BaseDirectory
28 from xdg.DesktopEntry import DesktopEntry
29 from xdg.Exceptions import ParsingError
30 except ImportError:
31 print
32 print "ERROR:", ME, "requires PyXDG to be installed"
33 print
34 sys.exit(1)
35
36 def main(argv=sys.argv):
37 if "--help" in argv[1:]:
38 show_help()
39 return 0
40 if "--version" in argv[1:]:
41 show_version()
42 return 0
43
44 # get the autostart directories
45 autodirs = BaseDirectory.load_config_paths("autostart")
46
47 # find all the autostart files
48 files = []
49 for dir in autodirs:
50 for path in glob.glob(os.path.join(dir, '*.desktop')):
51 try:
52 autofile = AutostartFile(path)
53 except ParsingError:
54 print "Invalid .desktop file: " + path
55 else:
56 if not autofile in files:
57 files.append(autofile)
58
59 list = False
60 if "--list" in argv[1:]:
61 list = True
62 argv.remove("--list")
63
64 # run them !
65 environments = argv[1:]
66 for autofile in files:
67 if list: autofile.display(environments)
68 else: autofile.run(environments)
69
70 class AutostartFile:
71 def __init__(self, path):
72 self.path = path
73 self.filename = os.path.basename(path)
74 self.dirname = os.path.dirname(path)
75 self.de = DesktopEntry(path)
76
77 def __eq__(self, other):
78 return self.filename == other.filename
79
80 def __str__(self):
81 return self.path + " : " + self.de.getName()
82
83 def _isexecfile(path):
84 return os.access(path, os.X_OK)
85
86 def _findFile(self, path, search, match_func):
87 # check empty path
88 if not path: return None
89 # check absolute path
90 if path[0] == '/':
91 if match_func(path): return path
92 else: return None
93 # check relative path
94 for dirname in search.split(os.pathsep):
95 if dirname != "":
96 candidate = os.path.join(dirname, path)
97 if (match_func(candidate)): return candidate
98
99 def _alert(self, str, info=False):
100 if info:
101 print "\t ", str
102 else:
103 print "\t*", str
104
105 def _showInEnvironment(self, envs, verbose=False):
106 default = not self.de.getOnlyShowIn()
107 noshow = False
108 force = False
109 for i in self.de.getOnlyShowIn():
110 if i in envs: force = True
111 for i in self.de.getNotShowIn():
112 if i in envs: noshow = True
113
114 if verbose:
115 if not default and not force:
116 s = ""
117 for i in self.de.getOnlyShowIn():
118 if s: s += ", "
119 s += i
120 self._alert("Excluded by: OnlyShowIn (" + s + ")")
121 if default and noshow and not force:
122 s = ""
123 for i in self.de.getOnlyShowIn():
124 if s: s += ", "
125 s += i
126 self._alert("Excluded by: NotShowIn (" + s + ")")
127 return (default and not noshow) or force
128
129 def _shouldRun(self, envs, verbose=False):
130 if not self.de.getExec():
131 if verbose: self._alert("Excluded by: Missing Exec field")
132 return False
133 if self.de.getHidden():
134 if verbose: self._alert("Excluded by: Hidden")
135 return False
136 if self.de.getTryExec():
137 if not self._findFile(self.de.getTryExec(), os.getenv("PATH"),
138 self._isexecfile):
139 if verbose: self._alert("Excluded by: TryExec (" +
140 self.de.getTryExec() + ")")
141 return False
142 if not self._showInEnvironment(envs, verbose):
143 return False
144 return True
145
146 def display(self, envs):
147 if self._shouldRun(envs):
148 print "[*] " + self.de.getName()
149 else:
150 print "[ ] " + self.de.getName()
151 self._alert("File: " + self.path, info=True)
152 if self.de.getExec():
153 self._alert("Executes: " + self.de.getExec(), info=True)
154 self._shouldRun(envs, True)
155 print
156
157 def run(self, envs):
158 here = os.getcwd()
159 if self.de.getPath():
160 os.chdir(self.de.getPath())
161 if self._shouldRun(envs):
162 args = ["/bin/sh", "-c", "exec " + self.de.getExec()]
163 os.spawnv(os.P_NOWAIT, args[0], args);
164 os.chdir(here)
165
166 def show_help():
167 print "Usage:", ME, "[OPTION]... [ENVIRONMENT]..."
168 print
169 print "This tool will run xdg autostart .desktop files"
170 print
171 print "OPTIONS"
172 print " --list Show a list of the files which would be run"
173 print " Files which would be run are marked with an asterix"
174 print " symbol [*]. For files which would not be run,"
175 print " information is given for why they are excluded"
176 print " --help Show this help and exit"
177 print " --version Show version and copyright information"
178 print
179 print "ENVIRONMENT specifies a list of environments for which to run autostart"
180 print "applications. If none are specified, only applications which do not "
181 print "limit themselves to certain environments will be run."
182 print
183 print "ENVIRONMENT can be one or more of:"
184 print " GNOME Gnome Desktop"
185 print " KDE KDE Desktop"
186 print " ROX ROX Desktop"
187 print " XFCE XFCE Desktop"
188 print " Old Legacy systems"
189 print
190
191 def show_version():
192 print ME, VERSION
193 print "Copyright (c) 2008 Dana Jansens"
194 print
195
196 if __name__ == "__main__":
197 sys.exit(main())
This page took 0.044883 seconds and 4 git commands to generate.