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