# -*- coding: utf-8 -*- try: import univention.ucslint.base as uub except ImportError: import ucslint.base as uub import os import re class UniventionPackageCheck(uub.UniventionPackageCheckDebian): def __init__(self): super(UniventionPackageCheck, self).__init__() self.name = '9017-Use-internal-libraries' def getMsgIds(self): return { '9017-1': [ uub.RESULT_WARN, 'cannot open file' ], '9017-2': [ uub.RESULT_WARN, 'found well-known LDAP object but no custom_groupname' ], } def postinit(self, path): """ checks to be run before real check or to create precalculated data for several runs. Only called once! """ pass def check(self, path): """ the real check """ super(UniventionPackageCheck, self).check(path) for fn in uub.FilteredDirWalkGenerator(path): # skip all files larger than 30kiB try: extension = fn.rsplit('.',1)[-1] # skip images, python binaries if extension in ('svg', 'png', 'jpg', 'gif', 'pyc'): continue # check only small files but all js/py/sh files if os.stat(fn).st_size > 50*1024 and extension not in ('js', 'py', 'sh', ): continue content = open(fn, 'r').read() except (IOError, OSError): self.addmsg( '9017-1', 'failed to open and read file', fn ) continue for txt in ['Domain Users', 'Domain Admins', 'Administrator', 'Windows Hosts']: regex = '(?!custom_groupname)[^\n]+%s' % (txt,) for (line, pos) in self._regexSearchString(content, regex, re.I | re.M): self.addmsg('9017-2', 'line contains "%s" without custom_groupname() call' % txt, fn, line, pos) def _regexSearchString(self, content, regex, regex_flags=0): pattern = re.compile(regex, regex_flags) result = [] pos = 0 while True: match = pattern.search(content, pos) if not match: break else: line = content.count('\n', 0, match.start()) + 1 linepos = match.start() - content.rfind('\n', 0, match.start()) pos = match.end() - 1 result.append( (line, linepos) ) return result def _searchString(self, content, txt): result = [] pos = 0 while True: fpos = content.lower().find( txt.lower(), pos ) if fpos < 0: break else: line = content.count('\n', 0, fpos) + 1 linepos = fpos - content.rfind('\n', 0, fpos) pos = fpos + len(txt) - 1 result.append( (line, linepos) ) return result