annotate cutils/treesum.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 7f5d05a625fd
children dac26a2d9de5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
176
7f5d05a625fd Implement preconditions for some debug logging
Franz Glasner <fzglas.hg@dom66.de>
parents: 174
diff changeset
20 import logging
124
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
21 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
22 import stat
124
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
23 import sys
128
7c646921a479 Add TIMESTAMP and ISOTIMESTAMP to the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 127
diff changeset
24 import time
124
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
25
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
26 from . import (__version__, __revision__)
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
27 from . import util
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
28 from .util import cm
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
29 from .util import digest
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
30 from .util import walk
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
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
33 def main(argv=None):
142
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
34
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
35 def _populate_generate_arguments(gp):
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
36 """Use to populate command aliases.
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
37
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
38 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
39 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
40
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
41 """
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
42 gp.add_argument(
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
43 "--algorithm", "-a", action="store", type=util.argv2algo,
172
804a823c63f5 Now the selection of the default algorithm depends on availiability in hashlib
Franz Glasner <fzglas.hg@dom66.de>
parents: 170
diff changeset
44 help="1 (aka sha1), 224, 256 (aka sha256), 384, 512 (aka sha512), "
142
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
45 "3 (alias for sha3-512), 3-224, 3-256, 3-384, 3-512, "
172
804a823c63f5 Now the selection of the default algorithm depends on availiability in hashlib
Franz Glasner <fzglas.hg@dom66.de>
parents: 170
diff changeset
46 "blake2b, blake2b-256, blake2s, "
142
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
47 "blake2 (alias for blake2b), "
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
48 "blake2-256 (alias for blake2b-256), "
172
804a823c63f5 Now the selection of the default algorithm depends on availiability in hashlib
Franz Glasner <fzglas.hg@dom66.de>
parents: 170
diff changeset
49 "md5. "
804a823c63f5 Now the selection of the default algorithm depends on availiability in hashlib
Franz Glasner <fzglas.hg@dom66.de>
parents: 170
diff changeset
50 "The default depends on availability in hashlib: "
804a823c63f5 Now the selection of the default algorithm depends on availiability in hashlib
Franz Glasner <fzglas.hg@dom66.de>
parents: 170
diff changeset
51 "blake2b-256, sha256 or sha1.")
142
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 "--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
54 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
55 gp.add_argument(
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
56 "--base64", action="store_true",
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
57 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
58 "(OpenBSD).")
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
59 gp.add_argument(
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
60 "--comment", action="append", default=[],
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
61 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
62 "Can be given more than once.")
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
63 gp.add_argument(
176
7f5d05a625fd Implement preconditions for some debug logging
Franz Glasner <fzglas.hg@dom66.de>
parents: 174
diff changeset
64 "--debug", action="store_true",
7f5d05a625fd Implement preconditions for some debug logging
Franz Glasner <fzglas.hg@dom66.de>
parents: 174
diff changeset
65 help="Activate debug logging to stderr")
7f5d05a625fd Implement preconditions for some debug logging
Franz Glasner <fzglas.hg@dom66.de>
parents: 174
diff changeset
66 gp.add_argument(
142
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
67 "--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
68 dest="follow_directory_symlinks",
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
69 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
70 "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
71 "\"--logical\" or \"--physical\" for arguments given "
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
72 "directly on the command line")
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
73 gp.add_argument(
158
d8cdd1985d43 Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents: 157
diff changeset
74 "--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
75 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
76 "computing directory digests. "
d8cdd1985d43 Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents: 157
diff changeset
77 "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
78 "considered.")
d8cdd1985d43 Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents: 157
diff changeset
79 gp.add_argument(
142
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
80 "--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
81 default=None,
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
82 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
83 " 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
84 " 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
85 gp.add_argument(
150
f84cf853da22 Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents: 149
diff changeset
86 "--minimal", nargs="?", const="", default=None, metavar="TAG",
f84cf853da22 Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents: 149
diff changeset
87 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
88 "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
89 gp.add_argument(
142
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
90 "--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
91 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
92 "automatically from the filesize.")
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
93 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
94 "--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
95 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
96 "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
97 "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
98 "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
99 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
100 "--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
101 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
102 "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
103 "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
104 gp.add_argument(
142
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
105 "--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
106 help="Dont use mmap.")
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
107 gp.add_argument(
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
108 "--output", "-o", action="store", metavar="OUTPUT",
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
109 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
110 "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
111 gp.add_argument(
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
112 "--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
113 default=None,
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
114 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
115 "arguments. This is the default.")
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
116 gp.add_argument(
168
bcc4441cf216 Implement "--print-size" to print file and accumulated directory sizes also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 161
diff changeset
117 "--print-size", action="store_true",
bcc4441cf216 Implement "--print-size" to print file and accumulated directory sizes also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 161
diff changeset
118 help="""Print the size of a file or the accumulated sizes of
bcc4441cf216 Implement "--print-size" to print file and accumulated directory sizes also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 161
diff changeset
119 directory content into the output also.
bcc4441cf216 Implement "--print-size" to print file and accumulated directory sizes also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 161
diff changeset
120 The size is not considered when computing digests. For symbolic links
bcc4441cf216 Implement "--print-size" to print file and accumulated directory sizes also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 161
diff changeset
121 the size is not printed also.""")
bcc4441cf216 Implement "--print-size" to print file and accumulated directory sizes also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 161
diff changeset
122 gp.add_argument(
170
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
123 "--size-only", action="store_true",
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
124 help="""Print only the size of files and for each directory its
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
125 accumulated directory size. Digests are not computed.""")
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
126 gp.add_argument(
142
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
127 "directories", nargs="*", metavar="DIRECTORY")
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
128
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
129 parser = argparse.ArgumentParser(
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
130 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
131 fromfile_prefix_chars='@',
17d6e760143f Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents: 147
diff changeset
132 add_help=False)
142
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
133
148
17d6e760143f Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents: 147
diff changeset
134 #
153
3505406ef9f3 Comment
Franz Glasner <fzglas.hg@dom66.de>
parents: 152
diff changeset
135 # Global options for all sub-commands.
3505406ef9f3 Comment
Franz Glasner <fzglas.hg@dom66.de>
parents: 152
diff changeset
136 # 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
137 #
17d6e760143f Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents: 147
diff changeset
138 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
139 gparser.add_argument(
142
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
140 "-v", "--version", action="version",
148
17d6e760143f Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents: 147
diff changeset
141 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
142 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
143 gparser.add_argument(
17d6e760143f Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents: 147
diff changeset
144 "-h", "--help", action="help",
17d6e760143f Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents: 147
diff changeset
145 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
146
148
17d6e760143f Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents: 147
diff changeset
147 #
17d6e760143f Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents: 147
diff changeset
148 # Subcommands
17d6e760143f Optimize help / usage output for the global options.
Franz Glasner <fzglas.hg@dom66.de>
parents: 147
diff changeset
149 #
142
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
150 subparsers = parser.add_subparsers(
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
151 dest="subcommand",
145
073e0faea599 Optimize help output for subcommands
Franz Glasner <fzglas.hg@dom66.de>
parents: 144
diff changeset
152 title="Commands",
142
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
153 description="This tool uses subcommands. "
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
154 "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
155 "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
156 "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
157 "is listed below:",
145
073e0faea599 Optimize help output for subcommands
Franz Glasner <fzglas.hg@dom66.de>
parents: 144
diff changeset
158 metavar="COMMAND")
124
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
159
142
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
160 genparser = subparsers.add_parser(
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
161 "generate",
143
492975912cad Wording
Franz Glasner <fzglas.hg@dom66.de>
parents: 142
diff changeset
162 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
163 description="Generate checksums for directory trees")
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
164 _populate_generate_arguments(genparser)
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
165 # And an alias for "generate"
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
166 genparser2 = subparsers.add_parser(
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
167 "gen",
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
168 help="Alias for \"generate\"",
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
169 description="Generate checksums for directory trees. "
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
170 "This is an alias to \"generate\".")
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
171 _populate_generate_arguments(genparser2)
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
172
147
ed35f3c9e2b5 Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents: 146
diff changeset
173 hparser = subparsers.add_parser(
ed35f3c9e2b5 Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents: 146
diff changeset
174 "help",
ed35f3c9e2b5 Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents: 146
diff changeset
175 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
176 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
177 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
178
ed35f3c9e2b5 Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents: 146
diff changeset
179 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
180 "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
181 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
182 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
183
147
ed35f3c9e2b5 Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents: 146
diff changeset
184 # 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
185 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
186
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
187 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
188 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
189 file=sys.stdout)
174
fc1055878775 Use "return 0" instead of "sys.exit(0)" when printing version and help#
Franz Glasner <fzglas.hg@dom66.de>
parents: 173
diff changeset
190 return 0
147
ed35f3c9e2b5 Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents: 146
diff changeset
191 if opts.subcommand == "help":
ed35f3c9e2b5 Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents: 146
diff changeset
192 if not opts.help_command:
ed35f3c9e2b5 Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents: 146
diff changeset
193 parser.print_help()
ed35f3c9e2b5 Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents: 146
diff changeset
194 else:
ed35f3c9e2b5 Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents: 146
diff changeset
195 if opts.help_command == "generate":
ed35f3c9e2b5 Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents: 146
diff changeset
196 genparser.print_help()
ed35f3c9e2b5 Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents: 146
diff changeset
197 elif opts.help_command == "gen":
ed35f3c9e2b5 Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents: 146
diff changeset
198 genparser2.print_help()
ed35f3c9e2b5 Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents: 146
diff changeset
199 elif opts.help_command == "version":
ed35f3c9e2b5 Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents: 146
diff changeset
200 vparser.print_help()
ed35f3c9e2b5 Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents: 146
diff changeset
201 elif opts.help_command == "help":
ed35f3c9e2b5 Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents: 146
diff changeset
202 hparser.print_help()
ed35f3c9e2b5 Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents: 146
diff changeset
203 else:
ed35f3c9e2b5 Add also a "help" subcommand to "treesum".
Franz Glasner <fzglas.hg@dom66.de>
parents: 146
diff changeset
204 parser.print_help()
174
fc1055878775 Use "return 0" instead of "sys.exit(0)" when printing version and help#
Franz Glasner <fzglas.hg@dom66.de>
parents: 173
diff changeset
205 return 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
206
146
7d8df8311e3b Optimize argument parsing for the "version" command
Franz Glasner <fzglas.hg@dom66.de>
parents: 145
diff changeset
207 # Reparse strictly
7d8df8311e3b Optimize argument parsing for the "version" command
Franz Glasner <fzglas.hg@dom66.de>
parents: 145
diff changeset
208 opts = parser.parse_args(args=argv)
7d8df8311e3b Optimize argument parsing for the "version" command
Franz Glasner <fzglas.hg@dom66.de>
parents: 145
diff changeset
209
176
7f5d05a625fd Implement preconditions for some debug logging
Franz Glasner <fzglas.hg@dom66.de>
parents: 174
diff changeset
210 # Minimal logging -- just for debugging - not for more "normal" use
7f5d05a625fd Implement preconditions for some debug logging
Franz Glasner <fzglas.hg@dom66.de>
parents: 174
diff changeset
211 logging.basicConfig(
7f5d05a625fd Implement preconditions for some debug logging
Franz Glasner <fzglas.hg@dom66.de>
parents: 174
diff changeset
212 level=logging.DEBUG if opts.debug else logging.WARNING,
7f5d05a625fd Implement preconditions for some debug logging
Franz Glasner <fzglas.hg@dom66.de>
parents: 174
diff changeset
213 stream=sys.stderr,
7f5d05a625fd Implement preconditions for some debug logging
Franz Glasner <fzglas.hg@dom66.de>
parents: 174
diff changeset
214 format="[%(asctime)s][%(levelname)s][%(process)d:%(name)s] %(message)s"
7f5d05a625fd Implement preconditions for some debug logging
Franz Glasner <fzglas.hg@dom66.de>
parents: 174
diff changeset
215 )
7f5d05a625fd Implement preconditions for some debug logging
Franz Glasner <fzglas.hg@dom66.de>
parents: 174
diff changeset
216 logging.captureWarnings(True)
7f5d05a625fd Implement preconditions for some debug logging
Franz Glasner <fzglas.hg@dom66.de>
parents: 174
diff changeset
217
124
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
218 return treesum(opts)
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
219
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
220
142
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
221 def gen_generate_opts(directories=[],
172
804a823c63f5 Now the selection of the default algorithm depends on availiability in hashlib
Franz Glasner <fzglas.hg@dom66.de>
parents: 170
diff changeset
222 algorithm=util.default_algotag(),
142
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
223 append_output=False,
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
224 base64=False,
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
225 comment=[],
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
226 follow_directory_symlinks=False,
158
d8cdd1985d43 Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents: 157
diff changeset
227 full_mode=False,
142
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
228 logical=None,
150
f84cf853da22 Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents: 149
diff changeset
229 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
230 mode=False,
142
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
231 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
232 mtime=False,
168
bcc4441cf216 Implement "--print-size" to print file and accumulated directory sizes also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 161
diff changeset
233 output=None,
170
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
234 print_size=False,
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
235 size_only=False):
131
3a18d71d7c50 Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents: 130
diff changeset
236 opts = argparse.Namespace(
3a18d71d7c50 Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents: 130
diff changeset
237 directories=directories,
172
804a823c63f5 Now the selection of the default algorithm depends on availiability in hashlib
Franz Glasner <fzglas.hg@dom66.de>
parents: 170
diff changeset
238 algorithm=util.argv2algo(algorithm),
131
3a18d71d7c50 Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents: 130
diff changeset
239 append_output=append_output,
3a18d71d7c50 Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents: 130
diff changeset
240 base64=base64,
135
dbf27681a1f6 Allow to put comments into the output with "--comment"
Franz Glasner <fzglas.hg@dom66.de>
parents: 134
diff changeset
241 comment=comment,
131
3a18d71d7c50 Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents: 130
diff changeset
242 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
243 logical=logical,
150
f84cf853da22 Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents: 149
diff changeset
244 minimal=minimal,
131
3a18d71d7c50 Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents: 130
diff changeset
245 mmap=mmap,
158
d8cdd1985d43 Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents: 157
diff changeset
246 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
247 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
248 metadata_mtime=mtime,
168
bcc4441cf216 Implement "--print-size" to print file and accumulated directory sizes also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 161
diff changeset
249 output=output,
170
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
250 print_size=print_size,
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
251 size_only=size_only)
124
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
252 return opts
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
253
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 def treesum(opts):
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
256 # 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
257 if opts.subcommand in ("generate", "gen"):
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
258 return generate_treesum(opts)
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
259 else:
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
260 raise RuntimeError(
71747e45b52c Prepare for using subcommands in "treesum.py".
Franz Glasner <fzglas.hg@dom66.de>
parents: 137
diff changeset
261 "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
262
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
263
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
264 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
265 # 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
266 if not opts.algorithm:
172
804a823c63f5 Now the selection of the default algorithm depends on availiability in hashlib
Franz Glasner <fzglas.hg@dom66.de>
parents: 170
diff changeset
267 opts.algorithm = util.argv2algo(util.default_algotag())
124
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
268 if not opts.directories:
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
269 opts.directories.append(".")
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
270
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
271 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
272 if hasattr(sys.stdout, "buffer"):
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
273 out_cm = cm.nullcontext(sys.stdout.buffer)
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
274 else:
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
275 out_cm = cm.nullcontext(sys.stdout)
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
276 else:
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
277 if opts.append_output:
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
278 out_cm = open(opts.output, "ab")
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
279 else:
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
280 out_cm = open(opts.output, "wb")
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
281
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
282 with out_cm as outfp:
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
283 for d in opts.directories:
177
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
284
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
285 V1DirectoryTreesumGenerator(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
286 opts.algorithm, opts.mmap, opts.base64, opts.logical,
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
287 opts.follow_directory_symlinks,
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
288 opts.metadata_mode,
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
289 opts.metadata_full_mode,
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
290 opts.metadata_mtime,
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
291 opts.size_only,
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
292 opts.print_size,
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
293 minimal=opts.minimal).generate(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
294 outfp, d, comment=opts.comment)
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
295
124
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
296 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
297 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
298 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
299 opts.metadata_mode,
158
d8cdd1985d43 Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents: 157
diff changeset
300 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
301 opts.metadata_mtime,
170
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
302 opts.size_only,
168
bcc4441cf216 Implement "--print-size" to print file and accumulated directory sizes also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 161
diff changeset
303 opts.print_size,
150
f84cf853da22 Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents: 149
diff changeset
304 minimal=opts.minimal,
135
dbf27681a1f6 Allow to put comments into the output with "--comment"
Franz Glasner <fzglas.hg@dom66.de>
parents: 134
diff changeset
305 comment=opts.comment)
124
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
306
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
307
177
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
308 class V1DirectoryTreesumGenerator(object):
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
309
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
310 def __init__(self, algorithm, use_mmap, use_base64,
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
311 handle_root_logical, follow_directory_symlinks,
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
312 with_metadata_mode, with_metadata_full_mode,
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
313 with_metadata_mtime, size_only, print_size,
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
314 minimal=None,):
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
315 super(V1DirectoryTreesumGenerator, self).__init__()
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
316 self._algorithm = algorithm
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
317 self._use_mmap = use_mmap
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
318 self._use_base64 = use_base64
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
319 self._handle_root_logical = handle_root_logical
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
320 self._follow_directory_symlinks = follow_directory_symlinks
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
321 self._with_metadata_mode = with_metadata_mode
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
322 self._with_metadata_full_mode = with_metadata_full_mode
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
323 self._with_metadata_mtime = with_metadata_mtime
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
324 self._size_only = size_only
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
325 self._print_size = print_size
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
326 self._minimal = minimal
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
327
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
328 def generate(self, outfp, root, comment=None):
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
329 """
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
330
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
331 :param outfp: a *binary* file with a "write()" and a "flush()" method
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
332
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
333 """
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
334 self._outfp = outfp
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
335 self._outfp.write(format_bsd_line("VERSION", "1", None, False))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
336 self._outfp.flush()
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
337
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
338 #
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
339 # Note: Given non-default flags that are relevant for
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
340 # directory traversal.
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
341 #
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
342 flags = []
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
343 if self._with_metadata_full_mode:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
344 flags.append("with-metadata-fullmode")
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
345 elif self._with_metadata_mode:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
346 flags.append("with-metadata-mode")
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
347 if self._with_metadata_mtime:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
348 flags.append("with-metadata-mtime")
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
349 if self._handle_root_logical:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
350 flags.append("logical")
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
351 if self._follow_directory_symlinks:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
352 flags.append("follow-directory-symlinks")
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
353 if self._size_only:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
354 flags.append("size-only")
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
355 else:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
356 if self._print_size:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
357 flags.append("print-size")
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
358 if flags:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
359 flags.sort()
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
360 self._outfp.write(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
361 format_bsd_line("FLAGS", ",".join(flags), None, False))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
362
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
363 if self._minimal is None:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
364 # Write execution timestamps in POSIX epoch and ISO format
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
365 ts = int(time.time())
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
366 self._outfp.write(format_bsd_line("TIMESTAMP", ts, None, False))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
367 ts = (datetime.datetime.utcfromtimestamp(ts)).isoformat("T")
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
368 self._outfp.write(format_bsd_line("ISOTIMESTAMP", ts, None, False))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
369
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
370 if comment:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
371 for line in comment:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
372 self._outfp.write(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
373 format_bsd_line("COMMENT", None, line, False))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
374
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
375 if self._minimal is not None:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
376 self._outfp.write(format_bsd_line(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
377 "ROOT", None, self._minimal if self._minimal else "", False))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
378 else:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
379 self._outfp.write(format_bsd_line("ROOT", None, root, False))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
380 self._outfp.flush()
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
381
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
382 if not self._handle_root_logical and os.path.islink(root):
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
383 linktgt = util.fsencode(os.readlink(root))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
384 linkdgst = self._algorithm[0]()
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
385 linkdgst.update(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
386 util.interpolate_bytes(b"%d:%s,", len(linktgt), linktgt))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
387 dir_dgst = self._algorithm[0]()
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
388 dir_dgst.update(b"1:L,")
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
389 dir_dgst.update(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
390 util.interpolate_bytes(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
391 b"%d:%s,", len(linkdgst.digest()), linkdgst.digest()))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
392 if self._size_only:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
393 self._outfp.write(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
394 format_bsd_line(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
395 "SIZE",
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
396 None,
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
397 "./@",
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
398 False,
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
399 0))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
400 else:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
401 self._outfp.write(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
402 format_bsd_line(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
403 self._algorithm[1],
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
404 dir_dgst.digest(),
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
405 "./@",
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
406 self._use_base64))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
407 self._outfp.flush()
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
408 return
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
409
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
410 self._generate(os.path.normpath(root), tuple())
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
411
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
412 def _generate(self, root, top):
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
413 logging.debug("Handling %s/%r", root, top)
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
414 path = os.path.join(root, *top) if top else root
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
415 with walk.ScanDir(path) as dirscan:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
416 fsobjects = list(dirscan)
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
417 fsobjects.sort(key=walk.WalkDirEntry.sort_key)
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
418 dir_dgst = self._algorithm[0]()
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
419 dir_size = 0
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
420 for fso in fsobjects:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
421 if fso.is_dir:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
422 if fso.is_symlink and not self._follow_directory_symlinks:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
423 linktgt = util.fsencode(os.readlink(fso.path))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
424 linkdgst = self._algorithm[0]()
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
425 linkdgst.update(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
426 util.interpolate_bytes(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
427 b"%d:%s,", len(linktgt), linktgt))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
428 dir_dgst.update(util.interpolate_bytes(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
429 b"1:S,%d:%s,", len(fso.fsname), fso.fsname))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
430 #
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
431 # - no mtime and no mode for symlinks
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
432 # - also does not count for dir_size
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
433 #
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
434 dir_dgst.update(util.interpolate_bytes(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
435 b"%d:%s,",
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
436 len(linkdgst.digest()), linkdgst.digest()))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
437 opath = "/".join(top) + "/" + fso.name if top else fso.name
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
438 if self._size_only:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
439 self._outfp.write(format_bsd_line(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
440 "SIZE", None, "%s/./@" % (opath,), False, 0))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
441 else:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
442 self._outfp.write(format_bsd_line(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
443 self._algorithm[1],
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
444 linkdgst.digest(),
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
445 "%s/./@" % (opath,),
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
446 self._use_base64))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
447 self._outfp.flush()
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
448 else:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
449 #
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
450 # Follow the symlink to dir or handle a "real" directory
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
451 #
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
452
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
453 # Get subdir data from recursing into it
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
454 sub_dir_dgst, sub_dir_size = self._generate(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
455 root, top + (fso.name, ))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
456
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
457 dir_size += sub_dir_size
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
458 dir_dgst.update(util.interpolate_bytes(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
459 b"1:d,%d:%s,", len(fso.fsname), fso.fsname))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
460 dir_dgst.update(util.interpolate_bytes(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
461 b"%d:%s,", len(sub_dir_dgst), sub_dir_dgst))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
462 if self._with_metadata_full_mode:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
463 modestr = normalized_mode_str(fso.stat.st_mode)
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
464 if not isinstance(modestr, bytes):
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
465 modestr = modestr.encode("ascii")
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
466 dir_dgst.update(util.interpolate_bytes(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
467 b"8:fullmode,%d:%s,", len(modestr), modestr))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
468 elif self._with_metadata_mode:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
469 modestr = normalized_compatible_mode_str(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
470 fso.stat.st_mode)
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
471 if not isinstance(modestr, bytes):
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
472 modestr = modestr.encode("ascii")
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
473 dir_dgst.update(util.interpolate_bytes(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
474 b"4:mode,%d:%s,", len(modestr), modestr))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
475 else:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
476 dir_dgst.update(util.interpolate_bytes(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
477 b"1:f,%d:%s,", len(fso.fsname), fso.fsname))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
478 dir_size += fso.stat.st_size
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
479 if self._with_metadata_mtime:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
480 mtime = datetime.datetime.utcfromtimestamp(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
481 int(fso.stat.st_mtime))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
482 mtime = mtime.isoformat("T") + "Z"
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
483 if not isinstance(mtime, bytes):
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
484 mtime = mtime.encode("ascii")
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
485 dir_dgst.update(util.interpolate_bytes(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
486 b"5:mtime,%d:%s,", len(mtime), mtime))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
487 if self._with_metadata_full_mode:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
488 modestr = normalized_mode_str(fso.stat.st_mode)
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
489 if not isinstance(modestr, bytes):
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
490 modestr = modestr.encode("ascii")
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
491 dir_dgst.update(util.interpolate_bytes(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
492 b"8:fullmode,%d:%s,", len(modestr), modestr))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
493 elif self._with_metadata_mode:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
494 modestr = normalized_compatible_mode_str(fso.stat.st_mode)
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
495 if not isinstance(modestr, bytes):
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
496 modestr = modestr.encode("ascii")
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
497 dir_dgst.update(util.interpolate_bytes(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
498 b"4:mode,%d:%s,", len(modestr), modestr))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
499 if not self._size_only:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
500 dgst = digest.compute_digest_file(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
501 self._algorithm[0], fso.path, use_mmap=self._use_mmap)
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
502 dir_dgst.update(util.interpolate_bytes(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
503 b"%d:%s,", len(dgst), dgst))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
504 opath = "/".join(top) + "/" + fso.name if top else fso.name
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
505 if self._size_only:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
506 self._outfp.write(format_bsd_line(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
507 "SIZE", None, opath, False, fso.stat.st_size))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
508 else:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
509 if self._print_size:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
510 self._outfp.write(format_bsd_line(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
511 self._algorithm[1], dgst, opath, self._use_base64,
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
512 fso.stat.st_size))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
513 else:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
514 self._outfp.write(format_bsd_line(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
515 self._algorithm[1], dgst, opath,
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
516 self._use_base64))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
517 self._outfp.flush()
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
518
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
519 opath = "/".join(top) + "/" if top else ""
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
520 if self._size_only:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
521 self._outfp.write(format_bsd_line(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
522 "SIZE", None, opath, False, dir_size))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
523 else:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
524 if self._print_size:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
525 self._outfp.write(format_bsd_line(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
526 self._algorithm[1], dir_dgst.digest(), opath,
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
527 self._use_base64, dir_size))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
528 else:
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
529 self._outfp.write(format_bsd_line(
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
530 self._algorithm[1], dir_dgst.digest(), opath,
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
531 self._use_base64))
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
532 self._outfp.flush()
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
533 return (dir_dgst.digest(), dir_size)
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
534
089c40240061 Add an alternate implementation for generating directory tree digests:
Franz Glasner <fzglas.hg@dom66.de>
parents: 176
diff changeset
535
124
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
536 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
537 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
538 follow_directory_symlinks, with_metadata_mode, with_metadata_full_mode,
170
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
539 with_metadata_mtime, size_only, print_size,
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
540 minimal=None, comment=None):
124
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
541 """
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
542
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
543 :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
544
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
545 """
134
d2c303695fb8 Put a file version number into the output.
Franz Glasner <fzglas.hg@dom66.de>
parents: 133
diff changeset
546 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
547 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
548
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
549 # 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
550 flags = []
159
5c23ad9a3f8f FIX: Add --mode and --full-mode to the "FLAGS" output
Franz Glasner <fzglas.hg@dom66.de>
parents: 158
diff changeset
551 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
552 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
553 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
554 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
555 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
556 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
557 if handle_root_logical:
129
bdd8ea43074b Output FLAGS as line "FLAGS = ..." instead of "FLAGS (...)"
Franz Glasner <fzglas.hg@dom66.de>
parents: 128
diff changeset
558 flags.append("logical")
131
3a18d71d7c50 Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents: 130
diff changeset
559 if follow_directory_symlinks:
3a18d71d7c50 Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents: 130
diff changeset
560 flags.append("follow-directory-symlinks")
170
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
561 if size_only:
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
562 flags.append("size-only")
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
563 else:
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
564 if print_size:
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
565 flags.append("print-size")
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
566 if flags:
169
91b8b2a8aebc Sort flags in the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 168
diff changeset
567 flags.sort()
129
bdd8ea43074b Output FLAGS as line "FLAGS = ..." instead of "FLAGS (...)"
Franz Glasner <fzglas.hg@dom66.de>
parents: 128
diff changeset
568 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
569 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
570
150
f84cf853da22 Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents: 149
diff changeset
571 if minimal is None:
f84cf853da22 Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents: 149
diff changeset
572 # 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
573 ts = int(time.time())
150
f84cf853da22 Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents: 149
diff changeset
574 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
575 ts = (datetime.datetime.utcfromtimestamp(ts)).isoformat("T")
f84cf853da22 Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents: 149
diff changeset
576 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
577 outfp.flush()
134
d2c303695fb8 Put a file version number into the output.
Franz Glasner <fzglas.hg@dom66.de>
parents: 133
diff changeset
578
150
f84cf853da22 Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents: 149
diff changeset
579 if comment:
f84cf853da22 Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents: 149
diff changeset
580 for line in comment:
f84cf853da22 Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents: 149
diff changeset
581 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
582
f84cf853da22 Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents: 149
diff changeset
583 if minimal is not None:
f84cf853da22 Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents: 149
diff changeset
584 outfp.write(
f84cf853da22 Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents: 149
diff changeset
585 format_bsd_line(
f84cf853da22 Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents: 149
diff changeset
586 "ROOT", None, minimal if minimal else "", False))
f84cf853da22 Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents: 149
diff changeset
587 else:
f84cf853da22 Implement "--minimal [TAG]" for treesum.
Franz Glasner <fzglas.hg@dom66.de>
parents: 149
diff changeset
588 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
589 outfp.flush()
128
7c646921a479 Add TIMESTAMP and ISOTIMESTAMP to the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 127
diff changeset
590
124
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
591 dir_digests = {}
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
592
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
593 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
594 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
595 linkdgst = algorithm[0]()
173
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
596 linkdgst.update(
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
597 util.interpolate_bytes(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
598 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
599 dir_dgst.update(b"1:L,")
161
df927ada9a37 In directory digests: include the octet-length of the checksums also
Franz Glasner <fzglas.hg@dom66.de>
parents: 160
diff changeset
600 dir_dgst.update(
173
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
601 util.interpolate_bytes(
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
602 b"%d:%s,", len(linkdgst.digest()), linkdgst.digest()))
170
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
603 if size_only:
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
604 outfp.write(
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
605 format_bsd_line(
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
606 "SIZE",
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
607 None,
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
608 "./@",
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
609 False,
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
610 0))
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
611 else:
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
612 outfp.write(
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
613 format_bsd_line(
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
614 algorithm[1],
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
615 dir_dgst.digest(),
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
616 "./@",
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
617 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
618 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
619 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
620
155
bf74ce3c968d When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents: 153
diff changeset
621 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
622 root,
3a18d71d7c50 Implement --follow-directory-symlinks when walking a directory tree
Franz Glasner <fzglas.hg@dom66.de>
parents: 130
diff changeset
623 follow_symlinks=follow_directory_symlinks):
124
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
624 dir_dgst = algorithm[0]()
168
bcc4441cf216 Implement "--print-size" to print file and accumulated directory sizes also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 161
diff changeset
625 dir_size = 0
bcc4441cf216 Implement "--print-size" to print file and accumulated directory sizes also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 161
diff changeset
626
155
bf74ce3c968d When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents: 153
diff changeset
627 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
628 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
629 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
630 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
631 linkdgst = algorithm[0]()
173
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
632 linkdgst.update(
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
633 util.interpolate_bytes(
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
634 b"%d:%s,", len(linktgt), linktgt))
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
635 dir_dgst.update(util.interpolate_bytes(
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
636 b"1:S,%d:%s,", 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
637 # no mtime and no mode for symlinks
173
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
638 dir_dgst.update(util.interpolate_bytes(
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
639 b"%d:%s,",
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
640 len(linkdgst.digest()), linkdgst.digest()))
155
bf74ce3c968d When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents: 153
diff changeset
641 opath = "/".join(top) + "/" + fso.name if top else fso.name
170
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
642 if size_only:
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
643 outfp.write(
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
644 format_bsd_line(
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
645 "SIZE",
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
646 None,
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
647 "%s/./@" % (opath,),
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
648 False,
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
649 0))
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
650 else:
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
651 outfp.write(
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
652 format_bsd_line(
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
653 algorithm[1],
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
654 linkdgst.digest(),
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
655 "%s/./@" % (opath,),
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
656 use_base64))
155
bf74ce3c968d When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents: 153
diff changeset
657 outfp.flush()
bf74ce3c968d When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents: 153
diff changeset
658 continue
bf74ce3c968d When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents: 153
diff changeset
659 # fetch from dir_digests
168
bcc4441cf216 Implement "--print-size" to print file and accumulated directory sizes also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 161
diff changeset
660 dgst, dsz = dir_digests[top + (fso.name,)]
bcc4441cf216 Implement "--print-size" to print file and accumulated directory sizes also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 161
diff changeset
661 dir_size += dsz
173
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
662 dir_dgst.update(util.interpolate_bytes(
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
663 b"1:d,%d:%s,", len(fso.fsname), fso.fsname))
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
664 dir_dgst.update(util.interpolate_bytes(
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
665 b"%d:%s,", len(dgst), dgst))
158
d8cdd1985d43 Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents: 157
diff changeset
666 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
667 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
668 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
669 modestr = modestr.encode("ascii")
173
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
670 dir_dgst.update(util.interpolate_bytes(
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
671 b"8:fullmode,%d:%s,", len(modestr), modestr))
158
d8cdd1985d43 Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents: 157
diff changeset
672 elif with_metadata_mode:
d8cdd1985d43 Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents: 157
diff changeset
673 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
674 if not isinstance(modestr, bytes):
d8cdd1985d43 Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents: 157
diff changeset
675 modestr = modestr.encode("ascii")
173
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
676 dir_dgst.update(util.interpolate_bytes(
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
677 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
678 else:
173
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
679 dir_dgst.update(util.interpolate_bytes(
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
680 b"1:f,%d:%s,", len(fso.fsname), fso.fsname))
168
bcc4441cf216 Implement "--print-size" to print file and accumulated directory sizes also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 161
diff changeset
681 dir_size += fso.stat.st_size
155
bf74ce3c968d When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents: 153
diff changeset
682 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
683 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
684 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
685 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
686 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
687 mtime = mtime.encode("ascii")
173
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
688 dir_dgst.update(util.interpolate_bytes(
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
689 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
690 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
691 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
692 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
693 modestr = modestr.encode("ascii")
173
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
694 dir_dgst.update(util.interpolate_bytes(
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
695 b"8:fullmode,%d:%s,", len(modestr), modestr))
158
d8cdd1985d43 Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents: 157
diff changeset
696 elif with_metadata_mode:
d8cdd1985d43 Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents: 157
diff changeset
697 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
698 if not isinstance(modestr, bytes):
d8cdd1985d43 Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents: 157
diff changeset
699 modestr = modestr.encode("ascii")
173
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
700 dir_dgst.update(util.interpolate_bytes(
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
701 b"4:mode,%d:%s,", len(modestr), modestr))
170
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
702 if not size_only:
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
703 dgst = digest.compute_digest_file(
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
704 algorithm[0], fso.path, use_mmap=use_mmap)
173
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
705 dir_dgst.update(util.interpolate_bytes(
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
706 b"%d:%s,", len(dgst), dgst))
155
bf74ce3c968d When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents: 153
diff changeset
707 opath = "/".join(top) + "/" + fso.name if top else fso.name
170
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
708 if size_only:
168
bcc4441cf216 Implement "--print-size" to print file and accumulated directory sizes also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 161
diff changeset
709 outfp.write(
bcc4441cf216 Implement "--print-size" to print file and accumulated directory sizes also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 161
diff changeset
710 format_bsd_line(
170
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
711 "SIZE", None, opath, False, fso.stat.st_size))
168
bcc4441cf216 Implement "--print-size" to print file and accumulated directory sizes also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 161
diff changeset
712 else:
170
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
713 if print_size:
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
714 outfp.write(
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
715 format_bsd_line(
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
716 algorithm[1], dgst, opath, use_base64,
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
717 fso.stat.st_size))
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
718 else:
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
719 outfp.write(
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
720 format_bsd_line(
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
721 algorithm[1], dgst, opath, use_base64))
124
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
722 outfp.flush()
127
6a50d02fe0ca Change the filename output: make it more consistent
Franz Glasner <fzglas.hg@dom66.de>
parents: 125
diff changeset
723 opath = "/".join(top) + "/" if top else ""
170
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
724 if size_only:
168
bcc4441cf216 Implement "--print-size" to print file and accumulated directory sizes also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 161
diff changeset
725 outfp.write(format_bsd_line(
170
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
726 "SIZE", None, opath, False, dir_size))
168
bcc4441cf216 Implement "--print-size" to print file and accumulated directory sizes also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 161
diff changeset
727 else:
170
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
728 if print_size:
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
729 outfp.write(format_bsd_line(
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
730 algorithm[1], dir_dgst.digest(), opath,
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
731 use_base64, dir_size))
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
732 else:
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
733 outfp.write(format_bsd_line(
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
734 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
735 outfp.flush()
168
bcc4441cf216 Implement "--print-size" to print file and accumulated directory sizes also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 161
diff changeset
736 dir_digests[top] = (dir_dgst.digest(), dir_size)
124
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
737
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
738
158
d8cdd1985d43 Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents: 157
diff changeset
739 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
740 # 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
741 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
742 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
743 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
744 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
745 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
746
27d1aaf5fe39 Implement "--mode" flag for "treesum.py" to consider file portable mode bits
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
747
158
d8cdd1985d43 Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents: 157
diff changeset
748 def normalized_mode_str(mode):
d8cdd1985d43 Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents: 157
diff changeset
749 modestr = "%o" % (mode,)
d8cdd1985d43 Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents: 157
diff changeset
750 if not modestr.startswith("0"):
d8cdd1985d43 Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents: 157
diff changeset
751 modestr = "0" + modestr
d8cdd1985d43 Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents: 157
diff changeset
752 return modestr
d8cdd1985d43 Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents: 157
diff changeset
753
d8cdd1985d43 Implement "--full-mode" for "treesum.py"
Franz Glasner <fzglas.hg@dom66.de>
parents: 157
diff changeset
754
170
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
755 def format_bsd_line(what, value, filename, use_base64, size=None):
124
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
756 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
757 else os.linesep.encode("utf-8")
170
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
758 if not isinstance(what, bytes):
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
759 what = what.encode("ascii")
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
760 if what == b"TIMESTAMP":
129
bdd8ea43074b Output FLAGS as line "FLAGS = ..." instead of "FLAGS (...)"
Franz Glasner <fzglas.hg@dom66.de>
parents: 128
diff changeset
761 assert filename is None
173
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
762 return util.interpolate_bytes(b"TIMESTAMP = %d%s", value, ls)
170
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
763 if what 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
764 assert filename is None
128
7c646921a479 Add TIMESTAMP and ISOTIMESTAMP to the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 127
diff changeset
765 if not isinstance(value, bytes):
7c646921a479 Add TIMESTAMP and ISOTIMESTAMP to the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 127
diff changeset
766 value = value.encode("ascii")
173
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
767 return util.interpolate_bytes(b"%s = %s%s", what, value, ls)
129
bdd8ea43074b Output FLAGS as line "FLAGS = ..." instead of "FLAGS (...)"
Franz Glasner <fzglas.hg@dom66.de>
parents: 128
diff changeset
768 assert filename is not None
170
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
769 if what == b"COMMENT":
135
dbf27681a1f6 Allow to put comments into the output with "--comment"
Franz Glasner <fzglas.hg@dom66.de>
parents: 134
diff changeset
770 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
771 filename = filename.encode("utf-8")
173
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
772 return util.interpolate_bytes(b"COMMENT (%s)%s", filename, ls)
124
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
773 if not isinstance(filename, bytes):
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
774 filename = util.fsencode(filename)
170
8945be6b404e Mode for treesum.py to print only the size of files and the accumulated size of a directory: --size-only.
Franz Glasner <fzglas.hg@dom66.de>
parents: 169
diff changeset
775 if what == b"SIZE":
173
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
776 return util.interpolate_bytes(b"SIZE (%s) = %d%s", filename, size, ls)
128
7c646921a479 Add TIMESTAMP and ISOTIMESTAMP to the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 127
diff changeset
777 if value is None:
173
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
778 return util.interpolate_bytes(b"%s (%s)%s", what, filename, ls)
124
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
779 if use_base64:
128
7c646921a479 Add TIMESTAMP and ISOTIMESTAMP to the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 127
diff changeset
780 value = base64.b64encode(value)
124
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
781 else:
128
7c646921a479 Add TIMESTAMP and ISOTIMESTAMP to the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 127
diff changeset
782 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
783 if filename != b"./@":
127
6a50d02fe0ca Change the filename output: make it more consistent
Franz Glasner <fzglas.hg@dom66.de>
parents: 125
diff changeset
784 filename = util.normalize_filename(filename, True)
168
bcc4441cf216 Implement "--print-size" to print file and accumulated directory sizes also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 161
diff changeset
785 if size is None:
173
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
786 return util.interpolate_bytes(
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
787 b"%s (%s) = %s%s", what, filename, value, ls)
168
bcc4441cf216 Implement "--print-size" to print file and accumulated directory sizes also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 161
diff changeset
788 else:
173
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
789 return util.interpolate_bytes(
e081b6ee5570 treesum.py now runs on Python3.4 also: use a workaround for its missing byte % formatting.
Franz Glasner <fzglas.hg@dom66.de>
parents: 172
diff changeset
790 b"%s (%s) = %s,%d%s", what, filename, value, size, ls)
124
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
791
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
792
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
793 if __name__ == "__main__":
3bd3f32b5e60 A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
794 sys.exit(main())