1 # Copyright (C) 2006, Thomas Leonard
2 # See the README file for details, or visit http://0install.net.
4 import sys
, os
, __main__
5 from logging
import info
6 from xml
.dom
import minidom
, XMLNS_NAMESPACE
8 from zeroinstall
.injector
import model
9 from zeroinstall
.injector
.policy
import Policy
10 from zeroinstall
import SafeException
15 "setup [ SOURCE-URI [ DIR ] ]"
17 if not os
.path
.isfile(ENV_FILE
):
18 raise SafeException("Run 0compile from a directory containing a '%s' file"
19 "specify a source URI as an argument." % ENV_FILE
)
21 interface
= doc
.documentElement
.getAttributeNS(None, 'interface')
26 dir = os
.path
.basename(interface
)
27 if dir.endswith('.xml'):
33 raise __main__
.UsageError()
35 interface
, args
= model
.canonical_iface_uri(args
[0]), args
[1:]
37 if os
.path
.exists(dir):
38 raise SafeException("Directory '%s' already exists." % dir)
40 # Prompt user to choose versions
41 if os
.spawnvp(os
.P_WAIT
, '0launch', ['0launch', '--gui', '--source', '--download-only', interface
]):
42 raise SafeException('Failed to select source files.')
44 # Get the chosen versions
45 policy
= Policy(interface
, src
= True)
50 raise Exception('Internal error: required source components not found!')
53 # Create build directory
54 if os
.path
.exists(dir):
55 raise SafeException("Directory '%s' already exists." % dir)
60 save_environment(policy
)
62 def save_environment(policy
):
63 impl
= minidom
.getDOMImplementation()
65 doc
= impl
.createDocument(XMLNS_0COMPILE
, "build-environment", None)
67 root
= doc
.documentElement
68 root
.setAttributeNS(XMLNS_NAMESPACE
, 'xmlns', XMLNS_0COMPILE
)
70 root
.setAttributeNS(None, 'interface', policy
.root
)
72 for needed_iface
in policy
.implementation
:
73 iface_elem
= doc
.createElementNS(XMLNS_0COMPILE
, 'interface')
74 iface_elem
.setAttributeNS(None, 'uri', needed_iface
.uri
)
75 root
.appendChild(iface_elem
)
77 impl
= policy
.implementation
[needed_iface
]
80 impl_elem
= doc
.createElementNS(XMLNS_0COMPILE
, 'implementation')
81 impl_elem
.setAttributeNS(None, 'id', impl
.id)
82 impl_elem
.setAttributeNS(None, 'version', impl
.get_version())
83 if impl
.interface
is not needed_iface
:
84 impl_elem
.setAttributeNS(None, 'from-feed', impl
.interface
.uri
)
86 if needed_iface
.uri
== policy
.root
:
87 command
= impl
.metadata
.get(XMLNS_0COMPILE
+ ' command', None)
88 if not command
: raise SafeException("Missing 'compile:command' attribute on <implementation>.")
89 impl_elem
.setAttributeNS(XMLNS_0COMPILE
, 'command', command
)
90 binary_main
= impl
.metadata
.get(XMLNS_0COMPILE
+ ' binary-main', None)
92 impl_elem
.setAttributeNS(XMLNS_0COMPILE
, 'binary-main', binary_main
)
94 iface_elem
.appendChild(impl_elem
)
96 for dep
in impl
.dependencies
.values():
98 dep_iface
= policy
.get_interface(dep
.interface
)
99 dep_impl
= policy
.get_implementation(dep_iface
)
101 dep_elem
= doc
.createElementNS(XMLNS_0COMPILE
, 'requires')
102 dep_elem
.setAttributeNS(None, 'interface', dep
.interface
)
103 impl_elem
.appendChild(dep_elem
)
105 for b
in dep
.bindings
:
106 if isinstance(b
, model
.EnvironmentBinding
):
107 env_elem
= doc
.createElementNS(XMLNS_0COMPILE
, 'environment')
108 env_elem
.setAttributeNS(None, 'name', b
.name
)
109 env_elem
.setAttributeNS(None, 'insert', b
.insert
)
111 env_elem
.setAttributeNS(None, 'default', b
.default
)
112 dep_elem
.appendChild(env_elem
)
114 raise Exception('Unknown binding type ' + b
)
116 doc
.writexml(file(ENV_FILE
, 'w'), addindent
= ' ', newl
= '\n')
118 __main__
.commands
.append(do_setup
)