o
    F7[T                     @   s   d dl Z d dlZd dlZzd dlmZ W n ey!   dd ZY nw d dlmZ d dlm	Z
 d dlmZ d dlmZ d d	lmZ d d
lmZ G dd deZedZedZedZedZedZedZG dd deZdS )    N)getpwuidc                 C   s   dgS )Nz	<unknown> )xr   r   0/usr/lib/python3/dist-packages/pysmi/compiler.py<lambda>   s    r   )__name__)__version__)MibInfo)SymtableCodeGenerror)debugc                   @   s   e Zd ZdZdd ZdS )	MibStatusa{  Indicate MIB transformation result.

    *MibStatus* is a subclass of Python string type. Some additional
    attributes may be set to indicate the details.

    The following *MibStatus* class instances are defined:

    * *compiled* - MIB is successfully transformed
    * *untouched* - fresh transformed version of this MIB already exisits
    * *failed* - MIB transformation failed. *error* attribute carries details.
    * *unprocessed* - MIB transformation required but waived for some reason
    * *missing* - ASN.1 MIB source can't be found
    * *borrowed* - MIB transformation failed but pre-transformed version was used
    c                 K   s(   |  | }|D ]
}t||||  q|S )N)	__class__setattr)selfkwargsnkr   r   r   
setOptions(   s   
zMibStatus.setOptionsN)r   
__module____qualname____doc__r   r   r   r   r   r      s    r   compiledZ	untouchedZfailedZunprocessedmissingborrowedc                   @   sL   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd ZdS )MibCompilera  Top-level, user-facing, composite MIB compiler object.

    MibCompiler implements high-level MIB transformation processing logic.
    It executes its actions by calling the following specialized objects:

      * *readers* - to acquire ASN.1 MIB data
      * *searchers* - to see if transformed MIB already exists and no processing is necessary
      * *parser* - to parse ASN.1 MIB into AST
      * *code generator* - to perform actual MIB transformation
      * *borrowers* - to fetch pre-transformed MIB if transformation is impossible
      * *writer* - to store transformed MIB data

    Required components must be passed to MibCompiler on instantiation. Those
    components are: *parser*, *codegenerator* and *writer*.

    Optional components could be set or modified at later phases of MibCompiler
    life. Unlike singular, required components, optional one can be present
    in sequences to address many possible sources of data. They are
    *readers*, *searchers* and *borrowers*.
    indexc                 C   s0   || _ || _t | _|| _g | _g | _g | _dS )zCreates an instance of *MibCompiler* class.

           Args:
               parser: ASN.1 MIB parser object
               codegen: MIB transformation object
               writer: transformed MIB storing object
        N)_parser_codegenr
   
_symbolgen_writer_sources
_searchers
_borrowers)r   parserZcodegenwriterr   r   r   __init__N   s   
zMibCompiler.__init__c                 G   B   | j | tjtj@ otdddd | j D   | S  | S )aa  Add more ASN.1 MIB source repositories.

        MibCompiler.compile will invoke each of configured source objects
        in order of their addition asking each to fetch MIB module specified
        by name.

        Args:
            sources: reader object(s)

        Returns:
            reference to itself (can be used for call chaining)

        zcurrent MIB source(s): %s, c                 S      g | ]}t |qS r   str.0r   r   r   r   
<listcomp>o       z*MibCompiler.addSources.<locals>.<listcomp>)r"   extendr   loggerflagCompilerjoin)r   Zsourcesr   r   r   
addSources^      zMibCompiler.addSourcesc                 G   r(   )a  Add more transformed MIBs repositories.

        MibCompiler.compile will invoke each of configured searcher objects
        in order of their addition asking each if already transformed MIB
        module already exists and is more recent than specified.

        Args:
            searchers: searcher object(s)

        Returns:
            reference to itself (can be used for call chaining)

        z%current compiled MIBs location(s): %sr)   c                 S   r*   r   r+   r-   r   r   r   r/      r0   z,MibCompiler.addSearchers.<locals>.<listcomp>)r#   r1   r   r2   r3   r4   )r   Z	searchersr   r   r   addSearcherss   r6   zMibCompiler.addSearchersc                 G   r(   )a#  Add more transformed MIBs repositories to borrow MIBs from.

        Whenever MibCompiler.compile encounters MIB module which neither of
        the *searchers* can find or fetched ASN.1 MIB module can not be
        parsed (due to syntax errors), these *borrowers* objects will be
        invoked in order of their addition asking each if already transformed
        MIB can be fetched (borrowed).

        Args:
            borrowers: borrower object(s)

        Returns:
            reference to itself (can be used for call chaining)

        zcurrent MIB borrower(s): %sr)   c                 S   r*   r   r+   r-   r   r   r   r/      r0   z,MibCompiler.addBorrowers.<locals>.<listcomp>)r$   r1   r   r2   r3   r4   )r   Z	borrowersr   r   r   addBorrowers   s   zMibCompiler.addBorrowersc                 C   sX   zt  }W n ty   d}Y nw ztt  }W ||fS  ty+   d}Y ||fS w )N)?r9   r9   r9   r9   r9   )r9   r9   r9   r9   r9   r9   r9   )osunameAttributeErrorr   getuid	Exception)r   platform_info	user_infor   r   r   _get_system_info   s   zMibCompiler._get_system_infoc                    sJ  i  i }i }i }i }i }dd |D }i }	|rF| d}
