a
    Lg                     @   sx   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
mZ ed
ee e
d dddZG dd	 d	ZdS )    N)defaultdict)contextmanager)OptionalListIteratorSet)pathreturnc              	   c   sJ   | d u rd V  d S t  }t |  zd V  W t | nt | 0 d S N)osgetcwdchdir)r   Z	saved_dir r   G/home2/Puru_Virtio_Blk/virtio-demo/qemu/tests/qemu-iotests/findtests.pyr      s    
r   c                   @   sz   e Zd Zdee ddddZeddddZeedd	d
Zdeee  eee  eee  ee ee dddZ	dS )
TestFinderN)test_dirr	   c              
   C   s   t t| _t| td| _|  jdd tdD 7  _| jD ]n}t|ddN}|D ]8}|drZ|	 dd  D ]}| j| 
| qx qqZW d    qD1 s0    Y  qDW d    n1 s0    Y  d S )	Nz[0-9][0-9][0-9]c                 S   s*   g | ]"}| d stj|d  r|qS )z.out)endswithr   r   isfile).0fr   r   r   
<listcomp>/   s   
z'TestFinder.__init__.<locals>.<listcomp>ztests/*utf-8encodingz	# group:    )r   setgroupsr   glob	all_testsZiglobopen
startswithsplitadd)selfr   tr   linegr   r   r   __init__*   s    



zTestFinder.__init__)fnamer	   c                 C   s   t |ddp}|D ]Z}| }|r|d dkr0q| }| |d }|dd  }|D ]}| j| | qVqW d    n1 s0    Y  d S )Nr   r   r   #   )r   stripr!   parse_test_namer   r"   )r#   r(   r   r%   wordsZ	test_filer   r&   r   r   r   add_group_file;   s    zTestFinder.add_group_file)namer	   c                 C   s^   d|v rt d| dtd|r2t|d}ntjd|}|| jvrZt d| d|S )	N/z5Paths are unsupported for test selection, requiring "z
" is wrongz\d+Z03testszTest "z" is not found)
ValueErrorre	fullmatchintr   r   joinr   )r#   r/   r   r   r   r,   J   s    

zTestFinder.parse_test_name)r   exclude_groupsr1   
start_fromr	   c                    s   |du rg }|du rg }|du r$g }t  }|rH|j fdd|D   n|rZ|rftdn| j d|vrd|vr|dg }|j fdd|D  }|D ]}| | qt|}|dur|d| |= |S )a&  Find tests

        Algorithm:

        1. a. if some @groups specified
             a.1 Take all tests from @groups
             a.2 Drop tests, which are in at least one of @exclude_groups or in
                 'disabled' group (if 'disabled' is not listed in @groups)
             a.3 Add tests from @tests (don't exclude anything from them)

           b. else, if some @tests specified:
             b.1 exclude_groups must be not specified, so just take @tests

           c. else (only @exclude_groups list is non-empty):
             c.1 Take all tests
             c.2 Drop tests, which are in at least one of @exclude_groups or in
                 'disabled' group

        2. sort

        3. If start_from specified, drop tests from first one to @start_from
           (not inclusive)
        Nc                 3   s   | ]} j | V  qd S r
   r   r   r&   r#   r   r   	<genexpr>       z(TestFinder.find_tests.<locals>.<genexpr>z0Can't exclude from individually specified tests.Zdisabledc                 3   s   | ]} j | V  qd S r
   r9   r:   r;   r   r   r<      r=   )	r   updater2   r   
differencer"   r,   sortedindex)r#   r   r7   r1   r8   resr$   Zsequencer   r;   r   
find_tests\   s,    

zTestFinder.find_tests)N)NNNN)
__name__
__module____qualname__r   strr'   r.   r,   r   rC   r   r   r   r   r   )   s       

r   )N)r   r   r3   collectionsr   
contextlibr   typingr   r   r   r   rG   r   r   r   r   r   r   <module>   s   