Prefer a local version to rox to a zero-install version.
[rox-lib.git] / python / rox / filer.py
blobee9fac9acb6c1b7e4d1464db8ca1803f215edaad
1 """An easy way to get ROX-Filer to do things."""
3 # Note: do a double-fork in case it's an old version of the filer
4 # and doesn't automatically background itself.
5 def _spawn(argv):
6 """Run a new process and forget about it."""
7 from os import fork, _exit, execvp, waitpid
8 child = fork()
9 if child == 0:
10 # We are the child
11 child = fork()
12 if child == 0:
13 # Grandchild
14 try:
15 execvp(argv[0], argv)
16 except:
17 pass
18 print "Warning: exec('%s') failed!" % argv[0]
19 _exit(1)
20 elif child == -1:
21 print "Error: fork() failed!"
22 _exit(1)
23 elif child == -1:
24 print "Error: fork() failed!"
25 waitpid(child, 0)
27 def spawn_rox(args):
28 """Run rox (either from PATH or through Zero Install) with the
29 given arguments."""
30 import os.path
31 for dir in os.environ.get('PATH', '').split(':'):
32 path = os.path.join(dir, 'rox')
33 if os.path.isfile(path):
34 _spawn(('rox',) + args)
35 return
36 if os.path.exists('/uri/0install/rox.sourceforge.net'):
37 _spawn(('/bin/0run', 'rox.sourceforge.net/rox 2002-01-01') + args)
38 else:
39 print "Didn't find rox in PATH, and Zero Install not present. Trying 'rox' anyway..."
40 _spawn(('rox',) + args)
42 def open_dir(dir):
43 "Open 'dir' in a new filer window."
44 spawn_rox(('-d', dir))
46 def examine(file):
47 """'file' may have changed (maybe you just created it, for example). Update
48 any filer views of it."""
49 spawn_rox(('-x', file))
51 def show_file(file):
52 """Open a directory and draw the user's attention to this file. Useful for
53 'Up' toolbar buttons that show where a file is saved."""
54 spawn_rox(('-s', file))