This repository has been archived by the owner on Sep 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ps1
221 lines (191 loc) · 5.32 KB
/
main.ps1
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
209
210
211
212
213
214
215
216
217
218
219
220
221
$FolderStructure = @(
".\asset\icon.ico"
".\data\input.json"
".\data\domains.csv"
".\src\ga.py"
".\src\sub\inputprocess.py"
".\src\sub\kernel.py"
".\src\sub\plot.py"
".\src\sub\translate.py"
".\src\sub\__init__.py"
# ".\src\test\multitaskdemo.py"
# ".\src\test\outread.py"
# ".\src\test\plotstuff.py"
# ".\src\test\recheck.py"
# ".\src\test\virtualkernel.py"
)
$Commands = @{
"py" = @{
"Info" = "Install at python.com or add python.exe to PATH"
"GetVersionCommand" = "((python -V) -split ' ')[-1]"
"RequiredVersion" = "3.2"
}
"abaqus" = @{
"Info" = "Install Abaqus or add abaqus.bat to PATH"
"GetVersionCommand" = "((abaqus information=version)[1] -split ' ')[-1]"
"RequiredVersion" = "0"
}
"pip" = @{
"Info" = "Install pip or add pip to PATH"
"GetVersionCommand" = "((pip --version) -split ' ')[1]"
"RequiredVersion" = "0"
}
}
$Packages = @(
"cycler"
"kiwisolver"
"matplotlib"
"numpy"
"Pillow"
"pyparsing"
"python-dateutil"
"six"
)
$UnusedFiles = @(
"__pycache__"
"*.dat"
"*.env"
"*.inp"
"*.ipm"
"*.lck"
"*.log"
"*.com"
"*.msg"
"*.prt"
"*.sim"
"*.sta"
"*.rec"
"*.pyc"
"*.odb"
)
function Clear-Line {
Param(
[Parameter(Position=1)]
[int32]$Count=1
)
$CurrentLine = $Host.UI.RawUI.CursorPosition.Y
$ConsoleWidth = $Host.UI.RawUI.BufferSize.Width
$i = 1
for ($i; $i -le $Count; $i++) {
[Console]::SetCursorPosition(0,($CurrentLine - $i))
[Console]::Write("{0,-$ConsoleWidth}" -f " ")
}
[Console]::SetCursorPosition(0,($CurrentLine - $Count))
}
function Test-DirectoryTree {
Param (
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string[]]$Paths
)
$Boolean = $true
foreach ($Path in $Paths) {
if (!(Test-Path $Path)) {
$Boolean = $false
Write-Warning "File doesn't exist: '$Path'
`rCheck again.`n`n"
}
}
return $Boolean
}
function Test-Command {
Param (
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[hashtable]$Commands
)
foreach ($Command in $Commands.GetEnumerator()) {
if ((Get-Command $Command.Key -ErrorAction SilentlyContinue) -eq $null) {
$Boolean += @{ $Command.Key = $false}
Write-Warning "Path doesn't exist: '$($Command.Key)'.
`r$($Command.Value.Info).`n`n"
}
else {
if ((Invoke-Expression $Command.Value.GetVersionCommand) -lt $Command.Value.RequiredVersion) {
$Boolean += @{ $Command.Key = $false}
Write-Warning "'$($Command.Key)' required at least version. $($Command.Value.RequiredVersion).`n`n"
}
else {
$Boolean += @{ $Command.Key = $true}
}
}
}
if ($Boolean.pip) {
if (($Boolean.Values -notcontains $false) -and (Test-Package -Packages $Packages)) {
return $true
}
}
return $false
}
function Test-Package {
Param (
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string[]]$Packages
)
$Boolean = $true
$InstallPackages = pip freeze | % { ($_ -split "==")[0] }
foreach ($Package in $Packages) {
if ($InstallPackages -notcontains $Package) {
$Boolean = $false
Write-Warning "Package '$Package' missing.
`rPlease install using pip or get into virtual environment.`n`n"
}
}
return $Boolean
}
function Clean-Directory {
Param (
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string[]]$NameList
)
$NameList | % { Get-ChildItem -Filter $_ -Recurse -Force | Remove-Item -Force -Recurse }
}
function Call-Process {
python .\ga.py 2>..\result\error.log
# WARNING: Need IS_LOGGING is always true
if (Test-Path -Path ..\result\output.json) {
$OutputFileLength = (Get-Content -Raw -Path ..\result\output.json | ConvertFrom-Json)[1].Length
}
else {
$OutputFileLength = 0
}
$InputFile = (Get-Content -Raw -Path ..\data\input.json) -replace '(?m)(?<=^([^"]|"[^"]*")*)//.*' | ConvertFrom-Json
$GenerationSize = $InputFile.CONSTANTS.GENERATION_SIZE
if ($OutputFileLength -lt $GenerationSize) {
Write-Output "Let me take a break"
Start-Sleep 1
Clear-Line
Write-Output "Removing unnecessary files..."
Start-Sleep 1
Clean-Directory -NameList $UnusedFiles
Clear-Line
Write-Output "Continuing..."
Start-Sleep 1
Clear-Line
Call-Process
}
else {
Clean-Directory -NameList $UnusedFiles
}
}
Write-Host "----------------------" -ForegroundColor Cyan
Write-Host "PROSTHETIC FOOT DESIGN" -ForegroundColor Cyan
Write-Host "----------------------" -ForegroundColor Cyan
Write-Output "Checking neccessary files and softwares..."
Start-Sleep 1
Clear-Line
$Check = @(
Test-DirectoryTree $FolderStructure
Test-Command $Commands
)
If ($Check -notcontains $false) {
Write-Output "Everything looks fine. Start running..."
Start-Sleep 1
Clear-Line
Push-Location .\src\
python -m sub.translate
Call-Process
Pop-Location
}