blob: 11e1ce2a1294c144466d7ca4cc5b8cc9f8c67dd2 [file] [log] [blame]
Haibo Huangb0bee822021-02-24 15:40:15 -08001# 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
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -05006from __future__ import print_function
7import os.path
Haibo Huangb0bee822021-02-24 15:40:15 -08008import sys
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -05009
Haibo Huangb0bee822021-02-24 15:40:15 -080010def fix_source_eol(path, is_dry_run = True, verbose = True, eol = '\n'):
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050011 """Makes sure that all sources have the specified eol sequence (default: unix)."""
Haibo Huangb0bee822021-02-24 15:40:15 -080012 if not os.path.isfile(path):
13 raise ValueError('Path "%s" is not a file' % path)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050014 try:
15 f = open(path, 'rb')
16 except IOError as msg:
17 print("%s: I/O Error: %s" % (file, str(msg)), file=sys.stderr)
18 return False
19 try:
20 raw_lines = f.readlines()
21 finally:
22 f.close()
23 fixed_lines = [line.rstrip('\r\n') + eol for line in raw_lines]
24 if raw_lines != fixed_lines:
25 print('%s =>' % path, end=' ')
26 if not is_dry_run:
27 f = open(path, "wb")
28 try:
29 f.writelines(fixed_lines)
30 finally:
31 f.close()
32 if verbose:
33 print(is_dry_run and ' NEED FIX' or ' FIXED')
34 return True
Elliott Hughes1601ea02021-12-07 09:43:38 -080035##
36##
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050037##
Haibo Huangb0bee822021-02-24 15:40:15 -080038##def _do_fix(is_dry_run = True):
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050039## from waftools import antglob
Haibo Huangb0bee822021-02-24 15:40:15 -080040## python_sources = antglob.glob('.',
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050041## includes = '**/*.py **/wscript **/wscript_build',
42## excludes = antglob.default_excludes + './waf.py',
Haibo Huangb0bee822021-02-24 15:40:15 -080043## prune_dirs = antglob.prune_dirs + 'waf-* ./build')
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050044## for path in python_sources:
Haibo Huangb0bee822021-02-24 15:40:15 -080045## _fix_python_source(path, is_dry_run)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050046##
Haibo Huangb0bee822021-02-24 15:40:15 -080047## cpp_sources = antglob.glob('.',
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050048## includes = '**/*.cpp **/*.h **/*.inl',
Haibo Huangb0bee822021-02-24 15:40:15 -080049## prune_dirs = antglob.prune_dirs + 'waf-* ./build')
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050050## for path in cpp_sources:
Haibo Huangb0bee822021-02-24 15:40:15 -080051## _fix_source_eol(path, is_dry_run)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050052##
53##
54##def dry_fix(context):
Haibo Huangb0bee822021-02-24 15:40:15 -080055## _do_fix(is_dry_run = True)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050056##
57##def fix(context):
Haibo Huangb0bee822021-02-24 15:40:15 -080058## _do_fix(is_dry_run = False)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050059##
60##def shutdown():
61## pass
62##
63##def check(context):
64## # Unit tests are run when "check" target is used
65## ut = UnitTest.unit_test()
66## ut.change_to_testfile_dir = True
67## ut.want_to_see_test_output = True
68## ut.want_to_see_test_error = True
69## ut.run()
70## ut.print_results()