Mercurial > hgrepos > Python > apps > py-cutils
diff cutils/dos2unix.py @ 72:ae2df602beb4
Make shasum.py and dos2unix sub-modules to the new "cutils" package
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Sat, 26 Feb 2022 19:20:20 +0100 |
| parents | dos2unix.py@26a8d4e7c8ee |
| children | c3268f4e752f |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cutils/dos2unix.py Sat Feb 26 19:20:20 2022 +0100 @@ -0,0 +1,103 @@ +r""" +:Author: Franz Glasner +:Copyright: (c) 2020-2022 Franz Glasner. + All rights reserved. +:License: BSD 3-Clause "New" or "Revised" License. + See :ref:`LICENSE <license>` for details. + If you cannot find LICENSE see + <https://opensource.org/licenses/BSD-3-Clause> +:ID: @(#) $HGid$ + +""" + +from __future__ import print_function, absolute_import + +from . import (__version__, __revision__, __date__) + +import argparse +import io +import sys + + +def main(argv=None): + aparser = argparse.ArgumentParser( + description="Python implementation of dos2unix", + fromfile_prefix_chars='@') + aparser.add_argument( + "--version", "-V", action="version", + version="%s (rv:%s)" % (__version__, __revision__)) + aparser.add_argument( + "--keepdate", "-k", action="store_true", + help="Keep the date stamp of output file same as input file.") + aparser.add_argument( + "--oldfile", "-o", action="store_false", dest="newfile", default=False, + help="Old file mode. Convert the file and write output to it." + " The program defaults to run in this mode." + " Wildcard names may be used. ") + aparser.add_argument( + "--newfile", "-n", action="store_true", dest="newfile", default=False, + help="New file mode. Convert the infile and write output to outfile." + " File names must be given in pairs and wildcard names should" + " NOT be used or you WILL lose your files.") + aparser.add_argument( + "--quiet", "-q", action="store_true", + help="Quiet mode. Suppress all warning and messages.") + + aparser.add_argument( + "files", nargs="+", metavar="FILE") + + opts = aparser.parse_args(args=argv) + + if opts.keepdate: + raise NotImplementedError("--keepdate, -k") + + return dos2unix(opts) + + +def gen_opts(files=[], newfile=False, keepdate=False, quiet=True): + if keepdate: + raise NotImplementedError("--keepdate, -k") + + if newfile and (len(files) % 2): + raise ValueError("need pairs of files") + + opts = argparse.Namespace(files=files, + newfile=newfile, + keepdate=keepdate, + quiet=quiet) + return opts + + +def dos2unix(opts): + if opts.newfile: + return _convert_copy(opts) + else: + return _convert_inplace(opts) + + +def _convert_inplace(opts): + lines = [] + for filename in opts.files: + with io.open(filename, "rt", encoding="iso-8859-1") as source: + for line in source: + lines.append(line.encode("iso-8859-1")) + with open(filename, "wb") as dest: + for line in lines: + dest.write(line) + + +def _convert_copy(opts): + if len(opts.files) % 2: + print("ERROR: need pairs of files", file=sys.stderr) + return 64 # :manpage:`sysexits(3)` EX_USAGE + idx = 0 + while idx < len(opts.files): + with io.open(opts.files[idx], "rt", encoding="iso-8859-1") as source: + with open(opts.files[idx+1], "wb") as dest: + for line in source: + dest.write(line.encode("iso-8859-1")) + idx += 2 + + +if __name__ == "__main__": + sys.exit(main())
