U
    a                     @   s   d dl Z d dlmZ d dlmZ d dlZeedddZeddd	Zedd
dZ	e
dddZe
dddZG dd dZdeeeeedddZdS )    N)copyfile)sleep)sreturnc                 C   s   d |   S )N )joinsplitlower)r    r
   5/mnt/c/Users/Owen/Downloads/autograder/tests/utils.pyclean   s    r   )r   c                   C   s   d tdd d S )N/)r   __file__r   r
   r
   r
   r   getAutograderDir   s    r   c                  C   s>   t  d } dd t| D }t|dkr.d S | d |d  S )Nz/sourcec                 S   s   g | ]}d |kr|qS )z.outr
   ).0xr
   r
   r   
<listcomp>   s      z"findExecutable.<locals>.<listcomp>r   r   )r   oslistdirlen)	sourceDirZoutFiler
   r
   r   findExecutable   s    
r   filesc                    s   t  }|d }dd t|D } fdd|D }t  D ]4}|D ]*}|dd |krF|dd  qFq>fdd D }t|dkrtd	d
	|d S )N/submission/c                 S   s*   g | ]"\}}}|D ]}t j||qqS r
   r   pathr   r   Zdpdn	filenamesfr
   r
   r   r       s       zcheckFiles.<locals>.<listcomp>c                    s"   g | ]}| d d  kr|qS r   r   r   ir   r
   r   r   $   s      r   r#   c                    s   g | ]}| kr|qS r
   r
   )r   file)filesSubmittedr
   r   r   .   s      r   z.ERROR: You are missing the following files:
{} )
r   r   walklistr   appendr   AssertionErrorformatr   )r   autograderDirsubmissionDirallFilesZsubmittedFilesr'   ZfileDirZfilesMissingr
   )r   r(   r   
checkFiles   s    r2   c                    sb   t  }|d }|d }dd t|D } fdd|D }|D ]}t|||dd   q@d S )Nr   z/source/c                 S   s*   g | ]"\}}}|D ]}t j||qqS r
   r   r   r
   r
   r   r   ;   s       zcopyFiles.<locals>.<listcomp>c                    s"   g | ]}| d d  kr|qS r"   r$   r%   r   r
   r   r   >   s      r   r#   )r   r   r*   r   r   )r   r/   r0   r   r1   Z
validFilesr'   r
   r   r   	copyFiles5   s    r3   c                   @   s<   e Zd ZU dZeed< eed< eed< eeedddZdS )	
Submissionz
    output - string containing the program's output (stdout)
    errors - string containing the program's errors (stderr)
    timedout - True if the submission timed out during execution, False otherwise
    outputerrorstimedoutr5   r6   r7   c                 C   s   || _ || _|| _d S )Nr8   )selfr5   r6   r7   r
   r
   r   __init__O   s    zSubmission.__init__N)__name__
__module____qualname____doc__str__annotations__boolr:   r
   r
   r
   r   r4   E   s
   
r4   皙?)
executable	inputFiletxtContentstimeoutr   c              
   C   s8  | dkrt  } | dkrtdz^|rJt|d}t| d}W 5 Q R X n|rXt|d}td tj| gtj||d}d}W n, tj	k
r } z|}d}W 5 d}~X Y nX |j
dkrd	n|j
d
d d}|jdkrd	n
|jd}	d}
t|dkr
|dd |
 }t|	dkr(|	dd |
 }	t||	|}|S )aU  
    Run the specified executable with given input and return its output

    executable - string containing the name of the executable to run. If not specified,
                    the first file that ends in ".out" will be executed. Default is `None`
    inputFile -  string containing the name of a text file containing user input, separated by newlines.
                    Default is `None`
    txtContents - string containing the user input, separated by newlines. Default is `None`.
    timeout - float specifying how many seconds to wait before terminating the program. Default is 0.1
    NzC.out file does not exist - your program may have failed to compile.rascii   )stdoutrF   inputFTr   s   ELFr   zutf-8z@

** The output exceeded 1500 characters, so it was truncated **i  )r   r-   openbytesreadr   
subprocessrunPIPETimeoutExpiredrJ   r   decodestderrr   r4   )rC   rD   rE   rF   r!   resultsr7   erJ   rT   ZtruncationMessage
submissionr
   r
   r   
runProgramU   s:    
$rX   )NNNrB   )r   shutilr   timer   rO   r?   r   r   r   r+   r2   r3   r4   floatrX   r
   r
   r
   r   <module>   s   