|
|v r/tjtj@ o-td|
  q|
|v rAtjtj@ o?td|
  q| jD ]}tjtj@ oRtd|  zg||
\}}| j|D ]V}| j	||\}}|||j
< |||f||j
< |
|v r||
= ||j |j
|v r|j
|	vrg |	|j
< |	|j
 |j
 tjtj@ otd|j
|
|jd|jpd	f  qaW  n tjy   tjtj@ otd
|
|f  Y qD tjy   t \}}}||_|
|_| jd|
 7  _tjtj@ o
td|drdpd||f  |||
< tj|d |
< Y qDw td|
 }|
|_tjtj@ o1td|
  |
|vr;|||
< |
 vrDt |
< |stjtj@ oYtdt|t|f  t|D ]}
||
 \}}}tjtj@ ottd|
  | jD ]}z|j|
|j |dd W qx tj!y   tjtj@ otd|
|f  Y qx tj"y   tjtj@ otd|
|f  ||
= t# |
< Y  nb tjy   t \}}}||_$|
|_| jd|
 7  _tjtj@ otd||f  Y qxw tjtj@ otd|
  |dr)|
|	vr)tjtj@ otd|
  ||
= t# |
< q^q^tjtj@ o>tdt|t|f  |% D ]}
||
 \}}}tjtj@ o\td|
|jf  | & \}}d|j d t't(t)* f d!|d" |d |d# |d f d$tj+,d%d  g}z3| j-j	||||d&|d'd(\}}|||f||
< ||
= tjtj@ otd)|
|j| j.f  W qC tjy   t \}}}| j-|_/|
|_| jd|
 7  _tjtj@ otd| j-|f  tj|d |
< |||
< ||
= Y qCw tjtj@ otd*t|t|f  |% D ]}
|dr5|
|	vr5tjtj@ o2td+|
  q| j0D ]_}tjtj@ oItd,|
|f  z-|j|
|d&d-\}}|t1|
g d.|f||
< ||
= tjtj@ ottd/|
|f  W  n  tjy   tjtj@ otd|t d" f  Y q8w qtjtj@ otd0t|t|f  |% D ]}
tjtj@ otd1|
  ||
 \}}}| jD ]}z|j|
|j |dd W q tj!y   tjtj@ otd|
|f  Y q tj"y   tjtj@ otd|
|f  ||
= t# |
< Y  n tjyL   t \}}}||_$|
|_| jd|
 7  _tjtj@ oHtd||f  Y qw tjtj@ oZtd|
  |dry|
|	vrytjtj@ ostd+|
  t# |
< n!tjtj@ otd2|
  ||
 ||
< t2j|j|j3|j
d3 |
< ||
= qtjtj@ otd*t|t|f  |r|dstjtj@ otd4d|  |D ]}
t4 |
< qψ S tjtj@ otd5d|d|f  |% D ]}
||
 \}}}zH|d6d7r| j.j5|
||d8d9 tjtj@ o td:|
| j.f  ||
= |
 vrBt6j|j|j3|j
|j7|j8|j9|j:|j;|j<d;	 |
< W q tjy   t \}}}| j-|_/|
|_| jd|
 7  _tjtj@ ottd<|| j.f  tj|d |
