progress.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #
  2. # Copyright (C) 2009 The Android Open Source Project
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import sys
  16. class Progress(object):
  17. def __init__(self, title, total=0):
  18. self._title = title
  19. self._total = total
  20. self._done = 0
  21. self._lastp = -1
  22. def update(self, inc=1):
  23. self._done += inc
  24. if self._total <= 0:
  25. sys.stderr.write('\r%s: %d, ' % (
  26. self._title,
  27. self._done))
  28. sys.stderr.flush()
  29. else:
  30. p = (100 * self._done) / self._total
  31. if self._lastp != p:
  32. self._lastp = p
  33. sys.stderr.write('\r%s: %3d%% (%d/%d) ' % (
  34. self._title,
  35. p,
  36. self._done,
  37. self._total))
  38. sys.stderr.flush()
  39. def end(self):
  40. if self._total <= 0:
  41. sys.stderr.write('\r%s: %d, done. \n' % (
  42. self._title,
  43. self._done))
  44. sys.stderr.flush()
  45. else:
  46. p = (100 * self._done) / self._total
  47. sys.stderr.write('\r%s: %3d%% (%d/%d), done. \n' % (
  48. self._title,
  49. p,
  50. self._done,
  51. self._total))
  52. sys.stderr.flush()