# Path: studio/util/util.py
# def rand_string(length):
#     return "".join([random.choice(string.ascii_letters + string.digits)
#                     for n in range(length)])


import os
from studio.util.util import rand_string
Utility functions for anything shared in common by ec2cloud_worker and
gcloud_worker
"""

INDENT = 4


def insert_user_startup_script(user_startup_script, startup_script_str,
                               logger):
    if user_startup_script is None:
        return startup_script_str

    try:
        with open(os.path.abspath(os.path.expanduser(
                user_startup_script))) as f:
            user_startup_script_lines = f.read().splitlines()
    except BaseException:
        if user_startup_script is not None:
            logger.warn("User startup script (%s) cannot be loaded" %
                        user_startup_script)
        return startup_script_str

    startup_script_lines = startup_script_str.splitlines()
    new_startup_script_lines = []
    whitespace = " " * INDENT
    for line in startup_script_lines:

        if line.startswith("studio remote worker") or \
                line.startswith("studio-remote-worker"):
            curr_working_dir = "curr_working_dir_%s" % rand_string(32)

# Path: studio/util/util.py
# def rand_string(length):
#     return "".join([random.choice(string.ascii_letters + string.digits)
#                     for n in range(length)])


# Path: studio/cloud_worker_util.py
import os
from studio.util.util import rand_string

Utility functions for anything shared in common by ec2cloud_worker and
gcloud_worker
"""

INDENT = 4


def insert_user_startup_script(user_startup_script, startup_script_str,
                               logger):
    if user_startup_script is None:
        return startup_script_str

    try:
        with open(os.path.abspath(os.path.expanduser(
                user_startup_script))) as f:
            user_startup_script_lines = f.read().splitlines()
    except BaseException:
        if user_startup_script is not None:
            logger.warn("User startup script (%s) cannot be loaded" %
                        user_startup_script)
        return startup_script_str

    startup_script_lines = startup_script_str.splitlines()
    new_startup_script_lines = []
    whitespace = " " * INDENT
    for line in startup_script_lines:

        if line.startswith("studio remote worker") or \
                line.startswith("studio-remote-worker"):
            curr_working_dir = "curr_working_dir_%s" % rand_string(32)

            func_name = "user_script_%s" % rand_string(32)