# -*- coding: utf-8 -*-

# Copyright 2019 Novo Nordisk Foundation Center for Biosustainability,
# Technical University of Denmark.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Update the mock repo that is used for the memote CLI and integration tests.

Dependencies are assumed to be satisfied by having memote installed in the
same project.

"""

from __future__ import absolute_import

import logging
import os
from os.path import join
from shutil import rmtree
from subprocess import check_output, call
from tempfile import mkdtemp
import tarfile

logging.basicConfig(level=logging.INFO)
LOGGER = logging.getLogger()

def update_mock_repo():
    """
    Clone and gzip the memote-mock-repo used for CLI and integration tests.

    The repo is hosted at
    'https://github.com/ChristianLieven/memote-mock-repo.git' and maintained
    separately from

    """
    target_file = os.path.abspath(
        join("tests", "data", "memote-mock-repo.tar.gz")
    )
    temp_dir = mkdtemp(prefix='tmp_mock')
    previous_wd = os.getcwd()
    try:
        LOGGER.info("Cloning repository.")
        os.chdir(temp_dir)
        check_output(
            ['git', 'clone',
             'https://github.com/ChristianLieven/memote-mock-repo.git']
        )
        os.chdir('memote-mock-repo/')
        LOGGER.info("Setting git to ignore filemode changes.")
        call(
            ['git', 'config',
             'core.fileMode', 'false']
        )
        call(
            ['git', 'config',
             'user.email', 'memote@opencobra.com']
        )
        call(
            ['git', 'config',
             'user.name', 'memote-bot']
        )
    finally:
        LOGGER.info("Compressing to tarball.")
        tar = tarfile.open(target_file, "w:gz")
        tar.add(
            join(temp_dir, 'memote-mock-repo/'),
            arcname="memote-mock-repo"
        )
        tar.close()
        LOGGER.info("Success!")
        LOGGER.info("Removing temporary directory.")
        rmtree(temp_dir)
        LOGGER.info("Success! The mock repo has been updated.")
        os.chdir(previous_wd)


if __name__ == "__main__":
    update_mock_repo()

@@ -912,16 +912,31 @@
 port os%0A
+import tarfile%0A
 from os.
@@ -982,50 +982,8 @@
 ree%0A
-from subprocess import check_output, call%0A
 from
@@ -1011,63 +1011,20 @@
 emp%0A
+%0A
 import 
-tarfile%0A%0Alogging.basicConfig(level=logging.INFO)
+git%0A
 %0ALOG
@@ -1050,16 +1050,17 @@
 gger()%0A%0A
+%0A
 def upda
@@ -1072,16 +1072,24 @@
 ck_repo(
+temp_dir
 ):%0A    %22
@@ -1406,218 +1406,133 @@
     
-temp
+repo
 _dir = 
-mkdtemp(prefix='tmp_mock')%0A    previous_wd = os.getcwd()%0A    try:%0A        LOGGER.info(%22Cloning repository.%22)%0A        os.chdir(temp_dir)%0A        check_output(%0A            %5B'git', 'clone',%0A    
+join(temp_dir, 'memote-mock-repo')%0A    LOGGER.info(%22Cloning repository.%22)%0A    repo = git.Repo.clone_from(%0A
         
  'ht
@@ -1527,17 +1527,16 @@
         
- 
 'https:/
@@ -1584,17 +1584,17 @@
 epo.git'
-%5D
+,
 %0A       
@@ -1598,52 +1598,23 @@
     
-)%0A        os.chdir('memote-mock-repo/')
+repo_dir
 %0A    
+)%0A
     
@@ -1676,75 +1676,87 @@
     
-    call(%0A            %5B'git', 'config',%0A             'core.
+with repo.config_writer() as writer:%0A        writer.set_value(%22core%22, %22
 fileMode
 ', '
@@ -1755,246 +1755,164 @@
 Mode
-', '
+%22, %22
 false
-'%5D%0A        )%0A        call(%0A            %5B'git', 'config',%0A             'user.email', 'memote@opencobra.com'%5D%0A        )%0A        call(%0A            %5B'git', 'config',%0A             'user.name', 'memote-bot'%5D%0A        )%0A    finally:%0A    
+%22).release()%0A        writer.set_value(%22user%22, %22name%22, %22memote-bot%22).release()%0A        writer.set_value(%22user%22, %22email%22, %22bot@memote.io%22).release()%0A
     
@@ -1958,17 +1958,12 @@
     
-    tar =
+with
  tar
@@ -1988,24 +1988,36 @@
 ile, %22w:gz%22)
+ as archive:
 %0A        tar
@@ -2013,19 +2013,23 @@
         
-t
 ar
+chive
 .add(%0A  
@@ -2042,43 +2042,16 @@
     
-join(temp_dir, 'memote-mock-repo/')
+repo_dir
 ,%0A  
@@ -2105,123 +2105,272 @@
     
-    tar.close()%0A        LOGGER.info(%22Success!%22)%0A        LOGGER.info(%22Removing temporary directory.%22)%0A        rmtree
+LOGGER.info(%22Success! The mock repo has been updated.%22)%0A%0A%0Aif __name__ == %22__main__%22:%0A    logging.basicConfig(%0A        level=logging.INFO,%0A        format=%22%5B%25(levelname)s%5D %25(message)s%22%0A    )%0A    temp_dir = mkdtemp(prefix='tmp_mock')%0A    try:%0A        update_mock_repo
 (tem
@@ -2376,16 +2376,29 @@
 mp_dir)%0A
+    finally:%0A
         
@@ -2414,129 +2414,61 @@
 fo(%22
-Success! The mock repo has been updated.%22)%0A        os.chdir(previous_wd)%0A%0A%0Aif __name__ == %22__main__%22:%0A    update_mock_repo(
+Removing temporary directory.%22)%0A        rmtree(temp_dir
 )%0A

use gitpython to update mock repo