Export chosen implementations to a temporary directory
[0export.git] / 0export
blobe101a0371294e79f4651816913509000e2ed6912
1 #!/usr/bin/env python
2 # Copyright (C) 2008, Thomas Leonard
3 # See the README file for details, or visit http://0install.net.
5 from optparse import OptionParser
6 import os, sys, tempfile, shutil
7 from logging import info, debug
9 zeroinstall_dir = os.environ.get('0IMPORT_ZEROINSTALL', None)
10 if zeroinstall_dir:
11 sys.path.insert(1, zeroinstall_dir)
13 version = '0.1'
15 parser = OptionParser(usage="usage: %prog URI")
17 parser.add_option("-v", "--verbose", help="more verbose output", action='count')
18 parser.add_option("-V", "--version", help="display version information", action='store_true')
20 (options, args) = parser.parse_args()
22 if options.version:
23 print "0export (zero-install) " + version
24 print "Copyright (C) 2008 Thomas Leonard"
25 print "This program comes with ABSOLUTELY NO WARRANTY,"
26 print "to the extent permitted by law."
27 print "You may redistribute copies of this program"
28 print "under the terms of the GNU General Public License."
29 print "For more information about these matters, see the file named COPYING."
30 sys.exit(0)
32 if options.verbose:
33 import logging
34 logger = logging.getLogger()
35 if options.verbose == 1:
36 logger.setLevel(logging.INFO)
37 else:
38 logger.setLevel(logging.DEBUG)
40 if len(args) != 1:
41 parser.print_help()
42 sys.exit(1)
44 from zeroinstall.zerostore import manifest
45 from zeroinstall.injector import policy, handler, model, iface_cache
46 from zeroinstall import SafeException
48 uri = model.canonical_iface_uri(args[0])
50 print "Finding versions of %s..." % uri
52 class NoLocalVersions:
53 def meets_restriction(self, impl):
54 if isinstance(impl, model.ZeroInstallImplementation):
55 i = impl.id
56 return not (i.startswith('/') or i.startswith('.'))
57 # Should package impls be OK?
58 return False
60 no_local = NoLocalVersions()
62 class NoLocalRestrictions(dict):
63 # This restriction applies to all interfaces, so ignore key
64 def get(self, key, default):
65 return [no_local]
67 try:
68 h = handler.Handler()
69 p = policy.Policy(uri, h)
71 # Don't let us choose local devel versions
72 p.solver.extra_restrictions = NoLocalRestrictions()
74 solved = p.solve_with_downloads()
75 h.wait_for_blocker(solved)
77 if not p.ready:
78 raise SafeException("Failed to select a set of versions!")
80 for iface, impl in p.solver.selections.items():
81 print " %s : %s" % (iface.get_name(), impl.get_version())
83 downloads = p.download_uncached_implementations()
84 if downloads:
85 print "Downloading implementations..."
86 h.wait_for_blocker(downloads)
88 tmp = tempfile.mkdtemp(prefix = '0export-')
89 try:
90 implementations = os.path.join(tmp, 'implementations')
91 for impl in p.solver.selections.values():
92 src = p.get_implementation_path(impl)
93 dst = os.path.join(implementations, impl.id)
94 shutil.copytree(src, dst)
95 manifest.verify(dst, impl.id)
96 for root, dirs, files in os.walk(dst):
97 os.chmod(root, 0755)
98 os.unlink(os.path.join(dst, '.manifest'))
99 finally:
100 shutil.rmtree(tmp)
101 except SafeException, ex:
102 print >>sys.stderr, str(ex)
103 sys.exit(1)