Skip to content

Commit

Permalink
test python scripts in github actions (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
heyAyushh authored Jun 4, 2024
1 parent 5ca6556 commit 5acc306
Show file tree
Hide file tree
Showing 18 changed files with 195 additions and 26 deletions.
14 changes: 14 additions & 0 deletions .ci/.exclude_files
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.venv/*
hello.py
.ci/scripts.py
code/nfts/upload-arweave/upload-arweave.en.py
code/local-development/airdropping-sol/airdropping-sol.en.py
code/basic-transactions/sending-sol/sending-sol.en.py
code/local-development/connecting-websocket/connecting-websocket.en.py
code/anchor/testing-with-anchor/client/testing_with_anchor.py
code/keypairs-and-wallets/vanity-publickeys/vanity-publickeys.en.py
code/serialization/clientdata/python.client.data.py
code/serialization/primitives/python.demo_primitives.py
code/serialization/instruction/python.client.py
code/local-development/connecting-cluster/connecting-cluster.en.py
code/local-development/connecting-private-cluster/connecting-private-cluster.en.py
43 changes: 43 additions & 0 deletions .ci/scripts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import os
import re
import subprocess
from concurrent.futures import ThreadPoolExecutor

exclude_file = '.ci/.exclude_files'
root_dir = os.path.curdir

with open(exclude_file, 'r') as f:
exclude_patterns = [re.compile(line.strip()) for line in f.readlines()]

def find_py_files(root_dir, exclude_patterns):
py_files = []
for root, dirs, files in os.walk(root_dir):
for file in files:
file_path = os.path.join(root, file)
relative_path = os.path.relpath(file_path, root_dir)
if file.endswith('.py') and ".preview." not in file:
ignore_file = any(pattern.search(relative_path) for pattern in exclude_patterns)
if not ignore_file:
py_files.append(relative_path)
return py_files

def execute_file(file_path):
result = subprocess.run(['python', file_path], capture_output=True, text=True)
return result.stdout, result.stderr, file_path

def main():
files_to_execute = find_py_files(root_dir, exclude_patterns)
print("Executing these python files:")
print("\n".join(files_to_execute))

with ThreadPoolExecutor() as executor:
futures = [executor.submit(execute_file, file) for file in files_to_execute]
for future in futures:
stdout, stderr, file_path = future.result()
print(f'\nExecuting {file_path}:\n\n Output: \n\r {stdout}')
if stderr:
print(f'\nError from {file_path}:\n\n Error: {stderr}')


if __name__ == "__main__":
main()
42 changes: 42 additions & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Test Python Files

on:
schedule:
- cron: "0 0 * * *"
push:
branches:
- master
pull_request:
types: [opened, synchronize, reopened]
branches:
- master

jobs:
run-py:
strategy:
matrix:
py-version: [3.9]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{matrix.py-version}}
cache: pip

- name: Create, activate virtual environment and Install dependencies
run: |
python -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt --upgrade pip
echo "$VIRTUAL_ENV/bin" >> $GITHUB_PATH
echo "VIRTUAL_ENV=$VIRTUAL_ENV" >> $GITHUB_ENV
- name: Run python files
run: |
source .venv/bin/activate
python .ci/scripts.py
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ node_modules
.DS_Store
**/*/target
.vscode
.history
.history
.venv
3 changes: 2 additions & 1 deletion code/basic-transactions/sending-sol/sending-sol.en.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
lamports=1_000_000)
))

client.send_transaction(transaction, sender)
result = client.send_transaction(transaction, sender)
print(result)
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@
off_curve_address = Pubkey.from_string('4BJXYkfvg37zEmBbsacZjeQDpTNx91KppxFJxRqrz48e') # Valid public key
print(off_curve_address.is_on_curve()) # Not on the ed25519 curve, therefore not suitable for users

error_pubkey = Pubkey.from_string("testPubkey"); # Is not a valid public key
try:
error_pubkey = Pubkey.from_string("testPubkey"); # Is not a valid public key
except:
print("testPubkey is not a valid public key as expected!")
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
from solders.keypair import Keypair
import based58

keypair = Keypair()
keypair = Keypair()
print("Keypair PublicKey: ", keypair.pubkey())

rawPK = bytes(keypair)
pk = based58.b58encode(rawPK).decode()
print("Keypair Secret: ", pk)
3 changes: 2 additions & 1 deletion code/keypairs-and-wallets/generate-mnemonic/from-bip39.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from mnemonic import Mnemonic

mnemo = Mnemonic("english")
words = mnemo.generate(strength=256)
words = mnemo.generate(strength=256)
print(words)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from solders.keypair import Keypair

b58_string = "5MaiiCavjCmn9Hs1o3eznqDEhRwxo7pXiAYez7keQUviUkauRiTMD8DrESdrNjN8zd9mTmVhRvBJeg5vhyvgrAhG"
keypair = Keypair.from_string(b58_string)
keypair = Keypair.from_base58_string(b58_string)
print("Created Keypair with Public Key: {}".format(keypair.pubkey()))
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
b58_string = "5MaiiCavjCmn9Hs1o3eznqDEhRwxo7pXiAYez7keQUviUkauRiTMD8DrESdrNjN8zd9mTmVhRvBJeg5vhyvgrAhG"
keypair = Keypair.from_string(b58_string)
keypair = Keypair.from_base58_string(b58_string)
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mnemo = Mnemonic("english")
seed = mnemo.to_seed("pill tomorrow foster begin walnut borrow virtual kick shift mutual shoe scatter")
keypair = Keypair.from_bytes(seed)
phrase = "pill tomorrow foster begin walnut borrow virtual kick shift mutual shoe scatter"
seed = mnemo.to_seed(phrase)
keypair = Keypair.from_seed(seed[:32])
6 changes: 4 additions & 2 deletions code/keypairs-and-wallets/mnemonic-to-keypair/from-bip39.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from mnemonic import Mnemonic

