3 # xdg-autostart runs things based on the XDG autostart specification
4 # Copyright (C) 2008 Dana Jansens
6 # XDG autostart specification can be found here:
7 # http://standards.freedesktop.org/autostart-spec/
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.
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.
27 from xdg import BaseDirectory
28 from xdg.DesktopEntry import DesktopEntry
29 from xdg.Exceptions import ParsingError
32 print "ERROR:", ME, "requires PyXDG to be installed"
36 def main(argv=sys.argv):
37 if "--help" in argv[1:]:
40 if "--version" in argv[1:]:
44 # get the autostart directories
45 autodirs = BaseDirectory.load_config_paths("autostart")
47 # find all the autostart files
50 for path in glob.glob(os.path.join(dir, '*.desktop')):
52 autofile = AutostartFile(path)
54 print "Invalid .desktop file: " + path
56 if not autofile in files:
57 files.append(autofile)
60 if "--list" in argv[1:]:
65 environments = argv[1:]
66 for autofile in files:
67 if list: autofile.list(environments)
68 else: autofile.run(environments)
71 def __init__(self, path):
73 self.filename = os.path.basename(path)
74 self.dirname = os.path.dirname(path)
75 self.de = DesktopEntry(path)
77 def __eq__(self, other):
78 return self.filename == other.filename
81 return self.path + " : " + self.de.getName()
84 return os.access(path, os.X_OK)
86 def findFile(self, path, search, match_func):
88 if not path: return None
91 if match_func(path): return path
94 for dirname in search.split(os.pathsep):
96 candidate = os.path.join(dirname, path)
97 if (match_func(candidate)): return candidate
99 def alert(self, str, info=False):
105 def showInEnvironment(self, envs, verbose=False):
106 default = not self.de.getOnlyShowIn()
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
115 if not default and not force:
117 for i in self.de.getOnlyShowIn():
120 self.alert("Excluded by: OnlyShowIn (" + s + ")")
121 if default and noshow and not force:
123 for i in self.de.getOnlyShowIn():
126 self.alert("Excluded by: NotShowIn (" + s + ")")
127 return (default and not noshow) or force
129 def shouldRun(self, envs, verbose=False):
130 if not self.de.getExec():
131 if verbose: self.alert("Excluded by: Missing Exec field")
133 if self.de.getHidden():
134 if verbose: self.alert("Excluded by: Hidden")
136 if self.de.getTryExec():
137 if not self.findFile(self.de.getTryExec(), os.getenv("PATH"),
139 if verbose: self.alert("Excluded by: TryExec (" +
140 self.de.getTryExec() + ")")
142 if not self.showInEnvironment(envs, verbose):
146 def list(self, envs):
148 if self.shouldRun(envs):
149 print "[*] " + self.de.getName()
151 print "[ ] " + self.de.getName()
152 self.alert("File: " + self.path, info=True)
153 if self.de.getExec():
154 self.alert("Executes: " + self.de.getExec(), info=True)
155 self.shouldRun(envs, True)
160 if self.de.getPath():
161 os.chdir(self.de.getPath())
162 if self.shouldRun(envs):
163 args = ["/bin/sh", "-c", "exec " + self.de.getExec()]
164 os.spawnv(os.P_NOWAIT, args[0], args);
168 print "Usage:", ME, "[OPTION]... [ENVIRONMENT]..."
170 print "This tool will run xdg autostart .desktop files"
173 print " --list Show a list of the files which would be run"
174 print " Files which would be run are marked with an asterix"
175 print " symbol [*]. For files which would not be run,"
176 print " information is given for why they are excluded"
177 print " --help Show this help and exit"
178 print " --version Show version and copyright information"
180 print "ENVIRONMENT specifies a list of environments for which to run autostart"
181 print "applications. If none are specified, only applications which do not "
182 print "limit themselves to certain environments will be run."
184 print "ENVIRONMENT can be one or more of:"
185 print " GNOME Gnome Desktop"
186 print " KDE KDE Desktop"
187 print " ROX ROX Desktop"
188 print " XFCE XFCE Desktop"
189 print " Old Legacy systems"
194 print "Copyright (c) 2008 Dana Jansens"
197 if __name__ == "__main__":