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()
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."
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"
40 if not reader
.update_from_cache(interface
):
41 print >>sys
.stderr
, "Interface still not in cache. Aborting."
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" + \
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
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' "$@"
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
)
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)"