3
&O$`$                 @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZdZdZe	dkrVe
d e  dddZd	d
 ZdddZdd Zdd ZdS )    Nalvarez11-Jan-2020Z__main__zNot an executable module!Tc             C   s@   |r$t j }|s<t| | tj  ntjt| |g}|j  dS )z
    cmds - a command, with arguments passed as a list, as described in subprocess.call.
    rm_list - list of files to remove after command has terminated
    fork - if fork = True, run in the background as a separate process
    N)osfork	run_cleansysexit	threadingZThreadstart)cmdsrm_listr   pidT r   ,/home/psgendb/BIRCHDEV/script/birchscript.pyCleanrun   s    

r   c             C   s>   x| D ]}t j| qW tjd x|D ]}tj| q(W dS )a"  
    In the current implementation, run_clean waits 5 seconds after running
    the commands before deleting files in rm_list. This should really be replaced
    with a more deterministic way of doing things. Ideally, the remove command should
    actually be part of the command list.
       N)
subprocesscalltimeZsleepr   remove)r
   r   Zcmd_runZrm_filer   r   r   r   '   s
    


r   c             C   s.   t j }|s*|r tj| |d n
tj|  d S )N)env)r   r   r   r   )Z	popenargsZENVr   r   r   r   forkrun4   s
    r   c             C   s,   t | }x|D ]}|j| qW |j  dS )z6
    Write the contents of one file to a new file
    N)openwriteclose)ZINFILE	h_OUTFILEZh_templiner   r   r   cat_to=   s    
r   c              C   s   t jjt jjtjds@tjt jjtjdddt jjtjd t	t jjtjdd} | j
 }| j  t	t jjtjdd} x|dd D ]}| j| qW | j|d	  | j  |d	 jd
S )a\  
    Choose a remote host on which to execute a program.
    Currently, this is dumb function whose sole task
    is to rotate a list of hosts in $BIRCH/.BIRCHrhosts
    so that no one host always gets jobs. It should be
    replaced with a more sophisticated function that finds hosts
    that the user has permissions to execute programs on.
    z.BIRCHrhostsZlocalZadminZBIRCHrhostsrw   Nr   z
)r   pathexistsjoinbirchenvHOMEshutilZcopyfileBIRCHr   	readlinesr   r   rstrip)Zh_hosts_fileZ	host_listhostr   r   r   
choosehostG   s    
r+   )T)N)r$   r   r&   r   r   r   r   
__author____date____name__printr   r   r   r   r   r+   r   r   r   r   <module>   s    

	
