Skip to content

Commit 1787bc3

Browse files
authored
Merge pull request #85 from Hashmap-Software-Agency/improve-run-file-intent
Improve run file intent
2 parents 1c0e892 + 310cc0d commit 1787bc3

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

devtools/update_environment.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Set the current working dir to parent directory
2+
import os
3+
import shutil
4+
import subprocess
5+
import sys
6+
from pathlib import Path
7+
from time import sleep
8+
9+
sys.path.append(Path.cwd().parent.as_posix())
10+
11+
if not Path("setup.py").exists():
12+
print("You're not in the right directory. Run this script from the "
13+
r"project's root directory, e.g. 'C:\users\your_user\projects\pyttman'.")
14+
exit(-1)
15+
16+
LAB_ENV_PATH = Path.cwd() / Path("dev_env")
17+
BUILD_OUTPUT_PATH = Path.cwd() / "dist"
18+
19+
if __name__ == "__main__":
20+
if not (LAB_ENV_PATH / "venv").exists():
21+
raise FileNotFoundError("Your dev environment is not set up, or "
22+
"is missing the virtual environment. "
23+
"Run devtools/create_environment.py first.")
24+
25+
print("Building new Pyttman package...")
26+
subprocess.check_call("python devtools/build.py".split())
27+
os.chdir(LAB_ENV_PATH.as_posix())
28+
subprocess.run("python -m virtualenv venv".split())
29+
30+
while not Path("venv").exists():
31+
sleep(0.01)
32+
33+
package_file = [
34+
i for i in os.listdir(BUILD_OUTPUT_PATH.as_posix()) if i.endswith("tar.gz")
35+
].pop()
36+
37+
print(f"Installing your new version of Pyttman from local build: '{package_file}'")
38+
package_file = (BUILD_OUTPUT_PATH / package_file).as_posix()
39+
venv_python = (LAB_ENV_PATH / "venv/scripts/python.exe").as_posix()
40+
subprocess.run(f"{venv_python} -m pip install multidict".split())
41+
subprocess.run(f"{venv_python} -m pip install --upgrade {package_file}".split())
42+
43+
clear_sc = "clear" if os.name == "posix" else "cls"
44+
os.system(clear_sc)
45+
os.system("cls")
46+
47+
print("\nFinished! Here's how to get started:",
48+
f"1. Activate the virtual environment:\n\tcd dev_env\n\tvenv/scripts/activate",
49+
f"2. Run the command 'pyttman' to see available commands to the Pyttman CLI",
50+
"3. If it's the first time you're running Pyttman, run 'pyttman new app {app_name}' to create a new project."
51+
"4. Run 'pyttman dev {app_name}' to start the development server.",
52+
"5. If you've made changes to the Pyttman framework which you want to test in your testing project, "
53+
"run this script again. Your app will be left untouched, but the Pyttman version is upgraded to "
54+
"your current HEAD in the Pyttman repo.",
55+
sep="\n")

pyttman/tools/pyttmancli/intents.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ def respond(self, message: Message) -> Reply | ReplyStream:
7272
print(f"- Attempt [{i + 1}/{num_retries}]:", end=" ")
7373
try:
7474
terraformer.terraform()
75+
except FileExistsError as e:
76+
return Reply(str(e))
77+
7578
except requests.exceptions.ConnectionError as e:
7679
print("failed.",
7780
"Cannot connect to the server, "
@@ -179,26 +182,22 @@ class RunFile(Intent, PyttmanCliComplainerMixin):
179182

180183
def respond(self, message: Message) -> Reply | ReplyStream:
181184
app_name = message.entities["app_name"]
182-
script_file = message.entities["script_file_name"]
183-
184-
if complaint := self.complain_app_not_found(app_name):
185-
return Reply(complaint)
186-
if script_file is None:
185+
if (script_path := message.entities["script_file_name"]) is None:
187186
return Reply("Filename must be entered, as a '.py' file. "
188187
f"Example: {self.example}")
188+
if complaint := self.complain_app_not_found(app_name):
189+
return Reply(complaint)
189190

190-
script_file = Path(script_file)
191191
app = bootstrap_app(devmode=True, module=app_name)
192192
app.hooks.trigger(LifeCycleHookType.before_start)
193193
global_variables = globals().copy()
194194
global_variables.update(locals())
195195
shell = code.InteractiveConsole(global_variables)
196-
script_path = Path.cwd() / script_file
197196

198197
with open(script_path.as_posix(), "r") as f:
199198
# Set variable to indicate for the running script that it's main
200199
source = f.read()
201-
code_obj = compile(source, script_file.as_posix(), "exec")
200+
code_obj = compile(source, script_path.as_posix(), "exec")
202201
global_variables["__name__"] = "__main__"
203202
exec(code_obj, global_variables)
204203
return Reply(f"Script file executed successfully.")

0 commit comments

Comments
 (0)