2021-03-12 04:17:19 +00:00
|
|
|
# Authors: see git history
|
|
|
|
#
|
|
|
|
# Copyright (c) 2010 Authors
|
|
|
|
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
|
|
|
|
|
2023-05-22 20:33:19 +00:00
|
|
|
import json
|
2019-04-11 03:37:16 +00:00
|
|
|
import os
|
|
|
|
import subprocess
|
2019-04-11 04:03:23 +00:00
|
|
|
import sys
|
2019-04-11 03:37:16 +00:00
|
|
|
|
|
|
|
from ..utils import get_bundled_dir
|
|
|
|
|
|
|
|
app_process = None
|
|
|
|
|
|
|
|
|
2023-05-22 20:33:19 +00:00
|
|
|
def open_url(url, port, pdf=False): # noqa: C901
|
2019-04-11 03:37:16 +00:00
|
|
|
global app
|
|
|
|
|
2023-05-22 20:33:19 +00:00
|
|
|
if not pdf:
|
|
|
|
url = f'{url}?port={port}'
|
|
|
|
os.environ['FLASKPORT'] = str(port)
|
|
|
|
|
|
|
|
# this creates the .json for dev mode to get translations
|
|
|
|
if getattr(sys, 'frozen', None) is None:
|
|
|
|
dynamic_port = {
|
|
|
|
"_comment1": "port should not be declared when commiting",
|
|
|
|
"port": port,
|
|
|
|
}
|
|
|
|
port_object = json.dumps(dynamic_port, indent=1)
|
|
|
|
with open(os.path.join("electron/src/lib/flaskserverport.json"), "w") as outfile:
|
|
|
|
outfile.write(port_object)
|
|
|
|
else:
|
|
|
|
url = f'http://{url}:{port}/'
|
|
|
|
|
2019-04-23 01:42:17 +00:00
|
|
|
cwd = None
|
2023-05-22 20:33:19 +00:00
|
|
|
searchstring = "http"
|
2019-04-18 01:48:44 +00:00
|
|
|
|
2019-04-11 04:03:23 +00:00
|
|
|
if getattr(sys, 'frozen', None) is not None:
|
|
|
|
electron_path = os.path.join(get_bundled_dir("electron"), "inkstitch-gui")
|
|
|
|
|
2019-04-12 21:45:57 +00:00
|
|
|
if sys.platform == "darwin":
|
2021-12-08 21:18:41 +00:00
|
|
|
electron_path = os.path.join(sys._MEIPASS, "electron", "inkstitch-gui.app", "Contents", "MacOS", "inkstitch-gui")
|
2019-11-11 22:59:51 +00:00
|
|
|
command = ["open", "-W", "-a", electron_path, "--args", url]
|
2019-04-12 21:45:57 +00:00
|
|
|
else:
|
2019-04-23 01:42:17 +00:00
|
|
|
command = [electron_path, url]
|
2019-04-11 05:00:28 +00:00
|
|
|
else:
|
2019-04-12 21:45:57 +00:00
|
|
|
# if we're not running in a pyinstaller bundle, run electron directly
|
2019-04-23 01:42:17 +00:00
|
|
|
command = ["yarn", "dev", url]
|
|
|
|
cwd = get_bundled_dir("electron")
|
2019-04-11 03:37:16 +00:00
|
|
|
|
2019-04-23 01:42:17 +00:00
|
|
|
# Any output on stdout will crash inkscape.
|
2021-12-08 21:18:41 +00:00
|
|
|
# In macos manual install the python env paths are incomplete
|
|
|
|
# Adding the yarn path to the env paths fixes this issue
|
|
|
|
if sys.platform == "darwin" and getattr(sys, 'frozen', None) is None:
|
|
|
|
mac_dev_env = os.environ.copy()
|
|
|
|
# these are paths installed by brew or macports
|
2023-04-28 19:02:05 +00:00
|
|
|
yarn_path = "/opt/homebrew/bin:/usr/local/bin:/opt/local/bin:"
|
2021-12-08 21:18:41 +00:00
|
|
|
if yarn_path in mac_dev_env["PATH"]:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
mac_dev_env["PATH"] = yarn_path + mac_dev_env["PATH"]
|
2023-05-22 20:33:19 +00:00
|
|
|
# checking URL for flask server address for printToPDF
|
|
|
|
if searchstring in url:
|
|
|
|
with open(os.devnull, 'w') as null:
|
|
|
|
subprocess.Popen(["yarn", "just-build"], cwd=cwd, stdout=null, env=mac_dev_env).wait()
|
|
|
|
else:
|
|
|
|
pass
|
|
|
|
|
2021-12-08 21:18:41 +00:00
|
|
|
with open(os.devnull, 'w') as null:
|
|
|
|
return subprocess.Popen(command, cwd=cwd, stdout=null, env=mac_dev_env)
|
|
|
|
else:
|
2023-05-22 20:33:19 +00:00
|
|
|
if searchstring in url and getattr(sys, 'frozen', None) is None:
|
|
|
|
with open(os.devnull, 'w') as null:
|
|
|
|
subprocess.Popen(["yarn", "just-build"], cwd=cwd, stdout=null).wait()
|
|
|
|
else:
|
|
|
|
pass
|
|
|
|
if sys.platform == "linux":
|
|
|
|
# Pyinstaller fix for gnome document view not opening.
|
|
|
|
lenv = dict(os.environ)
|
|
|
|
lp_key = 'LD_LIBRARY_PATH'
|
|
|
|
lp_orig = lenv.get(lp_key + '_ORIG')
|
|
|
|
if lp_orig is not None:
|
|
|
|
lenv[lp_key] = lp_orig # restore the original, unmodified value
|
|
|
|
else:
|
|
|
|
lenv.pop(lp_key, None)
|
|
|
|
|
|
|
|
with open(os.devnull, 'w') as null:
|
|
|
|
return subprocess.Popen(command, cwd=cwd, stdout=null, env=lenv)
|
|
|
|
else:
|
|
|
|
with open(os.devnull, 'w') as null:
|
|
|
|
return subprocess.Popen(command, cwd=cwd, stdout=null)
|