-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup.py
148 lines (119 loc) · 5 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os
import shutil
import argparse
import subprocess
from pathlib import Path
from typing import Optional, Tuple
from dataclasses import dataclass
@dataclass
class ModuleInfo:
"""Information about a Gno module."""
module_path: str
source_dir: str
destination_dir: str
class GnoModuleManager:
"""Manages Gno modules and their paths."""
def __init__(self, workdir: str):
self.workdir = workdir
self.gno_dir = os.path.join(workdir, "gno", "examples", "gno.land")
def extract_module_path(self, file_path: str) -> Optional[str]:
"""Extract module path from gno.mod file."""
try:
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
import re
match = re.search(r"module\s+([\w./]+)", content)
return match.group(1) if match else None
except (IOError, UnicodeDecodeError):
print(f"Error reading file: {file_path}")
return None
def find_parent_module(self, directory: str) -> Tuple[Optional[str], Optional[str]]:
"""Find the nearest parent module path and directory."""
current = directory
while current != os.path.dirname(current):
mod_file = os.path.join(current, "gno.mod")
if os.path.exists(mod_file):
return self.extract_module_path(mod_file), current
current = os.path.dirname(current)
return None, None
def get_module_info(self, src_dir: str, module_path: str) -> Optional[ModuleInfo]:
"""Create ModuleInfo from source directory and module path."""
if not module_path or not module_path.startswith("gno.land/"):
return None
relative_path = module_path.replace("gno.land/", "", 1)
dest_dir = os.path.join(self.gno_dir, relative_path)
return ModuleInfo(module_path, src_dir, dest_dir)
class ContractCopier:
"""Handles copying of contract files and tests."""
def __init__(self, module_manager: GnoModuleManager):
self.module_manager = module_manager
def copy_module(self, module_info: ModuleInfo) -> None:
"""Copy module files to destination."""
print(
f"Copying module from {module_info.source_dir} to {module_info.destination_dir}"
)
os.makedirs(os.path.dirname(module_info.destination_dir), exist_ok=True)
shutil.copytree(
module_info.source_dir, module_info.destination_dir, dirs_exist_ok=True
)
def copy_tests(self, src_test_dir: str, dest_dir: str) -> None:
"""Copy test files to destination."""
print(f"Copying tests from {src_test_dir} to {dest_dir}")
if os.path.exists(src_test_dir):
os.makedirs(dest_dir, exist_ok=True)
for test_file in os.listdir(src_test_dir):
src_file = os.path.join(src_test_dir, test_file)
dest_file = os.path.join(dest_dir, test_file)
if os.path.isfile(src_file):
shutil.copy2(src_file, dest_file)
def process_directory(self, root: str, dirs: list, files: list) -> None:
"""Process a directory for modules and tests."""
# Handle modules with gno.mod
if "gno.mod" in files:
module_path = self.module_manager.extract_module_path(
os.path.join(root, "gno.mod")
)
if module_info := self.module_manager.get_module_info(root, module_path):
self.copy_module(module_info)
# Handle test directories
if "tests" in dirs:
parent_module_path, parent_dir = self.module_manager.find_parent_module(
root
)
if module_info := self.module_manager.get_module_info(
parent_dir, parent_module_path
):
src_test_dir = os.path.join(root, "tests")
self.copy_tests(src_test_dir, module_info.destination_dir)
def clone_repository(workdir: str) -> None:
"""Clone the GnoSwap repository."""
os.chdir(workdir)
subprocess.run(
["git", "clone", "https://github.com/gnoswap-labs/gnoswap.git"], check=True
)
os.chdir("gnoswap")
def setup_contracts(workdir: str) -> None:
"""Set up all contracts and tests."""
module_manager = GnoModuleManager(workdir)
copier = ContractCopier(module_manager)
for root, dirs, files in os.walk("contract"):
copier.process_directory(root, dirs, files)
def main() -> None:
"""Main entry point for the script."""
parser = argparse.ArgumentParser(description="Set up GnoSwap contracts")
parser.add_argument(
"-w",
"--workdir",
help="Path to your work directory",
default=str(Path.home()),
)
parser.add_argument(
"-c", "--clone", action="store_true", help="Clone the repository"
)
args = parser.parse_args()
if args.clone:
clone_repository(args.workdir)
setup_contracts(args.workdir)
print("Setup completed successfully!")
if __name__ == "__main__":
main()