# /ciscripts/deploy/bii/deploy.py
#
# Copy directories into place to prepare for publishing biicode project
#
# See /LICENCE.md for Copyright information
"""Place a symbolic link of pandoc in a writable directory in PATH."""

import errno

import os

import shutil


def _move_directories_ignore_errors(directories, src, dst):
    """Move specified directories from :src: to :dst: ignoring errors."""
    for name in directories:
        try:
            os.rename(os.path.join(src, name),
                      os.path.join(dst, name))
        except OSError as error:
            if error.errno != errno.ENOENT:
                raise error


_BII_LAYOUT = [
    "bii",
    "bin",
    "lib",
    "blocks",
    "build"
]


def _get_bii_container(cont, util, shell):
    """Get pre-installed bii installation."""
    return cont.fetch_and_import("setup/project/configure_bii.py").get(cont,
                                                                       util,
                                                                       shell,
                                                                       None)


def run(cont, util, shell, argv=None):
    """Place a symbolic link of pandoc in a writable directory in PATH."""
    del argv

    cont.fetch_and_import("deploy/project/deploy.py").run(cont,
                                                          util,
                                                          shell)

    with util.Task("""Preparing for deployment to biicode"""):
        if os.environ.get("CI", None):
            build = cont.named_cache_dir("cmake-build", ephemeral=False)
            _move_directories_ignore_errors(_BII_LAYOUT, build, os.getcwd())

            if not util.which("bii"):
                path = util.find_usable_path_in_homedir(cont)
                with _get_bii_container(cont, util, shell).activated(util):
                    bii_binary = util.which("bii")
                    binary_dir = os.path.dirname(bii_binary)
                    biicode_module = os.path.abspath(os.path.join(binary_dir,
                                                                  "..",
                                                                  "biicode"))
                destination = os.path.join(path, "bii")
                with util.Task("""Copying bii binary from """
                               """{0} to {1}.""".format(bii_binary,
                                                        destination)):
                    try:
                        os.makedirs(os.path.dirname(destination))
                    except OSError as error:
                        if error.errno != errno.EEXIST:
                            raise error

                    shutil.copy(bii_binary, destination)
                    shutil.copytree(biicode_module,
                                    os.path.join(path, "biicode"))

@@ -1116,16 +1116,1547 @@
 None)%0A%0A%0A
+def _get_python_container(cont, util, shell):%0A    %22%22%22Get python container pertaining to biicode installation.%22%22%22%0A    configure_python = %22setup/project/configure_python.py%22%0A    py_ver = util.language_version(%22python2%22)%0A    return cont.fetch_and_import(configure_python).get(cont,%0A                                                       util,%0A                                                       shell,%0A                                                       py_ver)%0A%0A%0Adef _set_up_bii_installation(bii_binary_source,%0A                             python_binary_source,%0A                             bii_binary_destination,%0A                             biicode_module_source,%0A                             biicode_module_destination):%0A    %22%22%22Relocate the bii installation.%22%22%22%0A    try:%0A        os.makedirs(os.path.dirname(bii_binary_destination))%0A    except OSError as error:%0A        if error.errno != errno.EEXIST:%0A            raise error%0A%0A    shutil.copy(bii_binary_source, bii_binary_destination)%0A    shutil.copytree(biicode_module_source,%0A                    os.path.join(biicode_module_destination, %22biicode%22))%0A%0A    # Open the bii binary and re-write the shebang to%0A    # container the relative path to the python interpreter%0A    # from where we are now.%0A    with open(bii_binary_destination, %22r+%22) as bii_binary_file:%0A        bii_binary_lines = bii_binary_file.readlines()%0A        bii_binary_lines%5B0%5D = %22#!%7B%7D%22.format(python_binary_source)%0A        bii_binary_file.seek(0)%0A        bii_binary_file.write(%22%5Cn%22.join(bii_binary_lines))%0A%0A%0A
 def run(
@@ -3343,16 +3343,17 @@
    with 
+(
 _get_bii
@@ -3401,59 +3401,302 @@
 til)
-:%0A                    bii_binary = util.which(%22bii%22
+,%0A                      _get_python_container(cont,%0A                                            util,%0A                                            shell).activated(util)):%0A                    bii_binary = util.which(%22bii%22)%0A                    python_binary = os.path.relpath(util.which(%22python%22)
 )%0A  
@@ -4251,36 +4251,68 @@
                 
-try:
+_set_up_bii_installation(bii_binary,
 %0A               
@@ -4324,151 +4324,63 @@
     
-os.makedirs(os.path.dirname(destination))%0A                    except OSError as error:%0A                        if error.errno != errno.EEXIST:%0A
+                     python_binary,%0A                   
     
@@ -4397,38 +4397,36 @@
             
-  raise error%0A
+destination,
 %0A           
@@ -4438,45 +4438,9 @@
     
-shutil.copy(bii_binary, destination)%0A
+ 
     
@@ -4455,32 +4455,20 @@
         
-shutil.copytree(
+    
 biicode_
@@ -4515,35 +4515,19 @@
     
-os.path.join(path, %22biicode%22)
+         path
 )%0A

Put path to python in container in shebang for deploy