Mini Shell
3
�
LdU � @ sx d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZm Z dd� Z
dZdd � Zd
d� Z
dd
� Zdd� Zdd� ZdS )� N)�KickstartError)�_)�SSLError�RequestExceptionc C s d| kS )Nz://� )�locationr r �/usr/lib/python3.6/load.py�<lambda> s r Tc C s t | �rt| �S t| �S dS )z�Load a destination URL or file into a string.
Type of input is inferred automatically.
Arguments:
location -- URL or file name to load
Returns: string with contents
Raises: KickstartError on error readingN)�_is_url� _load_url�
_load_file)r r r r �load_to_str s
r
c C s� t | �r|t| �}y$t|d��}|j|� W dQ R X W nB tk
rv } z&ttd�| djt|�d� ��W Y dd}~X nX |S t | |� |S dS )a( Load a destination URL or file into a file name.
Type of input is inferred automatically.
Arguments:
location -- URL or file name to load
destination -- destination file name to write to
Returns: file name with contents
Raises: KickstartError on error reading or writing�wNzError writing file "%s":z: {e})�e)
r
r �open�write�IOErrorr r �format�str�
_copy_file)r �destination�contents�fhr r r r �load_to_file/ s 2
r c C s� yt j| td�}W n� tk
rT } z&ttd�| djt|�d� ��W Y dd}~X nB tk
r� } z&ttd�| djt|�d� ��W Y dd}~X nX |j t j
jkr�ttd�| djt|j �d� ��|jS ) z?Load a location (URL or filename) and return contents as string)Zverifyz!Error securely accessing URL "%s"z: {e})r NzError accessing URL "%s"z: {c})�c)
�requests�get�
SSL_VERIFYr r r r r r Zstatus_codeZcodes�ok�text)r Zrequestr r r r r I s 02"r c C s� yNt jr.t| d��}|j� jd�}W dQ R X nt| d��}|j� }W dQ R X W n6 tk
r� } zttd�t|� ��W Y dd}~X nX |S )z2Load a file's contents and return them as a string�rbzutf-8N�rzError opening file: %s) �sixZPY3r �read�decoder r r r )�filenamer r r r r r r X s &r c C sP yt j| |� W n: ttfk
rJ } zttd�t|� ��W Y dd}~X nX dS )zCopy file to destinationzError copying file: %sN)�shutilZcopyfile�OSErrorr r r r )r% r r r r r r g s r )r r&