Mercurial > hgrepos > Python > apps > py-cutils
annotate cutils/dos2unix.py @ 177:089c40240061
Add an alternate implementation for generating directory tree digests:
- Do not use something like os.walk() but use os.scandir() directly.
- Recursively generate the subdirectory digests only when needed and in
the right order.
This fixes that the order of subdirectories in the output did not
match the application order of its directory digests.
The new implementation also should make filtering (that will be
implemented later) easier.
NOTE: The tree digests of the old and the new implementation are identical.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Sat, 11 Jan 2025 17:41:28 +0100 |
| parents | dfe7bb0579e2 |
| children | 48430941c18c |
| rev | line source |
|---|---|
|
73
c3268f4e752f
Adjust all license notes to (a) more literally comply with the BSD3 templates and to the style guide
Franz Glasner <fzglas.hg@dom66.de>
parents:
72
diff
changeset
|
1 # -*- coding: utf-8 -*- |
|
c3268f4e752f
Adjust all license notes to (a) more literally comply with the BSD3 templates and to the style guide
Franz Glasner <fzglas.hg@dom66.de>
parents:
72
diff
changeset
|
2 # :- |
|
126
dfe7bb0579e2
Extend copyright years to 2025
Franz Glasner <fzglas.hg@dom66.de>
parents:
116
diff
changeset
|
3 # :Copyright: (c) 2020-2025 Franz Glasner |
|
73
c3268f4e752f
Adjust all license notes to (a) more literally comply with the BSD3 templates and to the style guide
Franz Glasner <fzglas.hg@dom66.de>
parents:
72
diff
changeset
|
4 # :License: BSD-3-Clause |
|
c3268f4e752f
Adjust all license notes to (a) more literally comply with the BSD3 templates and to the style guide
Franz Glasner <fzglas.hg@dom66.de>
parents:
72
diff
changeset
|
5 # :- |
| 116 | 6 r"""Pure Python implementation of `dos2unix`. |
|
36
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
7 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
8 """ |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
9 |
|
72
ae2df602beb4
Make shasum.py and dos2unix sub-modules to the new "cutils" package
Franz Glasner <fzglas.hg@dom66.de>
parents:
44
diff
changeset
|
10 from __future__ import print_function, absolute_import |
|
36
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
11 |
|
73
c3268f4e752f
Adjust all license notes to (a) more literally comply with the BSD3 templates and to the style guide
Franz Glasner <fzglas.hg@dom66.de>
parents:
72
diff
changeset
|
12 from . import (__version__, __revision__) |
|
c3268f4e752f
Adjust all license notes to (a) more literally comply with the BSD3 templates and to the style guide
Franz Glasner <fzglas.hg@dom66.de>
parents:
72
diff
changeset
|
13 |
|
c3268f4e752f
Adjust all license notes to (a) more literally comply with the BSD3 templates and to the style guide
Franz Glasner <fzglas.hg@dom66.de>
parents:
72
diff
changeset
|
14 |
|
c3268f4e752f
Adjust all license notes to (a) more literally comply with the BSD3 templates and to the style guide
Franz Glasner <fzglas.hg@dom66.de>
parents:
72
diff
changeset
|
15 __all__ = [] |
|
c3268f4e752f
Adjust all license notes to (a) more literally comply with the BSD3 templates and to the style guide
Franz Glasner <fzglas.hg@dom66.de>
parents:
72
diff
changeset
|
16 |
|
36
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
17 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
18 import argparse |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
19 import io |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
20 import sys |
|
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 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
23 def main(argv=None): |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
24 aparser = argparse.ArgumentParser( |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
25 description="Python implementation of dos2unix", |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
26 fromfile_prefix_chars='@') |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
27 aparser.add_argument( |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
28 "--version", "-V", action="version", |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
29 version="%s (rv:%s)" % (__version__, __revision__)) |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
30 aparser.add_argument( |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
31 "--keepdate", "-k", action="store_true", |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
32 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
|
33 aparser.add_argument( |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
34 "--oldfile", "-o", action="store_false", dest="newfile", default=False, |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
35 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
|
36 " The program defaults to run in this mode." |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
37 " Wildcard names may be used. ") |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
38 aparser.add_argument( |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
39 "--newfile", "-n", action="store_true", dest="newfile", default=False, |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
40 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
|
41 " 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
|
42 " NOT be used or you WILL lose your files.") |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
43 aparser.add_argument( |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
44 "--quiet", "-q", action="store_true", |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
45 help="Quiet mode. Suppress all warning and messages.") |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
46 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
47 aparser.add_argument( |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
48 "files", nargs="+", metavar="FILE") |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
49 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
50 opts = aparser.parse_args(args=argv) |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
51 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
52 if opts.keepdate: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
53 raise NotImplementedError("--keepdate, -k") |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
54 |
|
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
|
55 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
|
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 |
|
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 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
|
59 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
|
60 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
|
61 |
|
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 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
|
63 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
|
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 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
|
66 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
|
67 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
|
68 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
|
69 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
|
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 |
|
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 def dos2unix(opts): |
|
36
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
73 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
|
74 return _convert_copy(opts) |
|
36
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
75 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
|
76 return _convert_inplace(opts) |
|
36
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_inplace(opts): |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
80 lines = [] |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
81 for filename in opts.files: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
82 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
|
83 for line in source: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
84 lines.append(line.encode("iso-8859-1")) |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
85 with open(filename, "wb") as dest: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
86 for line in lines: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
87 dest.write(line) |
|
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 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
90 def _convert_copy(opts): |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
91 if len(opts.files) % 2: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
92 print("ERROR: need pairs of files", file=sys.stderr) |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
93 return 64 # :manpage:`sysexits(3)` EX_USAGE |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
94 idx = 0 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
95 while idx < len(opts.files): |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
96 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
|
97 with open(opts.files[idx+1], "wb") as dest: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
98 for line in source: |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
99 dest.write(line.encode("iso-8859-1")) |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
100 idx += 2 |
|
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 |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
103 if __name__ == "__main__": |
|
1de48e84a5fb
Implemented a dos2unix command.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
104 sys.exit(main()) |
