Files
firebase-ios-sdk/scripts/lib/checker.py
T
Gil 3e3bcb41f7 Rewrite lint.sh as check_lint.py (#3161)
* Rewrite lint.sh as check_lint.py

This makes several beneficial changes:

  * Runs in parallel: lint all now takes 5.5 seconds, down from 25.
  * Supports linting specific files, passed on the command-line.
  * Defaults to linting files changed since master, pass --all to lint
    everything.
  * Infers header language based on related files.
  * Lays the groundwork for linting more than just C++.
  * Adds support for linting python.

* Fix lint errors

* Use check_lint.py in check.sh

* Remove lint.sh

* Python lint configuration

* Enable python linting in travis
2019-06-14 09:22:55 -07:00

121 lines
3.0 KiB
Python

# Copyright 2019 Google
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import division
import math
import multiprocessing.pool
import sys
import threading
# Python 3 renamed Queue to queue
try:
import queue
except ImportError:
import Queue as queue
_TASKS = multiprocessing.cpu_count()
_output_lock = threading.Lock()
def shard(items):
"""Breaks down the given items into roughly equal sized lists.
The number of lists will be equal to the number of available processor cores.
"""
if not items:
return []
n = int(math.ceil(len(items) / _TASKS))
return _chunks(items, n)
def _chunks(items, n):
"""Yield successive n-sized chunks from items."""
for i in range(0, len(items), n):
yield items[i:i + n]
class Result(object):
def __init__(self, num_errors, output):
self.errors = num_errors
self.output = output
@staticmethod
def from_list(errors):
return Result(len(errors), '\n'.join(errors))
class Pool(object):
def __init__(self):
# Checkers submit tasks to be run and these are dropped in the _pending
# queue. Workers process that queue and results are put in the _results
# queue. _results is drained by the thread that calls join().
self._pending = queue.Queue()
self._results = queue.Queue()
def worker():
while True:
task, args = self._pending.get()
result = task(*args)
if result is not None:
self._results.put(result)
self._pending.task_done()
for i in range(_TASKS):
t = threading.Thread(target=worker)
t.daemon = True
t.start()
def submit(self, task, *args):
"""Submits a task for execution by the pool.
Args:
task: A callable routine that will perform the work.
*args: A list of arguments to pass that routine.
"""
self._pending.put((task, args))
def join(self):
"""Waits for the completion of all submitted tasks.
Returns:
The number of errors encountered.
"""
self._pending.join()
num_errors = 0
while not self._results.empty():
result = self._results.get()
num_errors += result.errors
sys.stdout.write(result.output)
self._results.task_done()
self._results.join()
return num_errors
def exit(self):
"""Waits for the completion of the submitted tasks and exits.
This calls join() and then exits with a 0 status code if there were no
errors, or 1 if there were.
"""
errors = self.join()
sys.exit(errors > 0)