releasetools: Remove the global diff_done in blockimgdiff.py.

pylint complains about undefined `diff_done`:

W:754, 8: Global variable 'diff_done' undefined at the module level (global-variable-undefined)
W:820,14: Global variable 'diff_done' undefined at the module level (global-variable-undefined)

It would still warn about using global statement after adding the
definition.

W:859, 8: Using the global statement (global-statement)
W:925,14: Using the global statement (global-statement)

This CL computes 'diff_done' via 'len(diff_queue)' instead. It also
moves the progress reporting _before_ the diff work. This way it avoids
showing 100% progress with still changing filenames (because multiple
workers could see an empty queue simultaneously upon finishing their own
works).

There're possible alternatives, such as using the 'nonlocal' keyword in
Python 3 (which we're not there yet), or by using mutable object instead
(e.g. 'diff_done = [0]'). This CL looks cleaner, since it just kills the
var.

Test: Generate a BBOTA incremental. Check the on-screen progress
      report.
Test: `pylint --rcfile=pylintrc blockimgdiff.py` no longer complains
      about the global diff_done.
Change-Id: I339824735527e1f794b5b1dc99ff3fdb2da85744
This commit is contained in:
Tao Bao 2018-02-12 12:08:05 -08:00
parent 7eb2afb226
commit 9739514769
1 changed files with 8 additions and 11 deletions

View File

@ -852,9 +852,6 @@ class BlockImageDiff(object):
patches = [None] * diff_total
error_messages = []
warning_messages = []
if sys.stdout.isatty():
global diff_done
diff_done = 0
# Using multiprocessing doesn't give additional benefits, due to the
# pattern of the code. The diffing work is done by subprocess.call, which
@ -870,8 +867,15 @@ class BlockImageDiff(object):
if not diff_queue:
return
xf_index, imgdiff, patch_index = diff_queue.pop()
xf = self.transfers[xf_index]
if sys.stdout.isatty():
diff_left = len(diff_queue)
progress = (diff_total - diff_left) * 100 / diff_total
# '\033[K' is to clear to EOL.
print(' [%3d%%] %s\033[K' % (progress, xf.tgt_name), end='\r')
sys.stdout.flush()
xf = self.transfers[xf_index]
patch = xf.patch
if not patch:
src_ranges = xf.src_ranges
@ -918,13 +922,6 @@ class BlockImageDiff(object):
with lock:
patches[patch_index] = (xf_index, patch)
if sys.stdout.isatty():
global diff_done
diff_done += 1
progress = diff_done * 100 / diff_total
# '\033[K' is to clear to EOL.
print(' [%d%%] %s\033[K' % (progress, xf.tgt_name), end='\r')
sys.stdout.flush()
threads = [threading.Thread(target=diff_worker)
for _ in range(self.threads)]