Skip to content

Instantly share code, notes, and snippets.

@webmaster128
Last active June 27, 2018 21:44
Show Gist options
  • Save webmaster128/77754aff4577651944bf3118d89e267f to your computer and use it in GitHub Desktop.
Save webmaster128/77754aff4577651944bf3118d89e267f to your computer and use it in GitHub Desktop.
Lisk lisk_bridge.sh just for custom commands. Usage: `python lisk_bridge_light.py -h 5594490 -c "echo 123 | hexdump -C"`
try:
import urllib2 as urlreq # Python 2.x
except:
import urllib.request as urlreq # Python 3.x
import json
import subprocess
import sys
import time
import argparse
parser = argparse.ArgumentParser(description='Lisk bridge light', add_help=False)
parser.add_argument('-h', '--height', dest='height', required=True,
type=int,
nargs=1, metavar=("height"))
parser.add_argument('-c', '--command', dest='command', required=True,
type=str,
help="command to execute (in shell) when target height is reached",
nargs=1, metavar=("command"))
args = parser.parse_args()
cutoff = args.height[0]
command = args.command[0]
print("Executing '{}' when target height is reached.".format(command))
height = None
while height is None or height < cutoff:
time.sleep(0.5)
req = urlreq.Request("http://localhost:7000/api/loader/status/sync")
try:
body = json.load(urlreq.urlopen(req))
height = body["height"]
remaining_blocks = cutoff-height
remaining_time = remaining_blocks*10
min, sec = divmod(remaining_time, 60)
hours, min = divmod(min, 60)
print("height: {}; target: {}; remaining: {} blocks ({}h {}m {}s)".format(height, cutoff, remaining_blocks, hours, min, sec))
except:
print("Could not get height")
height = None
if height != cutoff:
print("Cutoff not met. Aborting.")
sys.exit(1)
print("Executing '{}' now...".format(command))
subprocess.check_call(command, shell=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment