Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for script_env_variables #718

Merged
merged 23 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
afbb55a
Added support for `extra_env_variables`
jlstevens Sep 13, 2023
c12400d
Fixed flakes
jlstevens Sep 13, 2023
509c470
Fixed flake
jlstevens Sep 13, 2023
0707af8
Added support for `extra_env_variables` on Windows
jlstevens Sep 18, 2023
b95078f
Added support for `extra_env_variables` to .pkg installers
jlstevens Sep 19, 2023
c7a2965
Applied suggestions from code review
jlstevens Sep 25, 2023
03f4ef8
Declared extra_env_variables to be a dictionary
jlstevens Sep 25, 2023
7d43220
Renamed extra_env_variables to script_env_variables
jlstevens Sep 25, 2023
b431f51
Implemented single quoting on Unix
jlstevens Sep 25, 2023
42cb0e8
Updated documentation in CONSTRUCT.md
jlstevens Sep 25, 2023
611663b
Updated scripts example with more challenging test (Unix)
jlstevens Sep 25, 2023
ab47c41
Updated the test string to include escaped single quotes
jlstevens Sep 25, 2023
a0e4b51
Added more challenging test string for Windows
jlstevens Sep 25, 2023
057db61
Updated pre_install.bat
jlstevens Sep 25, 2023
e9c20d5
Updated test strings on Unix
jlstevens Sep 25, 2023
a34898d
Updated docs using make_docs.py
jlstevens Sep 26, 2023
e8f89ad
Fixed flakes
jlstevens Sep 26, 2023
709048c
More flake fixes
jlstevens Sep 26, 2023
5d5ebbf
Split up long string for flakes fix
jlstevens Sep 26, 2023
c0233b8
Disabling shellcheck warning regarding quoting
jlstevens Sep 26, 2023
d53155a
Added dashes and underscore to test string
jlstevens Sep 27, 2023
77fa220
Added news enhancement item
jlstevens Sep 27, 2023
aebede3
Flake fix
jlstevens Sep 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CONSTRUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,18 @@ is compulsory and the option to disable it will not be offered.

This option has no effect on `SH` installers.


### `extra_env_variables`

_required:_ no<br/>
_type:_ list<br/>

List of additional environment variables to be made available to the
pre_install and post_install scripts, in the form of VAR=VALUE
pairs. These environment variables are in addition to those in the
`post_install` section above and take precedence in the case of name
collisions. Unix only.

### `pre_uninstall`

_required:_ no<br/>
Expand Down
8 changes: 8 additions & 0 deletions constructor/construct.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,14 @@
('uninstall_name', False, str, '''
Application name in the Windows "Programs and Features" control panel.
Defaults to `${NAME} ${VERSION} (Python ${PYVERSION} ${ARCH})`.
'''),

('extra_env_variables', False, list, '''
List of additional environment variables to be made available to the
pre_install and post_install scripts, in the form of VAR=VALUE
pairs. These environment variables are in addition to those in the
`post_install` section above and take precedence in the case of name
collisions. Unix only.
'''),

