Ensure that the three standard file descriptors (stdin, stdout and stderr)
[zeroinstall.git] / 0alias
blobd5ed46f2271578fcb753268e970d50cb12052970
1 #!/usr/bin/env python
2 import os, sys
3 from optparse import OptionParser
5 from zeroinstall.injector import reader, model
7 first_path = os.environ['PATH'].split(':')[0]
9 parser = OptionParser(usage="usage: %%prog [options] alias interface [command]\n\n"
10 "Creates a script in the first directory in $PATH\n"
11 "(%s) to run 'interface'. For interfaces providing more than "
12 "one command, the desired command may also be given." % first_path)
13 parser.add_option("-V", "--version", help="display version information", action='store_true')
14 parser.disable_interspersed_args()
16 (options, args) = parser.parse_args()
18 if options.version:
19 import zeroinstall
20 print "0alias (zero-install) " + zeroinstall.version
21 print "Copyright (C) 2005 Thomas Leonard"
22 print "This program comes with ABSOLUTELY NO WARRANTY,"
23 print "to the extent permitted by law."
24 print "You may redistribute copies of this program"
25 print "under the terms of the GNU General Public License."
26 print "For more information about these matters, see the file named COPYING."
27 sys.exit(0)
29 def export(name, value):
30 """Try to guess the command to set an environment variable."""
31 shell = os.environ.get('SHELL', '?')
32 if 'csh' in shell:
33 return "setenv %s %s" % (name, value)
34 return "export %s=%s" % (name, value)
36 if len(args) < 2 or len(args) > 3:
37 parser.print_help()
38 sys.exit(1)
40 alias, interface_uri = args[:2]
41 if len(args) == 3:
42 main = "--main '%s'" % args[2]
43 else:
44 main = ""
46 try:
47 interface_uri = model.canonical_iface_uri(interface_uri)
49 interface = model.Interface(interface_uri)
50 if not reader.update_from_cache(interface):
51 print >>sys.stderr, "Interface '%s' not currently in cache. Fetching..." % interface_uri
52 if os.spawnlp(os.P_WAIT, '0launch', '0launch', '-d', interface_uri):
53 raise model.SafeException("0launch failed")
54 if not reader.update_from_cache(interface):
55 raise model.SafeException("Interface still not in cache. Aborting.")
56 if not os.access(first_path, os.W_OK):
57 raise model.SafeException("Directory '%s' is not writable.\n"
58 "0alias uses the first directory in $PATH, which is currently:\n\n%s\n\n"
59 "To create a directory for your scripts, use these commands:\n"
60 "$ mkdir ~/bin\n"
61 "$ %s" % (first_path, os.environ['PATH'], export('PATH', '$HOME/bin:$PATH')))
63 script = os.path.join(first_path, alias)
64 if os.path.exists(script):
65 raise model.SafeException("File '%s' already exists. Delete it first." % script)
66 sys.exit(1)
67 except model.SafeException, ex:
68 print >>sys.stderr, ex
69 sys.exit(1)
71 wrapper = file(script, 'w')
72 assert "'" not in interface_uri
73 assert "\\" not in interface_uri
74 print >>wrapper, '''#!/bin/sh
75 if [ "$*" = "--versions" ]; then
76 exec 0launch -gd '%s' "$@"
77 else
78 exec 0launch %s '%s' "$@"
79 fi''' % (interface_uri, main, interface_uri)
81 # Make new script executable
82 os.chmod(script, 0111 | os.fstat(wrapper.fileno()).st_mode)
83 wrapper.close()
85 print "Created script '%s'." % script
86 print "To edit policy: %s --versions" % alias
87 print "(note: some shells require you to type 'rehash' now)"