Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/dev/project/util.py
Views: 687
from __future__ import print_function12import os, json, socket, subprocess34join = os.path.join567def cmd(s):8print(s)9if subprocess.check_call(["bash", "-c", s]):10raise RuntimeError111213def chdir():14os.chdir(os.path.split(os.path.abspath(__file__))[0])151617def base_url(port=None, write=True):18print("base_url(port=%s)" % port)19try:20project_id = os.environ['COCALC_PROJECT_ID']21except KeyError:22raise RuntimeError(23"COCALC_PROJECT_ID environment variable not found. You can only use dev/project to run CoCalc from inside of a CoCalc project."24)25if port is None and write:26write_base_url = True27port = get_ports()['hub']28else:29write_base_url = False30base_url = "/{project_id}/port/{port}".format(31project_id=project_id, port=port)32if write_base_url:33open("../../data/base_url", 'w').write(base_url)34return base_url353637# http://stackoverflow.com/questions/2838244/get-open-tcp-port-in-python38def get_open_port():39s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)40s.bind(("", 0))41s.listen(1)42port = s.getsockname()[1]43s.close()44return port454647def get_ports():48P = os.path.split(os.path.abspath(__file__))[0]49path = join(P, 'ports')50if not os.path.exists(path):51os.mkdir(path)52ports = {'hub': 0, 'hub-api': 0, 'hub-share': 0, 'hub-share-2': 0, 'agent-port': 0}53for x in ports.keys():54file = join(path, x)55if os.path.exists(file):56ports[x] = int(open(file).read())57else:58ports[x] = get_open_port()59open(file, 'w').write(str(ports[x]))60return ports616263def test():64import sys65if len(sys.argv) >= 2 and sys.argv[1] == 'test':66return '--test'67else:68return ''697071