Mercurial > hgrepos > Python > apps > py-cutils
annotate 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 |
| 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 |
|
72
ae2df602beb4
Make shasum.py and dos2unix sub-modules to the new "cutils" package
Franz Glasner <fzglas.hg@dom66.de>
parents:
44
diff
changeset
|
13 from __future__ import print_function, absolute_import |
|
36
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
14 |
|
72
ae2df602beb4
Make shasum.py and dos2unix sub-modules to the new "cutils" package
Franz Glasner <fzglas.hg@dom66.de>
parents:
44
diff
changeset
|
15 from . import (__version__, __revision__, __date__) |
|
36
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
16 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
17 import argparse |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
18 import io |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
19 import sys |
|
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 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
22 def main(argv=None): |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
23 aparser = argparse.ArgumentParser( |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
24 description="Python implementation of dos2unix", |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
25 fromfile_prefix_chars='@') |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
26 aparser.add_argument( |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
27 "--version", "-V", action="version", |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
28 version="%s (rv:%s)" % (__version__, __revision__)) |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
29 aparser.add_argument( |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
30 "--keepdate", "-k", action="store_true", |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
31 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
|
32 aparser.add_argument( |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
33 "--oldfile", "-o", action="store_false", dest="newfile", default=False, |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
34 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
|
35 " The program defaults to run in this mode." |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
36 " Wildcard names may be used. ") |
|
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 "--newfile", "-n", action="store_true", dest="newfile", default=False, |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
39 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
|
40 " 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
|
41 " NOT be used or you WILL lose your files.") |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
42 aparser.add_argument( |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
43 "--quiet", "-q", action="store_true", |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
44 help="Quiet mode. Suppress all warning and messages.") |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
45 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
46 aparser.add_argument( |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
47 "files", nargs="+", metavar="FILE") |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
48 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
49 opts = aparser.parse_args(args=argv) |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
50 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
51 if opts.keepdate: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
52 raise NotImplementedError("--keepdate, -k") |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
53 |
|
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
|
54 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
|
55 |
|
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
|
56 |
|
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
|
57 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
|
58 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
|
59 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
|
60 |
|
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
|
61 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
|
62 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
|
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 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
|
65 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
|
66 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
|
67 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
|
68 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
|
69 |
|
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 |
|
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 def dos2unix(opts): |
|
36
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
72 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
|
73 return _convert_copy(opts) |
|
36
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
74 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
|
75 return _convert_inplace(opts) |
|
36
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
76 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
77 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
78 def _convert_inplace(opts): |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
79 lines = [] |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
80 for filename in opts.files: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
81 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
|
82 for line in source: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
83 lines.append(line.encode("iso-8859-1")) |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
84 with open(filename, "wb") as dest: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
85 for line in lines: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
86 dest.write(line) |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
87 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
88 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
89 def _convert_copy(opts): |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
90 if len(opts.files) % 2: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
91 print("ERROR: need pairs of files", file=sys.stderr) |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
92 return 64 # :manpage:`sysexits(3)` EX_USAGE |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
93 idx = 0 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
94 while idx < len(opts.files): |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
95 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
|
96 with open(opts.files[idx+1], "wb") as dest: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
97 for line in source: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
98 dest.write(line.encode("iso-8859-1")) |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
99 idx += 2 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
100 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
101 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
102 if __name__ == "__main__": |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
103 sys.exit(main()) |
