Mercurial > hgrepos > Python > apps > py-cutils
comparison cutils/shasum.py @ 104:08fd0609fdd4
Implement "--recurse" for shasum: recurse into directories
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Thu, 26 May 2022 23:14:38 +0200 |
| parents | f95918115c6b |
| children | b0631f320efd |
comparison
equal
deleted
inserted
replaced
| 103:af8318191ed3 | 104:08fd0609fdd4 |
|---|---|
| 90 help="""Use mmap if available. Default is to determine automatically | 90 help="""Use mmap if available. Default is to determine automatically |
| 91 from the filesize.""") | 91 from the filesize.""") |
| 92 aparser.add_argument( | 92 aparser.add_argument( |
| 93 "--no-mmap", action="store_false", dest="mmap", default=None, | 93 "--no-mmap", action="store_false", dest="mmap", default=None, |
| 94 help="Dont use mmap.") | 94 help="Dont use mmap.") |
| 95 | |
| 96 aparser.add_argument( | |
| 97 "--recurse", action="store_true", | |
| 98 help="Recurse into sub-directories while interpreting every FILE as a directory") | |
| 95 | 99 |
| 96 aparser.add_argument( | 100 aparser.add_argument( |
| 97 "--reverse", "-r", action="store_false", dest="bsd", default=False, | 101 "--reverse", "-r", action="store_false", dest="bsd", default=False, |
| 98 help="Explicitely select normal coreutils style output (to be option compatible with BSD style commands and :command:`openssl dgst -r`)") | 102 help="Explicitely select normal coreutils style output (to be option compatible with BSD style commands and :command:`openssl dgst -r`)") |
| 99 aparser.add_argument( | 103 aparser.add_argument( |
| 159 def generate_digests(opts): | 163 def generate_digests(opts): |
| 160 if opts.bsd: | 164 if opts.bsd: |
| 161 out = out_bsd | 165 out = out_bsd |
| 162 else: | 166 else: |
| 163 out = out_std | 167 out = out_std |
| 164 if not opts.files or (len(opts.files) == 1 and opts.files[0] == '-'): | 168 if opts.recurse: |
| 165 if PY2: | 169 if not opts.files: |
| 166 if sys.platform == "win32": | 170 opts.files.append(".") |
| 167 import os, msvcrt # noqa: E401 | 171 for dn in opts.files: |
| 168 msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) | 172 if not os.path.isdir(dn): |
| 169 source = sys.stdin | 173 if os.path.exists(dn): |
| 170 else: | 174 raise OSError(errno.ENOTDIR, "not a directory", dn) |
| 171 source = sys.stdin.buffer | 175 else: |
| 172 out(sys.stdout, | 176 raise OSError(errno.ENOENT, "directory does not exist", dn) |
| 173 compute_digest_stream(opts.algorithm[0], source), | 177 for dirpath, dirnames, dirfiles in os.walk(dn, followlinks=True): |
| 174 None, | 178 for fn in dirfiles: |
| 175 opts.algorithm[1], | 179 path = os.path.join(dirpath, fn) |
| 176 True, | 180 out(opts.dest or sys.stdout, |
| 177 opts.base64) | 181 compute_digest_file(opts.algorithm[0], path, |
| 178 else: | 182 use_mmap=opts.mmap), |
| 179 for fn in opts.files: | 183 path, |
| 180 out(opts.dest or sys.stdout, | 184 opts.algorithm[1], |
| 181 compute_digest_file(opts.algorithm[0], fn, | 185 True, |
| 182 use_mmap=opts.mmap), | 186 opts.base64) |
| 183 fn, | 187 else: |
| 188 if not opts.files or (len(opts.files) == 1 and opts.files[0] == '-'): | |
| 189 if PY2: | |
| 190 if sys.platform == "win32": | |
| 191 import msvcrt # noqa: E401 | |
| 192 msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) | |
| 193 source = sys.stdin | |
| 194 else: | |
| 195 source = sys.stdin.buffer | |
| 196 out(sys.stdout, | |
| 197 compute_digest_stream(opts.algorithm[0], source), | |
| 198 None, | |
| 184 opts.algorithm[1], | 199 opts.algorithm[1], |
| 185 True, | 200 True, |
| 186 opts.base64) | 201 opts.base64) |
| 202 else: | |
| 203 for fn in opts.files: | |
| 204 out(opts.dest or sys.stdout, | |
| 205 compute_digest_file(opts.algorithm[0], fn, | |
| 206 use_mmap=opts.mmap), | |
| 207 fn, | |
| 208 opts.algorithm[1], | |
| 209 True, | |
| 210 opts.base64) | |
| 187 return 0 | 211 return 0 |
| 188 | 212 |
| 189 | 213 |
| 190 def compare_digests_equal(given_digest, expected_digest, algo): | 214 def compare_digests_equal(given_digest, expected_digest, algo): |
| 191 """Compare a newly computed binary digest `given_digest` with a digest | 215 """Compare a newly computed binary digest `given_digest` with a digest |
