2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PowerShellでpyenvもどき(未完成)

Last updated at Posted at 2019-02-04

#更新
H31.2.4 現在設定中のPythonのデータ保存場所を、スクリプトのあるディレクトリになるように変更しました。

#使い方
pythonはC:\Program Files\Pythonに、バージョンごとにインストール。もしくはコードの一行目の$PYTHON_ROOTを変更。

インストールされているバージョンをリストアップ:

pyenv versions

現在選択中のバージョンを表示:

pyenv version

バージョン選択

pyenv global [version]

※pyenv local は実装されていません。
※各バージョンのパイソンのインストールは手動です。

#コード

pyenv.ps1
$PYTHON_ROOT = [string]"C:\Program Files\Python"
$VERSION_INFO = [string]((Split-Path -Parent $MyInvocation.MyCommand.Path) + "\pyenv_current_version.dat")


function get_versions_installed(){
	return Get-ChildItem $PYTHON_ROOT
}

function get_current_version(){
	if ( $(test-path $VERSION_INFO) -ne $True ){
		Write-Error("ERROR: current python version may not be defined")
		return $False
	}else{
		return Get-Content $VERSION_INFO
	}
}

function set_version($new_version){
	delete_python_path

	if ( $(test-path $VERSION_INFO) -ne $True ){
		New-Item "$VERSION_INFO" -itemType File
	}
	Set-Content -Path $VERSION_INFO $new_version

	set_python_path $new_version
}

function delete_python_path(){
	$paths = $env:path -split ";"
	#foreach($path in $paths){Write-Host $path}
	[int[]]$indices_to_delete = @()
	for ($i = 0; $i -lt $paths.Length; $i++){
		if ($paths[$i].Contains($PYTHON_ROOT)){
			$indices_to_delete += $i
		}
		if ($paths[$i] -eq ""){
			$indices_to_delete += $i
		}
	}
	$indices_to_delete = $indices_to_delete | Sort-Object -Descending
	foreach($i in $indices_to_delete){
		$paths[$i] = $null
	}
	$env:path = $paths -join ";"
}

function set_python_path($version){
	$env:path += "$PYTHON_ROOT\$version;"
	$env:path += "$PYTHON_ROOT\$version\Scripts;"
}

if ($args.Length -eq 0){
	Write-Host("usage: pyenv [version, versions, global (python version)]")
}else{
	switch ($args[0]){
		"version" {
			$current_version = get_current_version
			Write-Host($current_version)
		}
		"versions" {
			foreach ($ver in get_versions_installed){
				Write-Host($ver)
			}
		}	
		"global" {
			if ($args.Length -eq 2){
				$present_versions_data = get_versions_installed
				$present_versions = [string[]]@()
				foreach ($ver in $present_versions_data){
					$present_versions += [string]$ver
				}
				if ($present_versions -contains $args[1]){
					$ver = $args[1]
					set_version($ver)
					Write-Host("pyenv version has been set to $ver")
				}else{
					$ver = $args[1]
					Write-Error("python version $ver is not installed")
					exit $False
				}
			}
		}
		<#
		"test" {
			#delete_python_path
			#set_python_path $args[1]
		}
		#>
		

	}
}

#バグ
delete_python_path関数で既存のパスを消す際に空白の処理がうまくできてないので、使っていると$Env:Pathにセミコロンがたまっていく。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?