Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 1 | from __future__ import print_function |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 2 | import os.path |
| 3 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 4 | def fix_source_eol(path, is_dry_run = True, verbose = True, eol = '\n'): |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 5 | """Makes sure that all sources have the specified eol sequence (default: unix).""" |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 6 | if not os.path.isfile(path): |
| 7 | raise ValueError('Path "%s" is not a file' % path) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 8 | try: |
| 9 | f = open(path, 'rb') |
Christopher Dunn | 9aa6144 | 2014-11-19 23:10:02 -0600 | [diff] [blame] | 10 | except IOError as msg: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 11 | print("%s: I/O Error: %s" % (file, str(msg)), file=sys.stderr) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 12 | 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 Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 19 | print('%s =>' % path, end=' ') |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 20 | 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 Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 27 | print(is_dry_run and ' NEED FIX' or ' FIXED') |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 28 | return True |
| 29 | ## |
| 30 | ## |
| 31 | ## |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 32 | ##def _do_fix(is_dry_run = True): |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 33 | ## from waftools import antglob |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 34 | ## python_sources = antglob.glob('.', |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 35 | ## includes = '**/*.py **/wscript **/wscript_build', |
| 36 | ## excludes = antglob.default_excludes + './waf.py', |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 37 | ## prune_dirs = antglob.prune_dirs + 'waf-* ./build') |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 38 | ## for path in python_sources: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 39 | ## _fix_python_source(path, is_dry_run) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 40 | ## |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 41 | ## cpp_sources = antglob.glob('.', |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 42 | ## includes = '**/*.cpp **/*.h **/*.inl', |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 43 | ## prune_dirs = antglob.prune_dirs + 'waf-* ./build') |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 44 | ## for path in cpp_sources: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 45 | ## _fix_source_eol(path, is_dry_run) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 46 | ## |
| 47 | ## |
| 48 | ##def dry_fix(context): |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 49 | ## _do_fix(is_dry_run = True) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 50 | ## |
| 51 | ##def fix(context): |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 52 | ## _do_fix(is_dry_run = False) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 53 | ## |
| 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() |