1 # Copyright (C) 2006, Thomas Leonard
2 # See http://0install.net/0compile.html
4 import os
, sys
, __main__
6 from zeroinstall
.injector
import model
, namespaces
, qdom
7 from zeroinstall
import SafeException
9 from support
import BuildEnv
11 def do_setup(args
, get_dir_callback
= None):
12 "setup [ SOURCE-URI [ DIR ] ]"
14 assert get_dir_callback
is None
16 interface
= buildenv
.interface
19 buildenv
.get_selections(prompt
= True)
21 buildenv
= BuildEnv(need_config
= False)
26 create_dir
= os
.path
.basename(interface
)
27 if create_dir
.endswith('.xml'):
28 create_dir
= create_dir
[:-4]
29 if create_dir
.startswith('alias:'):
30 create_dir
= create_dir
.split(':', 1)[1]
31 assert os
.path
.dirname(create_dir
) == ''
32 assert create_dir
!= os
.path
.curdir
34 create_dir
= get_dir_callback(create_dir
)
40 raise __main__
.UsageError()
42 iface_uri
= model
.canonical_iface_uri(args
[0])
43 if os
.path
.isabs(iface_uri
):
44 # Use a relative path if the feed is inside the current directory.
45 # This is useful if the properties file is shared with other users.
46 rel_iface_uri
= os
.path
.relpath(iface_uri
, create_dir
or ".")
47 if not rel_iface_uri
.startswith("."):
48 iface_uri
= rel_iface_uri
50 root
= qdom
.parse(file(iface_uri
))
51 if root
.uri
== namespaces
.XMLNS_IFACE
and root
.name
== 'selections':
52 # Looks like this is a selections file, not an interface.
53 buildenv
.config
.set('compile', 'selections', iface_uri
)
54 iface_uri
= root
.getAttribute('interface')
55 buildenv
.config
.set('compile', 'interface', iface_uri
)
57 if create_dir
and os
.path
.exists(create_dir
):
58 raise SafeException("Directory '%s' already exists." % create_dir
)
59 buildenv
.get_selections()
65 print >>sys
.stderr
, "Failed to create new directory '%s'" % os
.path
.abspath(create_dir
)
68 print "Created directory %s" % create_dir
72 __main__
.commands
.append(do_setup
)