def dist(opts): config = load_config(opts) reset_conda_env(opts, config) try: # Create a build environment. build_path = os.path.join(opts.work_path, "build") rimraf(build_path) mkdirp(build_path) try: # Copy source. with mark_task(opts, "Copying source"): shell( opts, "cd {root_path} && git archive HEAD --format=tar | tar -x -C {build_path}", root_path=opts.root_path, build_path=build_path, ) # Download deps. download_conda_deps(opts) download_pip_deps(opts, config) # Copy libs. with mark_task(opts, "Copying libs"): shutil.copytree( opts.lib_path, os.path.join( build_path, os.path.relpath(opts.lib_path, opts.root_path)), ) # Compress the build. dist_path = os.path.join(opts.root_path, opts.dist_dir) mkdirp(dist_path) dist_file = os.path.join( "{name}-{version}-{os_name}-amd64.zip".format( name=config.get("name", os.path.basename(opts.root_path)), version=config.get("version", "1.0.0"), os_name=opts.os_name, )) with mark_task(opts, "Creating archive {}".format(dist_file)): dist_file_path = os.path.join(dist_path, dist_file) rimraf(dist_file_path) shell( opts, "cd {build_path} && zip -9 -qq -r {dist_file_path} './'", build_path=build_path, dist_file_path=dist_file_path, ) finally: rimraf(build_path) finally: delete_conda_env(opts)
def dist(opts): config = load_config(opts) reset_conda_env(opts, config) try: # Create a build environment. build_path = os.path.join(opts.work_path, "build") rimraf(build_path) mkdirp(build_path) try: # Copy source. with mark_task(opts, "Copying source"): shell( opts, "cd {root_path} && git archive HEAD --format=tar | tar -x -C {build_path}", root_path=opts.root_path, build_path=build_path, ) # Download deps. download_conda_deps(opts) download_pip_deps(opts, config) # Copy libs. with mark_task(opts, "Copying libs"): shutil.copytree( opts.lib_path, os.path.join(build_path, os.path.relpath(opts.lib_path, opts.root_path)), ) # Compress the build. dist_path = os.path.join(opts.root_path, opts.dist_dir) mkdirp(dist_path) dist_file = os.path.join("{name}-{version}-{os_name}-amd64.zip".format( name=config.get("name", os.path.basename(opts.root_path)), version=config.get("version", "1.0.0"), os_name=opts.os_name, )) with mark_task(opts, "Creating archive {}".format(dist_file)): dist_file_path = os.path.join(dist_path, dist_file) rimraf(dist_file_path) shell( opts, "cd {build_path} && zip -9 -qq -r {dist_file_path} './'", build_path=build_path, dist_file_path=dist_file_path, ) finally: rimraf(build_path) finally: delete_conda_env(opts)
def install_pip_deps(opts, config): deps = get_pip_deps(opts, config) if deps: with mark_task(opts, "Installing {} pip dependencies".format(opts.conda_env)): shell_local( opts, "pip install {args} {{deps}}".format(args=get_pip_args(opts, config)), deps=deps, )
def install_pip_deps_offline(opts, config): deps = glob.glob(os.path.join(opts.pip_lib_path, "*.*")) if deps: with mark_task(opts, "Installing {} pip dependencies".format(opts.conda_env)): shell_local( opts, "pip install --no-index --no-deps {deps}", deps=deps, )
def install_pip_deps_offline(opts, config): deps = glob.glob(os.path.join(opts.pip_lib_path, "*.*")) if deps: with mark_task(opts, "Installing {} pip dependencies".format(opts.conda_env)): shell_local( opts, "pip install {deps}", deps=deps, )
def reset_conda_env_offline(opts, config): delete_conda_env(opts) # Create a new env. with mark_task(opts, "Installing {} conda dependencies".format(opts.conda_env)): deps = glob.glob(os.path.join(opts.conda_lib_path, "*.tar.bz2")) shell( opts, "conda create --offline --yes --name {conda_env} {deps}", conda_env=opts.conda_env, deps=deps, )
def activate(opts): config = load_config(opts) package_name = config.get("name", os.path.basename(opts.root_path)) with mark_task(opts, "Activating {} environment".format(opts.conda_env)): shell_local_exec( opts, apply_styles(opts, """printf "{success}done!{plain} Deactivate environment with {code}exit{plain} or {code}[Ctl+D]{plain}. " && export PS1="(\[{code}\]{{package_name}}\[{plain}\]) \\h:\\W \\u\\$ " && bash"""), package_name=package_name, )
def download_pip_deps(opts, config): rimraf(opts.pip_lib_path) mkdirp(opts.pip_lib_path) deps = get_pip_deps(opts, config) if deps: with mark_task(opts, "Downloading pip dependencies"): shell_local( opts, "pip download {args} --dest {{dest_path}} {{deps}}".format(args=get_pip_args(opts, config)), dest_path=opts.pip_lib_path, deps=deps, )
def activate(opts): config = load_config(opts) package_name = config.get("name", os.path.basename(opts.root_path)) with mark_task(opts, "Activating {} environment".format(opts.conda_env)): shell_local_exec( opts, apply_styles( opts, """printf "{success}done!{plain} Deactivate environment with {code}exit{plain} or {code}[Ctl+D]{plain}. " && export PS1="(\[{code}\]{{package_name}}\[{plain}\]) \\h:\\W \\u\\$ " && bash""" ), package_name=package_name, )
def load_config(opts): with mark_task(opts, "Loading config from {}".format(opts.config_file)): package_json_path = os.path.join(opts.root_path, opts.config_file) if os.path.exists(package_json_path): with open(package_json_path, "rb") as package_json_handle: try: return Config(json.loads(package_json_handle.read().decode("utf-8")), []) except ValueError: raise TaskError("Invalid {} config file.".format(opts.config_file)) else: raise TaskWarning("Missing {} config file.".format(opts.config_file)) # Provide a fallback config. return Config({}, [])
def install(opts): config = load_config(opts) if opts.offline: reset_conda_env_offline(opts, config) install_pip_deps_offline(opts, config) else: reset_conda_env(opts, config) install_pip_deps(opts, config) # Run install scripts. install_scripts = config.get("pysh").get("install", []) if install_scripts: with mark_task(opts, "Running install scripts"): for install_script in install_scripts: shell_local(opts, install_script)
def download_conda_deps(opts): rimraf(opts.conda_lib_path) mkdirp(opts.conda_lib_path) deps = [ dep for dep in shell_local( opts, "conda list --explicit").decode().splitlines() if not dep.startswith("#") and not dep.startswith("@") ] if deps: with mark_task(opts, "Downloading conda dependencies"): for dep in deps: download( dep, os.path.join(opts.conda_lib_path, posixpath.basename(dep)), )
def download_conda_deps(opts): rimraf(opts.conda_lib_path) mkdirp(opts.conda_lib_path) deps = [ dep for dep in shell_local(opts, "conda list --explicit").decode().splitlines() if not dep.startswith("#") and not dep.startswith("@") ] if deps: with mark_task(opts, "Downloading conda dependencies"): for dep in deps: download( dep, os.path.join(opts.conda_lib_path, posixpath.basename(dep)), )
def reset_conda_env(opts, config): delete_conda_env(opts) # Create a new env. with mark_task(opts, "Installing {} conda dependencies".format(opts.conda_env)): python_version = config.get("pysh").get("python").get("version", "3") deps = [ "{}={}".format(*dep) for dep in get_deps(opts, config, "conda") ] shell( opts, "conda create --yes --name {conda_env} python={python_version} {deps}", conda_env=opts.conda_env, python_version=python_version, deps=deps, )
def load_config(opts): with mark_task(opts, "Loading config from {}".format(opts.config_file)): package_json_path = os.path.join(opts.root_path, opts.config_file) if os.path.exists(package_json_path): with open(package_json_path, "rb") as package_json_handle: try: return Config( json.loads(package_json_handle.read().decode("utf-8")), []) except ValueError: raise TaskError("Invalid {} config file.".format( opts.config_file)) else: raise TaskWarning("Missing {} config file.".format( opts.config_file)) # Provide a fallback config. return Config({}, [])
def delete_conda_env(opts): with mark_task(opts, "Cleaning {} environment".format(opts.conda_env)): shell(opts, "conda env remove --yes --name {conda_env}", conda_env=opts.conda_env)