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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
| Option Explicit
' Make sure to Import "pyenv-lib.vbs" before this file in a command. (for objfs/objweb variables)
' WScript.echo "kkotari: pyenv-install-lib.vbs..!"
Dim mirrors()
ReDim mirrors(0)
mirrors(0) = objws.Environment("Process")("PYTHON_BUILD_MIRROR_URL")
If mirrors(0) = "" Then
ReDim Preserve mirrors(2)
mirrors(0) = "https://www.python.org/ftp/python/"
mirrors(1) = "https://downloads.python.org/pypy/versions.json"
mirrors(2) = "https://api.github.com/repos/oracle/graalpython/releases"
End If
Const SFV_FileName = 0
Const SFV_URL = 1
Const SFV_Version = 2
Const VRX_Major = 0
Const VRX_Minor = 1
Const VRX_Patch = 2
Const VRX_Release = 3
Const VRX_RelNumber = 4
Const VRX_x64 = 5
Const VRX_ARM = 6
Const VRX_Web = 7
Const VRX_Ext = 8
Const VRX_Arch = 5
Const VRX_ZipRoot = 9
' Version definition array from LoadVersionsXML.
Const LV_Code = 0
Const LV_FileName = 1
Const LV_URL = 2
Const LV_x64 = 3
Const LV_Web = 4
Const LV_MSI = 5
Const LV_ZipRootDir = 6
' Const LV_ARM = 7 # need to validate what number is this
' Installation parameters used for clear/extract, extension of LV.
Const IP_InstallPath = 7
Const IP_InstallFile = 8
Const IP_Quiet = 9
Const IP_Dev = 10
Dim regexVer
Dim regexVerArch
Dim regexFile
Dim regexJsonUrl
Set regexVer = New RegExp
Set regexVerArch = New RegExp
Set regexFile = New RegExp
Set regexJsonUrl = New RegExp
With regexVer
.Pattern = "^(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:([a-z]+)(\d*))?$"
.Global = True
.IgnoreCase = True
End With
With regexVerArch
.Pattern = "^(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:([a-z]+)(\d*))?([\.-](?:amd64|arm64|win32))?$"
.Global = True
.IgnoreCase = True
End With
With regexFile
.Pattern = "^python-(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:([a-z]+)(\d*))?([\.-]amd64)?([\.-]arm64)?(-webinstall)?\.(exe|msi)$"
.Global = True
.IgnoreCase = True
End With
With regexJsonUrl
' example for graalpy: graalpy-24.0.1-windows-amd64.zip
' example for pypy: pypy3.7-v7.3.4-win64.zip
.Pattern = "download_url"": ?""(https://[^\s""]+/(((?:pypy\d+\.\d+-v|graalpy-)(\d+)(?:\.(\d+))?(?:\.(\d+))?-(win64|windows-amd64)?(windows-aarch64)?).zip))"""
.Global = True
.IgnoreCase = True
End With
' Adding -win32 as a post fix for x86 Arch
Function JoinWin32String(pieces)
' WScript.echo "kkotari: pyenv-install-lib.vbs JoinWin32String..!"
JoinWin32String = ""
If Len(pieces(VRX_Major)) Then JoinWin32String = JoinWin32String & pieces(VRX_Major)
If Len(pieces(VRX_Minor)) Then JoinWin32String = JoinWin32String &"."& pieces(VRX_Minor)
If Len(pieces(VRX_Patch)) Then JoinWin32String = JoinWin32String &"."& pieces(VRX_Patch)
If Len(pieces(VRX_Release)) Then JoinWin32String = JoinWin32String & pieces(VRX_Release)
If Len(pieces(VRX_RelNumber)) Then JoinWin32String = JoinWin32String & pieces(VRX_RelNumber)
If Len(pieces(VRX_ARM)) Then
JoinWin32String = JoinWin32String & "-arm"
ElseIf Len(pieces(VRX_x64)) = 0 Then
JoinWin32String = JoinWin32String & "-win32"
End If
End Function
' For x64 Arch
Function JoinInstallString(pieces)
' WScript.echo "kkotari: pyenv-install-lib.vbs JoinInstallString..!"
JoinInstallString = ""
If Len(pieces(VRX_Major)) Then JoinInstallString = JoinInstallString & pieces(VRX_Major)
If Len(pieces(VRX_Minor)) Then JoinInstallString = JoinInstallString &"."& pieces(VRX_Minor)
If Len(pieces(VRX_Patch)) Then JoinInstallString = JoinInstallString &"."& pieces(VRX_Patch)
If Len(pieces(VRX_Release)) Then JoinInstallString = JoinInstallString & pieces(VRX_Release)
If Len(pieces(VRX_RelNumber)) Then JoinInstallString = JoinInstallString & pieces(VRX_RelNumber)
If Len(pieces(VRX_x64)) Then JoinInstallString = JoinInstallString & pieces(VRX_x64)
If Len(pieces(VRX_ARM)) Then JoinInstallString = JoinInstallString & pieces(VRX_ARM)
If Len(pieces(VRX_Web)) Then JoinInstallString = JoinInstallString & pieces(VRX_Web)
If Len(pieces(VRX_Ext)) Then JoinInstallString = JoinInstallString &"."& pieces(VRX_Ext)
End Function
' Download exe file
Function DownloadFile(strUrl, strFile)
' WScript.echo "kkotari: pyenv-install-lib.vbs DownloadFile..!"
On Error Resume Next
objweb.Open "GET", strUrl, False
If Err.Number <> 0 Then
WScript.Echo ":: [ERROR] :: "& Err.Description
WScript.Quit 1
End If
objweb.Send
If Err.Number <> 0 Then
WScript.Echo ":: [ERROR] :: "& Err.Description
WScript.Quit 1
End If
On Error GoTo 0
If objweb.Status <> 200 Then
WScript.Echo ":: [ERROR] :: "& objweb.Status &" :: "& objweb.StatusText
WScript.Quit 1
End If
With CreateObject("ADODB.Stream")
.Open
.Type = 1
.Write objweb.responseBody
.SaveToFile strFile, 2
.Close
End With
End Function
Sub clear(params)
' WScript.echo "kkotari: pyenv-install-lib.vbs clear..!"
If objfs.FolderExists(params(IP_InstallPath)) Then _
objfs.DeleteFolder params(IP_InstallPath), True
If objfs.FileExists(params(IP_InstallFile)) Then _
objfs.DeleteFile params(IP_InstallFile), True
End Sub
' pyenv python versions DB scheme
Dim strDBSchema
' WScript.echo "kkotari: pyenv-install-lib.vbs DBSchema..!"
strDBSchema = _
"<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"">"& _
"<xs:element name=""versions"">"& _
"<xs:complexType>"& _
"<xs:sequence>"& _
"<xs:element name=""version"" maxOccurs=""unbounded"" minOccurs=""0"">"& _
"<xs:complexType>"& _
"<xs:sequence>"& _
"<xs:element name=""code"" type=""xs:string""/>"& _
"<xs:element name=""file"" type=""xs:string""/>"& _
"<xs:element name=""URL"" type=""xs:anyURI""/>"& _
"<xs:element name=""zipRootDir"" type=""xs:string"" minOccurs=""0"" maxOccurs=""1""/>"& _
"</xs:sequence>"& _
"<xs:attribute name=""x64"" type=""xs:boolean"" default=""false""/>"& _
"<xs:attribute name=""webInstall"" type=""xs:boolean"" default=""false""/>"& _
"<xs:attribute name=""msi"" type=""xs:boolean"" default=""true""/>"& _
"</xs:complexType>"& _
"</xs:element>"& _
"</xs:sequence>"& _
"</xs:complexType>"& _
"</xs:element>"& _
"</xs:schema>"
' Load versions xml to pyenv
Function LoadVersionsXML(xmlPath)
' WScript.echo "kkotari: pyenv-install-lib.vbs LoadVersionsXML..!"
Dim dbSchema
Dim doc
Dim schemaError
Set LoadVersionsXML = CreateObject("Scripting.Dictionary")
Set dbSchema = CreateObject("Msxml2.DOMDocument.6.0")
Set doc = CreateObject("Msxml2.DOMDocument.6.0")
If Not objfs.FileExists(xmlPath) Then Exit Function
With dbSchema
.validateOnParse = False
.resolveExternals = False
.loadXML strDBSchema
End With
With doc
Set .schemas = CreateObject("Msxml2.XMLSchemaCache.6.0")
.schemas.add "", dbSchema
.validateOnParse = False
.load xmlPath
Set schemaError = .validate
End With
With schemaError
If .errorCode <> 0 Then
WScript.Echo "Validation error in DB cache(0x"& Hex(.errorCode) & _
") on line "& .line &", pos "& .linepos &":"& vbCrLf & .reason
WScript.Quit 1
End If
End With
Dim versDict
Dim version
Dim code
Dim zipRootDirElement, zipRootDir
For Each version In doc.documentElement.childNodes
code = version.getElementsByTagName("code")(0).text
Set zipRootDirElement = version.getElementsByTagName("zipRootDir")
If zipRootDirElement.length = 1 Then
zipRootDir = zipRootDirElement(0).text
Else
zipRootDir = ""
End If
LoadVersionsXML.Item(code) = Array( _
code, _
version.getElementsByTagName("file")(0).text, _
version.getElementsByTagName("URL")(0).text, _
CBool(version.getAttribute("x64")), _
CBool(version.getAttribute("webInstall")), _
CBool(version.getAttribute("msi")), _
zipRootDir _
)
Next
End Function
' Append xml element
Sub AppendElement(doc, parent, tag, text)
' WScript.echo "kkotari: pyenv-install-lib.vbs AppendElement..!"
Dim elem
Set elem = doc.createElement(tag)
elem.text = text
parent.appendChild elem
End Sub
Function LocaleIndependantCStr(booleanVal)
If booleanVal Then
LocaleIndependantCStr = "true"
Else
LocaleIndependantCStr = "false"
End If
End Function
' Append new version to DB
Sub SaveVersionsXML(xmlPath, versArray)
' WScript.echo "kkotari: pyenv-install-lib.vbs SaveVersionsXML..!"
Dim doc
Set doc = CreateObject("Msxml2.DOMDocument.6.0")
Set doc.documentElement = doc.createElement("versions")
Dim versRow
Dim versElem
For Each versRow In versArray
Set versElem = doc.createElement("version")
doc.documentElement.appendChild versElem
With versElem
.setAttribute "x64", LocaleIndependantCStr(CBool(Len(versRow(SFV_Version)(VRX_x64)) OR Len(versRow(SFV_Version)(VRX_ARM))))
.setAttribute "webInstall", LocaleIndependantCStr(CBool(Len(versRow(SFV_Version)(VRX_Web))))
.setAttribute "msi", LocaleIndependantCStr(LCase(versRow(SFV_Version)(VRX_Ext)) = "msi")
End With
If versRow(SFV_Version)(VRX_Ext) = "zip" Then
AppendElement doc, versElem, "code", versRow(SFV_Version)(VRX_ZipRoot)
Else
AppendElement doc, versElem, "code", JoinWin32String(versRow(SFV_Version))
End If
AppendElement doc, versElem, "file", versRow(0)
AppendElement doc, versElem, "URL", versRow(1)
If versRow(SFV_Version)(VRX_Ext) = "zip" Then
AppendElement doc, versElem, "zipRootDir", versRow(SFV_Version)(VRX_ZipRoot)
End If
Next
' Use SAXXMLReader/MXXMLWriter to "pretty print" the XML data.
Dim writer
Dim parser
Dim outXML
Set writer = CreateObject("Msxml2.MXXMLWriter.6.0")
Set parser = CreateObject("Msxml2.SAXXMLReader.6.0")
Set outXML = CreateObject("ADODB.Stream")
With outXML
.Open
.Type = 1
End With
With writer
.encoding = "utf-8"
.indent = True
.output = outXML
End With
With parser
Set .contentHandler = writer
Set .dtdHandler = writer
Set .errorHandler = writer
.putProperty "http://xml.org/sax/properties/declaration-handler", writer
.putProperty "http://xml.org/sax/properties/lexical-handler", writer
.parse doc
End With
With outXML
.SaveToFile xmlpath, 2
.Close
End With
End Sub
' Test if ver1 < ver2
Function SymanticCompare(ver1, ver2)
Dim comp1, comp2
' Major
comp1 = ver1(VRX_Major)
comp2 = ver2(VRX_Major)
If Len(comp1) = 0 Then comp1 = 0: Else comp1 = CLng(comp1)
If Len(comp2) = 0 Then comp2 = 0: Else comp2 = CLng(comp2)
SymanticCompare = comp1 < comp2
If comp1 <> comp2 Then Exit Function
' Minor
comp1 = ver1(VRX_Minor)
comp2 = ver2(VRX_Minor)
If Len(comp1) = 0 Then comp1 = 0: Else comp1 = CLng(comp1)
If Len(comp2) = 0 Then comp2 = 0: Else comp2 = CLng(comp2)
SymanticCompare = comp1 < comp2
If comp1 <> comp2 Then Exit Function
' Patch
comp1 = ver1(VRX_Patch)
comp2 = ver2(VRX_Patch)
If Len(comp1) = 0 Then comp1 = 0: Else comp1 = CLng(comp1)
If Len(comp2) = 0 Then comp2 = 0: Else comp2 = CLng(comp2)
SymanticCompare = comp1 < comp2
If comp1 <> comp2 Then Exit Function
' Release
comp1 = ver1(VRX_Release)
comp2 = ver2(VRX_Release)
If Len(comp1) = 0 And Len(comp2) Then
SymanticCompare = False
Exit Function
ElseIf Len(comp1) And Len(comp2) = 0 Then
SymanticCompare = True
Exit Function
Else
SymanticCompare = comp1 < comp2
End If
If comp1 <> comp2 Then Exit Function
' Release Number
comp1 = ver1(VRX_RelNumber)
comp2 = ver2(VRX_RelNumber)
If Len(comp1) = 0 Then comp1 = 0: Else comp1 = CLng(comp1)
If Len(comp2) = 0 Then comp2 = 0: Else comp2 = CLng(comp2)
SymanticCompare = comp1 < comp2
If comp1 <> comp2 Then Exit Function
' x64
comp1 = ver1(VRX_x64)
comp2 = ver2(VRX_x64)
SymanticCompare = comp1 < comp2
If comp1 <> comp2 Then Exit Function
' ARM
comp1 = ver1(VRX_ARM)
comp2 = ver2(VRX_ARM)
SymanticCompare = comp1 < comp2
If comp1 <> comp2 Then Exit Function
' webinstall
comp1 = ver1(VRX_Web)
comp2 = ver2(VRX_Web)
SymanticCompare = comp1 < comp2
If comp1 <> comp2 Then Exit Function
' ext
comp1 = ver1(VRX_Ext)
comp2 = ver2(VRX_Ext)
SymanticCompare = comp1 < comp2
If comp1 <> comp2 Then Exit Function
End Function
' Modified from code by "Reverend Jim" at:
' https://www.daniweb.com/programming/code/515601/vbscript-implementation-of-quicksort
Sub SymanticQuickSort(arr, arrMin, arrMax)
Dim middle ' value of the element in the middle of the range
Dim swap ' temporary item for the swapping of two elements
Dim arrFrst ' index of the first element in the range to check
Dim arrLast ' index of the last element in the range to check
Dim arrMid ' index of the element in the middle of the range
If arrMax <= arrMin Then Exit Sub
' Start the checks at the lower and upper limits of the Array
arrFrst = arrMin
arrLast = arrMax
' Find the midpoint of the region to sort and the value of that element
arrMid = (arrMin + arrMax) \ 2
middle = arr(arrMid)
Do While (arrFrst <= arrLast)
' Find the first element > the element at the midpoint
Do While SymanticCompare(arr(arrFrst)(SFV_Version), middle(SFV_Version))
arrFrst = arrFrst + 1
If arrFrst = arrMax Then Exit Do
Loop
' Find the last element < the element at the midpoint
Do While SymanticCompare(middle(SFV_Version), arr(arrLast)(SFV_Version))
arrLast = arrLast - 1
If arrLast = arrMin Then Exit Do
Loop
' Pivot the two elements around the midpoint if they are out of order
If (arrFrst <= arrLast) Then
swap = arr(arrFrst)
arr(arrFrst) = arr(arrLast)
arr(arrLast) = swap
arrFrst = arrFrst + 1
arrLast = arrLast - 1
End If
Loop
' Sort sub-regions (recurse) if necessary
If arrMin < arrLast Then SymanticQuickSort arr, arrMin, arrLast
If arrFrst < arrMax Then SymanticQuickSort arr, arrFrst, arrMax
End Sub
Function JoinVersionString(pieces)
' WScript.echo "kkotari: pyenv-install-lib.vbs JoinVersionString..!"
JoinVersionString = ""
If Len(pieces(VRX_Major)) Then JoinVersionString = JoinVersionString & pieces(VRX_Major)
If Len(pieces(VRX_Minor)) Then JoinVersionString = JoinVersionString &"."& pieces(VRX_Minor)
If Len(pieces(VRX_Patch)) Then JoinVersionString = JoinVersionString &"."& pieces(VRX_Patch)
If Len(pieces(VRX_Release)) Then JoinVersionString = JoinVersionString & pieces(VRX_Release)
If Len(pieces(VRX_RelNumber)) Then JoinVersionString = JoinVersionString & pieces(VRX_RelNumber)
If Len(pieces(VRX_Arch)) Then JoinVersionString = JoinVersionString & pieces(VRX_Arch)
End Function
' Resolves latest python version by given prefix
' known=False to find latest _installed_ version
' See `pyenv latest --help`
Function FindLatestVersion(prefix, known)
Dim candidates
if known Then
Dim cachedVersions
Set cachedVersions = LoadVersionsXML(strDBFile)
Dim cachedVersion
Dim convertor()
ReDim Preserve convertor(-1)
For Each cachedVersion In cachedVersions.Keys
ReDim Preserve convertor(UBound(convertor) + 1)
convertor(UBound(convertor)) = cachedVersion
Next
candidates = convertor
else
candidates = GetInstalledVersions()
end if
Dim x
Dim matches
Dim bestMatch
Dim arch
arch = GetArchPostfix()
For x = 0 To UBound(candidates) Step 1
' startswith
If Left(candidates(x), Len(prefix)) = prefix Then
' Full match OR prefix plus '.'
If candidates(x) = prefix & arch Or Mid(candidates(x), Len(prefix) + 1, 1) = "." Then
Set matches = regexVerArch.Execute(candidates(x))
if matches.Count = 1 Then
' Skip dev builds, releases and so on
' Comparing each version by <major>.<minor>.<patch>
If matches(0).SubMatches(VRX_Release) = "" And matches(0).SubMatches(VRX_Arch) = arch Then
If IsEmpty(bestMatch) Then
Set bestMatch = matches(0).SubMatches
Else
If SymanticCompare(bestMatch, matches(0).SubMatches) Then
Set bestMatch = matches(0).SubMatches
End If
End If
End If
End If
End If
End If
Next
if IsEmpty(bestMatch) Then
FindLatestVersion = ""
else
FindLatestVersion = JoinVersionString(bestMatch)
end if
End Function
Function TryResolveVersion(prefix, known)
Dim resolved
resolved = FindLatestVersion(prefix, known)
If resolved = "" Then resolved = prefix
TryResolveVersion = resolved
End Function
|