#!/usr/bin/env python
import os
from pathlib import Path
import subprocess
from matador.session import Session
from .deployment import DeploymentCommand, substitute_keywords
from matador.commands.run_sql_script import run_sql_script


def _checkout_script(path, commit):
    repo_folder = Session.matador_repository_folder
    scriptPath = Path(repo_folder, path)
    targetScript = Path(
        Session.deployment_folder, scriptPath.name)

    subprocess.run(
        ['git', '-C', str(repo_folder), 'checkout', commit],
        stderr=subprocess.STDOUT,
        stdout=open(os.devnull, 'w'),
        check=True)

    originalFile = open(scriptPath, 'r')
    originalText = originalFile.read()
    newText = substitute_keywords(
        originalText, repo_folder, commit)
    originalFile.close()

    newFile = open(targetScript, 'w')
    newFile.write(newText)
    newFile.close()

    return targetScript


class DeploySqlScript(DeploymentCommand):

    def _execute(self):
        path = self.args[0]

        if len(os.path.dirname(path)) == 0:
            targetScript = os.path.join(Session.deployment_folder, path)
        else:
            commit = self.args[1]
            targetScript = _checkout_script(path, commit)

        run_sql_script(self._logger, targetScript)


class DeployOraclePackage(DeploymentCommand):

    def _execute(self):
        packageName = self.args[0]
        commit = self.args[1]

        repo_folder = Session.matador_repository_folder
        package = os.path.join(
            repo_folder, 'src', 'db_objects', 'packages', packageName,
            packageName)
        packageSpec = package + '.pks'
        packageBody = package + '.pkb'

        packageSpecScript = _checkout_script(packageSpec, commit)
        packageBodyScript = _checkout_script(packageBody, commit)

        run_sql_script(self._logger, packageSpecScript)
        run_sql_script(self._logger, packageBodyScript)

#!/usr/bin/env python
from pathlib import Path
from matador.session import Session
from .deployment import DeploymentCommand
from matador.commands.run_sql_script import run_sql_script
from matador import git


def _fetch_script(repo, script_path, commit_ref, target_folder):
    script = Path(repo.path, script_path)
    target_script = Path(target_folder, script.name)

    git.checkout(repo, commit_ref)

    with script.open('r') as f:
        original_text = f.read_text()
        f.close()

    new_text = git.substitute_keywords(original_text, repo, commit_ref)

    with target_script.open('w') as f:
        f.write(new_text)
        f.close()

    return target_script


class DeploySqlScript(DeploymentCommand):

    def _execute(self):
        path = Path(self.args[0])

        if path.parent.samefile(Path('.')):
            target_script = Path(Session.deployment_folder, path)
        else:
            commit = self.args[1]
            target_script = _fetch_script(
                Session.matador_repo, path, commit, Session.deployment_folder)

        run_sql_script(self._logger, target_script)


class DeployOraclePackage(DeploymentCommand):

    def _execute(self):
        package_name = self.args[0]
        commit = self.args[1]

        repo_folder = Session.matador_repository_folder
        package_folder = Path(
            repo_folder, 'src', 'db_objects', 'packages', package_name)
        package_spec = Path(package_folder, package_name + '.pks')
        package_body = Path(package_folder, package_name + '.pkb')

        spec_script = _fetch_script(
            Session.matador_repo, package_spec, commit,
            Session.deployment_folder)
        body_script = _fetch_script(
            Session.matador_repo, package_body, commit,
            Session.deployment_folder)

        run_sql_script(self._logger, spec_script)
        run_sql_script(self._logger, body_script)

Remove git subprocess calls from deploy sql script