# License AGPLv3 (http://www.gnu.org/licenses/agpl-3.0-standalone.html)
# Copyright (c) 2016-2018 ACSONE SA/NV
import os
from os.path import join as opj
import subprocess
import sys
import traceback

import click

from .oca_projects import (
    get_repositories_and_branches, temporary_clone, MAIN_BRANCHES,
)
from .dist_to_simple_index import dist_to_simple_index


@click.command()
@click.option('--target', required=True,
              type=click.Path(dir_okay=True, file_okay=False, exists=True),
              help="Root of a PEP 503 directory structure.")
@click.option('--repo',
              help="Repo to act on (default all OCA addons repos).")
@click.option('--branch',
              help="Branch to act on (default all branches from 8.0).")
@click.option('--push/--no-push',
              help="Git commit and push changes made.")
def main(target, repo, branch, push):
    """ OCA Git Bot for main addon branches

    \b
    - generate README.rst from readme/*.rst fragments
    - generate default setup.py
    - generate sdists and wheels to a PEP 503 directory

    Branches before 8.0 are ignored.
    """
    exit_code = 0
    target = os.path.abspath(target)
    repos = ()
    if repo:
        repos = (repo, )
    branches = MAIN_BRANCHES
    if branch:
        branches = (branch, )
    for repo, branch in get_repositories_and_branches(repos, branches):
        if branch in ('6.1', '7.0'):
            continue
        with temporary_clone(repo, branch):
            try:
                # generate README.rst
                sys.stderr.write(
                    "============> oca-gen-addon-readme in %s@%s\n" %
                    (repo, branch),
                )
                gen_addon_readme_cmd = [
                    'oca-gen-addon-readme',
                    '--repo-name', repo,
                    '--branch', branch,
                    '--addons-dir', '.',
                ]
                if push:
                    gen_addon_readme_cmd.append('--commit')
                subprocess.check_call(gen_addon_readme_cmd)
                # generate default setup.py
                sys.stderr.write(
                    "============> setuptools-odoo-make-default in %s@%s\n" %
                    (repo, branch),
                )
                make_default_setup_cmd = [
                    'setuptools-odoo-make-default',
                    '--addons-dir', '.',
                    '--metapackage', 'oca-' + repo,
                    '--clean',
                ]
                if push:
                    make_default_setup_cmd.append('--commit')
                subprocess.check_call(make_default_setup_cmd)
                # push changes to git, if any
                if push:
                    sys.stderr.write(
                        "============> git push in %s@%s\n" %
                        (repo, branch),
                    )
                    subprocess.check_call([
                        'git', 'push', 'origin', branch,
                    ])
                # make dists and wheels for each installable addon
                # and _metapackage
                sys.stderr.write(
                    "============> dist_to_simple_index in %s@%s\n" %
                    (repo, branch),
                )
                setup_dirs = [opj('setup', d) for d in os.listdir('setup')]
                dist_to_simple_index(target, setup_dirs)
            except Exception:
                exit_code = 1
                sys.stderr.write(
                    "/!\ exception in %s@%s\n" %
                    (repo, branch),
                )
                traceback.print_exc()
    sys.exit(exit_code)

@@ -361,16 +361,250 @@
 index%0A%0A%0A
+def _get_python(branch, python2):%0A    version = tuple(map(int, branch.split('.')))%0A    if version %3C (11, 0):%0A        return python2%0A    elif sys.version_info%5B0%5D == 3:%0A        return sys.executable%0A    else:%0A        return 'python3'%0A%0A%0A
 @click.c
@@ -1071,16 +1071,183 @@
 made.%22)%0A
+@click.option('--python2', default='python2',%0A              help=%22Python 2 executable to use when building python 2 %22%0A                   %22wheels for branches %3C 11.0%22)%0A
 def main
@@ -1273,16 +1273,25 @@
 ch, push
+, python2
 ):%0A    %22
@@ -3794,26 +3794,184 @@
 dex(
-target, setup_dirs
+%0A                    target, setup_dirs,%0A                    # use the right python to generate wheels%0A                    python=_get_python(branch, python2),%0A                
 )%0A  

make sure we build wheels with the correct python version