Include module for desktop build
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# Copyright (c) 2005-2010 Zooko Wilcox-O'Hearn
|
||||
# This file is part of pyutil; see README.rst for licensing terms.
|
||||
|
||||
from pyutil import lineutil
|
||||
|
||||
import sys
|
||||
|
||||
def main():
|
||||
if len(sys.argv) > 1 and "-s" in sys.argv[1:]:
|
||||
strip = True
|
||||
sys.argv.remove("-s")
|
||||
else:
|
||||
strip = False
|
||||
|
||||
if len(sys.argv) > 1 and "-n" in sys.argv[1:]:
|
||||
nobak = True
|
||||
sys.argv.remove("-n")
|
||||
else:
|
||||
nobak = False
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
pipe = False
|
||||
else:
|
||||
pipe = True
|
||||
|
||||
if pipe:
|
||||
lineutil.lineify_fileobjs(sys.stdin, sys.stdout)
|
||||
else:
|
||||
for fn in sys.argv[1:]:
|
||||
lineutil.lineify_file(fn, strip, nobak)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import bindann
|
||||
bindann.install_exception_handler()
|
||||
|
||||
import sys
|
||||
|
||||
inf = open(sys.argv[1], "r")
|
||||
|
||||
outf = open(sys.argv[1]+".dot", "w")
|
||||
outf.write("digraph %s {\n" % sys.argv[1].replace(".",""))
|
||||
|
||||
def parse_netstring(l, i):
|
||||
try:
|
||||
j = l.find(':', i)
|
||||
if j == -1:
|
||||
return (None, len(l),)
|
||||
lenval = int(l[i:j])
|
||||
val = l[j+1:j+1+lenval]
|
||||
# skip the comma
|
||||
assert l[j+1+lenval] == ","
|
||||
return (val, j+1+lenval+1,)
|
||||
except Exception, le:
|
||||
le.args = tuple(le.args + (l, i,))
|
||||
raise
|
||||
|
||||
def parse_ref(l, i):
|
||||
(attrname, i,) = parse_netstring(l, i)
|
||||
j = l.find(",", i)
|
||||
assert j != -1
|
||||
objid = l[i:j]
|
||||
return (objid, attrname, j+1,)
|
||||
|
||||
def parse_memdump_line(l):
|
||||
result = []
|
||||
|
||||
i = l.find('-')
|
||||
objid = l[:i]
|
||||
(objdesc, i,) = parse_netstring(l, i+1)
|
||||
|
||||
result.append((objid, objdesc,))
|
||||
|
||||
while i != -1 and i < len(l):
|
||||
(objid, attrname, i,) = parse_ref(l, i)
|
||||
result.append((objid, attrname,))
|
||||
|
||||
return result
|
||||
|
||||
for l in inf:
|
||||
if l[-1] != "\n":
|
||||
raise "waht the HECK? %r" % l
|
||||
res = parse_memdump_line(l.strip())
|
||||
# declare the node
|
||||
outf.write("\"%s\" [label=\"%s\"];\n" % (res[0][0], res[0][1],))
|
||||
|
||||
# declare all the edges
|
||||
for edge in res[1:]:
|
||||
if edge[1]:
|
||||
# a named edge
|
||||
outf.write("\"%s\" -> \"%s\" [style=bold, label=\"%s\"];\n" % (res[0][0], edge[0], edge[1],))
|
||||
else:
|
||||
# an anonymous edge
|
||||
outf.write("\"%s\" -> \"%s\";\n" % (res[0][0], edge[0]))
|
||||
|
||||
outf.write("}")
|
||||
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import os, sys
|
||||
|
||||
import zbase32
|
||||
|
||||
def main():
|
||||
if len(sys.argv) > 1:
|
||||
l = int(sys.argv[1])
|
||||
else:
|
||||
l = 64
|
||||
|
||||
bl = (l + 7) / 8
|
||||
|
||||
s = zbase32.b2a_l(os.urandom(bl), l)
|
||||
|
||||
# insert some hyphens for easier memorization
|
||||
chs = 3 + (len(s)%8==0)
|
||||
i = chs
|
||||
while i < len(s)-1:
|
||||
s = s[:i] + "-" + s[i:]
|
||||
i += 1
|
||||
chs = 7-chs
|
||||
i += chs
|
||||
|
||||
print s
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import os, sys
|
||||
|
||||
from random import randrange
|
||||
|
||||
import argparse
|
||||
|
||||
def main():
|
||||
CHUNKSIZE=2**20
|
||||
|
||||
parser = argparse.ArgumentParser(prog="randfile", description="Create a file of pseudorandom bytes (not cryptographically secure).")
|
||||
|
||||
parser.add_argument('-b', '--num-bytes', help="how many bytes to write per output file (default 20)", type=int, metavar="BYTES", default=20)
|
||||
parser.add_argument('-f', '--output-file-prefix', help="prefix of the name of the output file to create and fill with random bytes (default \"randfile\"", metavar="OUTFILEPRE", default="randfile")
|
||||
parser.add_argument('-n', '--num-files', help="how many files to write (default 1)", type=int, metavar="FILES", default=1)
|
||||
parser.add_argument('-F', '--force', help='overwrite any file already present', action='store_true')
|
||||
parser.add_argument('-p', '--progress', help='write an "x" for every file completed and a "." for every %d bytes' % CHUNKSIZE, action='store_true')
|
||||
args = parser.parse_args()
|
||||
|
||||
for i in xrange(args.num_files):
|
||||
bytesleft = args.num_bytes
|
||||
outputfname = args.output_file_prefix + "." + str(i)
|
||||
|
||||
if args.force:
|
||||
f = open(outputfname, "wb")
|
||||
else:
|
||||
flags = os.O_WRONLY|os.O_CREAT|os.O_EXCL | (hasattr(os, 'O_BINARY') and os.O_BINARY)
|
||||
fd = os.open(outputfname, flags)
|
||||
f = os.fdopen(fd, "wb")
|
||||
zs = [0]*CHUNKSIZE
|
||||
ts = [256]*CHUNKSIZE
|
||||
while bytesleft >= CHUNKSIZE:
|
||||
f.write(''.join(map(chr, map(randrange, zs, ts))))
|
||||
bytesleft -= CHUNKSIZE
|
||||
|
||||
if args.progress:
|
||||
sys.stdout.write(".") ; sys.stdout.flush()
|
||||
|
||||
zs = [0]*bytesleft
|
||||
ts = [256]*bytesleft
|
||||
f.write(''.join(map(chr, map(randrange, zs, ts))))
|
||||
|
||||
if args.progress:
|
||||
sys.stdout.write("x") ; sys.stdout.flush()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# output all but the first N lines of a file
|
||||
|
||||
# Allen Short and Jp Calderone wrote this coool version:
|
||||
import itertools, sys
|
||||
|
||||
def main():
|
||||
K = int(sys.argv[1])
|
||||
if len(sys.argv) > 2:
|
||||
fname = sys.argv[2]
|
||||
inf = open(fname, 'r')
|
||||
else:
|
||||
inf = sys.stdin
|
||||
|
||||
sys.stdout.writelines(itertools.islice(inf, K, None))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
# thus replacing my dumb version:
|
||||
# # from the Python Standard Library
|
||||
# import sys
|
||||
#
|
||||
# i = K
|
||||
# for l in sys.stdin.readlines():
|
||||
# if i:
|
||||
# i -= 1
|
||||
# else:
|
||||
# print l,
|
||||
@@ -0,0 +1,96 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import binascii, codecs, encodings, locale, os, sys, zlib
|
||||
|
||||
import argparse
|
||||
|
||||
def listcodecs(dir):
|
||||
names = []
|
||||
for filename in os.listdir(dir):
|
||||
if filename[-3:] != '.py':
|
||||
continue
|
||||
name = filename[:-3]
|
||||
# Check whether we've found a true codec
|
||||
try:
|
||||
codecs.lookup(name)
|
||||
except LookupError:
|
||||
# Codec not found
|
||||
continue
|
||||
except Exception:
|
||||
# Probably an error from importing the codec; still it's
|
||||
# a valid code name
|
||||
pass
|
||||
names.append(name)
|
||||
return names
|
||||
|
||||
def listem():
|
||||
return listcodecs(encodings.__path__[0])
|
||||
|
||||
def _canonical_encoding(encoding):
|
||||
if encoding is None:
|
||||
encoding = 'utf-8'
|
||||
encoding = encoding.lower()
|
||||
if encoding == "cp65001":
|
||||
encoding = 'utf-8'
|
||||
elif encoding == "us-ascii" or encoding == "646":
|
||||
encoding = 'ascii'
|
||||
|
||||
# sometimes Python returns an encoding name that it doesn't support for conversion
|
||||
# fail early if this happens
|
||||
try:
|
||||
u"test".encode(encoding)
|
||||
except (LookupError, AttributeError):
|
||||
raise AssertionError("The character encoding '%s' is not supported for conversion." % (encoding,))
|
||||
|
||||
return encoding
|
||||
|
||||
def get_output_encoding():
|
||||
return _canonical_encoding(sys.stdout.encoding or locale.getpreferredencoding())
|
||||
|
||||
def get_argv_encoding():
|
||||
if sys.platform == 'win32':
|
||||
# Unicode arguments are not supported on Windows yet; see Tahoe-LAFS tickets #565 and #1074.
|
||||
return 'ascii'
|
||||
else:
|
||||
return get_output_encoding()
|
||||
|
||||
output_encoding = get_output_encoding()
|
||||
argv_encoding = get_argv_encoding()
|
||||
|
||||
def type_unicode(argstr):
|
||||
return argstr.decode(argv_encoding)
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(prog="try_decoding", description="Try decoding some bytes with all sorts of different codecs and print out any that decode.")
|
||||
|
||||
parser.add_argument('inputfile', help='file to decode or "-" for stdin', type=argparse.FileType('rb'), metavar='INF')
|
||||
parser.add_argument('-t', '--target', help='unicode string to match against (if any)', type=type_unicode, metavar='T')
|
||||
parser.add_argument('-a', '--accept-bytes', help='include codecs which return bytes instead of returning unicode (they will be marked with "!!!" in the output)', action='store_true')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
inb = args.inputfile.read()
|
||||
|
||||
for codec in listem():
|
||||
try:
|
||||
u = inb.decode(codec)
|
||||
except (UnicodeDecodeError, IOError, TypeError, IndexError, UnicodeError, ValueError, zlib.error, binascii.Error):
|
||||
pass
|
||||
else:
|
||||
if isinstance(u, unicode):
|
||||
if args.target:
|
||||
if args.target != u:
|
||||
continue
|
||||
print "%19s" % codec,
|
||||
print ':',
|
||||
print u.encode(output_encoding)
|
||||
else:
|
||||
if not args.accept_bytes:
|
||||
continue
|
||||
print "%19s" % codec,
|
||||
print "!!! ",
|
||||
print ':',
|
||||
print u
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# randomize the lines of stdin or a file
|
||||
|
||||
import random, sys
|
||||
|
||||
def main():
|
||||
if len(sys.argv) > 1:
|
||||
fname = sys.argv[1]
|
||||
inf = open(fname, 'r')
|
||||
else:
|
||||
inf = sys.stdin
|
||||
|
||||
lines = inf.readlines()
|
||||
random.shuffle(lines)
|
||||
sys.stdout.writelines(lines)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,26 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import exceptions
|
||||
class UsageError(exceptions.Exception): pass
|
||||
|
||||
import sys
|
||||
import pkg_resources
|
||||
|
||||
def main():
|
||||
if len(sys.argv) <= 1:
|
||||
raise UsageError, "USAGE: verinfo DISTRIBUTIONNAME [PACKAGENAME]"
|
||||
DISTNAME=sys.argv[1]
|
||||
if len(sys.argv) >= 3:
|
||||
PACKNAME=sys.argv[2]
|
||||
else:
|
||||
PACKNAME=DISTNAME
|
||||
print "pkg_resources.require('%s') => " % (DISTNAME,),
|
||||
print pkg_resources.require(DISTNAME)
|
||||
print "import %s;print %s => " % (PACKNAME, PACKNAME,),
|
||||
x = __import__(PACKNAME)
|
||||
print x
|
||||
print "import %s;print %s.__version__ => " % (PACKNAME, PACKNAME,),
|
||||
print hasattr(x, '__version__') and x.__version__
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user