comparison cutils/treesum.py @ 150:f84cf853da22

Implement "--minimal [TAG]" for treesum. Product minimal output only.
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 03 Jan 2025 23:08:02 +0100
parents f717854be1de
children b26c4290e928
comparison
equal deleted inserted replaced
149:f717854be1de 150:f84cf853da22
67 "--logical", "-L", dest="logical", action="store_true", 67 "--logical", "-L", dest="logical", action="store_true",
68 default=None, 68 default=None,
69 help="Follow symbolic links given on command line arguments." 69 help="Follow symbolic links given on command line arguments."
70 " Note that this is a different setting as to follow symbolic" 70 " Note that this is a different setting as to follow symbolic"
71 " links to directories when traversing a directory tree.") 71 " links to directories when traversing a directory tree.")
72 gp.add_argument(
73 "--minimal", nargs="?", const="", default=None, metavar="TAG",
74 help="Produce minimal output only. If a TAG is given and not "
75 "empty use it as the leading \"ROOT (<TAG>)\" output.")
72 gp.add_argument( 76 gp.add_argument(
73 "--mmap", action="store_true", dest="mmap", default=None, 77 "--mmap", action="store_true", dest="mmap", default=None,
74 help="Use mmap if available. Default is to determine " 78 help="Use mmap if available. Default is to determine "
75 "automatically from the filesize.") 79 "automatically from the filesize.")
76 gp.add_argument( 80 gp.add_argument(
176 append_output=False, 180 append_output=False,
177 base64=False, 181 base64=False,
178 comment=[], 182 comment=[],
179 follow_directory_symlinks=False, 183 follow_directory_symlinks=False,
180 logical=None, 184 logical=None,
185 minimal=None,
181 mmap=None, 186 mmap=None,
182 output=None): 187 output=None):
183 opts = argparse.Namespace( 188 opts = argparse.Namespace(
184 directories=directories, 189 directories=directories,
185 algorithm=(util.algotag2algotype(algorithm), 190 algorithm=(util.algotag2algotype(algorithm),
187 append_output=append_output, 192 append_output=append_output,
188 base64=base64, 193 base64=base64,
189 comment=comment, 194 comment=comment,
190 follow_directory_symlinks=follow_directory_symlinks, 195 follow_directory_symlinks=follow_directory_symlinks,
191 logical=logical, 196 logical=logical,
197 minimal=minimal,
192 mmap=mmap, 198 mmap=mmap,
193 output=output) 199 output=output)
194 return opts 200 return opts
195 201
196 202
224 with out_cm as outfp: 230 with out_cm as outfp:
225 for d in opts.directories: 231 for d in opts.directories:
226 generate_treesum_for_directory( 232 generate_treesum_for_directory(
227 outfp, d, opts.algorithm, opts.mmap, opts.base64, opts.logical, 233 outfp, d, opts.algorithm, opts.mmap, opts.base64, opts.logical,
228 opts.follow_directory_symlinks, 234 opts.follow_directory_symlinks,
235 minimal=opts.minimal,
229 comment=opts.comment) 236 comment=opts.comment)
230 237
231 238
232 def generate_treesum_for_directory( 239 def generate_treesum_for_directory(
233 outfp, root, algorithm, use_mmap, use_base64, handle_root_logical, 240 outfp, root, algorithm, use_mmap, use_base64, handle_root_logical,
234 follow_directory_symlinks, comment=None): 241 follow_directory_symlinks, minimal=None, comment=None):
235 """ 242 """
236 243
237 :param outfp: a *binary* file with a "write()" and a "flush()" method 244 :param outfp: a *binary* file with a "write()" and a "flush()" method
238 245
239 """ 246 """
248 flags.append("follow-directory-symlinks") 255 flags.append("follow-directory-symlinks")
249 if flags: 256 if flags:
250 outfp.write(format_bsd_line("FLAGS", ",".join(flags), None, False)) 257 outfp.write(format_bsd_line("FLAGS", ",".join(flags), None, False))
251 outfp.flush() 258 outfp.flush()
252 259
253 # Write execution timestamps in POSIX epoch and ISO format 260 if minimal is None:
254 ts = time.time() 261 # Write execution timestamps in POSIX epoch and ISO format
255 outfp.write(format_bsd_line("TIMESTAMP", ts, None, False)) 262 ts = time.time()
256 ts = (datetime.datetime.utcfromtimestamp(ts)).isoformat("T") 263 outfp.write(format_bsd_line("TIMESTAMP", ts, None, False))
257 outfp.write(format_bsd_line("ISOTIMESTAMP", ts, None, False)) 264 ts = (datetime.datetime.utcfromtimestamp(ts)).isoformat("T")
258 outfp.flush() 265 outfp.write(format_bsd_line("ISOTIMESTAMP", ts, None, False))
259 266 outfp.flush()
260 if comment: 267
261 for line in comment: 268 if comment:
262 outfp.write(format_bsd_line("COMMENT", None, line, False)) 269 for line in comment:
263 outfp.write(format_bsd_line("ROOT", None, root, False)) 270 outfp.write(format_bsd_line("COMMENT", None, line, False))
271
272 if minimal is not None:
273 outfp.write(
274 format_bsd_line(
275 "ROOT", None, minimal if minimal else "", False))
276 else:
277 outfp.write(format_bsd_line("ROOT", None, root, False))
264 outfp.flush() 278 outfp.flush()
265 279
266 dir_digests = {} 280 dir_digests = {}
267 281
268 if not handle_root_logical and os.path.islink(root): 282 if not handle_root_logical and os.path.islink(root):