def add_source(self, source): """The source may have any type, it can be another rule, string, function or an outside object. However, it cannot be None. Returns source, once it has been added. """ if not source: raise TypeError() # The source is an object or another rule. if (isinstance(source, object) and not typecheck.is_function(source) and \ not typecheck.is_string(source)) or \ (typecheck.is_string(source) and source.startswith(":")): # First of all check for attribute "__build__" and call it to resolve # dependencies. if hasattr(source, "__build__"): self._add_callable_source(source.__build__) elif callable(source): self._add_callable_source(source.__call__) rule = parse_address(source) if rule: if source not in self.get_dependencies(): self.add_dependency(rule) source = rule #else: # logger.warning("Cannot find appropriate rule for the source: %s" % # source) # Source is a function or something that we should call. The result is # assumed to be an extra sources that will be added by add_sources(). elif callable(source): self._add_callable_source(source) return elif typecheck.is_string(source): if not os.path.isabs(source): source = os.path.abspath(os.path.join(self._work_dir, source)) # FIXME: can be add not string based sources? if typecheck.is_string(source) or isinstance(source, Rule): self._sources.append(source) return source
def __init__(self, target=None, name=None, execute=None, deps=[]): """Each rule has a name that identifies it within existed context. In case name wasn't provided, it becomes a target and vice-versa. """ if not target and not name: raise Exception("target and/or name have to be provided") if name and not typecheck.is_string(name): raise TypeError("name has to be a string: %s,%s" % (name, target)) self._target = target if not name: if typecheck.is_string(target): name = target elif typecheck.is_class(target) or typecheck.is_function(target): name = target.__name__ else: name = "i%d" % id(target) self._name = name self._description = None self._address = None self._address = self.locate() self._dependencies = [] # TODO: deprecated self._properties = dict() self._build_dir = None # TODO: test on windows and other systems! self.set_build_dir(path_utils.join( bb.config.user_settings.get("b3", "builddir"), os.getcwd()[1:])) if hasattr(self.__class__, "properties"): self.add_properties(self.__class__.properties) register_rule(self) if execute: if not callable(execute): raise TypeError() self.execute = types.MethodType(execute, self) # Defer dependency resolution after parsing the current BUILD file to # allow for forward references self._post_init(self._finalize_deps, deps)
def set_idle_action(self, action): if not typecheck.is_function(action): raise TypeError("action has to be a function: %s" % action) self._idle_action = action