Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python-packages/atri/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ name = "pypi"
click = "*"
python-socketio = {extras = ["asyncio_client"], version = "*"}
toml = "*"
pyyaml = "*"
questionary = "*"
requests = "*"
pymongo = {extras = ["srv"], version = "*"}
Expand Down
80 changes: 59 additions & 21 deletions python-packages/atri/src/atri/commands/connect_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import os
import traceback
import socketio
import yaml
from pathlib import Path
import io
from ..utils.install_package import install_package
from ..utils.run_shell_cmd import run_shell_cmd
import toml
from ..utils.printd import printd
from ..utils.call_compute import call_compute
Expand All @@ -16,6 +20,41 @@ def print_success_cb(success):
printd("Registered as atri-cli with ipc server.")
await sio.emit("registerAs", "atri-cli", callback=print_success_cb)

def add_packages_to_global_pipfile(global_pip_path: str, current_pip_path: str):
current_pip_file = toml.load(current_pip_path)
global_pip_file = toml.load(global_pip_path)

for pckg, version in current_pip_file['packages'].items():
global_pip_file['packages'][pckg] = version
for pckg, version in current_pip_file['dev-packages'].items():
global_pip_file['dev-packages'][pckg] = version

with open(global_pip_path, 'w') as f:
toml.dump(global_pip_file, f)

def add_packages_to_global_yml_file(global_yml_path : str, current_pip_path: str):
current_pip_file = toml.load(current_pip_path)
with open(global_yml_path, 'r') as stream:
data_loaded = yaml.safe_load(stream)
for pckg, version in current_pip_file['packages'].items():
if version != '*':
if type(version) == dict:
data_loaded['dependencies'] += [f'{pckg}{version["version"]}']
else:
data_loaded['dependencies'] += [f'{pckg}{version}']
else:
data_loaded['dependencies'] += [pckg]
for pckg, version in current_pip_file['dev-packages'].items():
if version != '*':
data_loaded['dependencies'] += [f'{pckg}{version}']
else:
data_loaded['dependencies'] += [pckg]
with io.open(global_yml_path, 'w', encoding='utf8') as outfile:
yaml.dump(data_loaded, outfile, default_flow_style=False, allow_unicode=True)




async def connect_ipc_server(port: str):
printd("Connecting to ipc server...")
sio = socketio.AsyncClient(
Expand Down Expand Up @@ -67,35 +106,34 @@ async def doBuildPython():
initial_pipfile_path = os.path.join(controllers_dir, "Pipfile")
# check if Pipfile exist in controller directory
if os.path.exists(initial_pipfile_path):
# read Pipfile
pipfile_data = toml.load(initial_pipfile_path)
pkgs = pipfile_data["packages"]
dev_pkgs = pipfile_data["dev-packages"]
# run pipenv install <package_name> for each file in Pipfile inside app_dir
for pkg in pkgs:
version = pkgs[pkg]
if type(version) != str:
version = version["version"]
child_proc = await install_package(app_dir, pkg, version)
current_env = get_virtualenv_type()
if current_env == 'pipenv':
add_packages_to_global_pipfile(os.path.join(app_dir, 'Pipfile'), initial_pipfile_path)
child_proc = await run_shell_cmd('pipenv install', app_dir)
_, stderr = await child_proc.communicate()

if child_proc.returncode != 0:
print("Failed: {} install".format(get_virtualenv_type()), pkg, version)
print("Failed: {} installation of internal dependencies".format(get_virtualenv_type()))
if stderr:
printd("[stderr]\n", stderr)
else:
printd("Installed", pkg, version)
for pkg in dev_pkgs:
version = dev_pkgs[pkg]
if type(version) != str:
version = version["version"]
child_proc = await install_package(app_dir, pkg, version)
printd("Installed Packages")

elif current_env == 'conda':

child_proc = await run_shell_cmd('conda env export > environment.yml', app_dir)
_, stderr = await child_proc.communicate()
add_packages_to_global_yml_file(os.path.join(app_dir, 'environment.yml'), initial_pipfile_path)
child_proc = await run_shell_cmd('conda env update -f environment.yml', app_dir)
_, stderr = await child_proc.communicate()

if child_proc.returncode != 0:
print("Failed: {} install".format(get_virtualenv_type()), pkg, version)
print("Failed: {} installation of internal dependencies".format(get_virtualenv_type()))
if stderr:
printd("[stderr]\n", stderr)
else:
printd("Installed", pkg, version)
printd("Installed Packages")

# delete Pipfile from controllers_dir
os.remove(initial_pipfile_path)
return True
Expand All @@ -108,7 +146,7 @@ async def doStartPythonServer():
python_server_proc = await call_serve(app_dir)
await python_server_proc.wait()
if python_server_proc.returncode != 0:
print("Error occured while running python -m controllers.server serve")
print("Error occurred while running python -m controllers.server serve")
returncode = python_server_proc.returncode
python_server_proc = None
return returncode
Expand All @@ -127,4 +165,4 @@ async def wrapper():
# with error message 'packet queue is empty, aborting'
sio = await start_ipc_connection(u_port, app_dir)
await sio.wait()
asyncio.run(wrapper())
asyncio.run(wrapper())