|
@@ -1,10 +1,3 @@
|
|
|
-#!/usr/bin/env tclsh
|
|
|
|
|
-# file github/github.tcl
|
|
|
|
|
-#https://wiki.tcl-lang.org/page/github%3A%3Agithub
|
|
|
|
|
-#
|
|
|
|
|
-#Version 1.1 - added a timer in seconds between files & folders
|
|
|
|
|
-
|
|
|
|
|
-# chicken and egg problem we need non-standard packages tls and json ...
|
|
|
|
|
package require tls
|
|
package require tls
|
|
|
package require http
|
|
package require http
|
|
|
::http::register https 443 ::tls::socket
|
|
::http::register https 443 ::tls::socket
|
|
@@ -14,85 +7,85 @@ namespace eval ::github {
|
|
|
if {[lsearch $::auto_path $libdir] == -1} {
|
|
if {[lsearch $::auto_path $libdir] == -1} {
|
|
|
lappend auto_path $libdir
|
|
lappend auto_path $libdir
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
-# I already placed the json folder below of the github folder
|
|
|
|
|
-package require json
|
|
|
|
|
-package provide github::github 0.2
|
|
|
|
|
-package provide github 0.2
|
|
|
|
|
|
|
+package provide github::github 0.4
|
|
|
|
|
+package provide github 0.4
|
|
|
|
|
|
|
|
-# Tcl package download
|
|
|
|
|
-proc ::github::github {cmd owner repo folder} {
|
|
|
|
|
|
|
+# Main procedure to download GitHub repository as a .tar.gz archive
|
|
|
|
|
+proc ::github::github {cmd owner repo folder {branch "BlackTools-3.0"}} {
|
|
|
variable libdir
|
|
variable libdir
|
|
|
- set url https://api.github.com/repos/$owner/$repo/contents/
|
|
|
|
|
- download $url $folder
|
|
|
|
|
|
|
+ set url https://api.github.com/repos/$owner/$repo/tarball/$branch
|
|
|
|
|
+ download_archive $url $folder
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-# Folder download
|
|
|
|
|
-proc ::github::download {url folder {debug true}} {
|
|
|
|
|
|
|
+# Background download with wget
|
|
|
|
|
+proc ::github::download_archive {url folder} {
|
|
|
|
|
+ # Create the folder if it doesn't exist
|
|
|
if {![file exists $folder]} {
|
|
if {![file exists $folder]} {
|
|
|
file mkdir $folder
|
|
file mkdir $folder
|
|
|
}
|
|
}
|
|
|
- set sfiles ""
|
|
|
|
|
- set dfiles ""
|
|
|
|
|
- set data [http::data [http::geturl $url]]
|
|
|
|
|
- set d [json::json2dict $data]
|
|
|
|
|
- set l [llength $d]
|
|
|
|
|
- set files [list]
|
|
|
|
|
-for {set i 0} {$i < $l} {incr i 1} {
|
|
|
|
|
- set dic [dict create {*}[lindex $d $i]]
|
|
|
|
|
- set file [dict get $dic download_url]
|
|
|
|
|
- set type [dict get $dic type]
|
|
|
|
|
-if {$file eq "null" && $type eq "dir"} {
|
|
|
|
|
- set file [dict get $dic url]
|
|
|
|
|
- set file [regsub {.ref=master} $file ""]
|
|
|
|
|
-}
|
|
|
|
|
-if {$type eq "file"} {
|
|
|
|
|
- lappend sfiles $file
|
|
|
|
|
-} else {
|
|
|
|
|
- lappend dfiles $file
|
|
|
|
|
|
|
+
|
|
|
|
|
+ # Define the path for the downloaded archive
|
|
|
|
|
+ set archive_path [file join $folder "bt.tar.gz"]
|
|
|
|
|
+
|
|
|
|
|
+ # Run the wget command in the background
|
|
|
|
|
+ set command "curl -L -o $archive_path $url &"
|
|
|
|
|
+
|
|
|
|
|
+ if {[catch {exec {*}$command} errMsg]} {
|
|
|
|
|
+ putlog "\[BT\] - AutoUpdate - Error: Failed to start download $url in background"
|
|
|
|
|
+ putlog "\[BT\] - AutoUpdate - Details: $errMsg"
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ # Check periodically if download is complete
|
|
|
|
|
+ utimer 5 [list ::github::check_download_complete $archive_path $folder]
|
|
|
}
|
|
}
|
|
|
-if {$sfiles != ""} {
|
|
|
|
|
- files $sfiles $folder 0 $dfiles
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
-if {$dfiles != ""} {
|
|
|
|
|
- dirs $dfiles $folder 0
|
|
|
|
|
|
|
+
|
|
|
|
|
+# Periodically check if the download is complete
|
|
|
|
|
+proc ::github::check_download_complete {archive_path folder} {
|
|
|
|
|
+ # If the download is complete, proceed with extraction
|
|
|
|
|
+ if {[file exists $archive_path] && [file size $archive_path] > 0} {
|
|
|
|
|
+ if {[catch {::github::unpack_archive $archive_path $folder} errMsg]} {
|
|
|
|
|
+ putlog "\[BT\] - AutoUpdate - Error: Failed to unpack archive $archive_path"
|
|
|
|
|
+ putlog "\[BT\] - AutoUpdate - Details: $errMsg"
|
|
|
|
|
+ } else {
|
|
|
|
|
+ putlog "\[BT\] - AutoUpdate - Successfully unpacked archive to $folder"
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ # Clean up the archive file after extraction
|
|
|
|
|
+ file delete $archive_path
|
|
|
|
|
+ } else {
|
|
|
|
|
+ # Re-check in 5 seconds if download isn't complete
|
|
|
|
|
+ utimer 5 [list ::github::check_download_complete $archive_path $folder]
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-# Folders make
|
|
|
|
|
-proc ::github::dirs {dirs dir num} {
|
|
|
|
|
- set file [lindex $dirs $num]
|
|
|
|
|
- set nfolder [file join $dir [file tail $file]]
|
|
|
|
|
- download $file $nfolder
|
|
|
|
|
- set counter [expr $num + 1]
|
|
|
|
|
-if {[lindex $dirs $counter] != ""} {
|
|
|
|
|
- utimer 2 [list ::github::dirs $dirs $dir $counter]
|
|
|
|
|
|
|
+# Unpack the .tar.gz archive into the specified folder
|
|
|
|
|
+proc ::github::unpack_archive {archive_path folder} {
|
|
|
|
|
+ # Define a temporary folder for unpacking
|
|
|
|
|
+ set temp_folder [file join $folder "temp_unpack"]
|
|
|
|
|
+
|
|
|
|
|
+ # Ensure the temporary folder exists
|
|
|
|
|
+ if {![file exists $temp_folder]} {
|
|
|
|
|
+ file mkdir $temp_folder
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
-# Files make
|
|
|
|
|
-proc ::github::files {files dir num dirs} {
|
|
|
|
|
- set item [lindex $files $num]
|
|
|
|
|
- set file [lindex $item 0]
|
|
|
|
|
- set fname [file tail $file]
|
|
|
|
|
- set fname [file join $dir $fname]
|
|
|
|
|
- set f [open $fname w]
|
|
|
|
|
- fconfigure $f -translation binary
|
|
|
|
|
- set tok [http::geturl $file -channel $f]
|
|
|
|
|
- set Stat [::http::status $tok]
|
|
|
|
|
- flush $f
|
|
|
|
|
- close $f
|
|
|
|
|
- http::cleanup $tok
|
|
|
|
|
- set counter [expr $num + 1]
|
|
|
|
|
-if {[lindex $files $counter] != ""} {
|
|
|
|
|
- utimer 2 [list ::github::files $files $dir $counter $dirs]
|
|
|
|
|
- } else {
|
|
|
|
|
-if {$dirs != ""} {
|
|
|
|
|
- dirs $dirs $dir 0
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ # Extract the tarball into the temporary folder
|
|
|
|
|
+ if {[catch {exec tar -xzf $archive_path -C $temp_folder} errMsg]} {
|
|
|
|
|
+ putlog "\[BT\] - AutoUpdate - Error: Failed to extract $archive_path"
|
|
|
|
|
+ putlog "\[BT\] - AutoUpdate - Details: $errMsg"
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
|
|
+ # Locate the first directory inside temp_unpack (e.g., "owner-repo-branch")
|
|
|
|
|
+ set extracted_dir [lindex [glob -directory $temp_folder *] 0]
|
|
|
|
|
+
|
|
|
|
|
+ # Move contents from the extracted directory to the target folder
|
|
|
|
|
+ foreach item [glob -directory $extracted_dir *] {
|
|
|
|
|
+ file rename -force $item $folder/
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ # Clean up the temporary folder and extracted directory
|
|
|
|
|
+ file delete -force $temp_folder
|
|
|
|
|
+}
|