U
    ee                     @   s   d Z ddlZddlZddlZddl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mZmZ dd	d
Zdd Zdd Zedkre  dS )a  
Continuous Integration helper script.
Automatically detects recipes modified in a changeset (compares with master)
and recompiles them.

To run locally, set the environment variables before running:
```
ANDROID_SDK_HOME=~/.buildozer/android/platform/android-sdk-20
ANDROID_NDK_HOME=~/.buildozer/android/platform/android-ndk-r9c
./ci/rebuild_update_recipes.py
```

Current limitations:
- will fail on conflicting requirements
  e.g. https://travis-ci.org/AndreMiras/python-for-android/builds/438840800
  the list of recipes was huge and result was:
  [ERROR]:   Didn't find any valid dependency graphs.
  [ERROR]:   This means that some of your requirements pull in conflicting dependencies.
- only rebuilds on sdl2 bootstrap
    N)Context)logger)current_directory)Recipe)TargetPythonCORE_RECIPESBROKEN_RECIPESorigin/developc                 C   sD   t jjd| }t }|D ]$}d|kr|dd }|| q|S )z`
    Returns a set of modified recipes between the current branch and the one
    in param.
    z--name-onlyzpythonforandroid/recipes//   )shcontribgitZdiffsetsplitadd)branchZgit_diffrecipes	file_pathZrecipe r   >/tmp/pip-unpacked-wheel-h4dze4ss/ci/rebuild_updated_recipes.pymodified_recipes"   s    r   c              
   C   s   |sdS t jd }t jd }|| j d|}td| tdD t	j
ddd	|d
|d|fdd |D dddD ]}t| q|W 5 Q R X dS )zH
    Builds an APK given a target Python and a set of requirements.
    NZANDROID_SDK_HOMEZANDROID_NDK_HOME,zrequirements: {}ztestapps/on_device_unit_tests/zsetup.pyZapkz	--sdk-dirz	--ndk-dirz--requirementsc                 S   s   g | ]}d | qS )z--arch=r   ).0archr   r   r   
<listcomp>D   s     zbuild.<locals>.<listcomp>T)Z_err_to_outZ_iter)osenvironr   namejoinr   infoformatr   r   pythonprint)target_pythonrequirementsZarchsZandroid_sdk_homeZandroid_ndk_homeliner   r   r   build3   s0    



      r'   c               	   C   s   t d} | jdddg d | tjdd  \}}td|j  t	j
}t }td| |t8 }td	| t }| D ]D}zt|| W q tk
r   || td
| Y qX qt| }||8 }td| t|||j d S )NZrebuild_updated_recipesz--archz#The archs to build for during testsappend)helpactiondefault   z2Building updated recipes for the following archs: zrecipes modified: {}zrecipes to build: {}z'removed {} from recipes because deletedz recipes to build (no broken): {})argparseArgumentParseradd_argumentparse_known_argssysargvr   r    r   r   python3r   r!   r   r   copyr   Z
get_recipe
ValueErrorremovewarningr   r'   )parserargsunknownr$   r   contextZrecipe_nameZbroken_recipesr   r   r   mainI   s6    

r<   __main__)r	   )__doc__r   r   r1   r-   Zpythonforandroid.buildr   Zpythonforandroidr   Zpythonforandroid.toolchainr   Zpythonforandroid.reciper   Zci.constantsr   r   r   r   r'   r<   __name__r   r   r   r   <module>   s   
%