Skip to content

Commit

Permalink
Merge pull request #44 from gramaziokohler/43-mac-support
Browse files Browse the repository at this point in the history
43 mac support
  • Loading branch information
gonzalocasas authored Aug 30, 2024
2 parents 990808e + 18ea604 commit f547de2
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 35 deletions.
3 changes: 3 additions & 0 deletions docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,6 @@ or an *interactive* plot (opening in browser).
- ``Plot Contours`` -- plots pair-wise joint distributions of the selected variables as contours


.. attention::

*static* mode is currently not supported on Mac.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
aixd >=0.10.0
compas >2,<3
flask
kaleido==0.1.0.post1; platform_system == "Windows"
37 changes: 37 additions & 0 deletions src/aixd_ara/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json
import os
import signal

from compas.data import DataEncoder
from flask import Flask
Expand Down Expand Up @@ -345,6 +347,38 @@ def model_input_output_dimensions():
return response


@app.route("/shutdown", methods=["GET"])
def shutdown():
os.kill(os.getpid(), signal.SIGINT)
print()
print()
print()
print("*********************************************************")
print("THIS APP HAS TERMINATED AND THIS WINDOW CAN BE CLOSED NOW")
print("*********************************************************")
print()
print()
print()
return "Server shuting down"


@app.route("/ping", methods=["GET"])
def ping():
return "Pong!"


def ara_welcome(*args):
print()
print()
print()
print("****************************")
print("HELLO! ARA SERVER IS RUNNING")
print("****************************")
print()
print()
print()


if __name__ == "__main__":
import sys

Expand All @@ -357,4 +391,7 @@ def model_input_output_dimensions():
print("Invalid port number: ", sys.argv[1], "Setting a default port number {}".format(DEFAULT_PORT))
port = DEFAULT_PORT

cli = sys.modules["flask.cli"]
cli.show_server_banner = ara_welcome

app.run(host="127.0.0.1", port=port, debug=False)
78 changes: 45 additions & 33 deletions src/aixd_ara/components/ara_Server/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from __future__ import division
from __future__ import print_function

import urllib2

import compas
import compas._os
from ghpythonlib.componentbase import executingcomponent as component
Expand Down Expand Up @@ -30,24 +32,23 @@ def __init__(
service=None,
max_conn_attempts=100,
autoreload=True,
capture_output=True,
show_window=False,
path=None,
working_directory=None,
):
self.python = compas._os.select_python(python)
self.service = "aixd_ara.api"
self.working_directory = working_directory
self.capture_output = capture_output
self.show_window = show_window
self.port = port

self.start_server()
self.start_process()

def start_server(self):
def start_process(self):
env = compas._os.prepare_environment()

try:
Popen

except NameError:
self._process = Process()
for name in env:
Expand All @@ -56,36 +57,46 @@ def start_server(self):
else:
self._process.StartInfo.EnvironmentVariables.Add(name, env[name])
self._process.StartInfo.UseShellExecute = True
self._process.StartInfo.RedirectStandardOutput = self.capture_output
self._process.StartInfo.RedirectStandardError = self.capture_output
self._process.StartInfo.RedirectStandardOutput = not self.show_window
self._process.StartInfo.RedirectStandardError = not self.show_window
self._process.StartInfo.FileName = self.python
self._process.StartInfo.Arguments = "-m {0} {1}".format(self.service, self.port)
self._process.Start()
else:
args = [self.python, "-m", self.service, str(self.port)]

kwargs = dict(env=env)
print(args)
if self.capture_output:

if self.show_window:
if compas.is_mono():
python_command = " ".join(args)
apple_script = """
tell application "Terminal"
do script "{}"
activate
end tell
""".format(
python_command
)
args = ["osascript", "-e", apple_script]
else:
kwargs["stdout"] = PIPE
kwargs["stderr"] = PIPE

if self.working_directory:
kwargs["cwd"] = self.working_directory

self._process = Popen(args, **kwargs)

def stop_server(self):
print("Stopping the server proxy.")
self._terminate_process()

def _terminate_process(self):
def stop_process(self):
"""Attempts to terminate the python process hosting the proxy server.
The process reference might not be present, e.g. in the case
of reusing an existing connection. In that case, this is a no-op.
"""
print("Stopping the server process.")

if not self._process:
return

try:
self._process.terminate()
except Exception:
Expand All @@ -108,37 +119,38 @@ def create_api_runner_id(component):

class ApiRunnerComponent(component):

def RunScript(self, start, stop, show_window):
def RunScript(self, start, stop, show_window=True):
key = create_api_runner_id(self)
if not show_window:
show_window = True # default

port = DEFAULT_PORT

if not start:
return None
if show_window:
python = "python"
capture_output = False
else:
python = "pythonw"
capture_output = True

if stop:
self.stop_previous(key)

if start:
self.stop_previous(key)
runner = ApiRunner(python=python, port=port, capture_output=capture_output, working_directory=api_path)
self.stop(key)
runner = ApiRunner(python=python, port=port, show_window=show_window, working_directory=api_path)
st[key] = runner

runner = st[key]
if stop:
self.stop(key)

return runner
def stop(self, key):
# Attempt gracefully stopping the server
try:
urllib2.urlopen("http://127.0.0.1:8765/shutdown", timeout=1)
except Exception:
pass

def stop_previous(self, key):
# stop previous API runner if any
previous_api_runner = st.get(key)
if previous_api_runner:
previous_api_runner.stop_server()
try:
previous_api_runner.stop_process()
except Exception as e:
print("Unable to stop previous process")
print(e)
st.pop(key)
else:
print("No previous process runner found")
10 changes: 8 additions & 2 deletions src/aixd_ara/components/ara_ShowFolder/code.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
"""Reveals the folder in the file explorer."""
# flake8: noqa

import os
import compas
import subprocess

if open:
os.startfile(path)
command = "open"
if compas.is_windows():
command = "explorer"

subprocess.Popen([command, path])

0 comments on commit f547de2

Please sign in to comment.