Skip to content

Commit

Permalink
Use bin2header to create Micromamba inline scripts (#1601)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonashaag authored Mar 30, 2022
1 parent 81f2ffe commit 5a61ae2
Show file tree
Hide file tree
Showing 17 changed files with 86 additions and 126 deletions.
28 changes: 28 additions & 0 deletions libmamba/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ project(libmamba)

set(LIBMAMBA_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
set(LIBMAMBA_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(LIBMAMBA_DATA_DIR ${CMAKE_CURRENT_SOURCE_DIR}/data)

# Versionning
# ===========
Expand Down Expand Up @@ -70,6 +71,30 @@ endif ()
# Source files
# ============

set(SHELL_SCRIPTS
micromamba.sh
micromamba.bat
activate.bat
_mamba_activate.bat
mamba_hook.bat
mamba_hook.ps1
Mamba.psm1
mamba.xsh
mamba.fish
compile_pyc.py
mamba_completion.posix)

foreach(script ${SHELL_SCRIPTS})
string(REPLACE "." "_" script_var ${script})
add_custom_command(OUTPUT shell_scripts/${script}.cpp
DEPENDS data/${script}
COMMAND python3 ${LIBMAMBA_DATA_DIR}/bin2header.py
--extern
-v data_${script_var}
-i ${CMAKE_CURRENT_SOURCE_DIR}/data/${script}
-o ${CMAKE_CURRENT_BINARY_DIR}/shell_scripts/${script}.cpp)
endforeach()

set(LIBMAMBA_SOURCES
${LIBMAMBA_SOURCE_DIR}/version.cpp
# Core API (low-level)
Expand Down Expand Up @@ -122,6 +147,9 @@ set(LIBMAMBA_SOURCES
${LIBMAMBA_SOURCE_DIR}/api/shell.cpp
${LIBMAMBA_SOURCE_DIR}/api/update.cpp
)
foreach(script ${SHELL_SCRIPTS})
list(APPEND LIBMAMBA_SOURCES shell_scripts/${script}.cpp)
endforeach()

set(LIBMAMBA_HEADERS
${LIBMAMBA_INCLUDE_DIR}/mamba/version.hpp
Expand Down
2 changes: 0 additions & 2 deletions libmamba/data/Mamba.psm1
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
R"MAMBARAW(
## ENVIRONMENT MANAGEMENT ######################################################

<#
Expand Down Expand Up @@ -287,4 +286,3 @@ Export-ModuleMember `

# We don't export TabExpansion as it's currently not implemented for Micromamba
# TabExpansion
)MAMBARAW"
2 changes: 0 additions & 2 deletions libmamba/data/_mamba_activate.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
R"MAMBARAW(
@REM Copyright (C) 2012 Anaconda, Inc
@REM SPDX-License-Identifier: BSD-3-Clause
@REM Helper routine for activation, deactivation, and reactivation.
Expand Down Expand Up @@ -51,4 +50,3 @@ R"MAMBARAW(
@IF "%CONDA_TEST_SAVE_TEMPS%x"=="x" @DEL /F /Q "%_TEMP_SCRIPT_PATH%"
@SET _TEMP_SCRIPT_PATH=
@SET "PROMPT=%CONDA_PROMPT_MODIFIER%%PROMPT%"
)MAMBARAW"
2 changes: 0 additions & 2 deletions libmamba/data/activate.bat
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
R"MAMBARAW(
@REM Copyright (C) 2021 QuantStack
@REM SPDX-License-Identifier: BSD-3-Clause
@CALL "%~dp0..\condabin\mamba_hook.bat"
micromamba activate %*
)MAMBARAW"
73 changes: 26 additions & 47 deletions libmamba/data/bin2header.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,65 +25,44 @@

import argparse
import sys
from pathlib import Path

comment = """
/*
This is a conda-specific repackaged launcher.c from Python setuptools.
The original source code for launcher.c can be found here:

https://raw.githubusercontent.com/python/cpython/3.7/PC/launcher.c
The source code for the conda.exe launcher can be found here:
https://github.com/conda/conda-build/tree/master/conda_build/launcher_sources
In conda-build / launcher_sources
*/
"""


def bin2header(data, var_name="var"):
out = []
out.append(comment)
out.append("\n")
out.append("char {var_name}[] = {{".format(var_name=var_name))
elems = [data[i : i + 12] for i in range(0, len(data), 12)]
for i, x in enumerate(elems):
line = ", ".join(["0x{val:02x}".format(val=c) for c in x])
out.append(
" {line}{end_comma}".format(
line=line, end_comma="," if i < len(elems) - 1 else ""
)
)
out.append("};")
out.append(
"std::size_t {var_name}_len = {data_len};".format(
var_name=var_name, data_len=len(data)
)
)
return "\n".join(out)
def bin2header(comment, data, var_name, extern=False):
yield comment
yield "#include <cstddef>"
if extern:
yield f"extern const char {var_name}[];"
yield f"extern const std::size_t {var_name}_len;"
yield f"const char {var_name}[] = {{"
indent = " "
for i in range(0, len(data), 12):
hex_chunk = ", ".join(f"0x{x:02x}" for x in data[i:][:12])
yield indent + hex_chunk + ","
yield indent + "0x00 // Terminating null byte"
yield "};"
yield f"const std::size_t {var_name}_len = {len(data)};"


def main():
parser = argparse.ArgumentParser(description="Generate binary header output")
parser.add_argument("-i", "--input", required=True, help="Input file")
parser.add_argument("-o", "--out", required=True, help="Output file")
parser.add_argument("-i", "--input", required=True, help="Input file", type=Path)
parser.add_argument("-o", "--out", required=True, help="Output file", type=Path)
parser.add_argument(
"-v", "--var", required=True, help="Variable name to use in file"
)

parser.add_argument(
"-e", "--extern", action="store_true", help="Add 'extern' declaration"
)
args = parser.parse_args()
if not args:
return 1

with open(args.input, "rb") as f:
data = f.read()

out = bin2header(data, args.var)
with open(args.out, "w") as f:
f.write(out)
argv_pretty = " ".join(
Path(arg).name if "/" in arg or "\\" in arg else arg for arg in sys.argv
)
comment = f"/* This file was generated using {argv_pretty} */"

return 0
out = bin2header(comment, args.input.read_bytes(), args.var, args.extern)
args.out.write_text("\n".join(out))


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
R"MAMBARAW(
from compileall import compile_file
from concurrent.futures import ProcessPoolExecutor
import os
import sys
from compileall import compile_file
from concurrent.futures import ProcessPoolExecutor


def main():
max_workers = int(os.environ.get("MAMBA_EXTRACT_THREADS", "0"))
Expand All @@ -20,8 +20,7 @@ def main():
success = all(r.result() for r in results)
return success


if __name__ == "__main__":
success = main()
sys.exit(int(not success))
)MAMBARAW"
4 changes: 0 additions & 4 deletions libmamba/data/mamba.fish
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
R"MAMBARAW(
if not set -q MAMBA_SHLVL
set -gx MAMBA_SHLVL "0"
set -gx PATH $MAMBA_ROOT_PREFIX/condabin $PATH
Expand Down Expand Up @@ -169,5 +167,3 @@ complete -x -c micromamba -n '__fish_mamba_using_command remove' -a '(__fish_mam
complete -x -c micromamba -n '__fish_mamba_using_command uninstall' -a '(__fish_mamba_packages)'
complete -x -c micromamba -n '__fish_mamba_using_command upgrade' -a '(__fish_mamba_packages)'
complete -x -c micromamba -n '__fish_mamba_using_command update' -a '(__fish_mamba_packages)'
)MAMBARAW"
3 changes: 0 additions & 3 deletions libmamba/data/mamba.xsh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
R"MAMBARAW(
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
# Much of this forked from https://github.com/gforsyth/xonda
Expand Down Expand Up @@ -111,5 +110,3 @@ def _mamba_completer(prefix, line, start, end, ctx):
__xonsh__.completers['mamba'] = _mamba_completer
# bump to top of list
__xonsh__.completers.move_to_end('mamba', last=False)

)MAMBARAW"
2 changes: 0 additions & 2 deletions libmamba/data/mamba_completion.posix
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
R"MAMBARAW(
if [ -n "${ZSH_VERSION:+x}" ]; then
autoload -U +X compinit && compinit
autoload -U +X bashcompinit && bashcompinit
Expand All @@ -17,4 +16,3 @@ if [ -n "${BASH_VERSION:+x}" ]; then
}
complete -o default -F _umamba_bash_completions micromamba
fi
)MAMBARAW"
2 changes: 0 additions & 2 deletions libmamba/data/mamba_hook.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
R"MAMBARAW(
@REM Copyright (C) 2021 QuantStack
@REM SPDX-License-Identifier: BSD-3-Clause
@REM This file is derived from conda_hook.bat
Expand All @@ -17,4 +16,3 @@ __MAMBA_INSERT_MAMBA_EXE__
@DOSKEY micromamba="%MAMBA_BAT%" $*

@SET CONDA_SHLVL=0
)MAMBARAW"
2 changes: 0 additions & 2 deletions libmamba/data/mamba_hook.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
R"MAMBARAW(
Import-Module "$Env:MAMBA_ROOT_PREFIX\condabin\Mamba.psm1"
)MAMBARAW"
2 changes: 0 additions & 2 deletions libmamba/data/micromamba.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
R"MAMBARAW(
@REM Copyright (C) 2012 Anaconda, Inc
@REM SPDX-License-Identifier: BSD-3-Clause

Expand Down Expand Up @@ -29,4 +28,3 @@ __MAMBA_INSERT_ROOT_PREFIX__
@IF [%1]==[uninstall] "%~dp0_mamba_activate" reactivate

@EXIT /B %errorlevel%
)MAMBARAW"
2 changes: 0 additions & 2 deletions libmamba/data/micromamba.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
R"MAMBARAW(
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

Expand Down Expand Up @@ -111,4 +110,3 @@ if [ -z "${CONDA_SHLVL+x}" ]; then
PS1=
fi
fi
)MAMBARAW"
11 changes: 11 additions & 0 deletions libmamba/include/mamba/core/shell_init.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@

#include "mamba_fs.hpp"

extern const char data_micromamba_sh[];
extern const char data_micromamba_bat[];
extern const char data_activate_bat[];
extern const char data__mamba_activate_bat[];
extern const char data_mamba_hook_bat[];
extern const char data_mamba_hook_ps1[];
extern const char data_Mamba_psm1[];
extern const char data_mamba_xsh[];
extern const char data_mamba_fish[];
extern const char data_mamba_completion_posix[];

namespace mamba
{
std::string guess_shell();
Expand Down
8 changes: 2 additions & 6 deletions libmamba/src/core/activation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ namespace mamba
fs::path PREFIX_STATE_FILE = fs::path("conda-meta") / "state";
fs::path PACKAGE_ENV_VARS_DIR = fs::path("etc") / "conda" / "env_vars.d";
std::string CONDA_ENV_VARS_UNSET_VAR = "***unset***"; // NOLINT(runtime/string)

constexpr const char mamba_posix_completion[] =
#include "../data/mamba_completion.posix"
;
} // namespace
} // namespace

/****************************
* Activator implementation *
Expand Down Expand Up @@ -705,7 +701,7 @@ namespace mamba
{
if (shell() == "posix")
{
builder << mamba_posix_completion;
builder << data_mamba_completion_posix;
}
}
if (Context::instance().auto_activate_base)
Expand Down
Loading

0 comments on commit 5a61ae2

Please sign in to comment.