Updated Debian changelog.
[zeroinstall.git] / 0alias
blob0e1c5005cd7ebd4da64bb9517d6e7f3edb4cd78c
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\n\n"
10 "Creates a script in the first directory in $PATH\n"
11 "(%s) to run 'interface'." % first_path)
12 parser.add_option("-V", "--version", help="display version information", action='store_true')
13 parser.disable_interspersed_args()
15 (options, args) = parser.parse_args()
17 if options.version:
18 import zeroinstall
19 print "0alias (zero-install) " + zeroinstall.version
20 print "Copyright (C) 2005 Thomas Leonard"
21 print "This program comes with ABSOLUTELY NO WARRANTY,"
22 print "to the extent permitted by law."
23 print "You may redistribute copies of this program"
24 print "under the terms of the GNU General Public License."
25 print "For more information about these matters, see the file named COPYING."
26 sys.exit(0)
28 if len(args) != 2:
29 parser.print_help()
30 sys.exit(1)
32 alias, interface_uri = args
34 interface = model.Interface(interface_uri)
35 if not reader.update_from_cache(interface):
36 print >>sys.stderr, "Interface '%s' not currently in cache. Fetching..." % interface_uri
37 if os.spawnlp(os.P_WAIT, '0launch', '0launch', '-d', interface_uri):
38 print >>sys.stderr, "0launch failed"
39 sys.exit(1)
40 if not reader.update_from_cache(interface):
41 print >>sys.stderr, "Interface still not in cache. Aborting."
42 sys.exit(1)
44 if not os.access(first_path, os.W_OK):
45 print >>sys.stderr, ("Directory '%s' is not writable.\n" % first_path) + \
46 "0alias uses the first directory in $PATH, which is currently:\n" + \
47 os.environ['PATH']
48 sys.exit(1)
50 script = os.path.join(first_path, alias)
51 if os.path.exists(script):
52 print >>sys.stderr, "File '%s' already exists. Delete it first." % script
53 sys.exit(1)
55 wrapper = file(script, 'w')
56 assert "'" not in interface_uri
57 assert "\\" not in interface_uri
58 print >>wrapper, '''#!/bin/sh
59 if [ "$*" = "--versions" ]; then
60 exec 0launch -gd '%s' "$@"
61 else
62 exec 0launch '%s' "$@"
63 fi''' % (interface_uri, interface_uri)
65 # Make new script executable
66 os.chmod(script, 0111 | os.fstat(wrapper.fileno()).st_mode)
67 wrapper.close()
69 print "Created script '%s'." % script
70 print "To edit policy: %s --versions" % alias
71 print "(note: some shells require you to type 'rehash' now)"