def main(argv): try: optv, argv = getopt.getopt(argv[1:], "h", ["help"]) except getopt.GetoptError as e: print(e, file=sys.stderr) usage(sys.stderr) sys.exit(2) for opt, _ in optv: if opt in ("-h", "--help"): usage(sys.stdout) sys.exit(0) if len(argv) != 1: usage(sys.stderr) sys.exit(2) storurl = argv[0] stor = storageFromURL(storurl) defer(stor.close) def _(txn): print(ashex(txn.tid)) zodbrestore(stor, asbinstream(sys.stdin), _)
def _(): zdump = open("%s/1%s.zdump.raw.ok" % (tdata, zkind), 'rb') defer(zdump.close) stor = storageFromURL('%s/2.fs' % tmpd) defer(stor.close) zodbrestore(stor, zdump)
def analyze(path, use_dbm, delta_fs, tidmin, tidmax): if delta_fs: fs = DeltaFileStorage(path, read_only=1) else: fs = storageFromURL(path, read_only=1) defer(fs.close) fsi = fs.iterator(tidmin, tidmax) report = Report(use_dbm, delta_fs) for txn in fsi: analyze_trans(report, txn) if use_dbm: shutil.rmtree(report.temp_dir) return report
def test_notify_reinstall(): ch = chan(10, dtype=gos.Signal) def _(): signal.Stop(ch) defer(_) for i in range(N): signal.Stop(ch) signal.Notify(ch, syscall.SIGUSR1) time.sleep(0.1*time.second) assert len(ch) == 0 killme(syscall.SIGUSR1) time.sleep(0.1*time.second) assert len(ch) == 1
def main(argv): hashonly = False pretty = 'raw' prettyok = {'raw', 'zpickledis'} try: optv, argv = getopt.getopt(argv[1:], "h", ["help", "hashonly", "pretty="]) except getopt.GetoptError as e: print(e, file=sys.stderr) usage(sys.stderr) sys.exit(2) for opt, arg in optv: if opt in ("-h", "--help"): usage(sys.stdout) sys.exit(0) if opt in ("--hashonly"): hashonly = True if opt in ("--pretty"): pretty = arg if pretty not in prettyok: print("E: unsupported pretty format: %s" % pretty, file=sys.stderr) sys.exit(2) try: storurl = argv[0] except IndexError: usage(sys.stderr) sys.exit(2) # parse tidmin..tidmax tidmin = tidmax = None if len(argv) > 1: try: tidmin, tidmax = parse_tidrange(argv[1]) except TidRangeInvalid as e: print("E: invalid tidrange: %s" % e, file=sys.stderr) sys.exit(2) stor = storageFromURL(storurl, read_only=True) defer(stor.close) zodbdump(stor, tidmin, tidmax, hashonly, pretty)
def close(h): def autoclose(): def _(): n = h._subnet with n._hostmu: n._nopenHosts -= 1 if n._nopenHosts < 0: panic("SubNetwork._nopenHosts < 0") if n._autoclose and n._nopenHosts == 0: n._closeWithoutHosts() h._close_once.do(_) defer(autoclose) with errctx("virtnet %s: host %s: close" % (qq(h._subnet._network), qq(h._name))): h._shutdown()
def test_zodbcommit(zext): tmpd = mkdtemp('', 'zodbcommit.') defer(lambda: rmtree(tmpd)) stor = storageFromURL('%s/2.fs' % tmpd) defer(stor.close) head = stor.lastTransaction() # commit some transactions via zodbcommit and verify if storage dump gives # what is expected. t1 = Transaction(z64, ' ', b'user name', b'description ...', zext(dumps({'a': 'b'}, _protocol)), [ ObjectData(p64(1), b'data1', 'sha1', sha1(b'data1')), ObjectData(p64(2), b'data2', 'sha1', sha1(b'data2'))]) t1.tid = zodbcommit(stor, head, t1) t2 = Transaction(z64, ' ', b'user2', b'desc2', b'', [ ObjectDelete(p64(2))]) t2.tid = zodbcommit(stor, t1.tid, t2) buf = BytesIO() zodbdump(stor, p64(u64(head)+1), None, out=buf) dumped = buf.getvalue() assert dumped == b''.join([_.zdump() for _ in (t1, t2)]) # ObjectCopy. XXX zodbcommit handled ObjectCopy by actually copying data, # not referencing previous transaction via backpointer. t3 = Transaction(z64, ' ', b'user3', b'desc3', b'', [ ObjectCopy(p64(1), t1.tid)]) t3.tid = zodbcommit(stor, t2.tid, t3) data1_1, _, _ = stor.loadBefore(p64(1), p64(u64(t1.tid)+1)) data1_3, _, _ = stor.loadBefore(p64(1), p64(u64(t3.tid)+1)) assert data1_1 == data1_3 assert data1_1 == b'data1' # just in case
def _zext_supported(): tmpd = mkdtemp('', 'zext_check.') defer(lambda: rmtree(tmpd)) dbfs = tmpd + '/1.fs' stor = FileStorage(dbfs, create=True) db = DB(stor) conn = db.open() root = conn.root() root._p_changed = True txn = transaction.get() txn.setExtendedInfo('a', 'b') txn.commit() for last_txn in stor.iterator(start=stor.lastTransaction()): break else: assert False, "cannot see details of last transaction" assert last_txn.extension == {'a': 'b'} return hasattr(last_txn, 'extension_bytes')
def main2(argv): verbose = False try: optv, argv = getopt.getopt(argv[1:], "hv", ["help", "verbose"]) except getopt.GetoptError as e: print(e, file=sys.stderr) usage(sys.stderr) sys.exit(2) for opt, _ in optv: if opt in ("-h", "--help"): usage(sys.stdout) sys.exit(0) if opt in ("-v", "--verbose"): verbose = True try: storurl1, storurl2 = argv[0:2] except ValueError: usage(sys.stderr) sys.exit(2) # parse tidmin..tidmax tidmin = tidmax = None if len(argv) > 2: try: tidmin, tidmax = parse_tidrange(argv[2]) except TidRangeInvalid as e: print("E: invalid tidrange: %s" % e, file=sys.stderr) sys.exit(2) stor1 = storageFromURL(storurl1, read_only=True) defer(stor1.close) stor2 = storageFromURL(storurl2, read_only=True) defer(stor2.close) zcmp = storcmp(stor1, stor2, tidmin, tidmax, verbose) sys.exit(1 if zcmp else 0)
def main(argv): try: optv, argv = getopt.getopt(argv[1:], "h", ["help"]) except getopt.GetoptError as e: print(e, file=sys.stderr) usage(sys.stderr) sys.exit(2) for opt, _ in optv: if opt in ("-h", "--help"): usage(sys.stdout) sys.exit(0) if len(argv) != 2: usage(sys.stderr) sys.exit(2) storurl = argv[0] at = fromhex(argv[1]) stor = storageFromURL(storurl) defer(stor.close) # artificial transaction header with tid=0 to request regular commit zin = b'txn 0000000000000000 " "\n' zin += sys.stdin.read() zin = BytesIO(zin) zr = zodbdump.DumpReader(zin) zr.lineno -= 1 # we prepended txn header txn = zr.readtxn() tail = zin.read() if tail: print('E: +%d: garbage after transaction' % zr.lineno, file=sys.stderr) sys.exit(1) tid = zodbcommit(stor, at, txn) print(ashex(tid))
def test_lonet_py_go(network): subnet = lonet.join(network) defer(subnet.close) def xaddr(addr): return lonet.Addr.parse(subnet.network(), addr) hb = subnet.new_host("β") lb = hb.listen(":1") c1 = hb.dial("α:1") assert c1.local_addr() == xaddr("β:2") assert c1.remote_addr() == xaddr("α:2") assert xread(c1) == "hello py" xwrite(c1, "hello go") c1.close() c2 = lb.accept() assert c2.local_addr() == xaddr("β:2") assert c2.remote_addr() == xaddr("α:2") xwrite(c2, "hello2 go") assert xread(c2) == "hello2 py" c2.close()
def main(argv): try: optv, argv = getopt.getopt(argv[1:], "h", ["help"]) except getopt.GetoptError as e: print(e, file=sys.stderr) usage(sys.stderr) sys.exit(2) for opt, _ in optv: if opt in ("-h", "--help"): usage(sys.stdout) sys.exit(0) try: storurl = argv[0] except IndexError: usage(sys.stderr) sys.exit(2) stor = storageFromURL(storurl, read_only=True) defer(stor.close) zodbinfo(stor, argv[1:])
def test_zodbrestore(zext): tmpd = mkdtemp('', 'zodbrestore.') defer(lambda: rmtree(tmpd)) zkind = '_!zext' if zext.disabled else '' # restore from testdata/1.zdump.ok and verify it gives result that is # bit-to-bit identical to testdata/1.fs tdata = dirname(__file__) + "/testdata" @func def _(): zdump = open("%s/1%s.zdump.raw.ok" % (tdata, zkind), 'rb') defer(zdump.close) stor = storageFromURL('%s/2.fs' % tmpd) defer(stor.close) zodbrestore(stor, zdump) _() zfs1 = _readfile("%s/1%s.fs" % (tdata, zkind)) zfs2 = _readfile("%s/2.fs" % tmpd) assert zfs1 == zfs2
def _test_virtnet_basic(subnet): # (verifying that error log stays empty) errorlog = StringIO() errorlogh = log.StreamHandler(errorlog) l = log.getLogger() l.addHandler(errorlogh) def _(): l.removeHandler(errorlogh) assert errorlog.getvalue() == "" defer(_) defer(subnet.close) def xaddr(addr): return lonet.Addr.parse(subnet.network(), addr) ha = subnet.new_host("α") hb = subnet.new_host("β") assert ha.network() == subnet.network() assert hb.network() == subnet.network() assert ha.name() == "α" assert hb.name() == "β" try: ha.dial(":0") except Exception as e: assert xerr.cause(e) is lonet.ErrConnRefused assert str(e) == "dial %s α:1->α:0: [Errno %d] connection refused" % ( subnet.network(), errno.ECONNREFUSED) else: assert 0, "connection not refused" l1 = ha.listen("") assert l1.addr() == xaddr("α:1") try: ha.dial(":0") except Exception as e: assert xerr.cause(e) is lonet.ErrConnRefused assert str(e) == "dial %s α:2->α:0: [Errno %d] connection refused" % ( subnet.network(), errno.ECONNREFUSED) else: assert 0, "connection not refused" def Tsrv(): c1s = l1.accept() assert c1s.local_addr() == xaddr("α:2") assert c1s.getsockname() == ("α", 2) assert c1s.remote_addr() == xaddr("β:1") assert c1s.getpeername() == ("β", 1) assert xread(c1s) == "ping" xwrite(c1s, "pong") c2s = l1.accept() assert c2s.local_addr() == xaddr("α:3") assert c2s.getsockname() == ("α", 3) assert c2s.remote_addr() == xaddr("β:2") assert c2s.getpeername() == ("β", 2) assert xread(c2s) == "hello" xwrite(c2s, "world") tsrv = Thread(target=Tsrv) tsrv.start() c1c = hb.dial("α:1") assert c1c.local_addr() == xaddr("β:1") assert c1c.getsockname() == ("β", 1) assert c1c.remote_addr() == xaddr("α:2") assert c1c.getpeername() == ("α", 2) xwrite(c1c, "ping") assert xread(c1c) == "pong" c2c = hb.dial("α:1") assert c2c.local_addr() == xaddr("β:2") assert c2c.getsockname() == ("β", 2) assert c2c.remote_addr() == xaddr("α:3") assert c2c.getpeername() == ("α", 3) xwrite(c2c, "hello") assert xread(c2c) == "world" tsrv.join() l2 = ha.listen(":0") assert l2.addr() == xaddr("α:4") subnet.close()
def test_signal(): # Notify/Stop with wrong chan dtype -> panic _ = panics("pychan: channel type mismatch") with _: signal.Notify(chan(2), syscall.SIGUSR1) with _: signal.Stop (chan(2)) with _: signal.Notify(chan(2, dtype='C.int'), syscall.SIGUSR1) with _: signal.Stop (chan(2, dtype='C.int')) # Notify/Ignore/Reset with wrong signal type _ = raises(TypeError) with _: signal.Notify(chan(dtype=gos.Signal), None) with _: signal.Ignore(None) with _: signal.Reset(None) # subscribe ch1(USR1), ch12(USR1,USR2) and ch2(USR2) ch1 = chan(2, dtype=gos.Signal) ch12 = chan(2, dtype=gos.Signal) ch2 = chan(2, dtype=gos.Signal) signal.Notify(ch1, syscall.SIGUSR1) signal.Notify(ch12, syscall.SIGUSR1, syscall.SIGUSR2) signal.Notify(ch2, syscall.SIGUSR2) def _(): signal.Reset() defer(_) for i in range(N): # raise SIGUSR1 -> should be delivered to ch1 and ch12 assert len(ch1) == 0 assert len(ch12) == 0 assert len(ch2) == 0 killme(syscall.SIGUSR1) waitfor(lambda: len(ch1) == 1 and len(ch12) == 1) sig1 = ch1.recv() sig12 = ch12.recv() assert sig1 == syscall.SIGUSR1 assert sig12 == syscall.SIGUSR1 # raise SIGUSR2 -> should be delivered to ch12 and ch2 assert len(ch1) == 0 assert len(ch12) == 0 assert len(ch2) == 0 killme(syscall.SIGUSR2) waitfor(lambda: len(ch12) == 1 and len(ch2) == 1) sig12 = ch12.recv() sig2 = ch2.recv() assert sig12 == syscall.SIGUSR2 assert sig2 == syscall.SIGUSR2 # if SIGUSR2 will be eventually delivered to ch1 - it will break # in SIGUSR1 check on next iteration. # Stop(ch2) -> signals should not be delivered to ch2 anymore signal.Stop(ch2) for i in range(N): # USR1 -> ch1, ch12 assert len(ch1) == 0 assert len(ch12) == 0 assert len(ch2) == 0 killme(syscall.SIGUSR1) waitfor(lambda: len(ch1) == 1 and len(ch12) == 1) sig1 = ch1.recv() sig12 = ch12.recv() assert sig1 == syscall.SIGUSR1 assert sig12 == syscall.SIGUSR1 # USR2 -> ch12, !ch2 assert len(ch1) == 0 assert len(ch12) == 0 assert len(ch2) == 0 killme(syscall.SIGUSR2) waitfor(lambda: len(ch12) == 1) sig12 = ch12.recv() assert sig12 == syscall.SIGUSR2 # if SIGUSR2 will be eventually delivered to ch2 - it will break on # next iteration. # Ignore(USR1) -> ch1 should not be delivered to anymore, ch12 should be delivered only USR2 signal.Ignore(syscall.SIGUSR1) for i in range(N): # USR1 -> ø assert len(ch1) == 0 assert len(ch12) == 0 assert len(ch2) == 0 killme(syscall.SIGUSR1) time.sleep(1E-6) # USR2 -> ch12 assert len(ch1) == 0 assert len(ch12) == 0 assert len(ch2) == 0 killme(syscall.SIGUSR2) waitfor(lambda: len(ch12) == 1) sig12 = ch12.recv() assert sig12 == syscall.SIGUSR2 # if SIGUSR1 or SIGUSR2 will be eventually delivered to ch1 or ch2 - it # will break on next iteration. # Notify after Ignore signal.Notify(ch1, syscall.SIGUSR1) for i in range(N): # USR1 -> ch1 assert len(ch1) == 0 assert len(ch12) == 0 assert len(ch2) == 0 killme(syscall.SIGUSR1) waitfor(lambda: len(ch1) == 1) sig1 = ch1.recv() assert sig1 == syscall.SIGUSR1 # USR2 -> ch12 assert len(ch1) == 0 assert len(ch12) == 0 assert len(ch2) == 0 killme(syscall.SIGUSR2) waitfor(lambda: len(ch12) == 1) sig12 = ch12.recv() assert sig12 == syscall.SIGUSR2
def test_stdlib_interop_KeyboardInterrupt(): # KeyboardInterrupt is raised by default with raises(KeyboardInterrupt): killme(syscall.SIGINT) time.sleep(1) ch1 = chan(2, dtype=gos.Signal) ch2 = chan(2, dtype=gos.Signal) def _(): signal.Reset(syscall.SIGINT) defer(_) # Notify disables raising KeyboardInterrupt by default on SIGINT signal.Notify(ch1, syscall.SIGINT) try: killme(syscall.SIGINT) waitfor(lambda: len(ch1) == 1) obj1 = ch1.recv() assert obj1 == syscall.SIGINT time.sleep(0.1) # just in case except KeyboardInterrupt: raise AssertionError("KeyboardInterrupt raised after signal.Notify +ch1") signal.Notify(ch2, syscall.SIGINT) try: killme(syscall.SIGINT) waitfor(lambda: len(ch1) == 1 and len(ch2) == 1) obj1 = ch1.recv() obj2 = ch2.recv() assert obj1 == syscall.SIGINT assert obj2 == syscall.SIGINT time.sleep(0.1) # just in case except KeyboardInterrupt: raise AssertionError("KeyboardInterrupt raised after signal.Notify +ch1 +ch2") # last Stop should reenable raising KeyboardInterrupt by default on SIGINT signal.Stop(ch1) try: killme(syscall.SIGINT) waitfor(lambda: len(ch2) == 1) obj2 = ch2.recv() assert obj2 == syscall.SIGINT time.sleep(0.1) # just in case assert len(ch1) == 0 except KeyboardInterrupt: raise AssertionError("KeyboardInterrupt raised after signal.Notify +ch1 +ch2 -ch1") signal.Stop(ch2) with raises(KeyboardInterrupt): killme(syscall.SIGINT) time.sleep(1) time.sleep(0.1) # just in case assert len(ch1) == 0 assert len(ch2) == 0 # Ignore disables raising KeyboardInterrupt by default on SIGINT signal.Ignore(syscall.SIGINT) try: killme(syscall.SIGINT) time.sleep(0.1) assert len(ch1) == 0 assert len(ch2) == 0 except KeyboardInterrupt: raise AssertionError("KeyboardInterrupt raised after signal.Ignore") # Reset restores original behaviour signal.Reset(syscall.SIGINT) with raises(KeyboardInterrupt): killme(syscall.SIGINT) time.sleep(1) time.sleep(0.1) # just in case assert len(ch1) == 0 assert len(ch2) == 0
def test_stdlib_interop(): import signal as pysig ch1 = chan(2, dtype=object) # NOTE not gos.Signal nor 'C.os::Signal' def _(signo, frame): ch1.send("USR1") pysig.signal(pysig.SIGUSR1, _) def _(): pysig.signal(pysig.SIGUSR1, pysig.SIG_IGN) defer(_) # verify that plain pysig delivery works for i in range(N): assert len(ch1) == 0 killme(syscall.SIGUSR1) waitfor(lambda: len(ch1) == 1) obj1 = ch1.recv() assert obj1 == "USR1" # verify that combined pysig + golang.os.signal delivery works ch2 = chan(2, dtype=gos.Signal) signal.Notify(ch2, syscall.SIGUSR1) def _(): signal.Stop(ch2) defer(_) for i in range(N): assert len(ch1) == 0 assert len(ch2) == 0 killme(syscall.SIGUSR1) waitfor(lambda: len(ch1) == 1 and len(ch2) == 1) obj1 = ch1.recv() sig2 = ch2.recv() assert obj1 == "USR1" assert sig2 == syscall.SIGUSR1 # Ignore stops delivery to both pysig and golang.os.signal signal.Ignore(syscall.SIGUSR1) for i in range(N): assert len(ch1) == 0 assert len(ch2) == 0 killme(syscall.SIGUSR1) time.sleep(1E-6) time.sleep(0.1) # just in case assert len(ch1) == 0 assert len(ch2) == 0 # after Reset pysig delivery is restored even after Ignore signal.Reset(syscall.SIGUSR1) for i in range(N): assert len(ch1) == 0 assert len(ch2) == 0 killme(syscall.SIGUSR1) waitfor(lambda: len(ch1) == 1) assert len(ch2) == 0 obj1 = ch1.recv() assert obj1 == "USR1" # Reset stops delivery to golang.os.signal and restores pysig delivery signal.Notify(ch2, syscall.SIGUSR1) signal.Reset(syscall.SIGUSR1) for i in range(N): assert len(ch1) == 0 assert len(ch2) == 0 killme(syscall.SIGUSR1) waitfor(lambda: len(ch1) == 1) assert len(ch2) == 0 obj1 = ch1.recv() assert obj1 == "USR1"
def test_workgroup_with(): # verify with support for sync.WorkGroup ctx, cancel = context.with_cancel(context.background()) defer(cancel) mu = sync.Mutex() # t1=ok, t2=ok l = [0, 0] with sync.WorkGroup(ctx) as wg: for i in range(2): def _(ctx, i): with mu: l[i] = i+1 wg.go(_, i) assert l == [1, 2] # t1=fail, t2=wait cancel, fail with raises(MyError) as exci: with sync.WorkGroup(ctx) as wg: def _(ctx): Iam_t1 = 0 raise MyError('hello (fail)') wg.go(_) def _(ctx): ctx.done().recv() raise MyError('world (after zzz)') wg.go(_) e = exci.value assert e.__class__ is MyError assert e.args == ('hello (fail)',) assert e.__cause__ is None assert e.__context__ is None assert e.__suppress_context__ == False if PyErr_Restore_traceback_ok: assert 'Iam_t1' in exci.traceback[-1].locals # t=ok, but code from under with raises l = [0] with raises(MyError) as exci: with sync.WorkGroup(ctx) as wg: def _(ctx): l[0] = 1 wg.go(_) def bad(): raise MyError('wow') bad() e = exci.value assert e.__class__ is MyError assert e.args == ('wow',) assert e.__cause__ is None assert e.__context__ is None assert e.__suppress_context__ == False assert exci.traceback[-1].name == 'bad' assert l[0] == 1 # t=fail, code from under with also raises with raises(MyError) as exci: with sync.WorkGroup(ctx) as wg: def f(ctx): raise MyError('fail from go') wg.go(f) def g(): raise MyError('just raise') g() e = exci.value assert e.__class__ is MyError assert e.args == ('fail from go',) assert e.__cause__ is None assert e.__context__ is not None assert e.__suppress_context__ == False assert exci.traceback[-1].name == 'f' e2 = e.__context__ assert e2.__class__ is MyError assert e2.args == ('just raise',) assert e2.__cause__ is None assert e2.__context__ is None assert e2.__suppress_context__ == False assert e2.__traceback__ is not None t2 = Traceback(e2.__traceback__) assert t2[-1].name == 'g'
def main(): defer(d3) defer(d2) defer(d1) raise RuntimeError("err")