Compare commits

...

2 Commits

Binary file not shown.

Binary file not shown.

@ -1,247 +1,247 @@
<# <#
.Synopsis .Synopsis
Activate a Python virtual environment for the current PowerShell session. Activate a Python virtual environment for the current PowerShell session.
.Description .Description
Pushes the python executable for a virtual environment to the front of the Pushes the python executable for a virtual environment to the front of the
$Env:PATH environment variable and sets the prompt to signify that you are $Env:PATH environment variable and sets the prompt to signify that you are
in a Python virtual environment. Makes use of the command line switches as in a Python virtual environment. Makes use of the command line switches as
well as the `pyvenv.cfg` file values present in the virtual environment. well as the `pyvenv.cfg` file values present in the virtual environment.
.Parameter VenvDir .Parameter VenvDir
Path to the directory that contains the virtual environment to activate. The Path to the directory that contains the virtual environment to activate. The
default value for this is the parent of the directory that the Activate.ps1 default value for this is the parent of the directory that the Activate.ps1
script is located within. script is located within.
.Parameter Prompt .Parameter Prompt
The prompt prefix to display when this virtual environment is activated. By The prompt prefix to display when this virtual environment is activated. By
default, this prompt is the name of the virtual environment folder (VenvDir) default, this prompt is the name of the virtual environment folder (VenvDir)
surrounded by parentheses and followed by a single space (ie. '(.venv) '). surrounded by parentheses and followed by a single space (ie. '(.venv) ').
.Example .Example
Activate.ps1 Activate.ps1
Activates the Python virtual environment that contains the Activate.ps1 script. Activates the Python virtual environment that contains the Activate.ps1 script.
.Example .Example
Activate.ps1 -Verbose Activate.ps1 -Verbose
Activates the Python virtual environment that contains the Activate.ps1 script, Activates the Python virtual environment that contains the Activate.ps1 script,
and shows extra information about the activation as it executes. and shows extra information about the activation as it executes.
.Example .Example
Activate.ps1 -VenvDir C:\Users\MyUser\Common\.venv Activate.ps1 -VenvDir C:\Users\MyUser\Common\.venv
Activates the Python virtual environment located in the specified location. Activates the Python virtual environment located in the specified location.
.Example .Example
Activate.ps1 -Prompt "MyPython" Activate.ps1 -Prompt "MyPython"
Activates the Python virtual environment that contains the Activate.ps1 script, Activates the Python virtual environment that contains the Activate.ps1 script,
and prefixes the current prompt with the specified string (surrounded in and prefixes the current prompt with the specified string (surrounded in
parentheses) while the virtual environment is active. parentheses) while the virtual environment is active.
.Notes .Notes
On Windows, it may be required to enable this Activate.ps1 script by setting the On Windows, it may be required to enable this Activate.ps1 script by setting the
execution policy for the user. You can do this by issuing the following PowerShell execution policy for the user. You can do this by issuing the following PowerShell
command: command:
PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
For more information on Execution Policies: For more information on Execution Policies:
https://go.microsoft.com/fwlink/?LinkID=135170 https://go.microsoft.com/fwlink/?LinkID=135170
#> #>
Param( Param(
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $false)]
[String] [String]
$VenvDir, $VenvDir,
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $false)]
[String] [String]
$Prompt $Prompt
) )
<# Function declarations --------------------------------------------------- #> <# Function declarations --------------------------------------------------- #>
<# <#
.Synopsis .Synopsis
Remove all shell session elements added by the Activate script, including the Remove all shell session elements added by the Activate script, including the
addition of the virtual environment's Python executable from the beginning of addition of the virtual environment's Python executable from the beginning of
the PATH variable. the PATH variable.
.Parameter NonDestructive .Parameter NonDestructive
If present, do not remove this function from the global namespace for the If present, do not remove this function from the global namespace for the
session. session.
#> #>
function global:deactivate ([switch]$NonDestructive) { function global:deactivate ([switch]$NonDestructive) {
# Revert to original values # Revert to original values
# The prior prompt: # The prior prompt:
if (Test-Path -Path Function:_OLD_VIRTUAL_PROMPT) { if (Test-Path -Path Function:_OLD_VIRTUAL_PROMPT) {
Copy-Item -Path Function:_OLD_VIRTUAL_PROMPT -Destination Function:prompt Copy-Item -Path Function:_OLD_VIRTUAL_PROMPT -Destination Function:prompt
Remove-Item -Path Function:_OLD_VIRTUAL_PROMPT Remove-Item -Path Function:_OLD_VIRTUAL_PROMPT
} }
# The prior PYTHONHOME: # The prior PYTHONHOME:
if (Test-Path -Path Env:_OLD_VIRTUAL_PYTHONHOME) { if (Test-Path -Path Env:_OLD_VIRTUAL_PYTHONHOME) {
Copy-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME -Destination Env:PYTHONHOME Copy-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME -Destination Env:PYTHONHOME
Remove-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME Remove-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME
} }
# The prior PATH: # The prior PATH:
if (Test-Path -Path Env:_OLD_VIRTUAL_PATH) { if (Test-Path -Path Env:_OLD_VIRTUAL_PATH) {
Copy-Item -Path Env:_OLD_VIRTUAL_PATH -Destination Env:PATH Copy-Item -Path Env:_OLD_VIRTUAL_PATH -Destination Env:PATH
Remove-Item -Path Env:_OLD_VIRTUAL_PATH Remove-Item -Path Env:_OLD_VIRTUAL_PATH
} }
# Just remove the VIRTUAL_ENV altogether: # Just remove the VIRTUAL_ENV altogether:
if (Test-Path -Path Env:VIRTUAL_ENV) { if (Test-Path -Path Env:VIRTUAL_ENV) {
Remove-Item -Path env:VIRTUAL_ENV Remove-Item -Path env:VIRTUAL_ENV
} }
# Just remove VIRTUAL_ENV_PROMPT altogether. # Just remove VIRTUAL_ENV_PROMPT altogether.
if (Test-Path -Path Env:VIRTUAL_ENV_PROMPT) { if (Test-Path -Path Env:VIRTUAL_ENV_PROMPT) {
Remove-Item -Path env:VIRTUAL_ENV_PROMPT Remove-Item -Path env:VIRTUAL_ENV_PROMPT
} }
# Just remove the _PYTHON_VENV_PROMPT_PREFIX altogether: # Just remove the _PYTHON_VENV_PROMPT_PREFIX altogether:
if (Get-Variable -Name "_PYTHON_VENV_PROMPT_PREFIX" -ErrorAction SilentlyContinue) { if (Get-Variable -Name "_PYTHON_VENV_PROMPT_PREFIX" -ErrorAction SilentlyContinue) {
Remove-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Scope Global -Force Remove-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Scope Global -Force
} }
# Leave deactivate function in the global namespace if requested: # Leave deactivate function in the global namespace if requested:
if (-not $NonDestructive) { if (-not $NonDestructive) {
Remove-Item -Path function:deactivate Remove-Item -Path function:deactivate
} }
} }
<# <#
.Description .Description
Get-PyVenvConfig parses the values from the pyvenv.cfg file located in the Get-PyVenvConfig parses the values from the pyvenv.cfg file located in the
given folder, and returns them in a map. given folder, and returns them in a map.
For each line in the pyvenv.cfg file, if that line can be parsed into exactly For each line in the pyvenv.cfg file, if that line can be parsed into exactly
two strings separated by `=` (with any amount of whitespace surrounding the =) two strings separated by `=` (with any amount of whitespace surrounding the =)
then it is considered a `key = value` line. The left hand string is the key, then it is considered a `key = value` line. The left hand string is the key,
the right hand is the value. the right hand is the value.
If the value starts with a `'` or a `"` then the first and last character is If the value starts with a `'` or a `"` then the first and last character is
stripped from the value before being captured. stripped from the value before being captured.
.Parameter ConfigDir .Parameter ConfigDir
Path to the directory that contains the `pyvenv.cfg` file. Path to the directory that contains the `pyvenv.cfg` file.
#> #>
function Get-PyVenvConfig( function Get-PyVenvConfig(
[String] [String]
$ConfigDir $ConfigDir
) { ) {
Write-Verbose "Given ConfigDir=$ConfigDir, obtain values in pyvenv.cfg" Write-Verbose "Given ConfigDir=$ConfigDir, obtain values in pyvenv.cfg"
# Ensure the file exists, and issue a warning if it doesn't (but still allow the function to continue). # Ensure the file exists, and issue a warning if it doesn't (but still allow the function to continue).
$pyvenvConfigPath = Join-Path -Resolve -Path $ConfigDir -ChildPath 'pyvenv.cfg' -ErrorAction Continue $pyvenvConfigPath = Join-Path -Resolve -Path $ConfigDir -ChildPath 'pyvenv.cfg' -ErrorAction Continue
# An empty map will be returned if no config file is found. # An empty map will be returned if no config file is found.
$pyvenvConfig = @{ } $pyvenvConfig = @{ }
if ($pyvenvConfigPath) { if ($pyvenvConfigPath) {
Write-Verbose "File exists, parse `key = value` lines" Write-Verbose "File exists, parse `key = value` lines"
$pyvenvConfigContent = Get-Content -Path $pyvenvConfigPath $pyvenvConfigContent = Get-Content -Path $pyvenvConfigPath
$pyvenvConfigContent | ForEach-Object { $pyvenvConfigContent | ForEach-Object {
$keyval = $PSItem -split "\s*=\s*", 2 $keyval = $PSItem -split "\s*=\s*", 2
if ($keyval[0] -and $keyval[1]) { if ($keyval[0] -and $keyval[1]) {
$val = $keyval[1] $val = $keyval[1]
# Remove extraneous quotations around a string value. # Remove extraneous quotations around a string value.
if ("'""".Contains($val.Substring(0, 1))) { if ("'""".Contains($val.Substring(0, 1))) {
$val = $val.Substring(1, $val.Length - 2) $val = $val.Substring(1, $val.Length - 2)
} }
$pyvenvConfig[$keyval[0]] = $val $pyvenvConfig[$keyval[0]] = $val
Write-Verbose "Adding Key: '$($keyval[0])'='$val'" Write-Verbose "Adding Key: '$($keyval[0])'='$val'"
} }
} }
} }
return $pyvenvConfig return $pyvenvConfig
} }
<# Begin Activate script --------------------------------------------------- #> <# Begin Activate script --------------------------------------------------- #>
# Determine the containing directory of this script # Determine the containing directory of this script
$VenvExecPath = Split-Path -Parent $MyInvocation.MyCommand.Definition $VenvExecPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
$VenvExecDir = Get-Item -Path $VenvExecPath $VenvExecDir = Get-Item -Path $VenvExecPath
Write-Verbose "Activation script is located in path: '$VenvExecPath'" Write-Verbose "Activation script is located in path: '$VenvExecPath'"
Write-Verbose "VenvExecDir Fullname: '$($VenvExecDir.FullName)" Write-Verbose "VenvExecDir Fullname: '$($VenvExecDir.FullName)"
Write-Verbose "VenvExecDir Name: '$($VenvExecDir.Name)" Write-Verbose "VenvExecDir Name: '$($VenvExecDir.Name)"
# Set values required in priority: CmdLine, ConfigFile, Default # Set values required in priority: CmdLine, ConfigFile, Default
# First, get the location of the virtual environment, it might not be # First, get the location of the virtual environment, it might not be
# VenvExecDir if specified on the command line. # VenvExecDir if specified on the command line.
if ($VenvDir) { if ($VenvDir) {
Write-Verbose "VenvDir given as parameter, using '$VenvDir' to determine values" Write-Verbose "VenvDir given as parameter, using '$VenvDir' to determine values"
} }
else { else {
Write-Verbose "VenvDir not given as a parameter, using parent directory name as VenvDir." Write-Verbose "VenvDir not given as a parameter, using parent directory name as VenvDir."
$VenvDir = $VenvExecDir.Parent.FullName.TrimEnd("\\/") $VenvDir = $VenvExecDir.Parent.FullName.TrimEnd("\\/")
Write-Verbose "VenvDir=$VenvDir" Write-Verbose "VenvDir=$VenvDir"
} }
# Next, read the `pyvenv.cfg` file to determine any required value such # Next, read the `pyvenv.cfg` file to determine any required value such
# as `prompt`. # as `prompt`.
$pyvenvCfg = Get-PyVenvConfig -ConfigDir $VenvDir $pyvenvCfg = Get-PyVenvConfig -ConfigDir $VenvDir
# Next, set the prompt from the command line, or the config file, or # Next, set the prompt from the command line, or the config file, or
# just use the name of the virtual environment folder. # just use the name of the virtual environment folder.
if ($Prompt) { if ($Prompt) {
Write-Verbose "Prompt specified as argument, using '$Prompt'" Write-Verbose "Prompt specified as argument, using '$Prompt'"
} }
else { else {
Write-Verbose "Prompt not specified as argument to script, checking pyvenv.cfg value" Write-Verbose "Prompt not specified as argument to script, checking pyvenv.cfg value"
if ($pyvenvCfg -and $pyvenvCfg['prompt']) { if ($pyvenvCfg -and $pyvenvCfg['prompt']) {
Write-Verbose " Setting based on value in pyvenv.cfg='$($pyvenvCfg['prompt'])'" Write-Verbose " Setting based on value in pyvenv.cfg='$($pyvenvCfg['prompt'])'"
$Prompt = $pyvenvCfg['prompt']; $Prompt = $pyvenvCfg['prompt'];
} }
else { else {
Write-Verbose " Setting prompt based on parent's directory's name. (Is the directory name passed to venv module when creating the virtual environment)" Write-Verbose " Setting prompt based on parent's directory's name. (Is the directory name passed to venv module when creating the virtual environment)"
Write-Verbose " Got leaf-name of $VenvDir='$(Split-Path -Path $venvDir -Leaf)'" Write-Verbose " Got leaf-name of $VenvDir='$(Split-Path -Path $venvDir -Leaf)'"
$Prompt = Split-Path -Path $venvDir -Leaf $Prompt = Split-Path -Path $venvDir -Leaf
} }
} }
Write-Verbose "Prompt = '$Prompt'" Write-Verbose "Prompt = '$Prompt'"
Write-Verbose "VenvDir='$VenvDir'" Write-Verbose "VenvDir='$VenvDir'"
# Deactivate any currently active virtual environment, but leave the # Deactivate any currently active virtual environment, but leave the
# deactivate function in place. # deactivate function in place.
deactivate -nondestructive deactivate -nondestructive
# Now set the environment variable VIRTUAL_ENV, used by many tools to determine # Now set the environment variable VIRTUAL_ENV, used by many tools to determine
# that there is an activated venv. # that there is an activated venv.
$env:VIRTUAL_ENV = $VenvDir $env:VIRTUAL_ENV = $VenvDir
if (-not $Env:VIRTUAL_ENV_DISABLE_PROMPT) { if (-not $Env:VIRTUAL_ENV_DISABLE_PROMPT) {
Write-Verbose "Setting prompt to '$Prompt'" Write-Verbose "Setting prompt to '$Prompt'"
# Set the prompt to include the env name # Set the prompt to include the env name
# Make sure _OLD_VIRTUAL_PROMPT is global # Make sure _OLD_VIRTUAL_PROMPT is global
function global:_OLD_VIRTUAL_PROMPT { "" } function global:_OLD_VIRTUAL_PROMPT { "" }
Copy-Item -Path function:prompt -Destination function:_OLD_VIRTUAL_PROMPT Copy-Item -Path function:prompt -Destination function:_OLD_VIRTUAL_PROMPT
New-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Description "Python virtual environment prompt prefix" -Scope Global -Option ReadOnly -Visibility Public -Value $Prompt New-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Description "Python virtual environment prompt prefix" -Scope Global -Option ReadOnly -Visibility Public -Value $Prompt
function global:prompt { function global:prompt {
Write-Host -NoNewline -ForegroundColor Green "($_PYTHON_VENV_PROMPT_PREFIX) " Write-Host -NoNewline -ForegroundColor Green "($_PYTHON_VENV_PROMPT_PREFIX) "
_OLD_VIRTUAL_PROMPT _OLD_VIRTUAL_PROMPT
} }
$env:VIRTUAL_ENV_PROMPT = $Prompt $env:VIRTUAL_ENV_PROMPT = $Prompt
} }
# Clear PYTHONHOME # Clear PYTHONHOME
if (Test-Path -Path Env:PYTHONHOME) { if (Test-Path -Path Env:PYTHONHOME) {
Copy-Item -Path Env:PYTHONHOME -Destination Env:_OLD_VIRTUAL_PYTHONHOME Copy-Item -Path Env:PYTHONHOME -Destination Env:_OLD_VIRTUAL_PYTHONHOME
Remove-Item -Path Env:PYTHONHOME Remove-Item -Path Env:PYTHONHOME
} }
# Add the venv to the PATH # Add the venv to the PATH
Copy-Item -Path Env:PATH -Destination Env:_OLD_VIRTUAL_PATH Copy-Item -Path Env:PATH -Destination Env:_OLD_VIRTUAL_PATH
$Env:PATH = "$VenvExecDir$([System.IO.Path]::PathSeparator)$Env:PATH" $Env:PATH = "$VenvExecDir$([System.IO.Path]::PathSeparator)$Env:PATH"

