o
    AIdG                     @   sh   d dl Z d dlZd dlZd dlmZ G dd deZe Zdd Z	dd Z
d	d
 Zdd ZdddZdS )    N)load_configc                   @   s    e Zd ZdZdd Zdd ZdS )	NamespacezA
    dict that also allows access to elements as attributes.
    c                 C   s   || vrt | | S N)AttributeError)selfkey r   R/home/thorsten/Data/Arbeit/PerFact/Git/ema-modules/MigrationMonster/bin/helpers.py__getattr__   s   zNamespace.__getattr__c                 C   s   || |< d S r   r   )r   r   valuer   r   r	   __setattr__   s   zNamespace.__setattr__N)__name__
__module____qualname____doc__r
   r   r   r   r   r	   r      s    r   c                 K   s6   ddi}| | t| tr|  } tj| fi |S )z9 Wrapper for subprocess.run that defaults to check=True. checkT)update
isinstancestrsplitsprun)cmdkwargsr   r   r	   r      s
   

r   c                  O   s@   g }| D ]}t |tr| }|tdg| fi | q|S )z# Run multiple commands with sudo.  sudo)r   r   r   appendr   )cmdsr   returnsr   r   r   r	   r   !   s   
r   c                  O   sL   g }| D ]}t |tr| }tdtjg| fi |}||d  q|S )z Execute commands in chroot chrootr   )r   r   r   r   env
mount_pathr   )r   r   r   r   retr   r   r	   chroot_cmds+   s   
r#   c                   C   s~   t jt jt jtdt_t jtjdt_t jtjdt_	t
di tt jtjdt_t jtj	tjjt_tS )Nz..configbuildztarget_disk_conf.pyr   )ospathrealpathjoindirname__file__r    	base_pathconfig_path
build_pathr   r   disk_config	mount_dirr!   r   r   r   r	   
create_env6   s   
r1   c                 C   s   dd |   D }ttt| }d}|  D ],\}}|jp d}| dd}d|v r7|dd}d|v s-|||d | d 7 }qt	j
|t	jd	}|jd
tddd | }	|rc|	jsc|g|	_|	jD ]	}
||
 }|  qfdS )a  
    Create an argument parser and parse the arguments to call one or more of
    several available commands.
    The available commands are taken from the dictionary avail_cmds, but only
    callables not starting with _.
    Underscores in the name are replaced by dashes for construction of the
    argument parser.
    The docstring of each function is shown in the help text.
    c                 S   s0   i | ]\}}| d st|r|d d|qS )_-)
startswithcallablereplace).0namefncr   r   r	   
<dictcomp>M   s    z&run_cmds_from_args.<locals>.<dictcomp>zAvailable commands:
 
 z     )epilogformatter_classcommand*z(Only execute these wrappers or commands.)typenargshelpN)itemsmaxmaplenkeysr   stripr6   ljustargparseArgumentParserRawTextHelpFormatteradd_argumentr   
parse_argsrA   )availdefaultr   maxlenhelptextr   r9   docparserr   cmdnamer   r   r	   run_cmds_from_argsB   s6   


rY   )NN)r&   rM   
subprocessr   perfact.genericr   dictr   r    r   r   r#   r1   rY   r   r   r   r	   <module>   s   	
