blob: a76880f9680cff770369fff34e6e8b2e8e2d81ea [file] [log] [blame]
Christopher Dunnbd1e8952014-11-19 23:30:47 -06001from __future__ import print_function
Christopher Dunndc0f7362011-06-21 21:18:49 +00002import os.path
3
Christopher Dunn494950a2015-01-24 15:29:52 -06004def fix_source_eol(path, is_dry_run = True, verbose = True, eol = '\n'):
Christopher Dunndc0f7362011-06-21 21:18:49 +00005 """Makes sure that all sources have the specified eol sequence (default: unix)."""
Christopher Dunn494950a2015-01-24 15:29:52 -06006 if not os.path.isfile(path):
7 raise ValueError('Path "%s" is not a file' % path)
Christopher Dunndc0f7362011-06-21 21:18:49 +00008 try:
9 f = open(path, 'rb')
Christopher Dunn9aa61442014-11-19 23:10:02 -060010 except IOError as msg:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060011 print("%s: I/O Error: %s" % (file, str(msg)), file=sys.stderr)
Christopher Dunndc0f7362011-06-21 21:18:49 +000012 return False
13 try:
14 raw_lines = f.readlines()
15 finally:
16 f.close()
17 fixed_lines = [line.rstrip('\r\n') + eol for line in raw_lines]
18 if raw_lines != fixed_lines:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060019 print('%s =>' % path, end=' ')
Christopher Dunndc0f7362011-06-21 21:18:49 +000020 if not is_dry_run:
21 f = open(path, "wb")
22 try:
23 f.writelines(fixed_lines)
24 finally:
25 f.close()
26 if verbose:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060027 print(is_dry_run and ' NEED FIX' or ' FIXED')
Christopher Dunndc0f7362011-06-21 21:18:49 +000028 return True
29##
30##
31##
Christopher Dunn494950a2015-01-24 15:29:52 -060032##def _do_fix(is_dry_run = True):
Christopher Dunndc0f7362011-06-21 21:18:49 +000033## from waftools import antglob
Christopher Dunn494950a2015-01-24 15:29:52 -060034## python_sources = antglob.glob('.',
Christopher Dunndc0f7362011-06-21 21:18:49 +000035## includes = '**/*.py **/wscript **/wscript_build',
36## excludes = antglob.default_excludes + './waf.py',
Christopher Dunn494950a2015-01-24 15:29:52 -060037## prune_dirs = antglob.prune_dirs + 'waf-* ./build')
Christopher Dunndc0f7362011-06-21 21:18:49 +000038## for path in python_sources:
Christopher Dunn494950a2015-01-24 15:29:52 -060039## _fix_python_source(path, is_dry_run)
Christopher Dunndc0f7362011-06-21 21:18:49 +000040##
Christopher Dunn494950a2015-01-24 15:29:52 -060041## cpp_sources = antglob.glob('.',
Christopher Dunndc0f7362011-06-21 21:18:49 +000042## includes = '**/*.cpp **/*.h **/*.inl',
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 cpp_sources:
Christopher Dunn494950a2015-01-24 15:29:52 -060045## _fix_source_eol(path, is_dry_run)
Christopher Dunndc0f7362011-06-21 21:18:49 +000046##
47##
48##def dry_fix(context):
Christopher Dunn494950a2015-01-24 15:29:52 -060049## _do_fix(is_dry_run = True)
Christopher Dunndc0f7362011-06-21 21:18:49 +000050##
51##def fix(context):
Christopher Dunn494950a2015-01-24 15:29:52 -060052## _do_fix(is_dry_run = False)
Christopher Dunndc0f7362011-06-21 21:18:49 +000053##
54##def shutdown():
55## pass
56##
57##def check(context):
58## # Unit tests are run when "check" target is used
59## ut = UnitTest.unit_test()
60## ut.change_to_testfile_dir = True
61## ut.want_to_see_test_output = True
62## ut.want_to_see_test_error = True
63## ut.run()
64## ut.print_results()