('pre_install', False, str, '''
Expand Down
2 changes: 2 additions & 0 deletions constructor/header.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ fi

# Export variables to make installer metadata available to pre/post install scripts
# NOTE: If more vars are added, make sure to update the examples/scripts tests too

_EXTRA_ENV_VARIABLES_='' # Templated extra environment variable(s)
export INSTALLER_NAME='__NAME__'
export INSTALLER_VER='__VERSION__'
export INSTALLER_PLAT='__PLAT__'
Expand Down
1 change: 1 addition & 0 deletions constructor/nsis/main.nsi.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,7 @@ Section "Install"
File /nonfatal /r __INDEX_CACHE__
File /r __REPODATA_RECORD__

@EXTRA_ENV_VARIABLES@
System::Call 'kernel32::SetEnvironmentVariable(t,t)i("CONDA_SAFETY_CHECKS", "disabled").r0'
System::Call 'kernel32::SetEnvironmentVariable(t,t)i("CONDA_EXTRA_SAFETY_CHECKS", "no").r0'
System::Call 'kernel32::SetEnvironmentVariable(t,t)i("CONDA_PKGS_DIRS", "$INSTDIR\pkgs")".r0'
Expand Down
1 change: 1 addition & 0 deletions constructor/osx/run_user_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export INSTALLER_VER="__VERSION__"
export INSTALLER_PLAT="__PLAT__"
export INSTALLER_TYPE="PKG"
export PRE_OR_POST="__PRE_OR_POST__"
_EXTRA_ENV_VARIABLES_='' # Templated extra environment variable(s)

# Run user-provided script
if [ -f "$PREFIX/pkgs/user_${PRE_OR_POST}" ]; then
Expand Down
4 changes: 4 additions & 0 deletions constructor/osxpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,12 @@ def move_script(src, dst, info, ensure_shebang=False, user_script_type=None):
'CONSTRUCTOR_VERSION': info['CONSTRUCTOR_VERSION'],
}
data = preprocess(data, ppd)
custom_variables = info.get('extra_env_variables', [])
jlstevens marked this conversation as resolved.
Show resolved Hide resolved
data = fill_template(data, replace)

data = data.replace("_EXTRA_ENV_VARIABLES_=''", '\n'.join([f'export {var}' for var in custom_variables]))
jlstevens marked this conversation as resolved.
Show resolved Hide resolved


with open(dst, 'w') as fo:
if (
ensure_shebang
Expand Down
3 changes: 3 additions & 0 deletions constructor/shar.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ def get_header(conda_exec, tarball, info):

data = read_header_template()
data = preprocess(data, ppd)
custom_variables = info.get('extra_env_variables', [])
jlstevens marked this conversation as resolved.
Show resolved Hide resolved
data = fill_template(data, replace)

data = data.replace("_EXTRA_ENV_VARIABLES_=''", '\n'.join(
[f'export {var}' for var in custom_variables]))
jlstevens marked this conversation as resolved.
Show resolved Hide resolved
return data


Expand Down
18 changes: 18 additions & 0 deletions constructor/winexe.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ def insert_tempfiles_commands(paths: os.PathLike) -> List[str]:
lines.append(f"File {path}")
return lines

def setup_extra_env_variables(info) -> List[str]:
"""Helper function to insert extra environment variables into nsis template.

Args:
info: Dictionary of information parsed from construct.yaml

Returns:
List[str]: Commands to be inserted into nsi template
"""
lines = []
for variable in info.get('extra_env_variables',[]):
split = variable.split('=')
if len(split)==2:
(name, val) = split
jlstevens marked this conversation as resolved.
Show resolved Hide resolved
lines.append(f"""System::Call 'kernel32::SetEnvironmentVariable(t,t)i("{name}", "{val}").r0'""")
jlstevens marked this conversation as resolved.
Show resolved Hide resolved

return lines

def custom_nsi_insert_from_file(filepath: os.PathLike) -> str:
"""Insert NSI script commands from file.
Expand Down Expand Up @@ -332,6 +349,7 @@ def make_nsi(info, dir_path, extra_files=None, temp_extra_files=None):
'${NAME} ${VERSION} (Python ${PYVERSION} ${ARCH})'
)),
('@EXTRA_FILES@', '\n '.join(extra_files_commands(extra_files, dir_path))),
('@EXTRA_ENV_VARIABLES@', '\n '.join(setup_extra_env_variables(info))),
(
'@CUSTOM_WELCOME_FILE@',
custom_nsi_insert_from_file(info.get('welcome_file', ''))
Expand Down
5 changes: 5 additions & 0 deletions examples/scripts/construct.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ channels:
- http://repo.anaconda.com/pkgs/main/
specs:
- python

extra_env_variables:
- CUSTOM_VARIABLE_1=CUSTOM1
- CUSTOM_VARIABLE_2=CUSTOM2
jlstevens marked this conversation as resolved.
Show resolved Hide resolved

pre_install: pre_install.sh # [unix]
pre_install: pre_install.bat # [win]
pre_install_desc: "Adding this description makes the script selectable in the UI"
Expand Down
2 changes: 2 additions & 0 deletions examples/scripts/post_install.bat
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ if not "%INSTALLER_VER%" == "X" exit 1
if not "%INSTALLER_PLAT%" == "win-64" exit 1
if not "%INSTALLER_TYPE%" == "EXE" exit 1
if "%PREFIX%" == "" exit 1
if not "%CUSTOM_VARIABLE_1%" == "CUSTOM1" exit 1
if not "%CUSTOM_VARIABLE_2%" == "CUSTOM2" exit 1
if not exist "%PREFIX%\pre_install_sentinel.txt" exit 1
5 changes: 5 additions & 0 deletions examples/scripts/post_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ echo "INSTALLER_NAME=${INSTALLER_NAME}"
echo "INSTALLER_VER=${INSTALLER_VER}"
echo "INSTALLER_PLAT=${INSTALLER_PLAT}"
echo "INSTALLER_TYPE=${INSTALLER_TYPE}"
echo "CUSTOM_VARIABLE_1=${CUSTOM_VARIABLE_1}"
echo "CUSTOM_VARIABLE_2=${CUSTOM_VARIABLE_2}"
echo "PREFIX=${PREFIX}"

test "${INSTALLER_NAME}" = "Scripts"
test "${INSTALLER_VER}" = "X"
test "${CUSTOM_VARIABLE_1}" = "CUSTOM1"
test "${CUSTOM_VARIABLE_2}" = "CUSTOM2"

if [[ $(uname -s) == Linux ]]; then
if [[ ${INSTALLER_PLAT} != linux-* ]]; then
exit 1
Expand Down
2 changes: 2 additions & 0 deletions examples/scripts/pre_install.bat
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ if not "%INSTALLER_VER%" == "X" exit 1
if not "%INSTALLER_PLAT%" == "win-64" exit 1
if not "%INSTALLER_TYPE%" == "EXE" exit 1
if "%PREFIX%" == "" exit 1
if not "%CUSTOM_VARIABLE_1%" == "CUSTOM1" exit 1
if not "%CUSTOM_VARIABLE_2%" == "CUSTOM2" exit 1
echo Added by pre-install script > "%PREFIX%\pre_install_sentinel.txt"
5 changes: 5 additions & 0 deletions examples/scripts/pre_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ echo "INSTALLER_NAME=${INSTALLER_NAME}"
echo "INSTALLER_VER=${INSTALLER_VER}"
echo "INSTALLER_PLAT=${INSTALLER_PLAT}"
echo "INSTALLER_TYPE=${INSTALLER_TYPE}"
echo "CUSTOM_VARIABLE_1=${CUSTOM_VARIABLE_1}"
echo "CUSTOM_VARIABLE_2=${CUSTOM_VARIABLE_2}"
echo "PREFIX=${PREFIX}"

test "${INSTALLER_NAME}" = "Scripts"
test "${INSTALLER_VER}" = "X"
test "${CUSTOM_VARIABLE_1}" = "CUSTOM1"
test "${CUSTOM_VARIABLE_2}" = "CUSTOM2"

if [[ $(uname -s) == Linux ]]; then
if [[ ${INSTALLER_PLAT} != linux-* ]]; then
exit 1
Expand Down
Loading