fixeol.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright 2010 Baptiste Lepilleur and The JsonCpp Authors
  2. # Distributed under MIT license, or public domain if desired and
  3. # recognized in your jurisdiction.
  4. # See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
  5. from __future__ import print_function
  6. import os.path
  7. import sys
  8. def fix_source_eol(path, is_dry_run = True, verbose = True, eol = '\n'):
  9. """Makes sure that all sources have the specified eol sequence (default: unix)."""
  10. if not os.path.isfile(path):
  11. raise ValueError('Path "%s" is not a file' % path)
  12. try:
  13. f = open(path, 'rb')
  14. except IOError as msg:
  15. print("%s: I/O Error: %s" % (file, str(msg)), file=sys.stderr)
  16. return False
  17. try:
  18. raw_lines = f.readlines()
  19. finally:
  20. f.close()
  21. fixed_lines = [line.rstrip('\r\n') + eol for line in raw_lines]
  22. if raw_lines != fixed_lines:
  23. print('%s =>' % path, end=' ')
  24. if not is_dry_run:
  25. f = open(path, "wb")
  26. try:
  27. f.writelines(fixed_lines)
  28. finally:
  29. f.close()
  30. if verbose:
  31. print(is_dry_run and ' NEED FIX' or ' FIXED')
  32. return True
  33. ##
  34. ##
  35. ##
  36. ##def _do_fix(is_dry_run = True):
  37. ## from waftools import antglob
  38. ## python_sources = antglob.glob('.',
  39. ## includes = '**/*.py **/wscript **/wscript_build',
  40. ## excludes = antglob.default_excludes + './waf.py',
  41. ## prune_dirs = antglob.prune_dirs + 'waf-* ./build')
  42. ## for path in python_sources:
  43. ## _fix_python_source(path, is_dry_run)
  44. ##
  45. ## cpp_sources = antglob.glob('.',
  46. ## includes = '**/*.cpp **/*.h **/*.inl',
  47. ## prune_dirs = antglob.prune_dirs + 'waf-* ./build')
  48. ## for path in cpp_sources:
  49. ## _fix_source_eol(path, is_dry_run)
  50. ##
  51. ##
  52. ##def dry_fix(context):
  53. ## _do_fix(is_dry_run = True)
  54. ##
  55. ##def fix(context):
  56. ## _do_fix(is_dry_run = False)
  57. ##
  58. ##def shutdown():
  59. ## pass
  60. ##
  61. ##def check(context):
  62. ## # Unit tests are run when "check" target is used
  63. ## ut = UnitTest.unit_test()
  64. ## ut.change_to_testfile_dir = True
  65. ## ut.want_to_see_test_output = True
  66. ## ut.want_to_see_test_error = True
  67. ## ut.run()
  68. ## ut.print_results()