Renamed to pkg2zero
[deb2zero.git] / support.py
blobaf179cffbf148fa2964b80a8bc04264072352066
1 # Copyright (C) 2008, Thomas Leonard
2 # See the COPYING file for details, or visit http://0install.net.
4 import sys, time
5 from optparse import OptionParser
6 import tempfile, shutil, os
7 from xml.dom import minidom
8 import subprocess
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 = 'pkg2zero'
17 def read_child(cmd):
18 child = subprocess.Popen(cmd, stdout = subprocess.PIPE)
19 output, unused = child.communicate()
20 if child.returncode:
21 print >>sys.stderr, output
22 print >>sys.stderr, "%s: code = %d" % (' '.join(cmd), child.returncode)
23 sys.exit(1)
24 return output
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)
31 if text:
32 new.appendChild(doc.createTextNode(text))
33 parent.appendChild(doc.createTextNode(after))
34 return new
36 class DebMappings:
37 def __init__(self):
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')
41 if mappings_file:
42 self.load(mappings_file)
43 else:
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):
48 if ':' in line:
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]
55 def process(self, s):
56 parts = [x.strip() for x in s.split('(', 1)]
57 if len(parts) == 2:
58 pkg, restrictions = parts
59 else:
60 pkg = parts[0]
61 restrictions = None
63 if pkg not in self.deb_mappings:
64 print "Ignoring dependency on %s; not in mappings file" % pkg
65 return None
67 r = model.InterfaceDependency(self.deb_mappings[pkg])
68 # TODO: version restrictions
69 return r
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))
80 stream.close()
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)