2 from xml
.dom
import minidom
, XMLNS_NAMESPACE
3 from zeroinstall
.injector
.namespaces
import XMLNS_IFACE
4 from zeroinstall
.injector
import model
, reader
6 _local_template_start
= """<?xml version="1.0" ?>
7 <?xml-stylesheet type='text/xsl'
8 href='http://0install.net/2006/stylesheets/interface.xsl'?>
11 _template
= """<?xml version="1.0" ?>
12 <?xml-stylesheet type='text/xsl'
13 href='http://0install.net/2006/stylesheets/interface.xsl'?>
15 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
17 <summary>cures all ills</summary>
19 A longer, multi-line description of the behaviour of the
20 program goes here. State clearly what the program is for
21 (clearly enough that people who don't want it will
24 Use a blank line to separate paragraphs.
27 <!-- Optionally, uncomment this to specify the program's homepage: -->
28 <!-- <homepage>http://site/prog</homepage> -->
30 <!-- Optionally, uncomment this to specify an icon: -->
31 <!-- <icon href='http://site/icon.png' type='image/png'/> -->
33 <!-- Optionally, uncomment this to give the address of
34 the signed master interface: -->
35 <!-- <feed-for interface='http://site/interface'/> -->
37 <!-- Set 'main' to the relative path of your default
38 executable within the implementation's directory.
39 E.g.: "myprog" or "bin/myprog" -->
41 <!-- List any libraries your program needs here -->
43 <requires interface="http://site/library">
44 <environment insert="python" name="PYTHONPATH"/>
48 <!-- List all implementations here.
49 For local interfaces, '.' is a relative path from this
50 interface file to the directory containing the program.
51 Usually, you can just leave it as '.'.
53 <implementation id="." version="0.1" released='Snapshot'/>
59 assert not os
.path
.exists(f
)
60 name
= os
.path
.basename(f
)
61 return _template
% (name
.split('.', 1)[0])
63 def create_from_local(local
):
64 iface
= model
.Interface(os
.path
.abspath(local
))
65 reader
.update(iface
, local
, local
= True)
66 doc
= minidom
.parseString(_local_template_start
+
67 '<interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface"/>')
68 root
= doc
.documentElement
69 def element(uri
, localName
, data
):
70 root
.appendChild(doc
.createTextNode('\n '))
71 element
= doc
.createElementNS(uri
, localName
)
72 element
.appendChild(doc
.createTextNode(data
))
74 root
.appendChild(element(XMLNS_IFACE
, 'name', iface
.name
))
75 root
.appendChild(element(XMLNS_IFACE
, 'summary', iface
.summary
))
76 root
.appendChild(element(XMLNS_IFACE
, 'description', iface
.description
+ '\n '))
78 if not iface
.feed_for
:
79 raise Exception("No <feed-for> in '%s'; can't use it as a local feed." % local
)
80 if len(iface
.feed_for
) != 1:
81 raise Exception("Multiple <feed-for> elements. Not supported, sorry!")
82 uri
= iface
.feed_for
.keys()[0]
84 root
.setAttribute('uri', uri
)
86 local_doc
= minidom
.parse(local
)
87 for icon
in local_doc
.getElementsByTagNameNS(XMLNS_IFACE
, 'icon'):
88 if icon
.parentNode
is local_doc
.documentElement
:
89 root
.appendChild(doc
.createTextNode('\n '))
90 root
.appendChild(doc
.importNode(icon
, True))
92 root
.appendChild(doc
.createTextNode('\n'))
94 # minidom's writer loses the newline after the PI
95 return _local_template_start
+ root
.toxml()