Package zeroinstall :: Package injector :: Module requirements
[frames] | no frames]

Source Code for Module zeroinstall.injector.requirements

  1  """ 
  2  Holds information about what the user asked for (which program, version constraints, etc). 
  3  """ 
  4   
  5  # Copyright (C) 2012, Thomas Leonard 
  6  # See the README file for details, or visit http://0install.net. 
  7   
  8  from zeroinstall import SafeException 
  9   
10 -class Requirements(object):
11 """ 12 Holds information about what the user asked for (which program, version constraints, etc). 13 """ 14 15 # Note: apps.py serialises our __slots__ 16 __slots__ = [ 17 'interface_uri', 18 'command', 19 'source', 20 'extra_restrictions', # {str: str} (iface -> range) 21 'os', 'cpu', 22 'message', 23 ] 24
25 - def __init__(self, interface_uri):
26 """@type interface_uri: str""" 27 self.interface_uri = interface_uri 28 self.command = 'run' 29 self.source = False 30 self.os = self.cpu = None 31 self.message = None 32 self.extra_restrictions = {}
33
34 - def parse_options(self, options):
35 self.extra_restrictions = self._handle_restrictions(options) 36 37 for uri, expr in self.extra_restrictions.items(): 38 if not expr: 39 raise SafeException("Missing version expression for {uri}".format(uri = uri)) 40 41 self.source = bool(options.source) 42 self.message = options.message 43 44 self.cpu = options.cpu 45 self.os = options.os 46 47 # (None becomes 'run', while '' becomes None) 48 if options.command is None: 49 if self.source: 50 self.command = 'compile' 51 else: 52 self.command = 'run' 53 else: 54 self.command = options.command or None
55
56 - def parse_update_options(self, options):
57 """Update the settings based on the options (used for "0install update APP"). 58 @return: whether any settings were changed 59 @rtype: bool 60 @since: 1.9""" 61 restriction_updates = self._handle_restrictions(options) 62 63 changed = False 64 for key in ['message', 'cpu', 'os', 'command']: 65 value = getattr(options, key) 66 if value is not None: 67 changed = changed or value != getattr(self, key) 68 setattr(self, key, value) 69 70 for uri, expr in restriction_updates.items(): 71 old = self.extra_restrictions.get(uri, None) 72 changed = expr != old 73 if expr: 74 self.extra_restrictions[uri] = expr 75 elif uri in self.extra_restrictions: 76 del self.extra_restrictions[uri] 77 78 if options.source and not self.source: 79 # (partly because it doesn't make much sense, and partly because you 80 # can't undo it, as there's no --not-source option) 81 raise SafeException("Can't update from binary to source type!") 82 return changed
83
84 - def get_as_options(self):
85 """@rtype: [str]""" 86 gui_args = [] 87 if self.extra_restrictions: 88 # Currently, we only handle the case of restrictions on the root 89 for uri, r in self.extra_restrictions.items(): 90 gui_args.insert(0, r) 91 gui_args.insert(0, uri) 92 gui_args.insert(0, '--version-for') 93 if self.source: 94 gui_args.insert(0, '--source') 95 if self.message: 96 gui_args.insert(0, self.message) 97 gui_args.insert(0, '--message') 98 if self.cpu: 99 gui_args.insert(0, self.cpu) 100 gui_args.insert(0, '--cpu') 101 if self.os: 102 gui_args.insert(0, self.os) 103 gui_args.insert(0, '--os') 104 gui_args.append('--command') 105 gui_args.append(self.command or '') 106 107 return gui_args
108
109 - def get_extra_restrictions(self, iface_cache):
110 """Create list of L{model.Restriction}s for each interface, based on these requirements. 111 @type iface_cache: L{zeroinstall.injector.iface_cache.IfaceCache} 112 @rtype: {L{model.Interface}: [L{model.Restriction}]}""" 113 from zeroinstall.injector import model 114 115 return dict((iface_cache.get_interface(uri), [model.VersionExpressionRestriction(expr)]) 116 for uri, expr in self.extra_restrictions.items())
117
118 - def _handle_restrictions(self, options):
119 """Gets the list of restrictions specified by the user. 120 Handles --before, --not-before, --version and --version-for options. 121 If a mapping isn't present, then the user didn't specify a restriction. 122 If an entry maps to None, the user wishes to remove the restriction.""" 123 version = options.version 124 125 interface_uri = self.interface_uri 126 127 # Convert old --before and --not_before to new --version-for 128 if options.before is not None or options.not_before is not None: 129 if version is not None: 130 raise SafeException("Can't use --before or --not-before with --version") 131 if options.before or options.not_before: 132 version = (options.not_before or '') + '..' 133 if options.before: 134 version += '!' + options.before 135 else: 136 version = '' # Reset 137 138 restrictions = dict((uri, (expr or None)) for (uri, expr) in (options.version_for or [])) 139 140 # Convert the --version short-cut to --version-for 141 if version is not None: 142 if interface_uri in restrictions: 143 raise SafeException("Can't use --version and --version-for to set {uri}".format(uri = interface_uri)) 144 restrictions[interface_uri] = version or None 145 146 return restrictions
147