-
Notifications
You must be signed in to change notification settings - Fork 3
/
create.py
80 lines (65 loc) · 2.4 KB
/
create.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Copyright (C) 2013, Thomas Leonard
# See the README file for details, or visit http://0install.net.
from __future__ import print_function
import os
from xml.dom import minidom
from zeroinstall.injector import namespaces
from __main__ import die
mydir = os.path.dirname(__file__)
def get_choice(msg, options):
print()
print(msg)
print()
for i, label in options:
print("{i}) {label}".format(i = i, label = label))
while True:
try:
n = int(input("\n> "))
except ValueError:
print("Not an integer")
continue
for i, label in options:
if i == n:
return n
print("Invalid choice")
def create(options):
template = options.template
if template.endswith('.xml.template'):
remote = True
elif template.endswith('.xml'):
remote = False
else:
die("'{template}' does not end with .xml.template or .xml".format(template = template))
print("'{template}' does not exist; creating new template.".format(template = template))
if not remote:
print("\nAs it ends with .xml, not .xml.template, I assume you want a feed for\n"
"a local project (e.g. a Git checkout). If you want a template for\n"
"publishing existing releases, use {template}.template instead.".format(
template = template))
if options.from_feed is None:
doc = minidom.parse(os.path.join(mydir, "example.xml"))
impls = doc.getElementsByTagNameNS(namespaces.XMLNS_IFACE, 'implementation')
impls[0].parentNode.removeChild(impls[0] if remote else impls[1])
choice = get_choice("Does your program need to be compiled before it can be used?", [
(1, "Generate a source template (e.g. for compiling C source code)"),
(2, "Generate a binary template (e.g. for a pre-compiled binary or script)"),
])
commands = doc.getElementsByTagNameNS(namespaces.XMLNS_IFACE, 'command')
commands[0].parentNode.removeChild(commands[choice - 1])
impl, = doc.getElementsByTagNameNS(namespaces.XMLNS_IFACE, 'implementation')
if choice == 1:
impl.setAttribute('arch', '*-src')
else:
impl.setAttribute('arch', '*-*')
else:
if remote:
import infer
doc = infer.from_feed(options.from_feed)
else:
die("--from-feed can only be used to create new templates, not local feeds".format(template=template))
assert not os.path.exists(template), template
print("\nWriting", template)
with open(template, 'wt') as stream:
stream.write('<?xml version="1.0"?>\n')
doc.documentElement.writexml(stream)
stream.write('\n')