mnemo = Mnemonic("english")
seed = mnemo.to_seed("pill tomorrow foster begin walnut borrow virtual kick shift mutual shoe scatter")
keypair = Keypair.from_bytes(seed)
phrase = "pill tomorrow foster begin walnut borrow virtual kick shift mutual shoe scatter"
seed = mnemo.to_seed(phrase)
keypair = Keypair.from_seed(seed[:32])
# Pubkey 5ZWj7a1f8tWkjBESHKgrLmXshuXxqeY9SYcfbshpAqPG
print("Created Keypair with Public Key: {}".format(keypair.pubkey()))
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
mnemo = Mnemonic("english")
seed = mnemo.to_seed("neither lonely flavor argue grass remind eye tag avocado spot unusual intact")
seed = mnemo.to_seed("pill tomorrow foster begin walnut borrow virtual kick shift mutual shoe scatter")

hd = HDKey.from_master_seed(seed)
keypairs = []
for i in range(10):
path = f"m/44'/501'/{i}'/0'"
keypair = Keypair.from_bytes(hd.derive(path).private_key)
print(f"{path} => {keypair.public_key()}")
derivation_path = f"m/44'/501'/{i}'/0'"
keypair = Keypair.from_seed_and_derivation_path(seed, derivation_path)
keypairs.append(keypair)
print(f"Keypair {i + 1} with Public Key: {keypair.pubkey()}")
15 changes: 8 additions & 7 deletions code/keypairs-and-wallets/mnemonic-to-keypair/from-bip44.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from solana.keypair import Keypair
from solders.keypair import Keypair
from mnemonic import Mnemonic
from hdkey import HDKey

mnemo = Mnemonic("english")
seed = mnemo.to_seed("neither lonely flavor argue grass remind eye tag avocado spot unusual intact")
mnemonic_phrase = "pill tomorrow foster begin walnut borrow virtual kick shift mutual shoe scatter"
seed = mnemo.to_seed(mnemonic_phrase)

hd = HDKey.from_master_seed(seed)
keypairs = []
for i in range(10):
path = f"m/44'/501'/{i}'/0'"
keypair = Keypair.from_bytes(hd.derive(path).private_key)
print(f"{path} => {keypair.public_key()}")
derivation_path = f"m/44'/501'/{i}'/0'"
keypair = Keypair.from_seed_and_derivation_path(seed, derivation_path)
keypairs.append(keypair)
print(f"Keypair {i + 1} with Public Key: {keypair.pubkey()}")
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
signature = keypair.sign_message(message)
verify_sign = signature.verify(keypair.pubkey(), message)

print(verify_sign) # bool
print(verify_sign) # True
2 changes: 1 addition & 1 deletion code/serialization/primitives/python.demo_primitives.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from borsh_construct import U8, U16, U32, String, HashMap
from borsh_construct import U8, U16, U32, CStruct, String, HashMap

# Schema to deserialize various types
primitive_schema = CStruct(
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"vuepress": "^2.0.0-beta.36"
},
"lint-staged": {
"*.{js,css,ts}": "prettier --write"
"*.{js,css,ts,py}": "prettier --write"
},
"dependencies": {
"http-server": "^14.1.1",
Expand Down
53 changes: 53 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
anchorpy==0.20.1
anchorpy-core==0.2.0
anyio==4.4.0
asn1crypto==1.5.1
attrs==23.2.0
base58==2.1.1
based58==0.1.1
borsh-construct==0.1.0
cbor2==5.6.3
certifi==2024.6.2
cffi==1.16.0
charset-normalizer==3.3.2
coincurve==20.0.0
construct==2.10.68
construct-typing==0.5.6
crcmod==1.7
ecdsa==0.19.0
ed25519==1.5
ed25519-blake2b==1.4.1
exceptiongroup==1.2.1
fastecdsa==2.3.2
h11==0.14.0
httpcore==1.0.5
httpx==0.27.0
idna==3.7
iniconfig==2.0.0
jsonalias==0.1.1
mnemonic==0.21
more-itertools==8.14.0
numpy==1.26.4
packaging==24.0
pluggy==1.5.0
py-sr25519-bindings==0.2.0
pycparser==2.22
pycryptodome==3.20.0
pyheck==0.1.5
PyNaCl==1.5.0
pytest==8.2.1
pytest-asyncio==0.23.7
requests==2.32.3
riemann-secpy256k1==0.2.8
six==1.16.0
sniffio==1.3.1
solana==0.34.0
solders==0.21.0
SQLAlchemy==2.0.30
sumtypes==0.1a6
toml==0.10.2
tomli==2.0.1
toolz==0.11.2
typing_extensions==4.12.1
urllib3==2.2.1
websockets==10.4

0 comments on commit 5acc306

Please sign in to comment.