a
    ifW                     @   sx   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d Zdd Zdd Z	dd	d
Z
dd Zdd Zedkrte  dS )    Nzreload-cmds.shc                  O   s   t | dtji| d S )Nfile)printsysstderr)argskwargs r   :/usr/share/crypto-policies/python/build-crypto-policies.pyeprint   s    r
   c                  C   s   t jdd} | jdddd | jdddd | jd	td
dd | jdddd | jdddd | jddd | jddd |  S )zParse the command lineF)allow_abbrevz--flat
store_truez1put all the generated files in a single directory)actionhelpz--testz7compare the generated config file with the existing onez--policyZPOLICYz"generate the specified policy only)typemetavarr   z--reloadcmdszDalso save reload cmds into reload-cmds.sh script in output directoryz--strictzfail on warnings	policydirz5a directory with base policy definition files (*.pol))r   	outputdirz.a target directory with generated config files)argparseArgumentParseradd_argumentstr
parse_args)parserr   r   r	   r      s.    
r   c                 C   sz  | j r$tj| j| d| d}n`tj| j|}tj|srzt| W n" typ   td|  Y dS 0 tj||d }| j	r"z^t
|dd}| }W d    n1 s0    Y  ||krtd| d| d	 W dS W d
S  t y   Y n$ ty    td|  Y dS 0 td| d|  t
|ddd}|| W d    n1 sf0    Y  t  d
S )N-z.txtz#Cannot create directory for policy Futf-8)encodingzConfig for z for policy z differs from the existing oneTzError reading generated file zSaving config for wmoder   )Zflatospathjoinr   isdirmkdirOSErrorr
   testopenreadFileNotFoundErrorr   write)cmdlinepolicy_nameZconfig_nameconfigr    dirpathfZ
old_configr   r   r	   save_config/   s<    
&*r/   c              
   C   s(  d}|d u rg }zt j|g|R d| ji}W n6 tyd } ztdt|  W Y d }~dS d }~0 0 dd ttD }|D ]}tj| }| }	|		|
|	j}
|dv s|	|
rz(d|g|}t| ||	j|
sd	}W n. ty   td
|	j  td d}Y n0 q|td|	j  d}q||S )Nr   r   zError:    c                 S   s   g | ]}d |v r|qS 	Generatorr   .0gr   r   r	   
<listcomp>`       z build_policy.<locals>.<listcomp>>   ZEMPTYz	GOST-ONLY:   zError saving config for zKeeping original configuration   zError testing config for    )cryptopoliciesZUnscopedCryptoPolicyr   
ValueErrorr
   r   dirpolicygenerators__dict__Zgenerate_configZscopedZSCOPESZtest_configr!   r/   ZCONFIG_NAMEr$   )r*   r+   subpolicy_nameserrZcpe
generatorsr5   clsgenr,   namer   r   r	   build_policyS   s:    

rH   c                 C   s   d}dd t tD }tj| jt}zRt|ddd0}|D ]}tj| }|	|j
 q<W d    n1 sl0    Y  W n ty   td d}Y n0 |S )	Nr   c                 S   s   g | ]}d |v r|qS r1   r   r3   r   r   r	   r6   y   r7   z$save_reload_cmds.<locals>.<listcomp>r   r   r   zError saving reload cmds   )r>   r?   r   r    r!   r   RELOAD_CMD_NAMEr&   r@   r)   Z
RELOAD_CMDr$   r
   )r*   rB   rD   r    r.   r5   rE   r   r   r	   save_reload_cmdsv   s    
0
rK   c                  C   s   t  } d}| jrtd | jrPdd | j dD }|^}}t| ||}nzt	| j
^}|D ]H}|jdsb|jddrbtj|j\}}|d	krbt| |}|rb qqbW d
   n1 s0    Y  |s| jrt| }t| d
S )z!The actual command implementationr   errorc                 S   s   g | ]}|r|qS r   r   )r4   nr   r   r	   r6      r7   zmain.<locals>.<listcomp>r8   .F)follow_symlinksz.polN)r   strictwarningsfilterwarningsZpolicyuppersplitrH   r   scandirr   rG   
startswithis_filer    splitextZ
reloadcmdsrK   r   exit)r*   rB   namesr+   rA   sdiextr   r   r	   main   s(    

$
r^   __main__)N)r   r   r   rQ   r<   r?   rJ   r
   r   r/   rH   rK   r^   __name__r   r   r   r	   <module>   s   $
#