-
Notifications
You must be signed in to change notification settings - Fork 17
/
deken-plugin.tcl
3176 lines (2844 loc) · 121 KB
/
deken-plugin.tcl
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
# META NAME PdExternalsSearch
# META DESCRIPTION Search for externals zipfiles on puredata.info
# META AUTHOR <Chris McCormick> [email protected]
# META AUTHOR <IOhannes m zmölnig> [email protected]
# ex: set setl sw=2 sts=2 et
# Search URL:
# http://deken.puredata.info/search?name=foobar
# TODOs
## + open embedded README
## - open README on homepage (aka "More info...")
## + remove library before unzipping
## + only show valid arch
## - only show most recent version (of each arch)
## - check whether the "cd" thing during unzip works on w32 and multiple drives
## - redirect ::deken::post to ::pdwindow::post (that is: use the results pane only for results)
## + make the "add to path" thingy configurable
# The minimum version of TCL that allows the plugin to run
package require Tcl 8.4 9
# If Tk or Ttk is needed
#package require Ttk
# Any elements of the Pd GUI that are required
# + require everything and all your script needs.
# If a requirement is missing,
# Pd will load, but the script will not.
package require http 2
# try enabling https if possible
if { [catch {package require tls} ] } {} else {
::tls::init -ssl2 false -ssl3 false -tls1 true
::http::register https 443 ::tls::socket
}
# try enabling PROXY support if possible
if { [catch {package require autoproxy} ] } {} else {
::autoproxy::init
if { ! [catch {package present tls} stdout] } {
::http::register https 443 ::autoproxy::tls_socket
}
}
package require pdwindow 0.1
package require pd_menucommands 0.1
package require pd_guiprefs
namespace eval ::deken:: {
variable version
variable installpath
variable userplatform
variable hideforeignarch
variable hideoldversions
# whether to use http:// or https://
variable protocol
# results: {{title} {cmd} {description} {url} {ctxmenu}}
variable results
# selected: {library} {cmd} ...
variable selected {}
}
namespace eval ::deken::preferences {
variable installpath
variable userinstallpath
# automatically detected platform
variable platform
# user specified platform
variable userplatform
# boolean whether non-matching archs should be hidden
variable hideforeignarch
variable hideoldversions
}
namespace eval ::deken::utilities { }
## only register this plugin if there isn't any newer version already registered
## (if ::deken::version is defined and is higher than our own version)
proc ::deken::versioncheck {version} {
if { [info exists ::deken::version ] } {
set v0 [split ${::deken::version} "."]
set v1 [split ${version} "."]
foreach x ${v0} y ${v1} {
if { ${x} > ${y} } {
set msg [format [_ "\[deken\] installed version \[%1\$s\] > %2\$s...skipping!" ] ${::deken::version} ${version} ]
::pdwindow::debug "${msg}\n"
return 0
}
if { ${x} < ${y} } {
set msg [format [_ "\[deken\] installed version \[%1\$s\] < %2\$s...overwriting!" ] ${::deken::version} ${version} ]
::pdwindow::debug "${msg}\n"
set ::deken::version ${version}
return 1
}
}
set msg [format [_ "\[deken\] installed version \[%1\$s\] == %2\$s...skipping!" ] ${::deken::version} ${version} ]
::pdwindow::debug "${msg}\n"
return 0
}
set ::deken::version ${version}
return 1
}
## put the current version of this package here:
if { [::deken::versioncheck 0.10.2] } {
namespace eval ::deken:: {
namespace export open_searchui
variable winid .externals_searchui
variable resultsid ${winid}.results
variable infoid ${winid}.results
variable platform
variable architecture_substitutes
variable installpath
variable statustext
variable statustimer
variable backends
variable progressvar
variable progresstext
variable progresstimer
namespace export register
}
namespace eval ::deken::search:: { }
## FIXXXXME only initialize vars if not yet set
set ::deken::backends {}
set ::deken::installpath {}
set ::deken::userplatform {}
set ::deken::hideforeignarch false
set ::deken::hideoldversions false
set ::deken::show_readme 1
set ::deken::remove_on_install 1
set ::deken::add_to_path 0
set ::deken::keep_package 0
set ::deken::verify_sha256 1
set ::deken::searchtype name
set ::deken::statustimer {}
set ::deken::progresstimer {}
set ::deken::preferences::installpath {}
set ::deken::preferences::userinstallpath {}
set ::deken::preferences::platform {}
set ::deken::preferences::userplatform {}
set ::deken::preferences::hideforeignarch {}
set ::deken::preferences::hideoldversions {}
set ::deken::preferences::show_readme {}
set ::deken::preferences::remove_on_install {}
set ::deken::preferences::add_to_path {}
set ::deken::preferences::add_to_path_temp {}
set ::deken::preferences::keep_package {}
set ::deken::preferences::verify_sha256 {}
set ::deken::platform(os) ${::tcl_platform(os)}
set ::deken::platform(machine) [string tolower ${::tcl_platform(machine)}]
set ::deken::platform(bits) [ expr {[ string length [ format %X -1 ] ] * 4} ]
set ::deken::platform(floatsize) 32
# architectures that can be substituted for each other
array set ::deken::architecture_substitutes {}
set ::deken::architecture_substitutes(x86_64) [list "amd64" ]
set ::deken::architecture_substitutes(amd64) [list "x86_64" ]
set ::deken::architecture_substitutes(i686) [list "i586" "i386"]
set ::deken::architecture_substitutes(i586) [list "i386"]
set ::deken::architecture_substitutes(armv6) [list "armv6l" "arm"]
set ::deken::architecture_substitutes(armv6l) [list "armv6" "arm"]
set ::deken::architecture_substitutes(armv7) [list "armv7l" "armv6l" "armv6" "arm"]
set ::deken::architecture_substitutes(armv7l) [list "armv7" "armv6l" "armv6" "arm"]
set ::deken::architecture_substitutes(powerpc) [list "ppc"]
set ::deken::architecture_substitutes(ppc) [list "powerpc"]
set ::deken::architecture_normalize(x86_64) "amd64"
set ::deken::architecture_normalize(i686) "i386"
set ::deken::architecture_normalize(i586) "i386"
set ::deken::architecture_normalize(i486) "i386"
set ::deken::architecture_normalize(armv6l) "armv6"
set ::deken::architecture_normalize(armv7l) "armv7"
set ::deken::architecture_normalize(powerpc) "ppc"
# normalize W32 OSs
if { [ string match "Windows *" "${::deken::platform(os)}" ] > 0 } {
# we are not interested in the w32 flavour, so we just use 'Windows' for all of them
set ::deken::platform(os) "Windows"
}
# normalize W32 CPUs
if { "Windows" eq "${::deken::platform(os)}" } {
# in redmond, intel only produces 32bit CPUs,...
if { "intel" eq "${::deken::platform(machine)}" } { set ::deken::platform(machine) "i686" }
# ... and all 64bit CPUs are manufactured by amd
#if { "amd64" eq "$::deken::platform(machine)" } { set ::deken::platform(machine) "x86_64" }
}
catch {
set ::deken::platform(machine) $::deken::architecture_normalize($::deken::platform(machine))
}
set ::deken::protocol "http"
if { ! [catch {package present tls} stdout] } {
set ::deken::protocol "https"
}
# ######################################################################
# ################ compatibility #######################################
# ######################################################################
# list-reverter (compat for tcl<8.5)
if {[info commands lreverse] == ""} {
proc lreverse list {
set res {}
set i [llength ${list}]
while {$i} {
lappend res [lindex ${list} [incr i -1]]
}
set res
} ;# RS
}
# ######################################################################
# ################ utilities ##########################################
# ######################################################################
proc ::deken::utilities::bool {value {fallback 0}} {
catch {set fallback [expr {bool(${value})} ] } stdout
return ${fallback}
}
proc ::deken::utilities::int {value {fallback 0}} {
catch {set fallback [expr {int(${value})} ] } stdout
return ${fallback}
}
proc ::deken::utilities::tristate {value {offset 0} {fallback 0} } {
catch {set fallback [expr {(int(${value}) + int(${offset}))% 3} ]} stdout
return ${fallback}
}
proc ::deken::utilities::expandpath {path} {
set map "@PD_PATH@"
lappend map ${::sys_libdir}
string map ${map} ${path}
}
proc ::deken::utilities::get_tmpfilename {{path ""} {ext ""} {prefix dekentmp}} {
for {set i 0} {true} {incr i} {
set tmpfile [file join ${path} ${prefix}.${i}${ext}]
if {![file exists ${tmpfile}]} {
return ${tmpfile}
}
}
}
proc ::deken::utilities::get_tmpdir {} {
proc _iswdir {d} { return [expr {[file isdirectory ${d}] * [file writable ${d}]}] }
set tmpdir ""
# TRASH_FOLDER: very old Macintosh. Mac OS X doesn't have this.
# TMPDIR: unices
# TMP, TEMP: windows
# TEPMDIR: for symmetry :-)
foreach {d} {TRASH_FOLDER TMPDIR TEMPDIR TEMP TMP} {
if { [info exists ::env(${d}) ] } {
set tmpdir $::env(${d})
if {[_iswdir ${tmpdir}]} {return ${tmpdir}}
}
}
set tmpdir "/tmp"
if {[_iswdir ${tmpdir}]} {return ${tmpdir}}
set tmpdir [pwd]
if {[_iswdir ${tmpdir}]} {return ${tmpdir}}
}
proc ::deken::utilities::is_writabledir {path} {
set fs [file separator]
set access [list RDWR CREAT EXCL TRUNC]
set tmpfile [::deken::utilities::get_tmpfilename ${path}]
# try creating tmpfile
if {![catch {open ${tmpfile} ${access}} channel]} {
close ${channel}
file delete ${tmpfile}
return true
}
return false
}
proc ::deken::utilities::get_writabledir {paths} {
foreach p ${paths} {
set xp [ ::deken::utilities::expandpath ${p} ]
if { [ ::deken::utilities::is_writabledir ${xp} ] } { return ${p} }
}
return
}
proc ::deken::utilities::rmrecursive {path} {
# recursively remove ${path} if it exists, traversing into each directory
# to delete single items (rather than just unlinking the parent directory)
set errors 0
set myname [lindex [info level 0] 0]
set children [glob -nocomplain -directory ${path} -types hidden *]
lappend children {*}[glob -nocomplain -directory ${path} *]
foreach child $children[set children {}] {
if {[file tail ${child}] in {. ..}} {
continue
}
if {[file isdirectory ${child}]} {
if {[file type ${child}] ne "link"} {
incr errors [${myname} ${child}]
}
}
if { [ catch { file delete -force ${child} } ] } {
incr errors
}
}
return ${errors}
}
# http://rosettacode.org/wiki/URL_decoding#Tcl
proc ::deken::utilities::urldecode {str} {
set specialMap {"[" "%5B" "]" "%5D"}
set seqRE {%([0-9a-fA-F]{2})}
set replacement {[format "%c" [scan "\1" "%2x"]]}
set modStr [regsub -all ${seqRE} [string map ${specialMap} ${str}] ${replacement}]
encoding convertfrom utf-8 [subst -nobackslashes -novariables ${modStr}]
}
proc ::deken::utilities::verbose {level message} {
::pdwindow::verbose ${level} "\[deken\] ${message}\n"
}
proc ::deken::utilities::debug {message} {
set winid ${::deken::winid}
if {[winfo exists ${winid}.tab.info]} {
::deken::post ${message} debug
} else {
::pdwindow::debug "\[deken\] ${message}\n"
}
}
if { [catch {package require tkdnd} ] } {
proc ::deken::utilities::dnd_init {windowid} { }
} else {
proc ::deken::utilities::dnd_init {windowid} {
::tkdnd::drop_target register ${windowid} DND_Files
bind ${windowid} <<Drop:DND_Files>> {::deken::utilities::dnd_drop_files %D}
}
proc ::deken::utilities::dnd_drop_files {files} {
foreach f ${files} {
if { [regexp -all -nocase "\.(zip|dek|tgz|tar\.gz)$" ${f} ] } {
set msg [format [_ "installing deken package '%s'" ] ${f}]
::deken::statuspost ${msg}
::deken::install_package_from_file ${f}
} else {
set msg [format [_ "ignoring '%s': doesn't look like a deken package" ] ${f}]
::deken::statuspost ${msg}
}
}
return "link"
}
}
namespace eval ::deken::utilities::unzipper:: {
# we put a number of unzip methods in this namespace.
# they are tried in in alphabetical(!) order
# zipfs from Tcl>=8.7 (it seems this only works with Tcl>=9.0)
catch {
zipfs root
proc builtin_zipfs {zipfile path} {
if { [catch {
set base [file join [zipfs root] deken]
if { [catch {
package require Tcl 8
# yikes! Tcl8.7 uses 'zipfs mount <mountpoint> <zipfile>'
zipfs mount ${base} ${zipfile}
} ] } {
# and Tcl9 uses 'zipfs mount <zipfile> <mountpoint>'
zipfs mount ${zipfile} ${base}
}
foreach x [glob [file join ${base} *]] {
file copy -force -- ${x} ${path}
}
zipfs unmount ${base}
} stdout ] } {
::deken::utilities::debug "unzipper::zipfs: ${stdout}"
return 0
}
return 1
}
}
# ::zipfile::decode from tcllib
catch {
package require zipfile::decode
proc tcllib_zipfile {zipfile path} {
if { [catch {
::zipfile::decode::unzipfile "${zipfile}" "${path}"
} stdout ] } {
::deken::utilities::debug "unzipper::zipfile::decode: ${stdout}"
return 0
}
return 1
}
}
proc untar {zipfile path} {
if { [catch {
exec tar -xf "${zipfile}" -C "${path}"
} stdout ] } {
::deken::utilities::debug "unzipper::tar ${stdout}"
return 0
}
return 1
}
proc unzip {zipfile path} {
set result 0
if { [catch {
exec unzip -uo "${zipfile}" -d "${path}"
set result 1
} stdout ] } {
::deken::utilities::debug "unzipper::unzip ${stdout}"
}
return ${result}
}
if {$::tcl_platform(platform) eq "windows"} {
## VisualBasic is w32 only
proc windows_visualbasic {zipfile path} {
## create script-file
set vbsscript [::deken::utilities::get_tmpfilename [::deken::utilities::get_tmpdir] ".vbs" ]
set script {
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
'The location of the zip file.
ZipFile = fso.GetAbsolutePathName(WScript.Arguments.Item(0))
'The folder the contents should be extracted to.
ExtractTo = fso.GetAbsolutePathName(WScript.Arguments.Item(1))
'If the extraction location does not exist create it.
If NOT fso.FolderExists(ExtractTo) Then
fso.CreateFolder(ExtractTo)
End If
'Extract the contents of the zip file.
set objShell = CreateObject("Shell.Application")
set FilesInZip=objShell.NameSpace(ZipFile).items
objShell.NameSpace(ExtractTo).CopyHere(FilesInZip)
'In case of an error, exit
If Err.Number <> 0 Then
Err.Clear
WScript.Quit 1
End If
Set fso = Nothing
Set objShell = Nothing
}
if {![catch {set fileId [open ${vbsscript} "w"]}]} {
puts ${fileId} ${script}
close ${fileId}
}
if {![file exists ${vbsscript}]} {
## still no script, give up
return 0
}
## try to call the script
## (and windows requires the file to have a .zip extension!!!)
if { [ catch {
set zipfilezip ${zipfile}.zip
file rename ${zipfile} ${zipfilezip}
exec cscript "${vbsscript}" "${zipfilezip}" .
file rename ${zipfilezip} ${zipfile}
} stdout ] } {
catch { file rename ${zipfilezip} ${zipfile} }
catch { file delete "${vbsscript}" }
::deken::utilities::debug "unzipper::VBS-unzip(${vbsscript}): ${stdout}"
return 0
}
catch { file delete "${vbsscript}" }
return 1
}
}
}
proc ::deken::utilities::unzipper {zipfile {path .}} {
set unzippers [lsort -dictionary [info procs ::deken::utilities::unzipper::*]]
foreach unzip ${unzippers} {
set result 0
catch {
set result [ ${unzip} ${zipfile} ${path} ]
}
if {${result} ne 0} {
return 1
}
}
return 0
}
proc ::deken::utilities::extract {installdir filename fullpkgfile {keep_package 1}} {
if { ! [ file isdirectory "${installdir}" ] } {
return 0
}
::deken::statuspost [format [_ "Installing '%s'" ] ${filename} ] debug
set PWD [ pwd ]
cd ${installdir}
set success 1
if { [ string match *.dek ${fullpkgfile} ] } then {
if { ! [ ::deken::utilities::unzipper ${fullpkgfile} ${installdir} ] } {
if { [ catch { exec unzip -uo ${fullpkgfile} } stdout ] } {
::deken::utilities::debug "${stdout}"
set success 0
}
}
} elseif { [ string match *.zip ${fullpkgfile} ] } then {
if { ! [ ::deken::utilities::unzipper ${fullpkgfile} ${installdir} ] } {
if { [ catch { exec unzip -uo ${fullpkgfile} } stdout ] } {
::deken::utilities::debug "${stdout}"
set success 0
}
}
} elseif { [ string match *.tar.* ${fullpkgfile} ]
|| [ string match *.tgz ${fullpkgfile} ]
} then {
if { [ catch { exec tar xf ${fullpkgfile} } stdout ] } {
::deken::utilities::debug "${stdout}"
set success 0
}
}
cd ${PWD}
if { ${success} > 0 } {
::deken::post [format [_ "Successfully unzipped %1\$s into %2\$s."] ${filename} ${installdir} ] debug
if { ! "${keep_package}" } {
catch { file delete ${fullpkgfile} }
}
} else {
# Open both the fullpkgfile folder and the zipfile itself
# NOTE: in tcl 8.6 it should be possible to use the zlib interface to actually do the unzip
set msg [_ "Unable to extract package automatically." ]
::deken::post "${msg}" warn
::pdwindow::error "${msg}\n"
set msg ""
append msg [_ "Please perform the following steps manually:" ]
append msg "\n"
append msg [format [_ "1. Unzip %s." ] ${fullpkgfile} ]
append msg "\n"
if { [string match "*.dek" ${fullpkgfile}] } {
append msg " "
append msg [_ "You might need to change the file-extension from .dek to .zip" ]
append msg "\n"
}
append msg [format [_ "2. Copy the contents into %s." ] ${installdir}]
append msg "\n"
append msg [format [_ "3. Remove %s. (optional)" ] ${fullpkgfile} ]
append msg "\n"
::deken::post "${msg}"
pd_menucommands::menu_openfile ${fullpkgfile}
pd_menucommands::menu_openfile ${installdir}
}
return ${success}
}
proc ::deken::utilities::uninstall {path library} {
# recursively remove ${path}/${library} if it exists
set fullpath [file join ${path} ${library}]
if {[file exists ${fullpath}]} {
::deken::post [format [_ "Removing '%s'" ] ${fullpath} ] debug
if { [catch {
file delete -force "${fullpath}"
} stdout ] } {
set msg [format [_ "Uninstalling %1\$s from %2\$s failed!"] ${library} ${path}]
::deken::utilities::debug "${msg}\n ${stdout}"
return 0
}
}
return 1
}
proc ::deken::utilities::sha256_sha256sum {filename} {
set hash {}
catch { set hash [lindex [exec sha256sum ${filename}] 0] }
return ${hash}
}
proc ::deken::utilities::sha256_shasum {filename} {
set hash {}
catch { set hash [lindex [exec shasum -a 256 ${filename}] 0] }
return ${hash}
}
proc ::deken::utilities::sha256_powershell {filename} {
set batscript [::deken::utilities::get_tmpfilename [::deken::utilities::get_tmpdir] ".bat" ]
set script {
@echo off
powershell -Command " & {Get-FileHash -Algorithm SHA256 -LiteralPath \"%1\" | Select-Object -ExpandProperty Hash}"
}
if {![catch {set fileId [open ${batscript} "w"]}]} {
puts ${fileId} ${script}
close ${fileId}
}
if {![file exists ${batscript}]} {
## still no script, give up
return ""
}
if { [ catch {
set hash [exec "${batscript}" "${filename}"]
} stdout ] } {
# ouch, couldn't run powershell script
::deken::utilities::verbose 1 "sha256.ps1 error: ${stdout}"
set hash ""
}
catch { file delete "${batscript}" }
return ${hash}
}
proc ::deken::utilities::sha256_msw {filename} {
set hash {}
catch {
regexp {([a-fA-F0-9]{64})} [exec certUtil -hashfile ${filename} SHA256] hash
}
return ${hash}
}
if { [catch {package require sha256} ] } { proc ::deken::utilities::sha256_tcllib {filename} {} } else {
proc ::deken::utilities::sha256_tcllib {filename} {
set hash {}
catch { set hash [::sha2::sha256 -hex -filename ${filename}] }
return ${hash}
}
}
proc ::deken::utilities::verify_sha256 {url pkgfile} {
set msg [format [_ "Skipping SHA256 verification of '%s'." ] ${url} ]
::deken::statuspost ${msg}
return -100
}
foreach impl {sha256sum shasum powershell msw tcllib} {
if { [::deken::utilities::sha256_${impl} ${::argv0}] ne "" } {
set ::deken::utilities::sha256_implementation ::deken::utilities::sha256_${impl}
proc ::deken::utilities::verify_sha256 {url pkgfile} {
::deken::statuspost [format [_ "SHA256 verification of '%s'" ] ${pkgfile} ] debug
::deken::syncgui
set retval 1
set isremote 1
set hashfile ""
# check if ${url} really is a local file
if { [file normalize ${url}] eq ${url} } {
# ${url} is really an absolute filename
# use it, if it exists
set hashfile "${url}.sha256"
set isremote 0
if { [file isfile ${hashfile} ] && [file readable ${hashfile}] } { } else {
set msg [format [_ "Unable to fetch reference SHA256 for '%s'." ] ${url} ]
::deken::utilities::verbose 0 ${msg}
::deken::statuspost ${msg} warn 0
return -10
}
} else {
# otherwise fetch it from the internet
if { [ catch {
set hashfile [::deken::utilities::download_file ${url}.sha256 [::deken::utilities::get_tmpfilename [::deken::utilities::get_tmpdir] ".sha256" ] ]
} stdout ] } {
::deken::utilities::verbose 0 "${stdout}"
# unable to download
set msg [format [_ "Unable to fetch reference SHA256 for '%s'." ] ${url} ]
::deken::utilities::verbose 0 ${msg}
::deken::statuspost ${msg} warn 0
return -10
}
}
if { "${hashfile}" eq "" } {
set retval -10
}
if { [ catch {
set fp [open ${hashfile} r]
set reference [string trim [string tolower [read ${fp}] ] ]
close ${fp}
if { ${isremote} } {
catch { file delete ${hashfile} }
}
# get hash of file
set hash [${::deken::utilities::sha256_implementation} ${pkgfile} ]
set hash [string trim [string tolower ${hash} ] ]
# check if hash is sane
if { [string length ${hash}] != 64 || ! [string is xdigit ${hash}] } {
::deken::statuspost [format [_ "File checksum looks invalid: '%s'." ] ${hash}] warn 0
}
# check if reference is sane
if { [string length ${reference}] != 64 || ! [string is xdigit ${reference}] } {
# this is more grave than the sanity check for the file hash
# (since for the file hash we depend on the user's machine being able to
# produce a proper SHA256 hash)
::deken::statuspost [format [_ "Reference checksum looks invalid: '%s'." ] ${reference}] error 0
}
if { [string first ${reference} ${hash}] >= 0 } {
set retval 1
} else {
# SHA256 verification failed...
set retval 0
}
} stdout ] } {
::deken::utilities::verbose 0 "${stdout}"
# unable to verify
set msg [format [_ "Unable to perform SHA256 verification for '%s'." ] ${url} ]
::deken::utilities::verbose 0 ${msg}
::deken::statuspost ${msg} warn 0
set retval -20
}
return ${retval}
}
# it seems we found a working sha256 implementation, don't try the other ones...
break
}
}
proc ::deken::utilities::httpuseragent {} {
set httpagent [::http::config -useragent]
set pdversion "Pd/${::PD_MAJOR_VERSION}.${::PD_MINOR_VERSION}.${::PD_BUGFIX_VERSION}${::PD_TEST_VERSION}"
set platformstring [::deken::platform2string]
set tclversion "Tcl/[info patchlevel]"
::http::config -useragent "Deken/${::deken::version} (${platformstring}) ${pdversion} ${tclversion}"
return ${httpagent}
}
# download a file to a location
# http://wiki.tcl.tk/15303
proc ::deken::utilities::download_file {url outputfilename {progressproc {}}} {
set URL [string map {{[} "%5b" {]} "%5d"} ${url}]
set downloadfilename [::deken::utilities::get_tmpfilename [file dirname ${outputfilename}] ]
set f [open ${downloadfilename} w]
fconfigure ${f} -translation binary
set httpagent [::deken::utilities::httpuseragent]
if { [catch {
if { ${progressproc} eq {} } {
set httpresult [::http::geturl ${URL} -binary true -channel ${f}]
} else {
set httpresult [::http::geturl ${URL} -binary true -progress ${progressproc} -channel ${f}]
}
set ncode [::http::ncode ${httpresult}]
if {${ncode} != 200} {
## FIXXME: we probably should handle redirects correctly (following them...)
set err [::http::code ${httpresult}]
set msg [format [_ "Unable to download from %1\$s \[%2\$s\]" ] ${url} ${err} ]
::deken::post "${msg}" debug
set outputfilename ""
}
::http::cleanup ${httpresult}
} stdout ] } {
set msg [format [_ "Unable to download from '%s'!" ] ${url} ]
tk_messageBox \
-title [_ "Download failed" ] \
-message "${msg}\n${stdout}" \
-icon error -type ok \
-parent ${::deken::winid}
set outputfilename ""
}
::http::config -useragent ${httpagent}
flush ${f}
close ${f}
if { "${outputfilename}" != "" } {
catch { file delete ${outputfilename} }
if {[file exists ${outputfilename}]} {
::deken::utilities::debug [format [_ "Unable to remove stray file '%s'" ] ${outputfilename} ]
set outputfilename ""
}
}
if { ${outputfilename} != "" && "${outputfilename}" != "${downloadfilename}" } {
if {[catch { file rename ${downloadfilename} ${outputfilename}}]} {
::deken::utilities::debug [format [_ "Unable to rename downloaded file to '%s'" ] ${outputfilename} ]
set outputfilename ""
}
}
if { "${outputfilename}" eq "" } {
file delete ${downloadfilename}
}
return ${outputfilename}
}
# parse a deken-packagefilename into it's components:
# v0:: <pkgname>[-v<version>-]?{(<arch>)}-externals.<ext>
# v1:: <pkgname>[\[<version\]]?{(<arch>)}
# return: list <pkgname> <version> [list <arch> ...]
proc ::deken::utilities::parse_filename {filename} {
set pkgname ${filename}
set archs [list]
set version ""
if { [ string match "*.dek" ${filename} ] } {
## deken filename v1: <library>[v<version>](<arch1>)(<arch2>).dek
set archstring ""
if { ! [regexp {^([^\[\]\(\)]+)((\[[^\[\]\(\)]+\])*)((\([^\[\]\(\)]+\))*).*\.dek$} ${filename} _ pkgname optionstring _ archstring] } {
# oops, somewhat unparseable (e.g. a copy)
} else {
foreach {o _} [lreplace [split ${optionstring} {[]}] 0 0] {
if {![string first v ${o}]} {
set version [string range ${o} 1 end]
} else { # ignoring unknown option...
return [list {} {} {}]
}
}
foreach {a _} [lreplace [split ${archstring} "()"] 0 0] { lappend archs ${a} }
}
} elseif { [ regexp {(.*)-externals\..*} ${filename} _ basename] } {
## deken filename v0
set pkgname ${basename}
# basename <pkgname>[-v<version>-]?{(<arch>)}
## strip off the archs
set baselist [split ${basename} () ]
# get pkgname + version
set pkgver [lindex ${baselist} 0]
if { ! [ regexp "(.*)-v(.*)-" ${pkgver} _ pkgname version ] } {
set pkgname ${pkgver}
set version ""
}
# get archs
foreach {a _} [lreplace ${baselist} 0 0] {
# in filename.v0 the semantics of the last arch field ("bits") was unclear
# since this format predates float64 builds, we just force it to 32
regsub -- {-[0-9]+$} ${a} {-32} a
lappend archs ${a}
}
if { "x${archs}${version}" == "x" } {
# try again as <pkgname>-v<version>
if { ! [ regexp "(.*)-v(.*)" ${pkgver} _ pkgname version ] } {
set pkgname ${pkgver}
set version ""
}
}
}
return [list ${pkgname} ${version} ${archs}]
}
# split filename extension from deken-packagefilename
proc ::deken::utilities::get_filenameextension {filename} {
if { [ regexp {.*(\.tar\.[^.]*)$} ${filename} _ ext ] } {
return ${ext}
}
return [file extension ${filename}]
}
# ######################################################################
# ################ preferences #########################################
# ######################################################################
proc ::deken::preferences::newwidget {basename} {
# calculate a widget name that has not yet been taken
set i 0
while {[winfo exists ${basename}${i}]} {incr i}
return ${basename}${i}
}
proc ::deken::preferences::create_pathpad {toplevel row {padx 2} {pady 2}} {
set pad [::deken::preferences::newwidget ${toplevel}.pad]
frame ${pad} -relief groove -borderwidth 2 -width 2 -height 2
grid ${pad} -sticky ew -row ${row} -column 0 -columnspan 3 -padx ${padx} -pady ${pady}
}
proc ::deken::preferences::create_packpad {toplevel {padx 2} {pady 2} } {
set mypad [::deken::preferences::newwidget ${toplevel}.pad]
frame ${mypad}
pack ${mypad} -padx ${padx} -pady ${pady} -expand 1 -fill "y"
return ${mypad}
}
proc ::deken::preferences::userpath_doit { winid } {
set installdir [::deken::do_prompt_installdir ${::deken::preferences::userinstallpath} ${winid}]
if { "${installdir}" != "" } {
set ::deken::preferences::userinstallpath "${installdir}"
}
}
proc ::deken::preferences::path_doit {rdb ckb path {mkdir true}} {
# handler for the check/create button
# if the path does not exist, disable the radiobutton and suggest to Create it
# if the path exists, check whether it is writable
# if it is writable, enable the radiobutton and disable the check/create button
# if it is not writable, keep the radiobutton disabled and suggest to (Re)Check
${ckb} configure -state normal
${rdb} configure -state disabled
if { ! [file exists ${path}] } {
${ckb} configure -text [_ "Create"]
if { ${mkdir} } {
catch { file mkdir ${path} }
}
}
if { [file exists ${path}] } {
${ckb} configure -text [_ "Check"]
}
if { [::deken::utilities::is_writabledir ${path} ] } {
${ckb} configure -state disabled
${rdb} configure -state normal
}
}
proc ::deken::preferences::create_pathentry {toplevel row var path {generic false}} {
# only add absolute paths to the pathentries
set xpath [ ::deken::utilities::expandpath ${path} ]
if {! ${generic}} {
if { [file pathtype ${xpath}] != "absolute"} { return }
}
set rdb [::deken::preferences::newwidget ${toplevel}.path]
set chk [::deken::preferences::newwidget ${toplevel}.doit]
set pad [::deken::preferences::newwidget ${toplevel}.pad]
radiobutton ${rdb} -value ${path} -text "${path}" -variable ${var}
frame ${pad}
button ${chk} -text "..." -command "::deken::preferences::path_doit ${rdb} ${chk} ${xpath}"
grid ${rdb} -sticky "w" -row ${row} -column 2
grid ${pad} -sticky "" -row ${row} -column 1 -padx 10
grid ${chk} -sticky nsew -row ${row} -column 0
if {! ${generic}} {
::deken::preferences::path_doit ${rdb} ${chk} ${xpath} false
}
return [list ${rdb} ${chk}]
}
proc ::deken::preferences::fill_frame {winid} {
::deken::preferences::create ${winid}
}
proc ::deken::preferences::create {winid} {
# urgh...we want to know when the window gets drawn,
# so we can query the size of the pathentries canvas
# in order to get the scrolling-region right!!!
# this seems to be so wrong...
bind ${winid} <Map> "::deken::preferences::mapped %W"
::deken::bind_globalshortcuts ${winid}
set ::deken::preferences::installpath ${::deken::installpath}
set ::deken::preferences::hideforeignarch ${::deken::hideforeignarch}
set ::deken::preferences::hideoldversions ${::deken::hideoldversions}
if { ${::deken::userplatform} == "" } {
set ::deken::preferences::platform DEFAULT
set ::deken::preferences::userplatform [ ::deken::platform2string ]
} else {
set ::deken::preferences::platform USER
set ::deken::preferences::userplatform ${::deken::userplatform}
}
set ::deken::preferences::installpath USER
set ::deken::preferences::userinstallpath ${::deken::installpath}
set ::deken::preferences::show_readme ${::deken::show_readme}
set ::deken::preferences::keep_package ${::deken::keep_package}
set ::deken::preferences::verify_sha256 ${::deken::verify_sha256}
set ::deken::preferences::remove_on_install ${::deken::remove_on_install}
set ::deken::preferences::add_to_path ${::deken::add_to_path}
set ::deken::preferences::add_to_path_temp ${::deken::preferences::add_to_path}
# this dialog allows us to select:
# - which directory to extract to
# - including all (writable) elements from ${::sys_staticpath}
# and option to create each of them
# - a directory chooser
# - whether to delete directories before re-extracting
# - whether to filter-out non-matching architectures
labelframe ${winid}.installdir -text [_ "Install externals to directory:" ] -padx 5 -pady 5 -borderwidth 1
canvas ${winid}.installdir.cnv \
-confine true
scrollbar ${winid}.installdir.scrollv \
-command "${winid}.installdir.cnv yview"
scrollbar ${winid}.installdir.scrollh \
-orient horizontal \
-command "${winid}.installdir.cnv xview"
${winid}.installdir.cnv configure \
-xscrollincrement 0 \
-xscrollcommand " ${winid}.installdir.scrollh set"
${winid}.installdir.cnv configure \
-yscrollincrement 0 \
-yscrollcommand " ${winid}.installdir.scrollv set" \
pack ${winid}.installdir.cnv -side left -fill both -expand 1
pack ${winid}.installdir.scrollv -side right -fill "y"
pack ${winid}.installdir.scrollh -side bottom -fill "x" -before ${winid}.installdir.cnv
pack ${winid}.installdir -fill both