"""
Utility functions to retrieve information about available services and setting up security for the Hops platform.

These utils facilitates development by hiding complexity for programs interacting with Hops services.
"""

import socket
import subprocess
import os
import pydoop.hdfs as hdfs

pdir = os.getcwd()
logpath = pdir + "/events"
os.makedirs(logpath)
logdir = os.path.dirname(logpath)

def register():

    pypath = os.getenv("PYSPARK_PYTHON")
    pydir = os.path.dirname(pypath)

    #find free port
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(('',0))
    addr, port = s.getsockname()
    s.close()

    subprocess.Popen([pypath, "%s/tensorboard"%pydir, "--logdir=%s"%logdir, "--port=%d"%port, "--debug"])
    host = socket.gethostname()
    tb_url = "http://{0}:{1}".format(host, port)

    #dump tb host:port to hdfs
    hdfs.dump(tb_url, hdfs.project_path() + "/Jupyter/.jupyter.tensorboard", user=hdfs.project_user())


def get_logdir():
    return logdir

@@ -289,17 +289,19 @@
 s hdfs%0A%0A
-p
+log
 dir = os
@@ -313,107 +313,85 @@
 wd()
-%0Alogpath = pdir + %22/events%22%0Aos.makedirs(logpath)%0Alogdir = os.path.dirname(logpath)%0A%0Adef register():
+ + %22/tensorboard%22%0A%0Adef register():%0A%0A    global logdir%0A    os.makedirs(logdir)
 %0A%0A  
@@ -830,16 +830,76 @@
 to hdfs%0A
+    #Current applicationId%0A    app_id = os.getenv(%22APP_ID%22)%0A
     hdfs
@@ -949,15 +949,22 @@
 er/.
-jupyter
+%22 + app_id + %22
 .ten

fix makedirs bug