From 3ccc99fe709a18a48a07531d4e5c761eb7eb19c3 Mon Sep 17 00:00:00 2001 From: Reece Williams Date: Thu, 24 Oct 2024 13:15:14 +0400 Subject: [PATCH 1/3] wip --- docs/scripts/main.py | 103 ++++++++++++++++++ .../03-demos/03-tokenfactory.md | 12 +- 2 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 docs/scripts/main.py diff --git a/docs/scripts/main.py b/docs/scripts/main.py new file mode 100644 index 0000000..c9bd122 --- /dev/null +++ b/docs/scripts/main.py @@ -0,0 +1,103 @@ +import os +import re +import subprocess +import time + +import requests + +# current file location +curr_dir = os.path.dirname(os.path.realpath(__file__)) +parent_dir = os.path.dirname(curr_dir) + +# Commands which block the process +BLOCKING_START_COMMANDS = ["local-ic start", "make testnet", "make sh-testnet"] +ignore_commands = ["gh repo create"] + +DEBUGGING = False + +def main(): + demos = os.path.join(parent_dir, "versioned_docs", "version-v0.50.x", "03-demos") + with open(os.path.join(demos, "03-tokenfactory.md"), "r") as f: + parse_docs(f.read()) + +def poll_for_start(API_URL: str, pid=-1, waitSeconds=300): + for i in range(waitSeconds + 1): + try: + requests.get(API_URL) + return + except Exception: + # if i % 5 == 0: + print(f"waiting for server to start (iter:{i}) ({API_URL})") + time.sleep(1) + + if pid != -1: + print(f"Killing process with pid for failing to start: {pid}") + os.system(f"kill {pid}") + + raise Exception("Server did not start") + +def clean_lines(text: str) -> list[str]: + # remove comment lines + sec = [line for line in text.split("\n") if not line.startswith("#")] + + + for l in sec: + l = l.strip() + # if l starts with any in ignore-commands, remove the line from the section + if any([l.startswith(cmd) for cmd in ignore_commands]): + sec.remove(l) + + # remove blank lines + sec = [line for line in sec if line.strip() != ""] + + return sec + +def get_env_variables_from_lines(lines: list[str]) -> dict[str, str]: + env_vars = {} + for line in lines: + # DENOM=factory/roll1hj5fveer5cjtn4wd6wstzugjfdxzl0xpg2te87/mytoken + match = re.match(r"([A-Z_]+)=(.*)", line) + if match: + print("MATCH:", match) + env_vars[match.group(1)] = match.group(2) + + return env_vars + +def parse_docs(text: str): + + + # split the text by ```bash + sections = text.split("```bash") + for section in sections[1:]: # first section has nothing we want + end = section.find("```") + sec = clean_lines(section[:end]) + + if len(sec) == 0: + # print("debugging: empty (commands are ignored / there is nothing here)") + continue + + secStr = "\n".join(sec) + + print("="*20, "\n", secStr, "\n", "="*20) + + # run sec as the os terminal + if not DEBUGGING: + # if sec contains anything from within BLOCKING_START_COMMANDS, it should run in its + # own terminal + + envs = get_env_variables_from_lines(sec) # TODO: keep up with these globally and unset at end? so we dont polute test + for k, v in envs.items(): + os.environ[k] = v + + if any([cmd in sec for cmd in BLOCKING_START_COMMANDS]): + pass + + # pid = subprocess.Popen(secStr, shell=True) + # poll_for_start("http://127.0.0.1:26657", pid, waitSeconds=25) + # else: + # os.system(sec) + + input("Press Enter to continue...") + +if __name__ == '__main__': + main() diff --git a/docs/versioned_docs/version-v0.50.x/03-demos/03-tokenfactory.md b/docs/versioned_docs/version-v0.50.x/03-demos/03-tokenfactory.md index 8421d19..c3614bc 100644 --- a/docs/versioned_docs/version-v0.50.x/03-demos/03-tokenfactory.md +++ b/docs/versioned_docs/version-v0.50.x/03-demos/03-tokenfactory.md @@ -66,7 +66,7 @@ The `denom_creation_fee` is a cost the application can set for creating new toke The `denom_creation_gas_consume` is the amount of indirect resource cost to consume for creating a new token. It is a more indirect approach to charging and is a better experience overall for developers on a network. -```bash +```text params: denom_creation_fee: [] denom_creation_gas_consume: "100000" @@ -92,7 +92,7 @@ rolld q tokenfactory denoms-from-creator $(rolld keys show acc0 -a)
denoms-from-creator output -```bash +```text denoms: - factory/roll1hj5fveer5cjtn4wd6wstzugjfdxzl0xpg2te87/mytoken ``` @@ -129,7 +129,7 @@ rolld q bank denom-metadata $DENOM
bank denom-metadata output -```bash +```text metadata: base: factory/roll1hj5fveer5cjtn4wd6wstzugjfdxzl0xpg2te87/mytoken denom_units: @@ -162,7 +162,7 @@ rolld q bank balances $(rolld keys show acc0 -a)
bank balances output -```bash +```text balances: - amount: "5000000" denom: factory/roll1hj5fveer5cjtn4wd6wstzugjfdxzl0xpg2te87/mytoken @@ -189,7 +189,7 @@ rolld q bank balances $(rolld keys show acc1 -a)
mint-to output -```bash +```text balances: - amount: "1000000" denom: factory/roll1hj5fveer5cjtn4wd6wstzugjfdxzl0xpg2te87/mytoken @@ -206,7 +206,7 @@ note, you can check for just a specific token balance with rolld q bank balance $(rolld keys show acc0 -a) $DENOM ``` -```bash +```text balance: amount: "5000000" denom: factory/roll1hj5fveer5cjtn4wd6wstzugjfdxzl0xpg2te87/mytoken From 0c96fbfa4b4c83dadbc727615b4849ea7ad41b23 Mon Sep 17 00:00:00 2001 From: Reece Williams Date: Thu, 24 Oct 2024 13:19:42 +0400 Subject: [PATCH 2/3] working base --- docs/scripts/main.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/docs/scripts/main.py b/docs/scripts/main.py index c9bd122..f6ac195 100644 --- a/docs/scripts/main.py +++ b/docs/scripts/main.py @@ -40,7 +40,6 @@ def clean_lines(text: str) -> list[str]: # remove comment lines sec = [line for line in text.split("\n") if not line.startswith("#")] - for l in sec: l = l.strip() # if l starts with any in ignore-commands, remove the line from the section @@ -58,7 +57,6 @@ def get_env_variables_from_lines(lines: list[str]) -> dict[str, str]: # DENOM=factory/roll1hj5fveer5cjtn4wd6wstzugjfdxzl0xpg2te87/mytoken match = re.match(r"([A-Z_]+)=(.*)", line) if match: - print("MATCH:", match) env_vars[match.group(1)] = match.group(2) return env_vars @@ -88,14 +86,13 @@ def parse_docs(text: str): envs = get_env_variables_from_lines(sec) # TODO: keep up with these globally and unset at end? so we dont polute test for k, v in envs.items(): os.environ[k] = v + print(f"{k}={v}") if any([cmd in sec for cmd in BLOCKING_START_COMMANDS]): - pass - - # pid = subprocess.Popen(secStr, shell=True) - # poll_for_start("http://127.0.0.1:26657", pid, waitSeconds=25) - # else: - # os.system(sec) + pid = subprocess.Popen(secStr, shell=True) + poll_for_start("http://127.0.0.1:26657", pid, waitSeconds=25) + else: + os.system(secStr) input("Press Enter to continue...") From df5d51161048854972c1022205b7df8176ce58d1 Mon Sep 17 00:00:00 2001 From: Reece Williams Date: Thu, 24 Oct 2024 13:30:31 +0400 Subject: [PATCH 3/3] wip --- docs/scripts/main.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/docs/scripts/main.py b/docs/scripts/main.py index f6ac195..c9eb5cc 100644 --- a/docs/scripts/main.py +++ b/docs/scripts/main.py @@ -13,6 +13,7 @@ BLOCKING_START_COMMANDS = ["local-ic start", "make testnet", "make sh-testnet"] ignore_commands = ["gh repo create"] +START_PID = -1 DEBUGGING = False def main(): @@ -47,9 +48,7 @@ def clean_lines(text: str) -> list[str]: sec.remove(l) # remove blank lines - sec = [line for line in sec if line.strip() != ""] - - return sec + return [line for line in sec if line.strip() != ""] def get_env_variables_from_lines(lines: list[str]) -> dict[str, str]: env_vars = {} @@ -62,7 +61,7 @@ def get_env_variables_from_lines(lines: list[str]) -> dict[str, str]: return env_vars def parse_docs(text: str): - + global START_PID # split the text by ```bash sections = text.split("```bash") @@ -83,18 +82,24 @@ def parse_docs(text: str): # if sec contains anything from within BLOCKING_START_COMMANDS, it should run in its # own terminal - envs = get_env_variables_from_lines(sec) # TODO: keep up with these globally and unset at end? so we dont polute test + # TODO: keep up with these globally and unset at end? so we dont polute test + envs = get_env_variables_from_lines(sec) for k, v in envs.items(): os.environ[k] = v print(f"{k}={v}") if any([cmd in sec for cmd in BLOCKING_START_COMMANDS]): - pid = subprocess.Popen(secStr, shell=True) - poll_for_start("http://127.0.0.1:26657", pid, waitSeconds=25) + START_PID = subprocess.Popen(secStr, shell=True).pid + print(f"Started process with pid: {START_PID}") + poll_for_start("http://127.0.0.1:26657", START_PID, waitSeconds=60) else: os.system(secStr) + time.sleep(2) + + # input("--- Press Enter to continue...") - input("Press Enter to continue...") + # TODO: verification logic here + # Kill / cleanup the instance (direct from pid would be nice, or just a killall ) if __name__ == '__main__': main()