Skip to content

Commit 1189e43

Browse files
jhoetterJWittmeyer
and
JWittmeyer
authored
fixes issue #6 (#7)
* fixes issue #6 * Adds GitPython requirement to setup script Co-authored-by: JWittmeyer <jens.wittmeyer@onetask.ai>
1 parent 4d526d3 commit 1189e43

File tree

5 files changed

+70
-12
lines changed

5 files changed

+70
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ If you like what we're working on, please leave a ⭐!
107107
```
108108
pip install kern-refinery
109109
```
110-
Once the library is installed, go to the directory where you want to store the data and run `refinery start`. To stop the server, run `refinery stop`.
110+
Once the library is installed, go to the directory where you want to store the data and run `refinery start`. This will automatically `git clone` this repository first if you haven't done so yet. To stop the server, run `refinery stop`.
111111

112112
### From repository
113113

cli.py

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,61 @@
1+
import os
2+
from contextlib import contextmanager
13
import sys
24
import platform
35
import subprocess
6+
from git import Repo
47
from wasabi import msg
58

9+
REFINERY_FOLDER = "refinery"
610

7-
def start():
8-
if platform.system() == "Windows":
9-
subprocess.run(["start.bat"])
11+
12+
def start(cur_dir: str):
13+
"""Starts the refinery server; if the refinery repository does not exist, it will be cloned from git first.
14+
15+
Args:
16+
cur_dir (str): The current directory.
17+
"""
18+
19+
def _start_server():
20+
if platform.system() == "Windows":
21+
subprocess.run(["start.bat"])
22+
else:
23+
subprocess.run(["./start"])
24+
25+
if not os.path.exists(REFINERY_FOLDER):
26+
msg.info(
27+
f"Cloning from code-kern-ai/refinery into repository {REFINERY_FOLDER}"
28+
)
29+
Repo.clone_from("https://github.com/code-kern-ai/refinery", REFINERY_FOLDER)
30+
with cd(REFINERY_FOLDER):
31+
_start_server()
32+
elif cur_dir == REFINERY_FOLDER:
33+
_start_server()
1034
else:
11-
subprocess.run(["./start"])
35+
with cd(REFINERY_FOLDER):
36+
_start_server()
37+
38+
39+
def stop(cur_dir: str):
40+
"""Stops the refinery server.
1241
42+
Args:
43+
cur_dir (str): The current directory.
44+
"""
1345

14-
def stop():
15-
if platform.system() == "Windows":
16-
subprocess.run(["stop.bat"])
46+
def _stop_server():
47+
if platform.system() == "Windows":
48+
subprocess.run(["stop.bat"])
49+
else:
50+
subprocess.run(["./stop"])
51+
52+
if cur_dir == REFINERY_FOLDER:
53+
_stop_server()
54+
elif os.path.exists(REFINERY_FOLDER):
55+
with cd(REFINERY_FOLDER):
56+
_stop_server()
1757
else:
18-
subprocess.run(["./stop"])
58+
msg.fail(f"Could not find repository {REFINERY_FOLDER}.")
1959

2060

2161
def main():
@@ -24,11 +64,27 @@ def main():
2464
msg.fail("Please provide arguments when running the `refinery` command.")
2565
msg.fail("`refinery start` to start the server, `refinery stop` to end it.")
2666
command = cli_args[0]
67+
cur_dir = os.path.split(os.getcwd())[-1]
2768
if command == "start":
28-
start()
69+
start(cur_dir)
2970
elif command == "stop":
30-
stop()
71+
stop(cur_dir)
3172
else:
3273
msg.fail(
3374
f"Could not understand command `{command}`. Type `refinery help` for some instructions."
3475
)
76+
77+
78+
# https://stackoverflow.com/questions/431684/equivalent-of-shell-cd-command-to-change-the-working-directory/24176022#24176022
79+
class cd:
80+
"""Context manager for changing the current working directory"""
81+
82+
def __init__(self, new_path):
83+
self.new_path = os.path.expanduser(new_path)
84+
85+
def __enter__(self):
86+
self.saved_path = os.getcwd()
87+
os.chdir(self.new_path)
88+
89+
def __exit__(self, etype, value, traceback):
90+
os.chdir(self.saved_path)

publish.sh

100644100755
File mode changed.

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
requests
2-
wasabi
2+
wasabi
3+
GitPython

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
install_requires=[
3737
"requests",
3838
"wasabi",
39+
"GitPython",
3940
],
4041
entry_points={
4142
"console_scripts": [

0 commit comments

Comments
 (0)