forked from tuya/TuyaOpen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.ps1
More file actions
208 lines (178 loc) · 6.57 KB
/
export.ps1
File metadata and controls
208 lines (178 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# Check if already in virtual environment by checking if current Python is in .venv
try {
$currentPython = (Get-Command python -ErrorAction SilentlyContinue).Source
if ($currentPython -and $currentPython.Contains(".venv")) {
Write-Host "Virtual environment is already activated."
exit 0
}
} catch {
# Python not found, continue with setup
}
# Set script root directory
$OPEN_SDK_ROOT = Split-Path -Parent $MyInvocation.MyCommand.Path
# Debug information
Write-Host "OPEN_SDK_ROOT = $OPEN_SDK_ROOT"
Write-Host "Current working directory = $(Get-Location)"
# Change working directory
Set-Location $OPEN_SDK_ROOT
# Check Python version
Write-Host "Checking Python version..."
try {
$pythonVersion = python --version 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Host "Error: Python is not installed or not in PATH!"
Write-Host "Please install Python 3.6.0 or higher."
Read-Host "Press any key to continue"
exit 1
}
Write-Host "Using Python $pythonVersion"
} catch {
Write-Host "Error: Python is not installed or not in PATH!"
Write-Host "Please install Python 3.6.0 or higher."
Read-Host "Press any key to continue"
exit 1
}
# Get Python version number
$versionMatch = $pythonVersion -match "Python (\d+)\.(\d+)"
if ($versionMatch) {
$major = [int]$matches[1]
$minor = [int]$matches[2]
if ($major -lt 3) {
Write-Host "Error: Python version $pythonVersion is too old!"
Write-Host "Please install Python 3.6.0 or higher."
Read-Host "Press any key to continue"
exit 1
}
if ($major -eq 3 -and $minor -lt 6) {
Write-Host "Error: Python version $pythonVersion is too old!"
Write-Host "Please install Python 3.6.0 or higher."
Read-Host "Press any key to continue"
exit 1
}
}
# Create virtual environment
$venvPath = Join-Path $OPEN_SDK_ROOT ".venv"
if (-not (Test-Path $venvPath)) {
Write-Host "Creating virtual environment..."
try {
python -m venv $venvPath
if ($LASTEXITCODE -ne 0) {
Write-Host "Error: Failed to create virtual environment!"
Write-Host "Please check your Python installation and try again."
Read-Host "Press any key to continue"
exit 1
}
Write-Host "Virtual environment created successfully."
} catch {
Write-Host "Error: Failed to create virtual environment!"
Write-Host "Please check your Python installation and try again."
Read-Host "Press any key to continue"
exit 1
}
} else {
Write-Host "Virtual environment already exists."
}
# Verify virtual environment was created properly
$pythonExe = Join-Path $venvPath "Scripts\python.exe"
$python3Exe = Join-Path $venvPath "Scripts\python3.exe"
$pipExe = Join-Path $venvPath "Scripts\pip.exe"
if (-not (Test-Path $pythonExe)) {
Write-Host "Error: Virtual environment Python executable not found: $pythonExe"
Read-Host "Press any key to continue"
exit 1
}
if (-not (Test-Path $pipExe)) {
Write-Host "Error: Virtual environment pip executable not found: $pipExe"
Read-Host "Press any key to continue"
exit 1
}
# Copy python.exe to python3.exe for compatibility
if (-not (Test-Path $python3Exe)) {
Copy-Item $pythonExe $python3Exe
}
# Activate virtual environment (set PATH to use virtual environment)
Write-Host "DEBUG: Activating virtual environment from $venvPath\Scripts"
$env:PATH = "$venvPath\Scripts;$env:PATH"
# Verify activation worked by checking if we're using the right Python
$activePython = (Get-Command python).Source
Write-Host "Virtual environment activated successfully: $activePython"
# Environmental variables
$env:OPEN_SDK_PYTHON = $pythonExe
$env:OPEN_SDK_PIP = $pipExe
$env:PATH = "$env:PATH;$OPEN_SDK_ROOT"
# Install dependencies
Write-Host "Installing dependencies..."
$requirementsPath = Join-Path $OPEN_SDK_ROOT "requirements.txt"
if (Test-Path $requirementsPath) {
try {
& $env:OPEN_SDK_PIP install -r $requirementsPath
if ($LASTEXITCODE -ne 0) {
Write-Host "Warning: Some dependencies may not have been installed correctly."
}
} catch {
Write-Host "Warning: Error occurred while installing dependencies."
}
} else {
Write-Host "Warning: requirements.txt file not found."
}
# Remove cache files
$cachePath = Join-Path $OPEN_SDK_ROOT ".cache"
New-Item -ItemType Directory -Path $cachePath -Force -ErrorAction SilentlyContinue | Out-Null
$envJsonPath = Join-Path $cachePath ".env.json"
if (Test-Path $envJsonPath) {
Remove-Item $envJsonPath -Force
}
$dontUpdatePlatform = Join-Path $cachePath ".dont_prompt_update_platform"
if (Test-Path $dontUpdatePlatform) {
Remove-Item $dontUpdatePlatform -Force
}
# hello tuya
$HELLO_TUYA = '
______ ____
/_ __/_ ____ _____ _ / __ \___ ___ ___
/ / / // / // / _ `/ / /_/ / _ \/ -_) _ \
/_/ \_,_/\_, /\_,_/ \____/ .__/\__/_//_/
/___/ /_/
'
Write-Host "****************************************"
Write-Host $HELLO_TUYA
Write-Host "Exit use: exit"
Write-Host "****************************************"
# Create a temporary script file for the new session
$tempScript = [System.IO.Path]::GetTempFileName() + ".ps1"
$scriptContent = @"
# Set environment variables
`$env:OPEN_SDK_PYTHON = '$pythonExe'
`$env:OPEN_SDK_PIP = '$pipExe'
`$env:PATH = '$venvPath\Scripts;' + `$env:PATH
`$env:PATH = `$env:PATH + ';$OPEN_SDK_ROOT'
# Set working directory
Set-Location '$OPEN_SDK_ROOT'
# Create custom prompt function (equivalent to set PROMPT=(tos) %PROMPT% in batch)
function prompt {
"(tos) `$(`$executionContext.SessionState.Path.CurrentLocation)`$('>' * (`$nestedPromptLevel + 1)) "
}
# Create tos.py function
function tos.py {
& `$env:OPEN_SDK_PYTHON '$OPEN_SDK_ROOT\tos.py' `$args
}
# Create exit function to clean up environment
function exit {
Write-Host "Exiting TuyaOpen environment..."
# Clean up environment variables
Remove-Item Env:OPEN_SDK_PYTHON -ErrorAction SilentlyContinue
Remove-Item Env:OPEN_SDK_PIP -ErrorAction SilentlyContinue
Remove-Item Env:OPEN_SDK_ROOT -ErrorAction SilentlyContinue
Write-Host "TuyaOpen environment deactivated."
# Exit PowerShell
[Environment]::Exit(0)
}
"@
# Write the script content to temporary file
$scriptContent | Out-File -FilePath $tempScript -Encoding UTF8
# Start interactive PowerShell session with the temporary script
Write-Host "Starting interactive session with virtual environment..."
Write-Host "tmpScript $tempScript"
powershell -NoExit -File $tempScript
# Clean up temporary file
Remove-Item $tempScript -Force -ErrorAction SilentlyContinue