Mercurial > hgrepos > Python > apps > py-cutils
annotate dos2unix.py @ 41:36782a2abb66
===== Added signature for changeset 2d9f283f301e
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Tue, 08 Dec 2020 09:43:35 +0100 |
| parents | 1de48e84a5fb |
| children | d856432a1cbb |
| 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 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
3 :Copyright: (c) 2020 Franz Glasner. |
|
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 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
62 if opts.newfile: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
63 return(_convert_copy(opts)) |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
64 else: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
65 return(_convert_inplace(opts)) |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
66 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
67 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
68 def _convert_inplace(opts): |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
69 lines = [] |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
70 for filename in opts.files: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
71 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
|
72 for line in source: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
73 lines.append(line.encode("iso-8859-1")) |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
74 with open(filename, "wb") as dest: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
75 for line in lines: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
76 dest.write(line) |
|
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 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
79 def _convert_copy(opts): |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
80 if len(opts.files) % 2: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
81 print("ERROR: need pairs of files", file=sys.stderr) |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
82 return 64 # :manpage:`sysexits(3)` EX_USAGE |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
83 idx = 0 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
84 while idx < len(opts.files): |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
85 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
|
86 with open(opts.files[idx+1], "wb") as dest: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
87 for line in source: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
88 dest.write(line.encode("iso-8859-1")) |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
89 idx += 2 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
90 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
91 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
92 if __name__ == "__main__": |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
93 sys.exit(main()) |
