blob: 054eb9b227bafa0a581ee17bb5d1e57164a45b06 [file] [log] [blame]
Sam Clegg63860612015-04-09 18:01:33 -07001# Copyright 2010 Baptiste Lepilleur
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
Christopher Dunnbd1e8952014-11-19 23:30:47 -06006from __future__ import print_function
Christopher Dunndc0f7362011-06-21 21:18:49 +00007import os.path
8
Christopher Dunn494950a2015-01-24 15:29:52 -06009def fix_source_eol(path, is_dry_run = True, verbose = True, eol = '\n'):
Christopher Dunndc0f7362011-06-21 21:18:49 +000010 """Makes sure that all sources have the specified eol sequence (default: unix)."""
Christopher Dunn494950a2015-01-24 15:29:52 -060011 if not os.path.isfile(path):
12 raise ValueError('Path "%s" is not a file' % path)
Christopher Dunndc0f7362011-06-21 21:18:49 +000013 try:
14 f = open(path, 'rb')
Christopher Dunn9aa61442014-11-19 23:10:02 -060015 except IOError as msg:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060016 print("%s: I/O Error: %s" % (file, str(msg)), file=sys.stderr)
Christopher Dunndc0f7362011-06-21 21:18:49 +000017 return False
18 try:
19 raw_lines = f.readlines()
20 finally:
21 f.close()
22 fixed_lines = [line.rstrip('\r\n') + eol for line in raw_lines]
23 if raw_lines != fixed_lines:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060024 print('%s =>' % path, end=' ')
Christopher Dunndc0f7362011-06-21 21:18:49 +000025 if not is_dry_run:
26 f = open(path, "wb")
27 try:
28 f.writelines(fixed_lines)
29 finally:
30 f.close()
31 if verbose:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060032 print(is_dry_run and ' NEED FIX' or ' FIXED')
Christopher Dunndc0f7362011-06-21 21:18:49 +000033 return True
34##
35##
36##
Christopher Dunn494950a2015-01-24 15:29:52 -060037##def _do_fix(is_dry_run = True):
Christopher Dunndc0f7362011-06-21 21:18:49 +000038## from waftools import antglob
Christopher Dunn494950a2015-01-24 15:29:52 -060039## python_sources = antglob.glob('.',
Christopher Dunndc0f7362011-06-21 21:18:49 +000040## includes = '**/*.py **/wscript **/wscript_build',
41## excludes = antglob.default_excludes + './waf.py',
Christopher Dunn494950a2015-01-24 15:29:52 -060042## prune_dirs = antglob.prune_dirs + 'waf-* ./build')
Christopher Dunndc0f7362011-06-21 21:18:49 +000043## for path in python_sources:
Christopher Dunn494950a2015-01-24 15:29:52 -060044## _fix_python_source(path, is_dry_run)
Christopher Dunndc0f7362011-06-21 21:18:49 +000045##
Christopher Dunn494950a2015-01-24 15:29:52 -060046## cpp_sources = antglob.glob('.',
Christopher Dunndc0f7362011-06-21 21:18:49 +000047## includes = '**/*.cpp **/*.h **/*.inl',
Christopher Dunn494950a2015-01-24 15:29:52 -060048## prune_dirs = antglob.prune_dirs + 'waf-* ./build')
Christopher Dunndc0f7362011-06-21 21:18:49 +000049## for path in cpp_sources:
Christopher Dunn494950a2015-01-24 15:29:52 -060050## _fix_source_eol(path, is_dry_run)
Christopher Dunndc0f7362011-06-21 21:18:49 +000051##
52##
53##def dry_fix(context):
Christopher Dunn494950a2015-01-24 15:29:52 -060054## _do_fix(is_dry_run = True)
Christopher Dunndc0f7362011-06-21 21:18:49 +000055##
56##def fix(context):
Christopher Dunn494950a2015-01-24 15:29:52 -060057## _do_fix(is_dry_run = False)
Christopher Dunndc0f7362011-06-21 21:18:49 +000058##
59##def shutdown():
60## pass
61##
62##def check(context):
63## # Unit tests are run when "check" target is used
64## ut = UnitTest.unit_test()
65## ut.change_to_testfile_dir = True
66## ut.want_to_see_test_output = True
67## ut.want_to_see_test_error = True
68## ut.run()
69## ut.print_results()