import click
import os
import popper.utils as pu
import popper.scm as scm
from popper.cli import pass_context

ci_files = {
    'travis': {
        './.travis.yml': """
---
dist: xenial
language: python
python: 3.7
services: docker
install:
- git clone --recursive https://github.com/systemslab/popper /tmp/popper
- export PATH=$PATH:/tmp/popper/cli/bin
- export PYTHONUNBUFFERED=1
script: popper run --recursive
"""
    },
    'circle': {
        './.circleci/config.yml': """
---
version: 2
jobs:
  build:
    machine: true
    steps:
    - checkout
    - run:
        command: |
        git clone --recursive https://github.com/systemslab/popper /tmp/popper
        export PATH=$PATH:/tmp/popper/cli/bin
        export PYTHONUNBUFFERED=1
        popper run
"""
    },
    'jenkins': {
        './Jenkinsfile': """
---
stage ('Popper') {{ node {{
  sh "git clone --recursive https://github.com/systemslab/popper /tmp/popper"
  sh "export PATH=$PATH:/tmp/popper/cli/bin"
  sh "export PYTHONUNBUFFERED=1"
  sh "popper run --recursive
}}}}
"""
    },
    'gitlab': {

        '.gitlab-ci.yml': """
---
image: docker:stable

variables:
  DOCKER_DRIVER: overlay

services:
- docker:dind

before_script:
- docker info
- apk update
- apk upgrade
- apk add python python-dev py-pip build-base git bash
- pip install virtualenv
- git clone --recursive https://github.com/systemslab/popper /tmp/popper
- export PATH=$PATH:/tmp/popper/cli/bin

popper:
  script: popper run --recursive
"""
    }
}


@click.command('ci', short_help='Generate CI service configuration files.')
@click.option(
    '--service',
    help='Name of CI service for which config files get generated.',
    type=click.Choice(['travis', 'circle', 'jenkins', 'gitlab']),
    required=True
)
@pass_context
def cli(ctx, service):
    """Generates configuration files for distinct CI services.
    """
    project_root = scm.get_root_folder()

    if service not in ci_files:
        pu.fail("Unrecognized service " + service)

    for ci_file, ci_file_content in pu.get_items(ci_files[service]):
        ci_file_content = ci_file_content
        ci_file = os.path.join(project_root, ci_file)
        # create parent folder
        if not os.path.isdir(os.path.dirname(ci_file)):
            os.makedirs(os.path.dirname(ci_file))

        # write content
        with open(ci_file, 'w') as f:
            f.write(ci_file_content)

    pu.info('Wrote CI configuration file successfully\n')

@@ -242,36 +242,24 @@
 - git clone 
---recursive 
 https://gith
@@ -286,60 +286,8 @@
 pper
- /tmp/popper%0A- export PATH=$PATH:/tmp/popper/cli/bin
 %0A- e
@@ -311,16 +311,41 @@
 FERED=1%0A
+- pip install popper/cli%0A
 script: 
@@ -550,36 +550,24 @@
   git clone 
---recursive 
 https://gith
@@ -594,66 +594,8 @@
 pper
- /tmp/popper%0A        export PATH=$PATH:/tmp/popper/cli/bin
 %0A   
@@ -625,16 +625,47 @@
 FERED=1%0A
+        pip install popper/cli%0A
         
@@ -776,36 +776,24 @@
  %22git clone 
---recursive 
 https://gith
@@ -820,65 +820,8 @@
 pper
- /tmp/popper%22%0A  sh %22export PATH=$PATH:/tmp/popper/cli/bin
 %22%0A  
@@ -851,16 +851,46 @@
 ERED=1%22%0A
+  sh %22pip install popper/cli%22%0A
   sh %22po
@@ -1213,20 +1213,8 @@
 one 
---recursive 
 http
@@ -1249,46 +1249,23 @@
 pper
- /tmp/popper%0A- export PATH=$PATH:/tmp/
+%0A- pip install 
 popp
@@ -1274,12 +1274,8 @@
 /cli
-/bin
 %0A%0Apo

modify 'popper ci' install scripts so they use pip (#584)