Allow MakeDist to use a specified sf.net account
[rox-lib.git] / python / rox / filer.py
blob662a2678b0589ddd848d7ce38a8a90dee24dd09f
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 from os import fork, _exit, execvp, waitpid
7 child = fork()
8 if child == 0:
9 # We are the child
10 child = fork()
11 if child == 0:
12 # Grandchild
13 try:
14 execvp(argv[0], argv)
15 except:
16 pass
17 print "Warning: exec('%s') failed!" % argv[0]
18 _exit(1)
19 elif child == -1:
20 print "Error: fork() failed!"
21 _exit(1)
22 elif child == -1:
23 print "Error: fork() failed!"
24 waitpid(child, 0)
26 def spawn_rox(args):
27 """Run rox (either from PATH or through Zero Install) with the
28 given arguments."""
29 import os.path
30 for bindir in os.environ.get('PATH', '').split(':'):
31 path = os.path.join(bindir, 'rox')
32 if os.path.isfile(path):
33 _spawn(('rox',) + args)
34 return
35 if os.path.exists('/uri/0install/rox.sourceforge.net'):
36 _spawn(('/bin/0run', 'rox.sourceforge.net/rox 2002-01-01') + args)
37 else:
38 print "Didn't find rox in PATH, and Zero Install not present. Trying 'rox' anyway..."
39 _spawn(('rox',) + args)
41 def open_dir(dir):
42 "Open 'dir' in a new filer window."
43 spawn_rox(('-d', dir))
45 def examine(file):
46 """'file' may have changed (maybe you just created it, for example). Update
47 any filer views of it."""
48 spawn_rox(('-x', file))
50 def show_file(file):
51 """Open a directory and draw the user's attention to this file. Useful for
52 'Up' toolbar buttons that show where a file is saved."""
53 spawn_rox(('-s', file))