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