1 # Copyright (C) 2008, Thomas Leonard
2 # See the COPYING file for details, or visit http://0install.net.
5 from optparse
import OptionParser
6 import tempfile
, shutil
, os
7 from xml
.dom
import minidom
10 from zeroinstall
.injector
import model
, qdom
, distro
11 from zeroinstall
.injector
.namespaces
import XMLNS_IFACE
12 from zeroinstall
.support
import basedir
14 config_site
= '0install.net'
15 config_prog
= 'deb2zero'
18 child
= subprocess
.Popen(cmd
, stdout
= subprocess
.PIPE
)
19 output
, unused
= child
.communicate()
21 print >>sys
.stderr
, output
22 print >>sys
.stderr
, "%s: code = %d" % (' '.join(cmd
), child
.returncode
)
26 def add_node(parent
, element
, text
= None, before
= ' ', after
= '\n'):
27 doc
= parent
.ownerDocument
28 parent
.appendChild(doc
.createTextNode(before
))
29 new
= doc
.createElementNS(XMLNS_IFACE
, element
)
30 parent
.appendChild(new
)
32 new
.appendChild(doc
.createTextNode(text
))
33 parent
.appendChild(doc
.createTextNode(after
))
38 self
.last_base
= 'http://0install.net/2008/3rd-party' # For suggesting new URIs
39 self
.deb_mappings
= {}
40 mappings_file
= basedir
.load_first_config(config_site
, config_prog
, 'deb-mappings')
42 self
.load(mappings_file
)
44 print >>sys
.stderr
, "Mappings file not found; dependencies will not be included."
46 def load(self
, mappings_file
):
47 for line
in file(mappings_file
):
49 deb
, zero
= [x
.strip() for x
in line
.split(':', 1)]
50 if deb
in self
.deb_mappings
:
51 print "Mapping for %s given twice in %s!" % (deb
, mappings_file
)
52 self
.deb_mappings
[deb
] = zero
53 self
.last_base
= zero
.rsplit('/', 1)[0]
56 parts
= [x
.strip() for x
in s
.split('(', 1)]
58 pkg
, restrictions
= parts
63 if pkg
not in self
.deb_mappings
:
64 print "Ignoring dependency on %s; not in mappings file" % pkg
67 r
= model
.InterfaceDependency(self
.deb_mappings
[pkg
])
68 # TODO: version restrictions
71 def lookup(self
, pkg
):
72 return self
.deb_mappings
.get(pkg
, None)
74 def add_mapping(self
, pkg
, uri
):
75 self
.deb_mappings
[pkg
] = uri
76 config_dir
= basedir
.save_config_path(config_site
, config_prog
)
77 mappings_file
= os
.path
.join(config_dir
, 'deb-mappings')
78 stream
= file(mappings_file
, 'a')
79 stream
.write("%s: %s\n" % (pkg
, uri
))
81 print "Wrote name mapping to %s" % mappings_file
83 def get_suggestion(self
, pkg_name
):
84 return "%s/%s.xml" % (self
.last_base
, pkg_name
)