3
•vh3 ã @ sŠ d dl mZmZmZ d dlZd dlZd dlZd dlZd dlZd dl Z d dl
mZ dZ
ddd„Zdd„ Zd d
„ Zdd„ Zd
d„ Zdd„ ZdS )é )Úprint_functionÚdivisionÚabsolute_importN)Úugettextz/etc/rhsm-host/c C sR |rDt |ƒt g ƒt f ƒgkr"|f}x |D ]}tjjt|ƒd ƒ q(W tj| ƒ dS )zDExit with a code and optional message(s). Saved a few lines of code.Ú
N)ÚtypeÚsysÚstderrÚwriteÚstrÚexit)ÚcodeZmsgsÚmsg© r ú/usr/lib/python3.6/utils.pyÚsystem_exit" s
r c C sl yt j| dd� W nT tk
rf } z8|jtjkr4dS |jtjkrVtt jtdƒj | ƒƒ W Y dd}~X nX dS )z‰
Attempts to create the path given (less any file)
:param path: path
:return: True if changes were made, false otherwise
ií )ÚmodeFz(Cannot create directory {}
Are you root?NT)
ÚosÚmakedirsÚOSErrorÚerrnoÚEEXISTÚEACCESr Ú EX_NOPERMÚ_Úformat)ÚpathÚer r r Ú
create_dir- s "r c C s� y2t j| ddd��}t||ƒ |jƒ W dQ R X W nX tk
rŠ } z<|jtjkrTdS |jtjkrxtt j
tdƒj| ƒƒ n‚ W Y dd}~X nX dS )zü
Attempts to create a file, with the given contents
:param path: The desired path to the file
:param contents: The contents to write to the file, should json-serializable
:return: True if the file was newly created, false otherwise
Úwzutf-8)ÚencodingNFz#Cannot create file {}
Are you root?T)
ÚioÚopenÚwrite_to_file_utf8Úflushr r r r r r r r r )r ÚcontentsÚfr r r r Úcreate_file? s
r' c C s t jjtƒrdS dS )z¡
Are we running in a docker container or not?
Assumes that if we see host rhsm configuration shared with us, we must
be running in a container.
TF)r r ÚexistsÚHOST_CONFIG_DIRr r r r Úin_containerW s r* c C s> t jr
| S | dk r6t| tƒr6t| tƒ r6| jdƒ} | S | S dS )z�
Transforms the provided string into unicode if it is not already
:param obj: the string to decode
:return: the unicode format of the string
Nzutf-8)ÚsixZPY3Ú
isinstancer ZunicodeÚdecode)Úobjr r r Ú make_utf8c s
r/ c C s | j ttj|dddd�ƒƒ dS )zÓ
Writes out the provided data to the specified file, with user-friendly indentation,
and in utf-8 encoding.
:param file: The file to write to
:param data: The data to be written
:return:
é FT)ÚindentZensure_asciiZ sort_keysN)r
r/ ÚjsonÚdumps)ÚfileÚdatar r r r# r s r# )N)Z
__future__r r r r! r2 r r r r+ Zsyspurpose.i18nr r r) r r r' r* r/ r# r r r r Ú