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