Mercurial > hgrepos > Python > apps > py-cutils
comparison dos2unix.py @ 36:1de48e84a5fb
Implemented a dos2unix command.
BUGS: Option --keepdate not yet supported
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Tue, 08 Dec 2020 09:29:39 +0100 |
| parents | |
| children | d856432a1cbb |
comparison
equal
deleted
inserted
replaced
| 35:f373bc5adc52 | 36:1de48e84a5fb |
|---|---|
| 1 r""" | |
| 2 :Author: Franz Glasner | |
| 3 :Copyright: (c) 2020 Franz Glasner. | |
| 4 All rights reserved. | |
| 5 :License: BSD 3-Clause "New" or "Revised" License. | |
| 6 See :ref:`LICENSE <license>` for details. | |
| 7 If you cannot find LICENSE see | |
| 8 <https://opensource.org/licenses/BSD-3-Clause> | |
| 9 :ID: @(#) $HGid$ | |
| 10 | |
| 11 """ | |
| 12 | |
| 13 from __future__ import print_function | |
| 14 | |
| 15 | |
| 16 try: | |
| 17 from _cutils import __version__ | |
| 18 except ImportError: | |
| 19 __version__ = "unknown" | |
| 20 | |
| 21 __revision__ = "|VCSRevision|" | |
| 22 __date__ = "|VCSJustDate|" | |
| 23 | |
| 24 | |
| 25 import argparse | |
| 26 import io | |
| 27 import sys | |
| 28 | |
| 29 | |
| 30 def main(argv=None): | |
| 31 aparser = argparse.ArgumentParser( | |
| 32 description="Python implementation of dos2unix", | |
| 33 fromfile_prefix_chars='@') | |
| 34 aparser.add_argument( | |
| 35 "--version", "-V", action="version", | |
| 36 version="%s (rv:%s)" % (__version__, __revision__)) | |
| 37 aparser.add_argument( | |
| 38 "--keepdate", "-k", action="store_true", | |
| 39 help="Keep the date stamp of output file same as input file.") | |
| 40 aparser.add_argument( | |
| 41 "--oldfile", "-o", action="store_false", dest="newfile", default=False, | |
| 42 help="Old file mode. Convert the file and write output to it." | |
| 43 " The program defaults to run in this mode." | |
| 44 " Wildcard names may be used. ") | |
| 45 aparser.add_argument( | |
| 46 "--newfile", "-n", action="store_true", dest="newfile", default=False, | |
| 47 help="New file mode. Convert the infile and write output to outfile." | |
| 48 " File names must be given in pairs and wildcard names should" | |
| 49 " NOT be used or you WILL lose your files.") | |
| 50 aparser.add_argument( | |
| 51 "--quiet", "-q", action="store_true", | |
| 52 help="Quiet mode. Suppress all warning and messages.") | |
| 53 | |
| 54 aparser.add_argument( | |
| 55 "files", nargs="+", metavar="FILE") | |
| 56 | |
| 57 opts = aparser.parse_args(args=argv) | |
| 58 | |
| 59 if opts.keepdate: | |
| 60 raise NotImplementedError("--keepdate, -k") | |
| 61 | |
| 62 if opts.newfile: | |
| 63 return(_convert_copy(opts)) | |
| 64 else: | |
| 65 return(_convert_inplace(opts)) | |
| 66 | |
| 67 | |
| 68 def _convert_inplace(opts): | |
| 69 lines = [] | |
| 70 for filename in opts.files: | |
| 71 with io.open(filename, "rt", encoding="iso-8859-1") as source: | |
| 72 for line in source: | |
| 73 lines.append(line.encode("iso-8859-1")) | |
| 74 with open(filename, "wb") as dest: | |
| 75 for line in lines: | |
| 76 dest.write(line) | |
| 77 | |
| 78 | |
| 79 def _convert_copy(opts): | |
| 80 if len(opts.files) % 2: | |
| 81 print("ERROR: need pairs of files", file=sys.stderr) | |
| 82 return 64 # :manpage:`sysexits(3)` EX_USAGE | |
| 83 idx = 0 | |
| 84 while idx < len(opts.files): | |
| 85 with io.open(opts.files[idx], "rt", encoding="iso-8859-1") as source: | |
| 86 with open(opts.files[idx+1], "wb") as dest: | |
| 87 for line in source: | |
| 88 dest.write(line.encode("iso-8859-1")) | |
| 89 idx += 2 | |
| 90 | |
| 91 | |
| 92 if __name__ == "__main__": | |
| 93 sys.exit(main()) |
