Fix Python 3 syntax errors in zonename_updater.py

This commit is contained in:
cclauss 2019-05-16 19:39:16 +02:00 committed by GitHub
parent 20371895c9
commit fd46f141ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,3 +1,4 @@
from __future__ import print_function
import time import time
import json import json
import os import os
@ -5,12 +6,14 @@ import sys
import re import re
import socket import socket
from six import string_types
from subprocess import call from subprocess import call
from bitcoinrpc.authproxy import AuthServiceProxy from bitcoinrpc.authproxy import AuthServiceProxy
def publish(): def publish():
print "* Signing and Publishing..." print("* Signing and Publishing...")
call(" ".join(command_sign_publish), shell=True) call(" ".join(command_sign_publish), shell=True)
@ -19,12 +22,12 @@ def processNameOp(domain, value, test=False):
return False return False
try: try:
data = json.loads(value) data = json.loads(value)
except Exception, err: except Exception as err:
print "Json load error: %s" % err print("Json load error: %s" % err)
return False return False
if "zeronet" not in data and "map" not in data: if "zeronet" not in data and "map" not in data:
# Namecoin standard use {"map": { "blog": {"zeronet": "1D..."} }} # Namecoin standard use {"map": { "blog": {"zeronet": "1D..."} }}
print "No zeronet and no map in ", data.keys() print("No zeronet and no map in ", data.keys())
return False return False
if "map" in data: if "map" in data:
# If subdomains using the Namecoin standard is present, just re-write in the Zeronet way # If subdomains using the Namecoin standard is present, just re-write in the Zeronet way
@ -34,7 +37,7 @@ def processNameOp(domain, value, test=False):
for subdomain in data_map: for subdomain in data_map:
if "zeronet" in data_map[subdomain]: if "zeronet" in data_map[subdomain]:
new_value[subdomain] = data_map[subdomain]["zeronet"] new_value[subdomain] = data_map[subdomain]["zeronet"]
if "zeronet" in data and isinstance(data["zeronet"], basestring): if "zeronet" in data and isinstance(data["zeronet"], string_types):
# { # {
# "zeronet":"19rXKeKptSdQ9qt7omwN82smehzTuuq6S9", # "zeronet":"19rXKeKptSdQ9qt7omwN82smehzTuuq6S9",
# .... # ....
@ -44,23 +47,23 @@ def processNameOp(domain, value, test=False):
return processNameOp(domain, json.dumps({"zeronet": new_value}), test) return processNameOp(domain, json.dumps({"zeronet": new_value}), test)
else: else:
return False return False
if "zeronet" in data and isinstance(data["zeronet"], basestring): if "zeronet" in data and isinstance(data["zeronet"], string_types):
# { # {
# "zeronet":"19rXKeKptSdQ9qt7omwN82smehzTuuq6S9" # "zeronet":"19rXKeKptSdQ9qt7omwN82smehzTuuq6S9"
# } is valid # } is valid
return processNameOp(domain, json.dumps({"zeronet": { "": data["zeronet"]}}), test) return processNameOp(domain, json.dumps({"zeronet": { "": data["zeronet"]}}), test)
if not isinstance(data["zeronet"], dict): if not isinstance(data["zeronet"], dict):
print "Not dict: ", data["zeronet"] print("Not dict: ", data["zeronet"])
return False return False
if not re.match("^[a-z0-9]([a-z0-9-]{0,62}[a-z0-9])?$", domain): if not re.match("^[a-z0-9]([a-z0-9-]{0,62}[a-z0-9])?$", domain):
print "Invalid domain: ", domain print("Invalid domain: ", domain)
return False return False
if test: if test:
return True return True
if "slave" in sys.argv: if "slave" in sys.argv:
print "Waiting for master update arrive" print("Waiting for master update arrive")
time.sleep(30) # Wait 30 sec to allow master updater time.sleep(30) # Wait 30 sec to allow master updater
# Note: Requires the file data/names.json to exist and contain "{}" to work # Note: Requires the file data/names.json to exist and contain "{}" to work
@ -69,32 +72,32 @@ def processNameOp(domain, value, test=False):
for subdomain, address in data["zeronet"].items(): for subdomain, address in data["zeronet"].items():
subdomain = subdomain.lower() subdomain = subdomain.lower()
address = re.sub("[^A-Za-z0-9]", "", address) address = re.sub("[^A-Za-z0-9]", "", address)
print subdomain, domain, "->", address print(subdomain, domain, "->", address)
if subdomain: if subdomain:
if re.match("^[a-z0-9]([a-z0-9-]{0,62}[a-z0-9])?$", subdomain): if re.match("^[a-z0-9]([a-z0-9-]{0,62}[a-z0-9])?$", subdomain):
names["%s.%s.bit" % (subdomain, domain)] = address names["%s.%s.bit" % (subdomain, domain)] = address
else: else:
print "Invalid subdomain:", domain, subdomain print("Invalid subdomain:", domain, subdomain)
else: else:
names["%s.bit" % domain] = address names["%s.bit" % domain] = address
new_names_raw = json.dumps(names, indent=2, sort_keys=True) new_names_raw = json.dumps(names, indent=2, sort_keys=True)
if new_names_raw != names_raw: if new_names_raw != names_raw:
open(names_path, "wb").write(new_names_raw) open(names_path, "wb").write(new_names_raw)
print "-", domain, "Changed" print("-", domain, "Changed")
return True return True
else: else:
print "-", domain, "Not changed" print("-", domain, "Not changed")
return False return False
def processBlock(block_id, test=False): def processBlock(block_id, test=False):
print "Processing block #%s..." % block_id print("Processing block #%s..." % block_id)
s = time.time() s = time.time()
block_hash = rpc.getblockhash(block_id) block_hash = rpc.getblockhash(block_id)
block = rpc.getblock(block_hash) block = rpc.getblock(block_hash)
print "Checking %s tx" % len(block["tx"]) print("Checking %s tx" % len(block["tx"]))
updated = 0 updated = 0
for tx in block["tx"]: for tx in block["tx"]:
try: try:
@ -103,9 +106,9 @@ def processBlock(block_id, test=False):
if "scriptPubKey" in vout and "nameOp" in vout["scriptPubKey"] and "name" in vout["scriptPubKey"]["nameOp"]: if "scriptPubKey" in vout and "nameOp" in vout["scriptPubKey"] and "name" in vout["scriptPubKey"]["nameOp"]:
name_op = vout["scriptPubKey"]["nameOp"] name_op = vout["scriptPubKey"]["nameOp"]
updated += processNameOp(name_op["name"].replace("d/", ""), name_op["value"], test) updated += processNameOp(name_op["name"].replace("d/", ""), name_op["value"], test)
except Exception, err: except Exception as err:
print "Error processing tx #%s %s" % (tx, err) print("Error processing tx #%s %s" % (tx, err))
print "Done in %.3fs (updated %s)." % (time.time() - s, updated) print("Done in %.3fs (updated %s)." % (time.time() - s, updated))
return updated return updated
# Connecting to RPC # Connecting to RPC
@ -153,7 +156,7 @@ if not os.path.isfile(config_path): # Create sample config
open(config_path, "w").write( open(config_path, "w").write(
json.dumps({'site': 'site', 'zeronet_path': '/home/zeronet', 'privatekey': '', 'lastprocessed': 223910}, indent=2) json.dumps({'site': 'site', 'zeronet_path': '/home/zeronet', 'privatekey': '', 'lastprocessed': 223910}, indent=2)
) )
print "* Example config written to %s" % config_path print("* Example config written to %s" % config_path)
sys.exit(0) sys.exit(0)
config = json.load(open(config_path)) config = json.load(open(config_path))
@ -180,10 +183,10 @@ while 1:
last_block = int(rpc.getblockchaininfo()["blocks"]) last_block = int(rpc.getblockchaininfo()["blocks"])
break # Connection succeeded break # Connection succeeded
except socket.timeout: # Timeout except socket.timeout: # Timeout
print ".", print(".", end=' ')
sys.stdout.flush() sys.stdout.flush()
except Exception, err: except Exception as err:
print "Exception", err.__class__, err print("Exception", err.__class__, err)
time.sleep(5) time.sleep(5)
rpc = AuthServiceProxy(rpc_auth, timeout=rpc_timeout) rpc = AuthServiceProxy(rpc_auth, timeout=rpc_timeout)
@ -191,7 +194,7 @@ if not config["lastprocessed"]: # First startup: Start processing from last blo
config["lastprocessed"] = last_block config["lastprocessed"] = last_block
print "- Testing domain parsing..." print("- Testing domain parsing...")
assert processBlock(223911, test=True) # Testing zeronetwork.bit assert processBlock(223911, test=True) # Testing zeronetwork.bit
assert processBlock(227052, test=True) # Testing brainwallets.bit assert processBlock(227052, test=True) # Testing brainwallets.bit
assert not processBlock(236824, test=True) # Utf8 domain name (invalid should skip) assert not processBlock(236824, test=True) # Utf8 domain name (invalid should skip)
@ -200,7 +203,7 @@ assert processBlock(236870, test=True) # Encoded domain (should pass)
assert processBlock(438317, test=True) # Testing namecoin standard artifaxradio.bit (should pass) assert processBlock(438317, test=True) # Testing namecoin standard artifaxradio.bit (should pass)
# sys.exit(0) # sys.exit(0)
print "- Parsing skipped blocks..." print("- Parsing skipped blocks...")
should_publish = False should_publish = False
for block_id in range(config["lastprocessed"], last_block + 1): for block_id in range(config["lastprocessed"], last_block + 1):
if processBlock(block_id): if processBlock(block_id):
@ -211,7 +214,7 @@ if should_publish:
publish() publish()
while 1: while 1:
print "- Waiting for new block" print("- Waiting for new block")
sys.stdout.flush() sys.stdout.flush()
while 1: while 1:
try: try:
@ -220,13 +223,13 @@ while 1:
rpc.waitforblock() rpc.waitforblock()
else: else:
rpc.waitfornewblock() rpc.waitfornewblock()
print "Found" print("Found")
break # Block found break # Block found
except socket.timeout: # Timeout except socket.timeout: # Timeout
print ".", print(".", end=' ')
sys.stdout.flush() sys.stdout.flush()
except Exception, err: except Exception as err:
print "Exception", err.__class__, err print("Exception", err.__class__, err)
time.sleep(5) time.sleep(5)
rpc = AuthServiceProxy(rpc_auth, timeout=rpc_timeout) rpc = AuthServiceProxy(rpc_auth, timeout=rpc_timeout)