Skip to content

Commit

Permalink
refactor files & add automated protobuf aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrl-Felix committed Aug 1, 2022
1 parent 27b18b4 commit b6fca64
Show file tree
Hide file tree
Showing 242 changed files with 293 additions and 17,166 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.idea
*.pyc
*.py
*.pyi
test/
dist/
cosmospy_protobuf.egg-info
cosmospy_protobuf.egg-info
tmp/
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ print(r.balances)

```

## Compile the Protobuf files

The files are compield using the ``grpc_tools.protoc`` command from the [grpcio-tools](https://pypi.org/project/grpcio-tools/) library.
To compile a .proto file use following command:
## Build yourself
There are two scripts helping you to fork this repository to work with any cosmos based coin.
1. Create a config in ``configs`` and take a existing one as example
2. Run the ``aggregate.py`` file with your filename without ``.json`` (Example ``python aggregate.py cosmos``)
3. Run the ``compile.py`` to compile all your files to protobuf
4. Build your package. You're done!
5.
## Protobuf compilation

The files are compiled using the ``grpc_tools.protoc`` command from the [grpcio-tools](https://pypi.org/project/grpcio-tools/) library.
To compile a .proto file manually use following command:
```
python -m grpc_tools.protoc -I <absolute path to project root> --python_out=. --grpc_python_out=. <absolute path to .proto file>
```
Expand All @@ -43,9 +50,6 @@ Note:
* To compile the whole project it is favorable to match all proto files by using `*.proto` instead of each individual file. You can also match the whole folders to compile multiple folders at the same time. Not that the folders might contain sub-folders.

## Other Cosmos based coins
Currently following coins are supported by this library:
* Akash
* Bitsong
* Cosmos
* Osmosis
* Umee
Currently following coins are maintained by me:
* Cosmos (this branch)
* Osmosis (branch: osmosis, package name: `osmosis-protobuf`)
79 changes: 79 additions & 0 deletions aggregate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import errno
import json
import os
import shutil
import time
from os.path import dirname, abspath, isfile
import argparse

from git import Repo

parser = argparse.ArgumentParser(description='Aggregate all protobuf files')
parser.add_argument('coin', type=str, help="Coin to parse from the .json file in the config folder")
args = parser.parse_args()

# Get current directory
d = dirname(abspath(__file__))

# Check if requested coin has a config
coin = args.coin
try:
config_path = os.path.join(d, f'configs/{coin.lower()}.json')
f = open(config_path, "r")
coin_config = json.load(f)
f.close()
except Exception:
print("Coin couldn't be found")
exit()


tmp_dir = os.path.join(d, "tmp")
if not os.path.isdir(tmp_dir):
os.mkdir(tmp_dir)

project_dir = os.path.join(tmp_dir, str(time.time()))
os.mkdir(project_dir)

# Delete all existing protobuf files
root_dir = 'src/cosmospy_protobuf'
root_abs_path = os.path.join(d, root_dir)
for filename in os.listdir(root_abs_path):
file_path = os.path.join(root_abs_path, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))

# Load config
i = 1
for repo_url, repo_config in coin_config.items():
print(f"Cloning {repo_url} | {repo_config['branch']}")
repo_dir = project_dir + "/" + str(i)
Repo.clone_from(
repo_url,
project_dir + "/" + str(i),
branch=repo_config['branch']
)

# Copy proto files to root_dir
for proto_folder in repo_config['paths']:
proto_dir = os.path.join(repo_dir, proto_folder)
category_name = proto_folder.split('/')[-1]
try:
shutil.copytree(proto_dir, root_abs_path + "/" + category_name, dirs_exist_ok=True, ignore=shutil.ignore_patterns("*.go"))
print(f"Copied {category_name}")
except OSError as exc: # python >2.5
try:
shutil.copy(proto_dir, root_abs_path)
print(f"File {proto_dir} copied successfully")
except:
raise

i += 1




32 changes: 32 additions & 0 deletions configs/cosmos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"https://github.com/cosmos/cosmos-sdk.git": {
"include": true,
"branch": "v0.45.3",
"paths": ["proto/cosmos"]
},
"https://github.com/tendermint/tendermint.git": {
"include": true,
"branch": "v0.34.14",
"paths": ["proto/tendermint"]
},
"https://github.com/cosmos/ibc-go": {
"include": true,
"branch": "v3.0.0",
"paths": ["proto/ibc", "third_party/proto/google"]
},
"https://github.com/cosmos/cosmos-proto.git": {
"include": true,
"branch": "main",
"paths": ["proto/cosmos_proto"]
},
"https://github.com/cosmos/gogoproto.git": {
"include": true,
"branch": "master",
"paths": ["gogoproto"]
},
"https://github.com/confio/ics23.git": {
"include": true,
"branch": "master",
"paths": ["proofs.proto"]
}
}
37 changes: 37 additions & 0 deletions configs/osmosis.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"https://github.com/osmosis-labs/osmosis.git": {
"include": true,
"branch": "v10.0.0",
"paths": ["proto/osmosis"]
},
"https://github.com/cosmos/cosmos-sdk.git": {
"include": true,
"branch": "v0.45.4",
"paths": ["proto/cosmos"]
},
"https://github.com/tendermint/tendermint.git": {
"include": true,
"branch": "v0.34.19",
"paths": ["proto/tendermint"]
},
"https://github.com/cosmos/ibc-go": {
"include": true,
"branch": "v3.0.0",
"paths": ["proto/ibc", "third_party/proto/google"]
},
"https://github.com/cosmos/cosmos-proto.git": {
"include": true,
"branch": "main",
"paths": ["proto/cosmos_proto"]
},
"https://github.com/cosmos/gogoproto.git": {
"include": true,
"branch": "master",
"paths": ["gogoproto"]
},
"https://github.com/confio/ics23.git": {
"include": true,
"branch": "master",
"paths": ["proofs.proto"]
}
}
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ build-backend = "hatchling.build"

[project]
name = "cosmospy_protobuf"
version = "0.0.1"
version = "0.0.2"
description = "This package contains a compiled python version of all cosmos protobuf files with their dependencies"
authors = [
{ name = "ctrl-felix", email = "[email protected]" },
]
readme = "README.md"
keywords = ["cosmospy", "proto", "cosmospy-protobuf"]
keywords = ["cosmospy", "proto", "cosmospy-protobuf", "cosmos", "cosmos-protobuf", "protobuf"]
license = {text = "BSD 3-Clause License"}
classifiers = [
"Programming Language :: Python :: 3",
Expand Down
99 changes: 0 additions & 99 deletions src/cosmospy_protobuf/akash/audit/v1beta1/audit.proto

This file was deleted.

Loading

0 comments on commit b6fca64

Please sign in to comment.