Ejemplo n.º 1
0
    def _is_strict(self, target):
        # The strict value is read from the following, in order:
        # 1. the option --[no-]strict, but only if explicitly set.
        # 2. java_thrift_library target in BUILD file, thrift_linter_strict = False,
        # 3. options, --[no-]strict-default
        task_options = self.get_options()
        subsystem_options = ScroogeLinter.global_instance().options

        task_strict_configured = not task_options.is_default('strict')
        subsystem_strict_configured = not subsystem_options.is_default(
            'strict')
        if task_strict_configured and subsystem_strict_configured:
            self.raise_conflicting_option("strict")
        if subsystem_strict_configured:
            return self._to_bool(subsystem_options.strict)
        if task_strict_configured:
            return self._to_bool(task_options.strict)

        if target.thrift_linter_strict is not None:
            return self._to_bool(target.thrift_linter_strict)

        task_strict_default_configured = not task_options.is_default(
            'strict_default')
        subsystem_strict_default_configured = not subsystem_options.is_default(
            'strict_default')
        if task_strict_default_configured and subsystem_strict_default_configured:
            self.raise_conflicting_option("strict_default")
        if task_strict_configured:
            return self._to_bool(task_options.strict_default)
        return self._to_bool(subsystem_options.strict_default)
Ejemplo n.º 2
0
    def execute(self):
        thrift_targets = self.get_targets(self._is_thrift)

        task_worker_count_configured = not self.get_options().is_default(
            "worker_count")
        subsystem_worker_count_configured = not ScroogeLinter.global_instance(
        ).options.is_default("worker_count")
        if task_worker_count_configured and subsystem_worker_count_configured:
            self.raise_conflicting_option("worker_count")
        worker_count = (self.get_options().worker_count
                        if task_worker_count_configured else
                        ScroogeLinter.global_instance().options.worker_count)

        with self.invalidated(thrift_targets) as invalidation_check:
            if not invalidation_check.invalid_vts:
                return

            with self.context.new_workunit(
                    'parallel-thrift-linter') as workunit:
                worker_pool = WorkerPool(workunit.parent,
                                         self.context.run_tracker,
                                         worker_count, workunit.name)

                scrooge_linter_classpath = self.tool_classpath(
                    'scrooge-linter')
                results = []
                errors = []
                for vt in invalidation_check.invalid_vts:
                    r = worker_pool.submit_async_work(
                        Work(self._lint,
                             [(vt.target, scrooge_linter_classpath)]))
                    results.append((r, vt))
                for r, vt in results:
                    r.wait()
                    # MapResult will raise _value in `get` if the run is not successful.
                    try:
                        r.get()
                    except ThriftLintError as e:
                        errors.append(str(e))
                    else:
                        vt.update()

                if errors:
                    raise TaskError('\n'.join(errors))
Ejemplo n.º 3
0
 def _resolve_conflicting_options(self, *, old_option: str,
                                  new_option: str):
     return resolve_conflicting_options(
         old_option=old_option,
         new_option=new_option,
         old_scope="lint-thrift",
         new_scope="scrooge-linter",
         old_container=self.get_options(),
         new_container=ScroogeLinter.global_instance().options,
     )
Ejemplo n.º 4
0
    def _is_strict(self, target):
        # The strict value is read from the following, in order:
        # 1. the option --[no-]strict, but only if explicitly set.
        # 2. java_thrift_library target in BUILD file, thrift_linter_strict = False,
        # 3. options, --[no-]strict-default
        subsystem_options = ScroogeLinter.global_instance().options

        if not subsystem_options.is_default("strict"):
            return self._to_bool(subsystem_options.strict)

        if target.thrift_linter_strict is not None:
            return self._to_bool(target.thrift_linter_strict)

        return self._to_bool(subsystem_options.strict_default)
Ejemplo n.º 5
0
    def _is_strict(self, target):
        # The strict value is read from the following, in order:
        # 1. the option --[no-]strict, but only if explicitly set.
        # 2. java_thrift_library target in BUILD file, thrift_linter_strict = False,
        # 3. options, --[no-]strict-default
        task_options = self.get_options()
        subsystem_options = ScroogeLinter.global_instance().options

        # NB: _resolve_conflicting_options() used to assert that both options aren't configured.
        self._resolve_conflicting_options(old_option="strict",
                                          new_option="strict")
        if not subsystem_options.is_default("strict"):
            return self._to_bool(subsystem_options.strict)
        if not task_options.is_default("strict"):
            return self._to_bool(task_options.strict)

        if target.thrift_linter_strict is not None:
            return self._to_bool(target.thrift_linter_strict)

        return self._to_bool(subsystem_options.strict_default)
Ejemplo n.º 6
0
    def _lint(self, target, classpath):
        self.context.log.debug(f'Linting {target.address.spec}')

        config_args = []

        config_args.extend(self.get_options().linter_args)
        config_args.extend(ScroogeLinter.global_instance().options.args)

        if self._is_strict(target):
            config_args.append('--fatal-warnings')
        else:
            # Make sure errors like missing-namespace are at least printed.
            config_args.append('--warnings')

        if self.get_options().ignore_errors:
            config_args.append('--ignore-errors')

        paths = list(target.sources_relative_to_buildroot())
        include_paths = calculate_include_paths([target], self._is_thrift)
        if target.include_paths:
            include_paths |= set(target.include_paths)
        for p in include_paths:
            config_args.extend(['--include-path', p])

        args = config_args + paths

        # If runjava returns non-zero, this marks the workunit as a
        # FAILURE, and there is no way to wrap this here.
        returncode = self.runjava(
            classpath=classpath,
            main='com.twitter.scrooge.linter.Main',
            args=args,
            jvm_options=self.get_options().jvm_options,
            # to let stdout/err through, but don't print tool's label.
            workunit_labels=[
                WorkUnitLabel.COMPILER, WorkUnitLabel.SUPPRESS_LABEL
            ])

        if returncode != 0:
            raise ThriftLintError(
                f'Lint errors in target {target.address.spec} for {paths}.')
Ejemplo n.º 7
0
    def execute(self):
        thrift_targets = self.get_targets(self._is_thrift)
        with self.invalidated(thrift_targets) as invalidation_check:
            if not invalidation_check.invalid_vts:
                return

            with self.context.new_workunit(
                    "parallel-thrift-linter") as workunit:
                worker_pool = WorkerPool(
                    workunit.parent,
                    self.context.run_tracker,
                    ScroogeLinter.global_instance().options.worker_count,
                    workunit.name,
                )

                scrooge_linter_classpath = self.tool_classpath(
                    "scrooge-linter")
                results = []
                errors = []
                for vt in invalidation_check.invalid_vts:
                    r = worker_pool.submit_async_work(
                        Work(self._lint,
                             [(vt.target, scrooge_linter_classpath)]))
                    results.append((r, vt))
                for r, vt in results:
                    r.wait()
                    # MapResult will raise _value in `get` if the run is not successful.
                    try:
                        r.get()
                    except ThriftLintError as e:
                        errors.append(str(e))
                    else:
                        vt.update()

                if errors:
                    raise TaskError("\n".join(errors))
Ejemplo n.º 8
0
 def skip_execution(self):
     return ScroogeLinter.global_instance().options.skip
Ejemplo n.º 9
0
 def skip_execution(self):
     return self.resolve_conflicting_skip_options(
         old_scope="thrift-linter",
         new_scope="scrooge-linter",
         subsystem=ScroogeLinter.global_instance(),
     )
Ejemplo n.º 10
0
 def skip_execution(self):
     return self.get_options().skip or ScroogeLinter.global_instance(
     ).options.skip