blob: 45252a07d9237ca41a7042d5c7af9b747fb4dad8 [file] [log] [blame]
Devin Jeanpierre59e4d352017-07-21 03:44:36 -07001# Copyright 2010 Baptiste Lepilleur and The JsonCpp Authors
Sam Clegg63860612015-04-09 18:01:33 -07002# 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
Christopher Dunnbd1e8952014-11-19 23:30:47 -06006from __future__ import print_function
Christopher Dunndc0f7362011-06-21 21:18:49 +00007import os.path
Mike Nabereznyb5e70f92015-07-03 15:47:00 -07008import sys
Christopher Dunndc0f7362011-06-21 21:18:49 +00009
Christopher Dunn494950a2015-01-24 15:29:52 -060010def fix_source_eol(path, is_dry_run = True, verbose = True, eol = '\n'):
Christopher Dunndc0f7362011-06-21 21:18:49 +000011 """Makes sure that all sources have the specified eol sequence (default: unix)."""
Christopher Dunn494950a2015-01-24 15:29:52 -060012 if not os.path.isfile(path):
13 raise ValueError('Path "%s" is not a file' % path)
Christopher Dunndc0f7362011-06-21 21:18:49 +000014 try:
15 f = open(path, 'rb')
Christopher Dunn9aa61442014-11-19 23:10:02 -060016 except IOError as msg:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060017 print("%s: I/O Error: %s" % (file, str(msg)), file=sys.stderr)
Christopher Dunndc0f7362011-06-21 21:18:49 +000018 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:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060025 print('%s =>' % path, end=' ')
Christopher Dunndc0f7362011-06-21 21:18:49 +000026 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:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060033 print(is_dry_run and ' NEED FIX' or ' FIXED')
Christopher Dunndc0f7362011-06-21 21:18:49 +000034 return True
35##
36##
37##
Christopher Dunn494950a2015-01-24 15:29:52 -060038##def _do_fix(is_dry_run = True):
Christopher Dunndc0f7362011-06-21 21:18:49 +000039## from waftools import antglob
Christopher Dunn494950a2015-01-24 15:29:52 -060040## python_sources = antglob.glob('.',
Christopher Dunndc0f7362011-06-21 21:18:49 +000041## includes = '**/*.py **/wscript **/wscript_build',
42## excludes = antglob.default_excludes + './waf.py',
Christopher Dunn494950a2015-01-24 15:29:52 -060043## prune_dirs = antglob.prune_dirs + 'waf-* ./build')
Christopher Dunndc0f7362011-06-21 21:18:49 +000044## for path in python_sources:
Christopher Dunn494950a2015-01-24 15:29:52 -060045## _fix_python_source(path, is_dry_run)
Christopher Dunndc0f7362011-06-21 21:18:49 +000046##
Christopher Dunn494950a2015-01-24 15:29:52 -060047## cpp_sources = antglob.glob('.',
Christopher Dunndc0f7362011-06-21 21:18:49 +000048## includes = '**/*.cpp **/*.h **/*.inl',
Christopher Dunn494950a2015-01-24 15:29:52 -060049## prune_dirs = antglob.prune_dirs + 'waf-* ./build')
Christopher Dunndc0f7362011-06-21 21:18:49 +000050## for path in cpp_sources:
Christopher Dunn494950a2015-01-24 15:29:52 -060051## _fix_source_eol(path, is_dry_run)
Christopher Dunndc0f7362011-06-21 21:18:49 +000052##
53##
54##def dry_fix(context):
Christopher Dunn494950a2015-01-24 15:29:52 -060055## _do_fix(is_dry_run = True)
Christopher Dunndc0f7362011-06-21 21:18:49 +000056##
57##def fix(context):
Christopher Dunn494950a2015-01-24 15:29:52 -060058## _do_fix(is_dry_run = False)
Christopher Dunndc0f7362011-06-21 21:18:49 +000059##
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()