Instead of hard-coding the GNU compile commands, set the command as an
[0compile.git] / setup.py
blob116b2f4eca179b3da64eb392e4e948c9ba49328a
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
12 from support import *
14 def do_setup(args):
15 "setup [ SOURCE-URI [ DIR ] ]"
16 if len(args) == 0:
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)
20 doc = get_env_doc()
21 interface = doc.documentElement.getAttributeNS(None, 'interface')
22 assert interface
23 else:
24 interface = args[0]
25 if len(args) == 1:
26 dir = os.path.basename(interface)
27 if dir.endswith('.xml'):
28 dir = dir[:-4]
29 assert '/' not in dir
30 elif len(args) == 2:
31 dir = args[1]
32 else:
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)
46 policy.freshness = 0
48 policy.recalculate()
49 if not policy.ready:
50 raise Exception('Internal error: required source components not found!')
52 if len(args) > 0:
53 # Create build directory
54 if os.path.exists(dir):
55 raise SafeException("Directory '%s' already exists." % dir)
56 os.mkdir(dir)
57 os.chdir(dir)
59 # Store choices
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]
78 assert impl
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)
91 if binary_main:
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)
110 if b.default:
111 env_elem.setAttributeNS(None, 'default', b.default)
112 dep_elem.appendChild(env_elem)
113 else:
114 raise Exception('Unknown binding type ' + b)
116 doc.writexml(file(ENV_FILE, 'w'), addindent = ' ', newl = '\n')
118 __main__.commands.append(do_setup)