def update(self): # type: () -> Set[ConfigHandler] """ Parse all `.info` files to build list of handlers. :returns: Set of all handlers. """ self._handlers.clear() self._multifiles.clear() self._subfiles.clear() handlers = set() # type: Set[ConfigHandler] for info in directory_files(INFO_DIR): if not info.endswith('.info'): continue for section in parseRfc822(open(info, 'r').read()): handler = self.get_handler(section) if handler: handlers.add(handler) for handler in handlers: for variable in handler.variables: v2h = self._handlers.setdefault(variable, set()) v2h.add(handler) self._save_cache() return handlers
def commit(self, ucr, filelist=list()): """Call handlers to (re-)generate files.""" _filelist = [] for fname in filelist: fname = os.path.expanduser(fname) fname = os.path.expandvars(fname) fname = os.path.abspath(fname) _filelist.append(fname) # find handlers pending_handlers = set() for fname in directory_files(INFO_DIR): if not fname.endswith('.info'): continue for section in parseRfc822(open(fname, 'r').read()): if not section.get('Type'): continue handler = None if _filelist: files = section.get('File') or section.get( 'Multifile') or () for filename in files: if not os.path.isabs(filename): filename = '/%s' % filename if filename in _filelist: handler = self.get_handler(section) break else: continue else: handler = self.get_handler(section) if handler: pending_handlers.add(handler) # call handlers for handler in pending_handlers: self.call_handler(ucr, handler)
def register(self, package, ucr): # type: (str, _UCR) -> Set[ConfigHandler] """ Register new info file for package. :param package: Name of the package to register. :param ucr: UCR instance. :returns: Set of (new) handlers. """ handlers = set() # type: Set[ConfigHandler] fname = os.path.join(INFO_DIR, '%s.info' % package) for section in parseRfc822(open(fname, 'r').read()): handler = self.get_handler(section) if handler: handlers.add(handler) for handler in handlers: if isinstance(handler, ConfigHandlerDiverting): handler.install_divert() values = {} # type: Dict[str, Optional[str]] for variable in handler.variables: v2h = self._handlers.setdefault(variable, set()) v2h.add(handler) values[variable] = ucr[variable] handler((ucr, values)) self._save_cache() return handlers
def _read_module_attributes_from_source_package(module): umc_module_definition_file = os.path.join( module.get('abs_path_to_src_pkg'), 'debian/', '{}.umc-modules'.format(module.get('module_name'))) with open(umc_module_definition_file, 'r') as fd: def_file = fd.read() return dh_ucs.parseRfc822(def_file)[0]
def read_modules(package, core=False): """Read UMC module definition from debian/<package>.umc-modules.""" modules = [] file_umc_module = os.path.join('debian/', package + '.umc-modules') if not os.path.isfile(file_umc_module): return modules with open(file_umc_module, 'rb') as fd: for item in dh_ucs.parseRfc822(fd.read()): # required fields if not core: for required in (MODULE, PYTHON, DEFINITION, JAVASCRIPT): if not item.get(required): raise Error( 'UMC module definition incomplete. key %s missing' % (required, )) # single values item['package'] = package module = UMC_Module(item) if core: if module.module_name != 'umc-core' or not module.xml_categories: raise Error('Module definition does not match core module') modules.append(module) return modules
def commit(self, ucr, filelist=list()): """Call handlers to (re-)generate files.""" _filelist = [] for fname in filelist: fname = os.path.expanduser(fname) fname = os.path.expandvars(fname) fname = os.path.abspath(fname) _filelist.append(fname) # find handlers pending_handlers = set() for fname in directory_files(INFO_DIR): if not fname.endswith('.info'): continue for section in parseRfc822(open(fname, 'r').read()): if not section.get('Type'): continue handler = None if _filelist: files = section.get('File') or \ section.get('Multifile') or () for filename in files: if not os.path.isabs(filename): filename = '/%s' % filename if filename in _filelist: handler = self.get_handler(section) break else: continue else: handler = self.get_handler(section) if handler: pending_handlers.add(handler) # call handlers for handler in pending_handlers: self.call_handler(ucr, handler)
def read_modules( package, core = False ): modules = [] file_umc_module = os.path.join( 'debian/', package + '.umc-modules' ) if not os.path.isfile( file_umc_module ): return modules f_umc_module = open( file_umc_module, 'r' ) for item in dh_ucs.parseRfc822( f_umc_module.read() ): # required fields if not core: for required in ( MODULE, PYTHON, DEFINITION, JAVASCRIPT ): if not required in item or not item[ required ]: raise AttributeError( 'UMC module definition incomplete. key %s missing' % required ) # single values item[ 'package' ] = package module = UMC_Module( item ) if core: if module.module_name != 'umc-core' or not module.xml_categories: raise ValueError( 'Module definition does not match core module' ) modules.append( module ) return modules
def _read_module_attributes_from_source_package(module): # type: (BaseModule) -> dh_umc.UMC_Module umc_module_definition_file = os.path.join(module['abs_path_to_src_pkg'], 'debian/', '{}.umc-modules'.format(module['module_name'])) with open(umc_module_definition_file, 'r') as fd: def_file = fd.read() return dh_ucs.parseRfc822(def_file)[0]
def read_modules(package, core=False): # type: (str, bool) -> List[UMC_Module] """ Read |UMC| module definition from :file:`debian/<package>.umc-modules`. :param package: Name of the package. :param core: Import as core-module, e.g. the ones shipped with |UDM| itself. :returns: List of |UMC| module definitions. """ modules = [] # type: List[UMC_Module] file_umc_module = os.path.join('debian/', package + '.umc-modules') if not os.path.isfile(file_umc_module): return modules with open(file_umc_module, 'rb') as fd: for item in dh_ucs.parseRfc822(fd.read()): # required fields if not core: for required in (MODULE, PYTHON, DEFINITION, JAVASCRIPT): if not item.get(required): raise Error( 'UMC module definition incomplete. key %s missing' % (required, )) # single values item['package'] = package module = UMC_Module(item) if core: if module.module_name != 'umc-core' or not module.xml_categories: raise Error('Module definition does not match core module') modules.append(module) return modules
def unregister(self, package, ucr): # type: (str, _UCR) -> Set[ConfigHandler] """ Un-register info file for package. :param package: Name of the package to un-register. :param ucr: UCR instance. :returns: Set of (then obsolete) handlers. """ obsolete_handlers = set() # type: Set[ConfigHandler] mf_handlers = set( ) # type: Set[ConfigHandlerMultifile] # Remaining Multifile handlers fname = os.path.join(INFO_DIR, '%s.info' % package) for section in parseRfc822(open(fname, 'r').read()): try: typ = section['Type'][0] except LookupError: continue if typ == 'file': handler = self.get_handler(section) elif typ == 'subfile': mfile = section['Multifile'][0] sfile = section['Subfile'][0] try: handler = self._multifiles[mfile] except KeyError: continue # skip SubFile w/o MultiFile name = os.path.join(FILE_DIR, sfile) handler.remove_subfile(name) mf_handlers.add(handler) elif typ == 'multifile': mfile = section['Multifile'][0] handler = self._multifiles[mfile] handler.def_count -= 1 mf_handlers.add(handler) else: continue if not handler: # Bug #17913 print(("Skipping internal error: no handler for %r in %s" % (section, package)), file=sys.stderr) continue if isinstance( handler, ConfigHandlerDiverting) and handler.uninstall_divert(): obsolete_handlers.add(handler) for handler in mf_handlers - obsolete_handlers: self.call_handler(ucr, handler) try: # remove cache file to force rebuild of cache os.unlink(ConfigHandlers.CACHE_FILE) except EnvironmentError: pass return obsolete_handlers
def unregister(self, package, ucr): """Un-register info file for package. Returns set of (then obsolete) handlers.""" obsolete_handlers = set() # Obsolete handlers mf_handlers = set() # Remaining Multifile handlers fname = os.path.join(INFO_DIR, '%s.info' % package) for section in parseRfc822(open(fname, 'r').read()): try: typ = section['Type'][0] except LookupError: continue if typ == 'file': handler = self.get_handler(section) elif typ == 'subfile': mfile = section['Multifile'][0] sfile = section['Subfile'][0] try: handler = self._multifiles[mfile] except KeyError: continue # skip SubFile w/o MultiFile name = os.path.join(FILE_DIR, sfile) handler.remove_subfile(name) mf_handlers.add(handler) elif typ == 'multifile': mfile = section['Multifile'][0] handler = self._multifiles[mfile] handler.def_count -= 1 mf_handlers.add(handler) else: continue if not handler: # Bug #17913 print >> sys.stderr, ("Skipping internal error: no handler " + "for %r in %s" % (section, package)) continue if handler.uninstall_divert(): obsolete_handlers.add(handler) for handler in mf_handlers - obsolete_handlers: self.call_handler(ucr, handler) try: # remove cache file to force rebuild of cache os.unlink(ConfigHandlers.CACHE_FILE) except EnvironmentError: pass return obsolete_handlers
def commit(self, ucr, filelist=list()): # type: (_UCR, Iterable[str]) -> None """ Call handlers to (re-)generate files. :param ucr: UCR instance. :param filelist: List of files to re-generate. By default *all* files will be re-generated and all modules and scripts will we re-invoked! """ _filelist = [] for fname in filelist: fname = os.path.expanduser(fname) fname = os.path.expandvars(fname) fname = os.path.abspath(fname) _filelist.append(fname) # find handlers pending_handlers = set() for fname in directory_files(INFO_DIR): if not fname.endswith('.info'): continue for section in parseRfc822( open(fname, 'r', encoding='utf-8').read()): if not section.get('Type'): continue handler = None if _filelist: files = section.get('File') or section.get( 'Multifile') or () for filename in files: if not os.path.isabs(filename): filename = '/%s' % filename if filename in _filelist: handler = self.get_handler(section) break else: continue else: handler = self.get_handler(section) if handler: pending_handlers.add(handler) # call handlers for handler in pending_handlers: self.call_handler(ucr, handler)
def register(self, package, ucr): """Register new info file for package.""" handlers = set() fname = os.path.join(INFO_DIR, '%s.info' % package) for section in parseRfc822(open(fname, 'r').read()): handler = self.get_handler(section) if handler: handlers.add(handler) for handler in handlers: if isinstance(handler, ConfigHandlerDiverting): handler.install_divert() values = {} for variable in handler.variables: v2h = self._handlers.setdefault(variable, set()) v2h.add(handler) values[variable] = ucr[variable] handler((ucr, values)) self._save_cache() return handlers
def update(self): """Parse all .info files to build list of handlers.""" self._handlers.clear() self._multifiles.clear() self._subfiles.clear() handlers = set() for info in directory_files(INFO_DIR): if not info.endswith('.info'): continue for section in parseRfc822(open(info, 'r').read()): handler = self.get_handler(section) if handler: handlers.add(handler) for handler in handlers: for variable in handler.variables: v2h = self._handlers.setdefault(variable, set()) v2h.add(handler) self._save_cache() return handlers
def register(self, package, ucr): # type: (str, _UCR) -> Set[ConfigHandler] """ Register new info file for package. :param package: Name of the package to register. :param ucr: UCR instance. :returns: Set of (new) handlers. """ handlers = set() # type: Set[ConfigHandler] fname = os.path.join(INFO_DIR, '%s.info' % package) for section in parseRfc822(open(fname, 'r', encoding='utf-8').read()): handler = self.get_handler(section) if handler: handlers.add(handler) for handler in handlers: if isinstance(handler, ConfigHandlerDiverting): handler.install_divert() values = {} # type: Dict[str, Tuple[None, Optional[str]]] for variable in handler.variables: v2h = self._handlers.setdefault(variable, set()) v2h.add(handler) values[variable] = (None, ucr[variable]) try: _re = re.compile(variable) except re.error: continue values.update((key, (None, val)) for key, val in ucr.items() if _re.match(key)) handler((ucr, values)) self._save_cache() return handlers