[PATCH 1 of 2] sha1sum.py: sha1sum for platforms which do not provide it

David M. Carr david at carrclan.us
Mon Oct 31 08:30:17 CDT 2011


+1, as someone who was developing an unrelated change on Mac OS X and
had this test fail because sha1sum wasn't present.

On Sun, Oct 30, 2011 at 3:08 PM, Christian Ebert <blacktrash at gmx.net> wrote:
> # HG changeset patch
> # User Christian Ebert <blacktrash at gmx.net>
> # Date 1320001536 -3600
> # Branch stable
> # Node ID 96211cffd4d151f9c1fb34d38bb67eba5b92b944
> # Parent  87248de091352f746e8cd1cee01af29138c8e443
> sha1sum.py: sha1sum for platforms which do not provide it
>
> sha1sum might not be present on some platforms, for instance MacOS X,
> therefore provide a python script mimicking the already present
> md5sum.py.
>
> diff --git a/tests/sha1sum.py b/tests/sha1sum.py
> new file mode 100755
> --- /dev/null
> +++ b/tests/sha1sum.py
> @@ -0,0 +1,42 @@
> +#!/usr/bin/env python
> +#
> +# Based on python's Tools/scripts/md5sum.py
> +#
> +# This software may be used and distributed according to the terms
> +# of the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2, which is
> +# GPL-compatible.
> +
> +import sys, os
> +
> +try:
> +    from hashlib import sha1
> +except ImportError:
> +    from sha import sha as sha1
> +
> +try:
> +    import msvcrt
> +    msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
> +    msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
> +except ImportError:
> +    pass
> +
> +for filename in sys.argv[1:]:
> +    try:
> +        fp = open(filename, 'rb')
> +    except IOError, msg:
> +        sys.stderr.write('%s: Can\'t open: %s\n' % (filename, msg))
> +        sys.exit(1)
> +
> +    m = sha1()
> +    try:
> +        while True:
> +            data = fp.read(8192)
> +            if not data:
> +                break
> +            m.update(data)
> +    except IOError, msg:
> +        sys.stderr.write('%s: I/O error: %s\n' % (filename, msg))
> +        sys.exit(1)
> +    sys.stdout.write('%s  %s\n' % (m.hexdigest(), filename))
> +
> +sys.exit(0)
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at selenic.com
> http://selenic.com/mailman/listinfo/mercurial-devel
>



-- 
David M. Carr
david at carrclan.us


More information about the Mercurial-devel mailing list