U Yzf_0@sdZddlZddlZddlZddlZddlmZmZdZddZ dd Z d d Z d d Z ddZ ddZddZddZddZddZddZd/ddZddZd d!Zd"d#Zd0d%d&Zd'd(Zd)d*Zd+d,Zd-d.ZdS)1z Utilities N) struct_unpackPY2FcCszrt|tr|WSt|}|dkr,|dWS|dkrT|dd|}td|dWS|dd|}td|dWSYnXdS) z&Unpacks the given buffer to an integerrr +----------+------------------------- | length | a string goes here +----------+------------------------- If the string is bigger than 250, then it looks like this: <- 1b -><- 2/3/8 -> +------+-----------+------------------------- | type | length | a string goes here +------+-----------+------------------------- if type == ü: length is code in next 2 bytes elif type == ý: length is code in next 3 bytes elif type == þ: length is code in next 8 bytes NULL has a special value. If the buffer starts with û then it's a NULL and we return None as value. Returns a tuple (trucated buffer, bytes). rr(rNr!rr)r rlsizeZfstrrrread_lc_strings  r8cCsg}dddd}t|}d}||kr ||}|dkr:dS|dkrV|d|d 7}q|d kr|}|||d ||d |d |7}qd}z ||}Wntk rYdSXt||d ||d }|||d ||||d |d ||7}qt|S) zXReads all length encoded strings from the given buffer Returns a list of bytes r3r!r)r2r4r5rrNr(rr1)r appendKeyErrorrtuple)r ZbyteslstsizesZbuf_lenposfirstrr7rrrread_lc_string_lists4     r?cCs|dkr|dkrtd|dk rhz||}Wntk rJtdYnX||dd|d|fS|dk rzt||StddS)zr Reads a string up until a character or for a given size. Returns a tuple (trucated buffer, string). Nz&read_string() needs either end or sizezend byte not present in bufferrrz.read_string() needs either end or size (weird))rindexr0)r endr.idxrrr read_string s rCcCs4zt|d|}WnYnX||d|fS)zMRead an integer from buffer Returns a tuple (truncated buffer, int) rNr6r-rrrread_int!s rDcCs|s td|d}|dkr,|dddfS|dkrH|ddt|fS|dkrr|ddtd|dddfS|d kr|d dtd |dd d dfS|d kr|ddtd|dddfStddS)z Takes a buffer and reads an length code string from the start. Returns a tuple with buffer less the integer and the integer read. z Empty buffer.rr(rNr2r!zMsz"_digest_buffer..cSsg|]}dt|qSrH)ordrIrrrrLNs)r strjoin)r rrr_digest_bufferJs rPcCsL|r<|r"|dkr"t|d|}nt|}t|d|n tt|dS)z2Debug function printing output of _digest_buffer()rz: N)rPprint)Zabufferprefixlimitdigestrrr print_bufferQs  rVc Csi}tjdd}tj|s"|St|P}|D]D}|d}t|dkrLq0|d}|dd d}|||<q0W5QRX|S) zsParse the contents of /etc/os-release file. Returns: A dictionary containing release information. /etcz os-release=r3rr " ospathrOexistsopensplitr lowerrstripstrip)distroZos_release_filefile_objline key_valuekeyvaluerrr_parse_os_release]s     rjc Csi}tjdd}tj|r|t|P}|D]D}|d}t|dkrHq,|d}|dd d}|||<q,W5QRX|S) ztParse the contents of /etc/lsb-release file. Returns: A dictionary containing release information. rWz lsb-releaserXr3rrrYrZr[)rdZlsb_release_filererfrgrhrirrr_parse_lsb_releasers     rkc Csi}ttjd}ztjd|d}Wn tk rDYW5QRdSX|t }|D]T}| d}t |dkrxq\|d dd  }|d d }tr|d n|||<q\W5QRX|S) ztParse the output of the lsb_release command. Returns: A dictionary containing release information. w)Z lsb_releasez-a)stderrN:r3r _r zutf-8)r_r\devnull subprocess check_outputOSErrordecodesysgetfilesystemencoding splitlinesr`r replacerarcrencode)rdrrstdoutlinesrfrgrhrirrr_parse_lsb_release_commands$   "r~cCst}|r,|dd|dd|ddfSts\t}|r\|dd|dd|ddfSt}|r|dd|d d|d dfSd S) aNTries to determine the name of the Linux OS distribution name. First tries to get information from ``/etc/os-release`` file. If fails, tries to get the information of ``/etc/lsb-release`` file. And finally the information of ``lsb-release`` command. Returns: A tuple with (`name`, `version`, `codename`) Z distrib_idrGZdistrib_releaseZdistrib_codenameZdistributor_idreleaseZcodenamenameZ version_idZversion_codename)rGrGrG)rkgetrr~rj)rdrrrlinux_distributions(          r)NN)NrQ)__doc__r\rsrrwZcatch23rrZ__MYSQL_DEBUG__rrrr"r$r&r'r,r0r8r?rCrDrFrPrVrjrkr~rrrrrs4      5)