3
Z>a_                 @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ ej	dZ
dd Zdd Zdd	 Zed
krejdjejjedd e  dS )    N)load_json_config_fileZComputeConfigurationsc               C   s
   t jdS )Na          This script Generates Makefile.json from Configrations.json.

        Starting in the current directory, it walks down every subdirectory
        looking for Configurations.json files. Every found Configurations.json
        file is assumed to look similar to the following format:

        {
          "ENTRY": "ARPProcessPacket",
          "CBMCFLAGS":
          [
              "--unwind 1",
              "--unwindset vARPRefreshCacheEntry.0:7,memcmp.0:17",
              "--nondet-static"
          ],
          "OBJS":
          [
            "$(ENTRY)_harness.goto",
            "$(FREERTOS_PLUS_TCP)/FreeRTOS_ARP.goto"
          ],
          "DEF":
          [
            {"disableClashDetection": ["ipconfigARP_USE_CLASH_DETECTION=0"]},
            {"enableClashDetection": ["ipconfigARP_USE_CLASH_DETECTION=1"]}
          ]
        }

        The format is mainly taken from the Makefile.json files.
        The only difference is that it expects a list of json object in the DEF
        section. This script will generate a Makefile.json in a subdirectory and
        copy the harness to each subdirectory.
        The key is later taken as the name for the configuration subdirectory
        prexied by 'config_'.

        So for the above script, we get two subdirectories:
        -config_disableClashDetection
        -config_enableClashDetection

        As an example, the resulting Makefile.json for the
        config_disableClashDetection directory will be:

        {
          "ENTRY": "ARPProcessPacket",
          "CBMCFLAGS": [
            "--unwind 1",
            "--unwindset vARPRefreshCacheEntry.0:7,memcmp.0:17",
            "--nondet-static"
          ],
          "OBJS": [
            "$(ENTRY)_harness.goto",
            "$(FREERTOS_PLUS_TCP)/FreeRTOS_ARP.goto"
          ],
          "DEF": [
            "ipconfigARP_USE_CLASH_DETECTION=0"
          ]
        }

        These Makefile.json files then can be turned into Makefiles for running
        the proof by executing the make-proof-makefiles.py script.
        )textwrapdedent r   r   X/home/aniruddha/Desktop/UnitTestSetup/test/cbmc/proofs/make_configuration_directories.pyprolog%   s    ;r   c          #   C   s  t tjj| d}y|d }W n tk
r<   tjd d S X xr|D ]h}tj| y"dd |j	 D d }|| }W nJ t
tfk
r } z*tj| tjtjd tjd| d S d }~X nX tjj| d	| }tj|jd
d
d d}	x:|D ]2}
|
jdrtjtjj| |
tjj||
 d
}	qW |	sJtjd|  tjd d S tj|}||d< d|j	 krx|d |d< nd
|d< ttjj|dd}tj||dd W d Q R X qFW d S )NzConfigurations.jsonDEFz3Expected DEF as key in a Configurations.json files.c             S   s   g | ]}|d kr|qS )EXPECTEDr   ).0namer   r   r   
<listcomp>o   s    zprocess.<locals>.<listcomp>r   ar              The expected layout for an entry in the Configurations.json
            file is a dictonary. Here is an example of the expected format:

            "DEF":
            [
              {"disableClashDetection": ["ipconfigARP_USE_CLASH_DETECTION=0"]},
              {"enableClashDetection": ["ipconfigARP_USE_CLASH_DETECTION=1"]}
            ]
                zThe offending entry is %sZconfig_T)exist_okparentsFz	harness.cz&Could not find a harness in folder %s.z(This folder is not processed do the end!r	   zMakefile.jsonw   )indent)r   ospathjoinKeyErrorLOGGERerrorloggingdebugkeysAttributeError
IndexErrorr   r   pathlibZPathmkdirendswithshutilcopycollectionsOrderedDictopenjsondump)ZfolderfilesZjson_contentZdef_listconfigZ
confignameZ
configbodyeZnew_config_folderZharness_copiedfileZcurrent_configZoutput_filer   r   r   processd   sN    



	



r+   c              C   s0   x*t jdD ]\} }}d|krt| | qW d S )N.zConfigurations.json)r   walkr+   )Zfldr_Zfylesr   r   r   main   s    r/   __main__z#{script}: %(levelname)s %(message)s)script)format)r"   r%   r   r   r   r    r   make_proof_makefilesr   	getLoggerr   r   r+   r/   __name__basicConfigr2   r   basename__file__r   r   r   r   <module>   s   
?6