< |||
< ||
= Y qw tjtj@ otd=d fd>d D    S   S )?aH  Transform requested and possibly referred MIBs.

        The *compile* method should be invoked when *MibCompiler* object
        is operational meaning at least *sources* are specified.

        Once called with a MIB module name, *compile* will:

        * fetch ASN.1 MIB module with given name by calling *sources*
        * make sure no such transformed MIB already exists (with *searchers*)
        * parse ASN.1 MIB text with *parser*
        * perform actual MIB transformation into target format with *code generator*
        * may attempt to borrow pre-transformed MIB through *borrowers*
        * write transformed MIB through *writer*

        The above sequence will be performed for each MIB name given in
        *mibnames* and may be performed for all MIBs referred to from
        MIBs being processed.

        Args:
            mibnames: list of ASN.1 MIBs names
            options: options that affect the way PySMI components work

        Returns:
            A dictionary of MIB module names processed (keys) and *MibStatus*
            class instances (values)

        c                 S   s   g | ]}|qS r   r   r-   r   r   r   r/      s    z'MibCompiler.compile.<locals>.<listcomp>r   zMIB %s already parsedzMIB %s already failedztrying source %sz0%s (%s) read from %s, immediate dependencies: %sr)   z<none>zno %s found at %sz
 at MIB %sz%serror %s from %signoreErrorsz	ignoring zfailing on r   zMIB source %s not foundzno %s found everywherez MIBs analyzed %s, MIBs failed %sz checking if %s requires updatingrebuild)rC   z'no compiled MIB %s available through %sz2will be using existing compiled MIB %s found by %szerror from %s: %sz*no suitable compiled MIB %s found anywhereZnoDepsz.excluding imported MIB %s from code generationzMIBs parsed %s, MIBs failed %szcompiling %s read from %szASN.1 source %sProduced by %s-%s at %s,On host %s platform %s version %s by user %s      Using Python version %s
genTexts
textFilter)commentsrJ   rK   z"%s read from %s and compiled by %szMIBs built %s, MIBs failed %sz(excluding imported MIB %s from borrowingztrying to borrow %s from %s)rJ   )nameimportedz%s borrowed with %sz/MIBs available for borrowing %s, MIBs failed %sz,checking if failed MIB %s requires borrowingzwill borrow MIB %s)pathfilealiaszfailing with problem MIBs %sz-proceeding with built MIBs %s, failed MIBs %sZ	writeMibsTdryRunrR   z%s stored by %s)	rO   rP   rQ   oidoidsidentityrevision
enterprise
compliancezerror %s from %szMIBs modified: %sc                    s   g | ]
} | d v r|qS ))r   r   r   r-   Z	processedr   r   r/     s    )=popr   r2   r3   r"   getDatar   parser    ZgenCoderM   r1   rN   appendrO   r4   r   ZPySmiReaderFileNotFoundError
PySmiErrorsysexc_infosourcemibnamemsggetstatusFailedr   statusMissinglentupler#   Z
fileExistsmtimeZPySmiFileNotFoundErrorZPySmiFileNotModifiedErrorstatusUntouchedsearchercopyrA   packageNamepackageVersiontimeasctimeversionsplitr   r!   Zhandlerr$   r	   statusBorrowedrP   statusUnprocessedputDatastatusCompiledrT   rU   rV   rW   rX   rY   )r   ZmibnamesoptionsZ
parsedMibsZ
failedMibsZborrowedMibsZ	builtMibsZsymbolTableMapZmibsToParseZcanonicalMibNamesrc   rb   ZfileInfoZfileDataZmibTreeZmibInfoZsymbolTable	exc_classexctbrl   r?   r@   rL   ZmibDataZborrowerr   rZ   r   compile   sD  







F


	 
 

&





  

 zMibCompiler.compilec           	      K   s  |   \}}dttt f d|d |d |d |d f dtjdd  g}z| jj	| j
| jj||| j| j
d|d	d
 W d S  tjy   t \}}}| jd| j
 7  _tjtj@ oltd|| j
f  |druY d S t|dr|||w )NrD   rE   rF   r   rG   rH   rI   )rL   Zold_index_datarR   rS   z at MIB index %szerror %s when building %srB   with_traceback)rA   rn   ro   rp   rq   r`   rr   rs   r!   rv   	indexFiler   ZgenIndexr\   re   r   r_   ra   rd   r   r2   r3   hasattrr}   )	r   ZprocessedMibsrx   r?   r@   rL   ry   rz   r{   r   r   r   
buildIndex  s8   	


zMibCompiler.buildIndexN)r   r   r   r   r~   r'   r5   r7   r8   rA   r|   r   r   r   r   r   r   7   s      fr   )r`   r:   rp   pwdr   ImportErrorZpysmir   rn   r   ro   Zpysmi.mibinfor	   Zpysmi.codegen.symtabler
   r   r   r,   r   rw   rk   rf   ru   rg   rt   objectr   r   r   r   r   <module>   s,   