Skip to content

Commit 5acc306

Browse files
authored
test python scripts in github actions (#605)
1 parent 5ca6556 commit 5acc306

File tree

18 files changed

+195
-26
lines changed

18 files changed

+195
-26
lines changed

.ci/.exclude_files

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.venv/*
2+
hello.py
3+
.ci/scripts.py
4+
code/nfts/upload-arweave/upload-arweave.en.py
5+
code/local-development/airdropping-sol/airdropping-sol.en.py
6+
code/basic-transactions/sending-sol/sending-sol.en.py
7+
code/local-development/connecting-websocket/connecting-websocket.en.py
8+
code/anchor/testing-with-anchor/client/testing_with_anchor.py
9+
code/keypairs-and-wallets/vanity-publickeys/vanity-publickeys.en.py
10+
code/serialization/clientdata/python.client.data.py
11+
code/serialization/primitives/python.demo_primitives.py
12+
code/serialization/instruction/python.client.py
13+
code/local-development/connecting-cluster/connecting-cluster.en.py
14+
code/local-development/connecting-private-cluster/connecting-private-cluster.en.py

.ci/scripts.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import os
2+
import re
3+
import subprocess
4+
from concurrent.futures import ThreadPoolExecutor
5+
6+
exclude_file = '.ci/.exclude_files'
7+
root_dir = os.path.curdir
8+
9+
with open(exclude_file, 'r') as f:
10+
exclude_patterns = [re.compile(line.strip()) for line in f.readlines()]
11+
12+
def find_py_files(root_dir, exclude_patterns):
13+
py_files = []
14+
for root, dirs, files in os.walk(root_dir):
15+
for file in files:
16+
file_path = os.path.join(root, file)
17+
relative_path = os.path.relpath(file_path, root_dir)
18+
if file.endswith('.py') and ".preview." not in file:
19+
ignore_file = any(pattern.search(relative_path) for pattern in exclude_patterns)
20+
if not ignore_file:
21+
py_files.append(relative_path)
22+
return py_files
23+
24+
def execute_file(file_path):
25+
result = subprocess.run(['python', file_path], capture_output=True, text=True)
26+
return result.stdout, result.stderr, file_path
27+
28+
def main():
29+
files_to_execute = find_py_files(root_dir, exclude_patterns)
30+
print("Executing these python files:")
31+
print("\n".join(files_to_execute))
32+
33+
with ThreadPoolExecutor() as executor:
34+
futures = [executor.submit(execute_file, file) for file in files_to_execute]
35+
for future in futures:
36+
stdout, stderr, file_path = future.result()
37+
print(f'\nExecuting {file_path}:\n\n Output: \n\r {stdout}')
38+
if stderr:
39+
print(f'\nError from {file_path}:\n\n Error: {stderr}')
40+
41+
42+
if __name__ == "__main__":
43+
main()

.github/workflows/python.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Test Python Files
2+
3+
on:
4+
schedule:
5+
- cron: "0 0 * * *"
6+
push:
7+
branches:
8+
- master
9+
pull_request:
10+
types: [opened, synchronize, reopened]
11+
branches:
12+
- master
13+
14+
jobs:
15+
run-py:
16+
strategy:
17+
matrix:
18+
py-version: [3.9]
19+
os: [ubuntu-latest]
20+
runs-on: ${{ matrix.os }}
21+
steps:
22+
- name: Checkout repository
23+
uses: actions/checkout@v3
24+
25+
- name: Set up Python
26+
uses: actions/setup-python@v4
27+
with:
28+
python-version: ${{matrix.py-version}}
29+
cache: pip
30+
31+
- name: Create, activate virtual environment and Install dependencies
32+
run: |
33+
python -m venv .venv
34+
source .venv/bin/activate
35+
python -m pip install -r requirements.txt --upgrade pip
36+
echo "$VIRTUAL_ENV/bin" >> $GITHUB_PATH
37+
echo "VIRTUAL_ENV=$VIRTUAL_ENV" >> $GITHUB_ENV
38+
39+
- name: Run python files
40+
run: |
41+
source .venv/bin/activate
42+
python .ci/scripts.py

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ node_modules
55
.DS_Store
66
**/*/target
77
.vscode
8-
.history
8+
.history
9+
.venv

code/basic-transactions/sending-sol/sending-sol.en.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@
2020
lamports=1_000_000)
2121
))
2222

23-
client.send_transaction(transaction, sender)
23+
result = client.send_transaction(transaction, sender)
24+
print(result)

code/keypairs-and-wallets/check-valid-publickey/check-valid-publickey.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@
77
off_curve_address = Pubkey.from_string('4BJXYkfvg37zEmBbsacZjeQDpTNx91KppxFJxRqrz48e') # Valid public key
88
print(off_curve_address.is_on_curve()) # Not on the ed25519 curve, therefore not suitable for users
99

10-
error_pubkey = Pubkey.from_string("testPubkey"); # Is not a valid public key
10+
try:
11+
error_pubkey = Pubkey.from_string("testPubkey"); # Is not a valid public key
12+
except:
13+
print("testPubkey is not a valid public key as expected!")
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
from solders.keypair import Keypair
2+
import based58
23

3-
keypair = Keypair()
4+
keypair = Keypair()
5+
print("Keypair PublicKey: ", keypair.pubkey())
6+
7+
rawPK = bytes(keypair)
8+
pk = based58.b58encode(rawPK).decode()
9+
print("Keypair Secret: ", pk)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from mnemonic import Mnemonic
22

33
mnemo = Mnemonic("english")
4-
words = mnemo.generate(strength=256)
4+
words = mnemo.generate(strength=256)
5+
print(words)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from solders.keypair import Keypair
22

33
b58_string = "5MaiiCavjCmn9Hs1o3eznqDEhRwxo7pXiAYez7keQUviUkauRiTMD8DrESdrNjN8zd9mTmVhRvBJeg5vhyvgrAhG"
4-
keypair = Keypair.from_string(b58_string)
4+
keypair = Keypair.from_base58_string(b58_string)
55
print("Created Keypair with Public Key: {}".format(keypair.pubkey()))
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
b58_string = "5MaiiCavjCmn9Hs1o3eznqDEhRwxo7pXiAYez7keQUviUkauRiTMD8DrESdrNjN8zd9mTmVhRvBJeg5vhyvgrAhG"
2-
keypair = Keypair.from_string(b58_string)
2+
keypair = Keypair.from_base58_string(b58_string)

0 commit comments

Comments
 (0)