Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
EOASkripts/scripts/init.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
150 lines (131 sloc)
3.78 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from utils.settings import BASE_DIR, load_config, create_docker_env_file | |
from utils.functions import exec_in_container, run | |
from stop import stop | |
from pathlib import Path | |
import shlex | |
import shutil | |
import pwd | |
import os | |
import subprocess | |
from time import sleep | |
BASE_DIR = Path( __file__ ).parent.parent | |
def create_dir(dir): | |
print( "creating dir '{}'".format( dir ) ) | |
Path(dir).mkdir( | |
parents = True, | |
exist_ok = True | |
) | |
def copy_dir(src, dst): | |
print( "'{}' -> '{}'".format( src, dst ) ) | |
if Path(dst).exists(): | |
shutil.rmtree( | |
dst | |
) | |
shutil.copytree( | |
src=src, | |
dst=dst | |
) | |
def create_dirs( config ): | |
create_dir( | |
BASE_DIR / config['DEPENDENCIES_DIR'] | |
) | |
create_dir( | |
BASE_DIR / config['HOME_DIR'] | |
) | |
create_dir( | |
BASE_DIR / config['INPUT_DIR'] | |
) | |
create_dir( | |
BASE_DIR / config['OUTPUT_DIR'] | |
) | |
in_link = Path(BASE_DIR / config['HOME_DIR'] / 'input' ) | |
if not(in_link.is_symlink()) and not(in_link.exists()): | |
in_link . symlink_to( | |
config['INPUT_DIR_IN_CONTAINER'], | |
target_is_directory = True, | |
) | |
out_link = Path(BASE_DIR / config['HOME_DIR'] / 'output' ) | |
if not(out_link.is_symlink()) and not(out_link.exists()): | |
out_link . symlink_to( | |
config['OUTPUT_DIR_IN_CONTAINER'], | |
target_is_directory = True, | |
) | |
def install_git_dep( | |
repo_name, | |
repo_uri, | |
repo_hash, | |
force = False, | |
init_script = None, | |
): | |
DEP_DIR = BASE_DIR / config['DEPENDENCIES_DIR'] | |
# remove repo, if necessary: | |
if force: | |
if (DEP_DIR / repo_name).exists(): | |
shutil.rmtree( DEP_DIR / repo_name ) | |
if not((DEP_DIR / repo_name).exists()): | |
subprocess.check_call( | |
["git", "clone", repo_uri, DEP_DIR / repo_name] | |
) | |
subprocess.check_call( | |
["git", "checkout", repo_hash], | |
cwd = DEP_DIR / repo_name | |
) | |
if init_script is not None: | |
subprocess.check_call( | |
shlex.split( init_script ), | |
cwd = DEP_DIR / repo_name | |
) | |
if __name__ == '__main__': | |
from argparse import ArgumentParser | |
parser = ArgumentParser( | |
description="initialize the repository: download git deps, create directories, etc" | |
) | |
parser.add_argument( | |
"--force-deps", | |
action = "store_true" | |
) | |
parser.add_argument( | |
"--build", | |
action = "store_true" | |
) | |
args = parser.parse_args() | |
# orig_config_file -> env_file: | |
create_docker_env_file() | |
# load env_file: | |
config = load_config() | |
create_dirs( config ) | |
## install git dependencies: | |
install_git_dep( | |
repo_name = "TEI", | |
repo_uri = "https://github.com/TEIC/TEI.git", | |
repo_hash = "7fbbc7c5c2ba533dc70f561b27101dbfa7f519a0", | |
force = args.force_deps, | |
) | |
install_git_dep( | |
repo_name = "Stylesheets", | |
repo_uri = "https://github.com/TEIC/stylesheets.git", | |
repo_hash = "2a01be46ee82fce5eba6074359b3d18db2222e0c", | |
force = args.force_deps, | |
) | |
install_git_dep( | |
repo_name = "eoa-publication-model", | |
repo_uri = "https://github.molgen.mpg.de/EditionOpenAccess/eoa-publication-model.git", | |
repo_hash = "ea8a2eb99b22703f1ef92544352701f07c08d17e", | |
force = args.force_deps, | |
) | |
# rebuild docker image: | |
if args.build: | |
run( | |
env = config, | |
build = True, | |
) | |
stop( | |
env = config, | |
) | |
# copy example publication to $INPUT_DIR: | |
copy_dir( | |
BASE_DIR / config['DEPENDENCIES_DIR'] / 'eoa-publication-model/examples', | |
BASE_DIR / config['INPUT_DIR'] / 'example' | |
) | |
print( "done." ) |