<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Protocol of experiments 08.05.2017
Goal
Step2:  Then we define two util functions.
First one if for running bash command.
Step4:  Second one is for writing string in the file.
Step5:  Than we define some constants for future use.
Step6:  Compose bash command for running detached docker container from tooldog-analyser-sandbox image, that will be destroyed after stopping.
Step7:  Save container ID for future use.
Step9:  Yay! Now we can do stuff inside the container.
Let's define one more util function for running commands inside the created container.
Step10:  We install argparse2tool
Step11:  As well as the tool for analysing
Step12:  Now we are ready to generate CWL
Step13:  Write CWL to file
Step14:  Finally, let's stop the container. It will be removed automatically.
<ASSISTANT_TASK:>
Python Code:
import subprocess
Explanation: Protocol of experiments 08.05.2017
Goal: Provide proof of concept and working example for the idea of running ToolDog analyser in the Docker container
Requirements:
1. Installed Docker
1. Installed Python3, pip
First, we import a library for running bash commands from Python code.
End of explanation
def run_command(command):
    Run bash command and return the result
    :param str command: String representation of bash command
    
    :return: Returns a generator of output of the result of running bash command in bytes
    :rtype: iter
    
    command = command.split()
    p = subprocess.Popen(command,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT)
    return iter(p.stdout.readline, b'')
Explanation: Then we define two util functions.
First one if for running bash command.
End of explanation
def write_to_file(filename, string = ''):
    Write string to file
    :param str filename: Filename
    :param str string: String to write
    
    f = open(filename, 'w')
    f.write(string)
    f.close()
Explanation: Second one is for writing string in the file.
End of explanation
PYTHON_VERSION = 2 # version of python tool
TOOL_NAME = 'alignment' # name of the tool, assuming it is available via PyPi
OUTPUT_FORMAT = 'cwl' # output: cwl / xml
OUTPUT_FILENAME = TOOL_NAME + "." + OUTPUT_FORMAT # output filename
PYTHON_PATH = "/usr/local/lib/python3.5/dist-packages/" if PYTHON_VERSION == 3 else \
"/usr/local/lib/python2.7/dist-packages/" # PYTHONPATH, required for argparse2tool
Explanation: Than we define some constants for future use.
End of explanation
cmd = 'docker run -d -e PYTHONPATH=' + PYTHON_PATH + ' --rm inkuzm/tooldog-analyser-sandbox tail -f /dev/null'
Explanation: Compose bash command for running detached docker container from tooldog-analyser-sandbox image, that will be destroyed after stopping.
End of explanation
CONTAINER_ID = ""
for line in run_command(cmd):
    CONTAINER_ID = line.decode("utf-8")[:-1]
    print(CONTAINER_ID)
Explanation: Save container ID for future use.
End of explanation
def exec_command_in_container(command, cid):
    Run bash command inside container cid
    :param str command: String representation of bash command
    
    :return: Returns a generator of output of the result of running bash command in bytes
    :rtype: iter
      
    cmd = 'docker exec ' + cid + ' ' + command
    return run_command(cmd)
Explanation: Yay! Now we can do stuff inside the container.
Let's define one more util function for running commands inside the created container.
End of explanation
for line in exec_command_in_container('pip3 install argparse2tool', CONTAINER_ID):
    print(line.decode("utf-8")[:-1])
Explanation: We install argparse2tool:
End of explanation
for line in exec_command_in_container('pip3 install ' + TOOL_NAME, CONTAINER_ID):
    print(line.decode("utf-8")[:-1])
Explanation: As well as the tool for analysing:
End of explanation
cwl_tool = '' # string representation of cwl tool
for line in exec_command_in_container('tooldog --generate_cwl_tool', CONTAINER_ID):
    cwl_tool += line.decode("utf-8")[:-1]
Explanation: Now we are ready to generate CWL:
End of explanation
write_to_file(OUTPUT_FILENAME, cwl_tool)
Explanation: Write CWL to file:
End of explanation
run_command('docker stop ' + CONTAINER_ID)
Explanation: Finally, let's stop the container. It will be removed automatically.
End of explanation
<END_TASK>