Mercurial > hgrepos > Python > apps > py-cutils
annotate cutils/treesum.py @ 160:432fe71300d3
Remove debug output
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Tue, 07 Jan 2025 12:55:55 +0100 |
| parents | 5c23ad9a3f8f |
| children | df927ada9a37 |
| rev | line source |
|---|---|
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
2 # :- |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
3 # :Copyright: (c) 2020-2025 Franz Glasner |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
4 # :License: BSD-3-Clause |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
5 # :- |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
6 r"""Generate and verify checksums for directory trees. |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
7 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
8 """ |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
9 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
10 from __future__ import print_function, absolute_import |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
11 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
12 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
13 __all__ = [] |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
14 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
15 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
16 import argparse |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
17 import base64 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
18 import binascii |
|
128
7c646921a479
Add TIMESTAMP and ISOTIMESTAMP to the output
Franz Glasner <fzglas.hg@dom66.de>
parents:
127
diff
changeset
|
19 import datetime |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
20 import os |
|
157
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
21 import stat |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
22 import sys |
|
128
7c646921a479
Add TIMESTAMP and ISOTIMESTAMP to the output
Franz Glasner <fzglas.hg@dom66.de>
parents:
127
diff
changeset
|
23 import time |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
24 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
25 from . import (__version__, __revision__) |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
26 from . import util |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
27 from .util import cm |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
28 from .util import digest |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
29 from .util import walk |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
30 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
31 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
32 def main(argv=None): |
|
142
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
33 |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
34 def _populate_generate_arguments(gp): |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
35 """Use to populate command aliases. |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
36 |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
37 This is because :class:`argparse.ArgumentParser` does not |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
38 support them for all supported Python versions. |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
39 |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
40 """ |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
41 gp.add_argument( |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
42 "--algorithm", "-a", action="store", type=util.argv2algo, |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
43 help="1 (aka sha1), 224, 256, 384, 512, " |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
44 "3 (alias for sha3-512), 3-224, 3-256, 3-384, 3-512, " |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
45 "blake2b, blake2b-256 (default), blake2s, " |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
46 "blake2 (alias for blake2b), " |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
47 "blake2-256 (alias for blake2b-256), " |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
48 "md5") |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
49 gp.add_argument( |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
50 "--append-output", action="store_true", dest="append_output", |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
51 help="Append to the output file instead of overwriting it.") |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
52 gp.add_argument( |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
53 "--base64", action="store_true", |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
54 help="Output checksums in base64 notation, not hexadecimal " |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
55 "(OpenBSD).") |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
56 gp.add_argument( |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
57 "--comment", action="append", default=[], |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
58 help="Put given comment COMMENT into the output as \"COMMENT\". " |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
59 "Can be given more than once.") |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
60 gp.add_argument( |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
61 "--follow-directory-symlinks", "-l", action="store_true", |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
62 dest="follow_directory_symlinks", |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
63 help="Follow symbolic links to directories when walking a " |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
64 "directory tree. Note that this is different from using " |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
65 "\"--logical\" or \"--physical\" for arguments given " |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
66 "directly on the command line") |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
67 gp.add_argument( |
|
158
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
68 "--full-mode", action="store_true", dest="metadata_full_mode", |
|
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
69 help="Consider all mode bits as returned from stat(2) when " |
|
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
70 "computing directory digests. " |
|
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
71 "Note that mode bits on symbolic links itself are not " |
|
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
72 "considered.") |
|
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
73 gp.add_argument( |
|
142
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
74 "--logical", "-L", dest="logical", action="store_true", |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
75 default=None, |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
76 help="Follow symbolic links given on command line arguments." |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
77 " Note that this is a different setting as to follow symbolic" |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
78 " links to directories when traversing a directory tree.") |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
79 gp.add_argument( |
|
150
f84cf853da22
Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents:
149
diff
changeset
|
80 "--minimal", nargs="?", const="", default=None, metavar="TAG", |
|
f84cf853da22
Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents:
149
diff
changeset
|
81 help="Produce minimal output only. If a TAG is given and not " |
|
f84cf853da22
Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents:
149
diff
changeset
|
82 "empty use it as the leading \"ROOT (<TAG>)\" output.") |
|
f84cf853da22
Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents:
149
diff
changeset
|
83 gp.add_argument( |
|
142
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
84 "--mmap", action="store_true", dest="mmap", default=None, |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
85 help="Use mmap if available. Default is to determine " |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
86 "automatically from the filesize.") |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
87 gp.add_argument( |
|
157
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
88 "--mode", action="store_true", dest="metadata_mode", |
|
158
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
89 help="Consider the permission bits of stat(2) using S_IMODE (i.e. " |
|
157
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
90 "all bits without the filetype bits) when " |
|
158
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
91 "computing directory digests. Note that mode bits on " |
|
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
92 "symbolic links itself are not considered.") |
|
157
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
93 gp.add_argument( |
|
151
b26c4290e928
Implement "--mtime" for treesum to include a file's mtime in a directory digest.
Franz Glasner <fzglas.hg@dom66.de>
parents:
150
diff
changeset
|
94 "--mtime", action="store_true", dest="metadata_mtime", |
|
b26c4290e928
Implement "--mtime" for treesum to include a file's mtime in a directory digest.
Franz Glasner <fzglas.hg@dom66.de>
parents:
150
diff
changeset
|
95 help="Consider the mtime of files (non-directories) when " |
|
b26c4290e928
Implement "--mtime" for treesum to include a file's mtime in a directory digest.
Franz Glasner <fzglas.hg@dom66.de>
parents:
150
diff
changeset
|
96 "generating digests for directories. Digests for files are " |
|
b26c4290e928
Implement "--mtime" for treesum to include a file's mtime in a directory digest.
Franz Glasner <fzglas.hg@dom66.de>
parents:
150
diff
changeset
|
97 "not affected.") |
|
b26c4290e928
Implement "--mtime" for treesum to include a file's mtime in a directory digest.
Franz Glasner <fzglas.hg@dom66.de>
parents:
150
diff
changeset
|
98 gp.add_argument( |
|
142
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
99 "--no-mmap", action="store_false", dest="mmap", default=None, |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
100 help="Dont use mmap.") |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
101 gp.add_argument( |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
102 "--output", "-o", action="store", metavar="OUTPUT", |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
103 help="Put the checksum into given file. " |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
104 "If not given or if it is given as `-' then stdout is used.") |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
105 gp.add_argument( |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
106 "--physical", "-P", dest="logical", action="store_false", |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
107 default=None, |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
108 help="Do not follow symbolic links given on comment line " |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
109 "arguments. This is the default.") |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
110 gp.add_argument( |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
111 "directories", nargs="*", metavar="DIRECTORY") |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
112 |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
113 parser = argparse.ArgumentParser( |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
114 description="Generate and verify checksums for directory trees.", |
|
148
17d6e760143f
Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents:
147
diff
changeset
|
115 fromfile_prefix_chars='@', |
|
17d6e760143f
Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents:
147
diff
changeset
|
116 add_help=False) |
|
142
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
117 |
|
148
17d6e760143f
Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents:
147
diff
changeset
|
118 # |
| 153 | 119 # Global options for all sub-commands. |
| 120 # In a group because this allows a customized title. | |
|
148
17d6e760143f
Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents:
147
diff
changeset
|
121 # |
|
17d6e760143f
Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents:
147
diff
changeset
|
122 gparser = parser.add_argument_group(title="Global Options") |
|
17d6e760143f
Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents:
147
diff
changeset
|
123 gparser.add_argument( |
|
142
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
124 "-v", "--version", action="version", |
|
148
17d6e760143f
Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents:
147
diff
changeset
|
125 version="%s (rv:%s)" % (__version__, __revision__), |
|
17d6e760143f
Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents:
147
diff
changeset
|
126 help="Show program's version number and exit") |
|
17d6e760143f
Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents:
147
diff
changeset
|
127 gparser.add_argument( |
|
17d6e760143f
Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents:
147
diff
changeset
|
128 "-h", "--help", action="help", |
|
17d6e760143f
Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents:
147
diff
changeset
|
129 help="Show this help message and exit") |
|
142
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
130 |
|
148
17d6e760143f
Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents:
147
diff
changeset
|
131 # |
|
17d6e760143f
Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents:
147
diff
changeset
|
132 # Subcommands |
|
17d6e760143f
Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents:
147
diff
changeset
|
133 # |
|
142
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
134 subparsers = parser.add_subparsers( |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
135 dest="subcommand", |
|
145
073e0faea599
Optimize help output for subcommands
Franz Glasner <fzglas.hg@dom66.de>
parents:
144
diff
changeset
|
136 title="Commands", |
|
142
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
137 description="This tool uses subcommands. " |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
138 "To see detailed help for a specific subcommand use " |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
139 "the -h/--help option after the subcommand name. " |
|
148
17d6e760143f
Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents:
147
diff
changeset
|
140 "A list of valid commands and their short descriptions " |
|
17d6e760143f
Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents:
147
diff
changeset
|
141 "is listed below:", |
|
145
073e0faea599
Optimize help output for subcommands
Franz Glasner <fzglas.hg@dom66.de>
parents:
144
diff
changeset
|
142 metavar="COMMAND") |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
143 |
|
142
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
144 genparser = subparsers.add_parser( |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
145 "generate", |
| 143 | 146 help="Generate checksums for directory trees.", |
|
142
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
147 description="Generate checksums for directory trees") |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
148 _populate_generate_arguments(genparser) |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
149 # And an alias for "generate" |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
150 genparser2 = subparsers.add_parser( |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
151 "gen", |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
152 help="Alias for \"generate\"", |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
153 description="Generate checksums for directory trees. " |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
154 "This is an alias to \"generate\".") |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
155 _populate_generate_arguments(genparser2) |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
156 |
|
147
ed35f3c9e2b5
Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents:
146
diff
changeset
|
157 hparser = subparsers.add_parser( |
|
ed35f3c9e2b5
Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents:
146
diff
changeset
|
158 "help", |
|
ed35f3c9e2b5
Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents:
146
diff
changeset
|
159 help="Show this help message or a subcommand's help and exit", |
|
ed35f3c9e2b5
Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents:
146
diff
changeset
|
160 description="Show this help message or a subcommand's help and exit.") |
|
ed35f3c9e2b5
Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents:
146
diff
changeset
|
161 hparser.add_argument("help_command", nargs='?', metavar="COMMAND") |
|
ed35f3c9e2b5
Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents:
146
diff
changeset
|
162 |
|
ed35f3c9e2b5
Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents:
146
diff
changeset
|
163 vparser = subparsers.add_parser( |
|
144
b39f8082ced1
Make a "version" subcommand to also print the program's version number for "treesum"
Franz Glasner <fzglas.hg@dom66.de>
parents:
143
diff
changeset
|
164 "version", |
|
b39f8082ced1
Make a "version" subcommand to also print the program's version number for "treesum"
Franz Glasner <fzglas.hg@dom66.de>
parents:
143
diff
changeset
|
165 help="Show the program's version number and exit", |
|
b39f8082ced1
Make a "version" subcommand to also print the program's version number for "treesum"
Franz Glasner <fzglas.hg@dom66.de>
parents:
143
diff
changeset
|
166 description="Show the program's version number and exit.") |
|
b39f8082ced1
Make a "version" subcommand to also print the program's version number for "treesum"
Franz Glasner <fzglas.hg@dom66.de>
parents:
143
diff
changeset
|
167 |
|
147
ed35f3c9e2b5
Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents:
146
diff
changeset
|
168 # Parse leniently to just check for "version" and/or help |
|
146
7d8df8311e3b
Optimize argument parsing for the "version" command
Franz Glasner <fzglas.hg@dom66.de>
parents:
145
diff
changeset
|
169 opts, _dummy = parser.parse_known_args(args=argv) |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
170 |
|
144
b39f8082ced1
Make a "version" subcommand to also print the program's version number for "treesum"
Franz Glasner <fzglas.hg@dom66.de>
parents:
143
diff
changeset
|
171 if opts.subcommand == "version": |
|
b39f8082ced1
Make a "version" subcommand to also print the program's version number for "treesum"
Franz Glasner <fzglas.hg@dom66.de>
parents:
143
diff
changeset
|
172 print("%s (rv:%s)" % (__version__, __revision__), |
|
b39f8082ced1
Make a "version" subcommand to also print the program's version number for "treesum"
Franz Glasner <fzglas.hg@dom66.de>
parents:
143
diff
changeset
|
173 file=sys.stdout) |
|
b39f8082ced1
Make a "version" subcommand to also print the program's version number for "treesum"
Franz Glasner <fzglas.hg@dom66.de>
parents:
143
diff
changeset
|
174 sys.exit(0) |
|
147
ed35f3c9e2b5
Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents:
146
diff
changeset
|
175 if opts.subcommand == "help": |
|
ed35f3c9e2b5
Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents:
146
diff
changeset
|
176 if not opts.help_command: |
|
ed35f3c9e2b5
Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents:
146
diff
changeset
|
177 parser.print_help() |
|
ed35f3c9e2b5
Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents:
146
diff
changeset
|
178 else: |
|
ed35f3c9e2b5
Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents:
146
diff
changeset
|
179 if opts.help_command == "generate": |
|
ed35f3c9e2b5
Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents:
146
diff
changeset
|
180 genparser.print_help() |
|
ed35f3c9e2b5
Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents:
146
diff
changeset
|
181 elif opts.help_command == "gen": |
|
ed35f3c9e2b5
Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents:
146
diff
changeset
|
182 genparser2.print_help() |
|
ed35f3c9e2b5
Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents:
146
diff
changeset
|
183 elif opts.help_command == "version": |
|
ed35f3c9e2b5
Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents:
146
diff
changeset
|
184 vparser.print_help() |
|
ed35f3c9e2b5
Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents:
146
diff
changeset
|
185 elif opts.help_command == "help": |
|
ed35f3c9e2b5
Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents:
146
diff
changeset
|
186 hparser.print_help() |
|
ed35f3c9e2b5
Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents:
146
diff
changeset
|
187 else: |
|
ed35f3c9e2b5
Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents:
146
diff
changeset
|
188 parser.print_help() |
|
ed35f3c9e2b5
Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents:
146
diff
changeset
|
189 sys.exit(0) |
|
144
b39f8082ced1
Make a "version" subcommand to also print the program's version number for "treesum"
Franz Glasner <fzglas.hg@dom66.de>
parents:
143
diff
changeset
|
190 |
|
146
7d8df8311e3b
Optimize argument parsing for the "version" command
Franz Glasner <fzglas.hg@dom66.de>
parents:
145
diff
changeset
|
191 # Reparse strictly |
|
7d8df8311e3b
Optimize argument parsing for the "version" command
Franz Glasner <fzglas.hg@dom66.de>
parents:
145
diff
changeset
|
192 opts = parser.parse_args(args=argv) |
|
7d8df8311e3b
Optimize argument parsing for the "version" command
Franz Glasner <fzglas.hg@dom66.de>
parents:
145
diff
changeset
|
193 |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
194 return treesum(opts) |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
195 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
196 |
|
142
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
197 def gen_generate_opts(directories=[], |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
198 algorithm="BLAKE2b-256", |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
199 append_output=False, |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
200 base64=False, |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
201 comment=[], |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
202 follow_directory_symlinks=False, |
|
158
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
203 full_mode=False, |
|
142
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
204 logical=None, |
|
150
f84cf853da22
Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents:
149
diff
changeset
|
205 minimal=None, |
|
157
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
206 mode=False, |
|
142
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
207 mmap=None, |
|
151
b26c4290e928
Implement "--mtime" for treesum to include a file's mtime in a directory digest.
Franz Glasner <fzglas.hg@dom66.de>
parents:
150
diff
changeset
|
208 mtime=False, |
|
142
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
209 output=None): |
|
131
3a18d71d7c50
Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents:
130
diff
changeset
|
210 opts = argparse.Namespace( |
|
3a18d71d7c50
Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents:
130
diff
changeset
|
211 directories=directories, |
|
3a18d71d7c50
Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents:
130
diff
changeset
|
212 algorithm=(util.algotag2algotype(algorithm), |
|
3a18d71d7c50
Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents:
130
diff
changeset
|
213 algorithm), |
|
3a18d71d7c50
Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents:
130
diff
changeset
|
214 append_output=append_output, |
|
3a18d71d7c50
Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents:
130
diff
changeset
|
215 base64=base64, |
|
135
dbf27681a1f6
Allow to put comments into the output with "--comment"
Franz Glasner <fzglas.hg@dom66.de>
parents:
134
diff
changeset
|
216 comment=comment, |
|
131
3a18d71d7c50
Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents:
130
diff
changeset
|
217 follow_directory_symlinks=follow_directory_symlinks, |
|
3a18d71d7c50
Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents:
130
diff
changeset
|
218 logical=logical, |
|
150
f84cf853da22
Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents:
149
diff
changeset
|
219 minimal=minimal, |
|
131
3a18d71d7c50
Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents:
130
diff
changeset
|
220 mmap=mmap, |
|
158
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
221 metadata_full_mode=full_mode, |
|
157
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
222 metadata_mode=mode, |
|
151
b26c4290e928
Implement "--mtime" for treesum to include a file's mtime in a directory digest.
Franz Glasner <fzglas.hg@dom66.de>
parents:
150
diff
changeset
|
223 metadata_mtime=mtime, |
|
131
3a18d71d7c50
Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents:
130
diff
changeset
|
224 output=output) |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
225 return opts |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
226 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
227 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
228 def treesum(opts): |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
229 # XXX TBD: opts.check and opts.checklist (as in shasum.py) |
|
142
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
230 if opts.subcommand in ("generate", "gen"): |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
231 return generate_treesum(opts) |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
232 else: |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
233 raise RuntimeError( |
|
71747e45b52c
Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents:
137
diff
changeset
|
234 "command `{}' not yet handled".format(opts.subcommand)) |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
235 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
236 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
237 def generate_treesum(opts): |
|
149
f717854be1de
Put the defaults generation when generating directory digests into "generate_treesum()" instead of "main()"
Franz Glasner <fzglas.hg@dom66.de>
parents:
148
diff
changeset
|
238 # Provide defaults |
|
f717854be1de
Put the defaults generation when generating directory digests into "generate_treesum()" instead of "main()"
Franz Glasner <fzglas.hg@dom66.de>
parents:
148
diff
changeset
|
239 if not opts.algorithm: |
|
f717854be1de
Put the defaults generation when generating directory digests into "generate_treesum()" instead of "main()"
Franz Glasner <fzglas.hg@dom66.de>
parents:
148
diff
changeset
|
240 opts.algorithm = util.argv2algo("blake2b-256") |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
241 if not opts.directories: |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
242 opts.directories.append(".") |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
243 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
244 if opts.output is None or opts.output == "-": |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
245 if hasattr(sys.stdout, "buffer"): |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
246 out_cm = cm.nullcontext(sys.stdout.buffer) |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
247 else: |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
248 out_cm = cm.nullcontext(sys.stdout) |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
249 else: |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
250 if opts.append_output: |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
251 out_cm = open(opts.output, "ab") |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
252 else: |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
253 out_cm = open(opts.output, "wb") |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
254 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
255 with out_cm as outfp: |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
256 for d in opts.directories: |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
257 generate_treesum_for_directory( |
|
131
3a18d71d7c50
Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents:
130
diff
changeset
|
258 outfp, d, opts.algorithm, opts.mmap, opts.base64, opts.logical, |
|
135
dbf27681a1f6
Allow to put comments into the output with "--comment"
Franz Glasner <fzglas.hg@dom66.de>
parents:
134
diff
changeset
|
259 opts.follow_directory_symlinks, |
|
157
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
260 opts.metadata_mode, |
|
158
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
261 opts.metadata_full_mode, |
|
151
b26c4290e928
Implement "--mtime" for treesum to include a file's mtime in a directory digest.
Franz Glasner <fzglas.hg@dom66.de>
parents:
150
diff
changeset
|
262 opts.metadata_mtime, |
|
150
f84cf853da22
Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents:
149
diff
changeset
|
263 minimal=opts.minimal, |
|
135
dbf27681a1f6
Allow to put comments into the output with "--comment"
Franz Glasner <fzglas.hg@dom66.de>
parents:
134
diff
changeset
|
264 comment=opts.comment) |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
265 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
266 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
267 def generate_treesum_for_directory( |
|
131
3a18d71d7c50
Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents:
130
diff
changeset
|
268 outfp, root, algorithm, use_mmap, use_base64, handle_root_logical, |
|
158
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
269 follow_directory_symlinks, with_metadata_mode, with_metadata_full_mode, |
|
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
270 with_metadata_mtime, |
|
151
b26c4290e928
Implement "--mtime" for treesum to include a file's mtime in a directory digest.
Franz Glasner <fzglas.hg@dom66.de>
parents:
150
diff
changeset
|
271 minimal=None, comment=None): |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
272 """ |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
273 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
274 :param outfp: a *binary* file with a "write()" and a "flush()" method |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
275 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
276 """ |
|
134
d2c303695fb8
Put a file version number into the output.
Franz Glasner <fzglas.hg@dom66.de>
parents:
133
diff
changeset
|
277 outfp.write(format_bsd_line("VERSION", "1", None, False)) |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
278 outfp.flush() |
|
125
12d6ec1f8613
Implement "--logical" and "--physical" to control following symlinked directories when given on the commandline
Franz Glasner <fzglas.hg@dom66.de>
parents:
124
diff
changeset
|
279 |
|
12d6ec1f8613
Implement "--logical" and "--physical" to control following symlinked directories when given on the commandline
Franz Glasner <fzglas.hg@dom66.de>
parents:
124
diff
changeset
|
280 # Note given non-default flags that are relevant for directory traversal |
|
12d6ec1f8613
Implement "--logical" and "--physical" to control following symlinked directories when given on the commandline
Franz Glasner <fzglas.hg@dom66.de>
parents:
124
diff
changeset
|
281 flags = [] |
|
159
5c23ad9a3f8f
FIX: Add --mode and --full-mode to the "FLAGS" output
Franz Glasner <fzglas.hg@dom66.de>
parents:
158
diff
changeset
|
282 if with_metadata_full_mode: |
|
5c23ad9a3f8f
FIX: Add --mode and --full-mode to the "FLAGS" output
Franz Glasner <fzglas.hg@dom66.de>
parents:
158
diff
changeset
|
283 flags.append("with-metadata-fullmode") |
|
5c23ad9a3f8f
FIX: Add --mode and --full-mode to the "FLAGS" output
Franz Glasner <fzglas.hg@dom66.de>
parents:
158
diff
changeset
|
284 elif with_metadata_mode: |
|
5c23ad9a3f8f
FIX: Add --mode and --full-mode to the "FLAGS" output
Franz Glasner <fzglas.hg@dom66.de>
parents:
158
diff
changeset
|
285 flags.append("with-metadata-mode") |
|
151
b26c4290e928
Implement "--mtime" for treesum to include a file's mtime in a directory digest.
Franz Glasner <fzglas.hg@dom66.de>
parents:
150
diff
changeset
|
286 if with_metadata_mtime: |
|
b26c4290e928
Implement "--mtime" for treesum to include a file's mtime in a directory digest.
Franz Glasner <fzglas.hg@dom66.de>
parents:
150
diff
changeset
|
287 flags.append("with-metadata-mtime") |
|
125
12d6ec1f8613
Implement "--logical" and "--physical" to control following symlinked directories when given on the commandline
Franz Glasner <fzglas.hg@dom66.de>
parents:
124
diff
changeset
|
288 if handle_root_logical: |
|
129
bdd8ea43074b
Output FLAGS as line "FLAGS = ..." instead of "FLAGS (...)"
Franz Glasner <fzglas.hg@dom66.de>
parents:
128
diff
changeset
|
289 flags.append("logical") |
|
131
3a18d71d7c50
Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents:
130
diff
changeset
|
290 if follow_directory_symlinks: |
|
3a18d71d7c50
Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents:
130
diff
changeset
|
291 flags.append("follow-directory-symlinks") |
|
125
12d6ec1f8613
Implement "--logical" and "--physical" to control following symlinked directories when given on the commandline
Franz Glasner <fzglas.hg@dom66.de>
parents:
124
diff
changeset
|
292 if flags: |
|
129
bdd8ea43074b
Output FLAGS as line "FLAGS = ..." instead of "FLAGS (...)"
Franz Glasner <fzglas.hg@dom66.de>
parents:
128
diff
changeset
|
293 outfp.write(format_bsd_line("FLAGS", ",".join(flags), None, False)) |
|
134
d2c303695fb8
Put a file version number into the output.
Franz Glasner <fzglas.hg@dom66.de>
parents:
133
diff
changeset
|
294 outfp.flush() |
|
125
12d6ec1f8613
Implement "--logical" and "--physical" to control following symlinked directories when given on the commandline
Franz Glasner <fzglas.hg@dom66.de>
parents:
124
diff
changeset
|
295 |
|
150
f84cf853da22
Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents:
149
diff
changeset
|
296 if minimal is None: |
|
f84cf853da22
Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents:
149
diff
changeset
|
297 # Write execution timestamps in POSIX epoch and ISO format |
|
152
46cb438fa520
Truncate the timestamp in the output to seconds
Franz Glasner <fzglas.hg@dom66.de>
parents:
151
diff
changeset
|
298 ts = int(time.time()) |
|
150
f84cf853da22
Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents:
149
diff
changeset
|
299 outfp.write(format_bsd_line("TIMESTAMP", ts, None, False)) |
|
f84cf853da22
Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents:
149
diff
changeset
|
300 ts = (datetime.datetime.utcfromtimestamp(ts)).isoformat("T") |
|
f84cf853da22
Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents:
149
diff
changeset
|
301 outfp.write(format_bsd_line("ISOTIMESTAMP", ts, None, False)) |
|
f84cf853da22
Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents:
149
diff
changeset
|
302 outfp.flush() |
|
134
d2c303695fb8
Put a file version number into the output.
Franz Glasner <fzglas.hg@dom66.de>
parents:
133
diff
changeset
|
303 |
|
150
f84cf853da22
Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents:
149
diff
changeset
|
304 if comment: |
|
f84cf853da22
Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents:
149
diff
changeset
|
305 for line in comment: |
|
f84cf853da22
Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents:
149
diff
changeset
|
306 outfp.write(format_bsd_line("COMMENT", None, line, False)) |
|
f84cf853da22
Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents:
149
diff
changeset
|
307 |
|
f84cf853da22
Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents:
149
diff
changeset
|
308 if minimal is not None: |
|
f84cf853da22
Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents:
149
diff
changeset
|
309 outfp.write( |
|
f84cf853da22
Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents:
149
diff
changeset
|
310 format_bsd_line( |
|
f84cf853da22
Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents:
149
diff
changeset
|
311 "ROOT", None, minimal if minimal else "", False)) |
|
f84cf853da22
Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents:
149
diff
changeset
|
312 else: |
|
f84cf853da22
Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents:
149
diff
changeset
|
313 outfp.write(format_bsd_line("ROOT", None, root, False)) |
|
134
d2c303695fb8
Put a file version number into the output.
Franz Glasner <fzglas.hg@dom66.de>
parents:
133
diff
changeset
|
314 outfp.flush() |
|
128
7c646921a479
Add TIMESTAMP and ISOTIMESTAMP to the output
Franz Glasner <fzglas.hg@dom66.de>
parents:
127
diff
changeset
|
315 |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
316 dir_digests = {} |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
317 |
|
125
12d6ec1f8613
Implement "--logical" and "--physical" to control following symlinked directories when given on the commandline
Franz Glasner <fzglas.hg@dom66.de>
parents:
124
diff
changeset
|
318 if not handle_root_logical and os.path.islink(root): |
|
12d6ec1f8613
Implement "--logical" and "--physical" to control following symlinked directories when given on the commandline
Franz Glasner <fzglas.hg@dom66.de>
parents:
124
diff
changeset
|
319 linktgt = util.fsencode(os.readlink(root)) |
|
12d6ec1f8613
Implement "--logical" and "--physical" to control following symlinked directories when given on the commandline
Franz Glasner <fzglas.hg@dom66.de>
parents:
124
diff
changeset
|
320 linkdgst = algorithm[0]() |
|
132
8b73dca5db97
Encode the link targets as netstrings also before digesting them
Franz Glasner <fzglas.hg@dom66.de>
parents:
131
diff
changeset
|
321 linkdgst.update(b"%d:%s," % (len(linktgt), linktgt)) |
|
125
12d6ec1f8613
Implement "--logical" and "--physical" to control following symlinked directories when given on the commandline
Franz Glasner <fzglas.hg@dom66.de>
parents:
124
diff
changeset
|
322 dir_dgst = algorithm[0]() |
|
137
69f8a3db8fbd
Change the tag for symlinks given on the command line: no "filename" here
Franz Glasner <fzglas.hg@dom66.de>
parents:
136
diff
changeset
|
323 dir_dgst.update(b"1:L,") |
|
125
12d6ec1f8613
Implement "--logical" and "--physical" to control following symlinked directories when given on the commandline
Franz Glasner <fzglas.hg@dom66.de>
parents:
124
diff
changeset
|
324 dir_dgst.update(linkdgst.digest()) |
| 133 | 325 outfp.write( |
| 326 format_bsd_line( | |
| 327 algorithm[1], | |
| 328 dir_dgst.digest(), | |
| 329 "./@", | |
| 330 use_base64)) | |
|
125
12d6ec1f8613
Implement "--logical" and "--physical" to control following symlinked directories when given on the commandline
Franz Glasner <fzglas.hg@dom66.de>
parents:
124
diff
changeset
|
331 outfp.flush() |
|
12d6ec1f8613
Implement "--logical" and "--physical" to control following symlinked directories when given on the commandline
Franz Glasner <fzglas.hg@dom66.de>
parents:
124
diff
changeset
|
332 return |
|
12d6ec1f8613
Implement "--logical" and "--physical" to control following symlinked directories when given on the commandline
Franz Glasner <fzglas.hg@dom66.de>
parents:
124
diff
changeset
|
333 |
|
155
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
334 for top, fsobjects in walk.walk( |
|
131
3a18d71d7c50
Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents:
130
diff
changeset
|
335 root, |
|
3a18d71d7c50
Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents:
130
diff
changeset
|
336 follow_symlinks=follow_directory_symlinks): |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
337 dir_dgst = algorithm[0]() |
|
155
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
338 for fso in fsobjects: |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
339 if fso.is_dir: |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
340 if fso.is_symlink and not follow_directory_symlinks: |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
341 linktgt = util.fsencode(os.readlink(fso.path)) |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
342 linkdgst = algorithm[0]() |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
343 linkdgst.update(b"%d:%s," % (len(linktgt), linktgt)) |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
344 dir_dgst.update(b"1:S,%d:%s," |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
345 % (len(fso.fsname), fso.fsname)) |
|
157
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
346 # no mtime and no mode for symlinks |
|
155
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
347 dir_dgst.update(linkdgst.digest()) |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
348 opath = "/".join(top) + "/" + fso.name if top else fso.name |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
349 outfp.write( |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
350 format_bsd_line( |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
351 algorithm[1], |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
352 linkdgst.digest(), |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
353 "%s/./@" % (opath,), |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
354 use_base64)) |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
355 outfp.flush() |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
356 continue |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
357 # fetch from dir_digests |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
358 dgst = dir_digests[top + (fso.name,)] |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
359 dir_dgst.update(b"1:d,%d:%s," % (len(fso.fsname), fso.fsname)) |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
360 dir_dgst.update(dgst) |
|
158
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
361 if with_metadata_full_mode: |
|
157
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
362 modestr = normalized_mode_str(fso.stat.st_mode) |
|
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
363 if not isinstance(modestr, bytes): |
|
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
364 modestr = modestr.encode("ascii") |
|
158
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
365 dir_dgst.update(b"8:fullmode,%d:%s," |
|
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
366 % (len(modestr), modestr)) |
|
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
367 elif with_metadata_mode: |
|
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
368 modestr = normalized_compatible_mode_str(fso.stat.st_mode) |
|
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
369 if not isinstance(modestr, bytes): |
|
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
370 modestr = modestr.encode("ascii") |
|
157
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
371 dir_dgst.update(b"4:mode,%d:%s," % (len(modestr), modestr)) |
|
155
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
372 else: |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
373 dir_dgst.update(b"1:f,%d:%s," % (len(fso.fsname), fso.fsname)) |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
374 if with_metadata_mtime: |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
375 mtime = datetime.datetime.utcfromtimestamp( |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
376 int(fso.stat.st_mtime)) |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
377 mtime = mtime.isoformat("T") + "Z" |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
378 if not isinstance(mtime, bytes): |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
379 mtime = mtime.encode("ascii") |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
380 dir_dgst.update(b"5:mtime,%d:%s," % (len(mtime), mtime)) |
|
158
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
381 if with_metadata_full_mode: |
|
157
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
382 modestr = normalized_mode_str(fso.stat.st_mode) |
|
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
383 if not isinstance(modestr, bytes): |
|
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
384 modestr = modestr.encode("ascii") |
|
158
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
385 dir_dgst.update(b"8:fullmode,%d:%s," |
|
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
386 % (len(modestr), modestr)) |
|
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
387 elif with_metadata_mode: |
|
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
388 modestr = normalized_compatible_mode_str(fso.stat.st_mode) |
|
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
389 if not isinstance(modestr, bytes): |
|
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
390 modestr = modestr.encode("ascii") |
|
157
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
391 dir_dgst.update(b"4:mode,%d:%s," % (len(modestr), modestr)) |
|
155
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
392 dgst = digest.compute_digest_file( |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
393 algorithm[0], fso.path, use_mmap=use_mmap) |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
394 dir_dgst.update(dgst) |
|
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
395 opath = "/".join(top) + "/" + fso.name if top else fso.name |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
396 outfp.write( |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
397 format_bsd_line( |
|
155
bf74ce3c968d
When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents:
153
diff
changeset
|
398 algorithm[1], dgst, opath, use_base64)) |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
399 outfp.flush() |
|
127
6a50d02fe0ca
Change the filename output: make it more consistent
Franz Glasner <fzglas.hg@dom66.de>
parents:
125
diff
changeset
|
400 opath = "/".join(top) + "/" if top else "" |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
401 outfp.write(format_bsd_line( |
|
127
6a50d02fe0ca
Change the filename output: make it more consistent
Franz Glasner <fzglas.hg@dom66.de>
parents:
125
diff
changeset
|
402 algorithm[1], dir_dgst.digest(), opath, use_base64)) |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
403 outfp.flush() |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
404 dir_digests[top] = dir_dgst.digest() |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
405 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
406 |
|
158
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
407 def normalized_compatible_mode_str(mode): |
|
157
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
408 # XXX FIXME: Windows and "executable" |
|
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
409 modebits = stat.S_IMODE(mode) |
|
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
410 modestr = "%o" % (modebits,) |
|
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
411 if not modestr.startswith("0"): |
|
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
412 modestr = "0" + modestr |
|
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
413 return modestr |
|
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
414 |
|
27d1aaf5fe39
Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
415 |
|
158
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
416 def normalized_mode_str(mode): |
|
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
417 modestr = "%o" % (mode,) |
|
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
418 if not modestr.startswith("0"): |
|
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
419 modestr = "0" + modestr |
|
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
420 return modestr |
|
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
421 |
|
d8cdd1985d43
Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents:
157
diff
changeset
|
422 |
|
128
7c646921a479
Add TIMESTAMP and ISOTIMESTAMP to the output
Franz Glasner <fzglas.hg@dom66.de>
parents:
127
diff
changeset
|
423 def format_bsd_line(digestname, value, filename, use_base64): |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
424 ls = os.linesep if isinstance(os.linesep, bytes) \ |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
425 else os.linesep.encode("utf-8") |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
426 if not isinstance(digestname, bytes): |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
427 digestname = digestname.encode("ascii") |
|
128
7c646921a479
Add TIMESTAMP and ISOTIMESTAMP to the output
Franz Glasner <fzglas.hg@dom66.de>
parents:
127
diff
changeset
|
428 if digestname == b"TIMESTAMP": |
|
129
bdd8ea43074b
Output FLAGS as line "FLAGS = ..." instead of "FLAGS (...)"
Franz Glasner <fzglas.hg@dom66.de>
parents:
128
diff
changeset
|
429 assert filename is None |
|
128
7c646921a479
Add TIMESTAMP and ISOTIMESTAMP to the output
Franz Glasner <fzglas.hg@dom66.de>
parents:
127
diff
changeset
|
430 return b"TIMESTAMP = %d%s" % (value, ls) |
|
134
d2c303695fb8
Put a file version number into the output.
Franz Glasner <fzglas.hg@dom66.de>
parents:
133
diff
changeset
|
431 if digestname in (b"ISOTIMESTAMP", b"FLAGS", b"VERSION"): |
|
129
bdd8ea43074b
Output FLAGS as line "FLAGS = ..." instead of "FLAGS (...)"
Franz Glasner <fzglas.hg@dom66.de>
parents:
128
diff
changeset
|
432 assert filename is None |
|
128
7c646921a479
Add TIMESTAMP and ISOTIMESTAMP to the output
Franz Glasner <fzglas.hg@dom66.de>
parents:
127
diff
changeset
|
433 if not isinstance(value, bytes): |
|
7c646921a479
Add TIMESTAMP and ISOTIMESTAMP to the output
Franz Glasner <fzglas.hg@dom66.de>
parents:
127
diff
changeset
|
434 value = value.encode("ascii") |
|
129
bdd8ea43074b
Output FLAGS as line "FLAGS = ..." instead of "FLAGS (...)"
Franz Glasner <fzglas.hg@dom66.de>
parents:
128
diff
changeset
|
435 return b"%s = %s%s" % (digestname, value, ls) |
|
bdd8ea43074b
Output FLAGS as line "FLAGS = ..." instead of "FLAGS (...)"
Franz Glasner <fzglas.hg@dom66.de>
parents:
128
diff
changeset
|
436 assert filename is not None |
|
135
dbf27681a1f6
Allow to put comments into the output with "--comment"
Franz Glasner <fzglas.hg@dom66.de>
parents:
134
diff
changeset
|
437 if digestname == b"COMMENT": |
|
dbf27681a1f6
Allow to put comments into the output with "--comment"
Franz Glasner <fzglas.hg@dom66.de>
parents:
134
diff
changeset
|
438 if not isinstance(filename, bytes): |
|
dbf27681a1f6
Allow to put comments into the output with "--comment"
Franz Glasner <fzglas.hg@dom66.de>
parents:
134
diff
changeset
|
439 filename = filename.encode("utf-8") |
|
dbf27681a1f6
Allow to put comments into the output with "--comment"
Franz Glasner <fzglas.hg@dom66.de>
parents:
134
diff
changeset
|
440 return b"COMMENT (%s)%s" % (filename, ls) |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
441 if not isinstance(filename, bytes): |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
442 filename = util.fsencode(filename) |
|
128
7c646921a479
Add TIMESTAMP and ISOTIMESTAMP to the output
Franz Glasner <fzglas.hg@dom66.de>
parents:
127
diff
changeset
|
443 if value is None: |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
444 return b"%s (%s)%s" % (digestname, filename, ls) |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
445 if use_base64: |
|
128
7c646921a479
Add TIMESTAMP and ISOTIMESTAMP to the output
Franz Glasner <fzglas.hg@dom66.de>
parents:
127
diff
changeset
|
446 value = base64.b64encode(value) |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
447 else: |
|
128
7c646921a479
Add TIMESTAMP and ISOTIMESTAMP to the output
Franz Glasner <fzglas.hg@dom66.de>
parents:
127
diff
changeset
|
448 value = binascii.hexlify(value) |
|
130
d5621028ce39
Change again the filename tags to be used for symlinks
Franz Glasner <fzglas.hg@dom66.de>
parents:
129
diff
changeset
|
449 if filename != b"./@": |
|
127
6a50d02fe0ca
Change the filename output: make it more consistent
Franz Glasner <fzglas.hg@dom66.de>
parents:
125
diff
changeset
|
450 filename = util.normalize_filename(filename, True) |
|
128
7c646921a479
Add TIMESTAMP and ISOTIMESTAMP to the output
Franz Glasner <fzglas.hg@dom66.de>
parents:
127
diff
changeset
|
451 return b"%s (%s) = %s%s" % (digestname, filename, value, ls) |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
452 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
453 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
454 if __name__ == "__main__": |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
455 sys.exit(main()) |