@ -38,11 +38,11 @@ deactivate () {
# unset irrelevant variables # unset irrelevant variables
deactivate nondestructive deactivate nondestructive
VIRTUAL_ENV="__VENV_DIR__" VIRTUAL_ENV="/home/meowrain/software/LearnEnglishWithSentences/libs"
export VIRTUAL_ENV export VIRTUAL_ENV
_OLD_VIRTUAL_PATH="$PATH" _OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/__VENV_BIN_NAME__:$PATH" PATH="$VIRTUAL_ENV/bin:$PATH"
export PATH export PATH
# unset PYTHONHOME if set # unset PYTHONHOME if set
@ -55,9 +55,9 @@ fi
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
_OLD_VIRTUAL_PS1="${PS1:-}" _OLD_VIRTUAL_PS1="${PS1:-}"
PS1="__VENV_PROMPT__${PS1:-}" PS1="(libs) ${PS1:-}"
export PS1 export PS1
VIRTUAL_ENV_PROMPT="__VENV_PROMPT__" VIRTUAL_ENV_PROMPT="(libs) "
export VIRTUAL_ENV_PROMPT export VIRTUAL_ENV_PROMPT
fi fi

@ -8,17 +8,17 @@ alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PA
# Unset irrelevant variables. # Unset irrelevant variables.
deactivate nondestructive deactivate nondestructive
setenv VIRTUAL_ENV "__VENV_DIR__" setenv VIRTUAL_ENV "/home/meowrain/software/LearnEnglishWithSentences/libs"
set _OLD_VIRTUAL_PATH="$PATH" set _OLD_VIRTUAL_PATH="$PATH"
setenv PATH "$VIRTUAL_ENV/__VENV_BIN_NAME__:$PATH" setenv PATH "$VIRTUAL_ENV/bin:$PATH"
set _OLD_VIRTUAL_PROMPT="$prompt" set _OLD_VIRTUAL_PROMPT="$prompt"
if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then
set prompt = "__VENV_PROMPT__$prompt" set prompt = "(libs) $prompt"
setenv VIRTUAL_ENV_PROMPT "__VENV_PROMPT__" setenv VIRTUAL_ENV_PROMPT "(libs) "
endif endif
alias pydoc python -m pydoc alias pydoc python -m pydoc

@ -33,10 +33,10 @@ end
# Unset irrelevant variables. # Unset irrelevant variables.
deactivate nondestructive deactivate nondestructive
set -gx VIRTUAL_ENV "__VENV_DIR__" set -gx VIRTUAL_ENV "/home/meowrain/software/LearnEnglishWithSentences/libs"
set -gx _OLD_VIRTUAL_PATH $PATH set -gx _OLD_VIRTUAL_PATH $PATH
set -gx PATH "$VIRTUAL_ENV/__VENV_BIN_NAME__" $PATH set -gx PATH "$VIRTUAL_ENV/bin" $PATH
# Unset PYTHONHOME if set. # Unset PYTHONHOME if set.
if set -q PYTHONHOME if set -q PYTHONHOME
@ -56,7 +56,7 @@ if test -z "$VIRTUAL_ENV_DISABLE_PROMPT"
set -l old_status $status set -l old_status $status
# Output the venv prompt; color taken from the blue of the Python logo. # Output the venv prompt; color taken from the blue of the Python logo.
printf "%s%s%s" (set_color 4B8BBE) "__VENV_PROMPT__" (set_color normal) printf "%s%s%s" (set_color 4B8BBE) "(libs) " (set_color normal)
# Restore the return status of the previous command. # Restore the return status of the previous command.
echo "exit $old_status" | . echo "exit $old_status" | .
@ -65,5 +65,5 @@ if test -z "$VIRTUAL_ENV_DISABLE_PROMPT"
end end
set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV" set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV"
set -gx VIRTUAL_ENV_PROMPT "__VENV_PROMPT__" set -gx VIRTUAL_ENV_PROMPT "(libs) "
end end

@ -0,0 +1,8 @@
#!/home/meowrain/software/LearnEnglishWithSentences/libs/bin/python3
# -*- coding: utf-8 -*-
import re
import sys
from dotenv.__main__ import cli
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(cli())

@ -0,0 +1,8 @@
#!/home/meowrain/software/LearnEnglishWithSentences/libs/bin/python3
# -*- coding: utf-8 -*-
import re
import sys
from charset_normalizer.cli.normalizer import cli_detect
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(cli_detect())

@ -0,0 +1,8 @@
#!/home/meowrain/software/LearnEnglishWithSentences/libs/bin/python3
# -*- coding: utf-8 -*-
import re
import sys
from pip._internal.cli.main import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())

@ -0,0 +1,8 @@
#!/home/meowrain/software/LearnEnglishWithSentences/libs/bin/python3
# -*- coding: utf-8 -*-
import re
import sys
from pip._internal.cli.main import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())

@ -0,0 +1,8 @@
#!/home/meowrain/software/LearnEnglishWithSentences/libs/bin/python3
# -*- coding: utf-8 -*-
import re
import sys
from pip._internal.cli.main import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())

@ -0,0 +1 @@
python3

@ -0,0 +1 @@
/usr/bin/python3

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save