コード例 #1
0
	def check_py(self, py_files: Iterable[str]) -> None:
		"""Check Python files."""
		for fn in py_files:
			try:
				content = open(fn, 'r').read()
			except EnvironmentError:
				self.addmsg('0008-2', 'failed to open and read file', fn)
				continue

			self.debug('testing %s' % fn)
			for row, col, match in uub.line_regexp(content, RE_TRANSLATION):
				self.addmsg('0008-1', 'substitutes before translation: %s' % match.group(1), fn, row, col)
コード例 #2
0
	def check_po(self, po_files: Iterable[str]) -> None:
		"""Check Portable Object files."""
		for fn in po_files:
			try:
				content = open(fn, 'r').read()
			except EnvironmentError:
				self.addmsg('0008-2', 'failed to open and read file', fn)
				continue

			match = RE_CHARSET.search(content)
			if not match:
				self.addmsg('0008-5', 'cannot find charset definition', fn)
			elif not match.group(1).lower() in ('utf-8'):
				self.addmsg('0008-6', 'invalid charset (%s) defined' % (match.group(1)), fn)

			self.debug('testing %s' % fn)
			for regex, errid, errtxt in [
				(RE_FUZZY, '0008-3', 'contains "fuzzy"'),
				(RE_EMPTY, '0008-4', 'contains empty msgstr')
			]:
				for row, col, match in uub.line_regexp(content, regex):
					self.addmsg(errid, errtxt, fn, row, col)
コード例 #3
0
    def check_scripts(self, path: str) -> None:
        debianpath = join(path, 'debian')
        version = self.get_debian_version(path)
        for script_path in uub.FilteredDirWalkGenerator(debianpath,
                                                        suffixes=self.SCRIPTS):
            package, suffix = self.split_pkg(script_path)

            other_scripts = self.SCRIPTS - {suffix}
            other_actions = set(action for actions in self.ACTIONS.values()
                                for action in actions) - self.ACTIONS[suffix]
            self.debug('script=%s' % suffix)
            self.debug('actions=%s' % ' '.join(sorted(self.ACTIONS[suffix])))
            self.debug('other_script=%s' % ' '.join(sorted(other_scripts)))
            self.debug('other_actions=%s' % ' '.join(sorted(other_actions)))

            with open(script_path, 'r') as script_file:
                content = script_file.read()

            for row, line in enumerate(content.splitlines(), start=1):
                if not line.startswith('#'):
                    break
                for script_name in other_scripts:
                    if script_name in line:
                        self.addmsg('0018-1',
                                    'wrong script name: %r' % (line.strip(), ),
                                    script_path,
                                    row,
                                    line=line)

            for row, line in enumerate(content.splitlines(), start=1):
                if line.startswith('#'):
                    continue
                for match in self.RE_TEST.finditer(line):
                    try:
                        actions = self.parse_test(split(
                            match.group('cond'))) & other_actions
                    except ValueError as ex:
                        self.debug('Failed %s:%d: %s in %s' %
                                   (script_path, row, ex, line))
                        continue
                    if actions:
                        self.addmsg(
                            '0018-3',
                            'Invalid actions "%s" in Debian maintainer script'
                            % (','.join(actions), ),
                            script_path,
                            row,
                            line=line)

                for match in self.RE_COMPARE_VERSIONS.finditer(line):
                    ver_a, op, ver_b = match.groups()
                    for arg in (ver_a, ver_b):
                        if self.RE_ARG2.match(arg):
                            continue
                        if not RE_DEBIAN_PACKAGE_VERSION.match(arg):
                            self.debug("%s:%d: Unknown argument %r" %
                                       (script_path, row, arg))
                            continue

                        ver = Version(arg)
                        self.debug("%s << %s?" % (ver, version))
                        if ver.numeric and version.numeric and ver.numeric[
                                0] < version.numeric[0] - 1:
                            self.addmsg(
                                '0018-5',
                                'Maintainer script contains old upgrade code for %s << %s'
                                % (ver, version), script_path, row,
                                match.start(0), line)

            for row, col, match in uub.line_regexp(content, self.RE_CASE):
                for cases in match.group('cases').split(';;'):
                    cases = cases.lstrip('\t\n\r (')
                    cases = cases.split(')', 1)[0]
                    actions = set(action for case in cases.split('|')
                                  for action in split(case)) & other_actions
                    if actions:
                        self.addmsg(
                            '0018-3',
                            'Invalid actions "%s" in Debian maintainer script'
                            % (','.join(actions), ),
                            script_path,
                            row,
                            col,
                            line=line)
コード例 #4
0
    def check(self, path: str) -> None:
        """ the real check """
        super(UniventionPackageCheck, self).check(path)

        tester = uub.UPCFileTester()
        tester.addTest(
            re.compile(r'\.has_key\s*\('),
            '0009-5',
            'dict.has_key is deprecated in python3 - please use "if key in dict:"',
            cntmax=0)
        tester.addTest(re.compile(r'''\braise\s*(?:'[^']+'|"[^"]+")'''),
                       '0009-6',
                       'raise "text" is deprecated in python3',
                       cntmax=0)
        tester.addTest(re.compile(
            r"""\b(?:if|while)\b.*(?:(?:!=|<>|==)\s*None\b|\bNone\s*(?:!=|<>|==)).*:"""
        ),
                       '0009-7',
                       'fragile comparison with None',
                       cntmax=0)
        tester.addTest(re.compile(
            r'''(?:baseConfig|configRegistry|ucr)(?:\[.+\]|\.get\(.+\)).*\bin\s*
			[\[\(]
			(?:\s*(['"])(?:yes|no|1|0|true|false|on|off|enabled?|disabled?)\1\s*,?\s*){3,}
			[\]\)]''', re.VERBOSE | re.IGNORECASE),
                       '0009-8',
                       'use ucr.is_true() or .is_false()',
                       cntmax=0)
        tester.addTest(
            re.compile(
                r'''\.search\s*\(
			.*?\b
			attr
			\s*=\s*
			(?:(?P<list>\[)|(?P<tuple>\())
			\s*
			(?P<str>["'])
			dn
			(?P=str)
			\s*
			(?(list)\])(?(tuple)\))
			''', re.VERBOSE),
            '0009-11',
            'Use uldap.searchDn() instead of uldap.search(attr=["dn"])',
            cntmax=0)

        for fn in python_files(path):
            tester.open(fn)
            if not tester.raw:
                continue
            msglist = tester.runTests()
            self.msg.extend(msglist)

            match = self.RE_HASHBANG.match(tester.lines[0])
            if match:
                version, space, option, tail = match.groups()
                if not version:
                    self.addmsg(
                        '0009-2',
                        'file does not specify python version in hashbang', fn,
                        1)
                elif version not in {'2.7', '3'}:
                    self.addmsg(
                        '0009-3',
                        'file specifies wrong python version in hashbang', fn,
                        1)
                if space and not option:
                    self.addmsg(
                        '0009-4',
                        'file contains whitespace after python command', fn, 1)
                if tail:
                    self.addmsg('0009-9',
                                'hashbang contains more than one option', fn,
                                1)

            for row, col, m in uub.line_regexp(tester.raw, RE_LENIENT):
                txt = m.group("str")
                if not txt:
                    continue
                if self.RE_STRING.match(txt):
                    continue

                self.addmsg('0009-10',
                            'invalid Python string literal: %s' % (txt, ), fn,
                            row, col)

            try:
                tree = ast.parse(tester.raw, fn)
                visitor = FindAssign(self, fn)
                visitor.visit(tree)
            except Exception as ex:
                self.addmsg('0009-1', 'Parsing failed: %s' % ex, fn)