~aleteoryx/tclircc

e10a5fa099523037ae8a6410ccaba505ac35021f — aleteoryx a month ago c1e9562
storing a copy of the SPDX DB for license rendering
3 files changed, 116 insertions(+), 0 deletions(-)

A spdx/dump.tcldict.gz
A spdx/gen.tcl
A spdx/lib.tcl
A spdx/dump.tcldict.gz => spdx/dump.tcldict.gz +0 -0
A spdx/gen.tcl => spdx/gen.tcl +58 -0
@@ 0,0 1,58 @@
#!/bin/env tclsh
# script to convert the spdx db to a dict

package require http
package require json
package require tls
tls::init -autoservername true
http::register https 443 ::tls::socket

set dump_path [dict get [info frame [info frame]] file]
set dump_path [file dirname $dump_path]
set dump_path [file join $dump_path dump.tcldict.gz]
puts "will save to $dump_path"

set tok [http::geturl "https://spdx.org/licenses/licenses.json"]
if {[http::ncode $tok] != 200} {
  puts stderr "got [http::code $tok] downloading index: [http::data $tok]"
  exit -1
}
set licenses [json::json2dict [http::data $tok]]
unset $tok

set licenses [dict get $licenses licenses]
set dump {}

set lcount [llength $licenses]
puts "downloading $lcount licenses"

set progress 0
foreach license $licenses {
  set tok [http::geturl [dict get $license detailsUrl]]
  if {[http::ncode $tok] != 200} {
    puts stderr "got [http::code $tok] downloading license [dict get $license licenseId]: [http::data $tok]"
    exit -2
  }
  set details [json::json2dict [http::data $tok]]
  unset $tok

  dict set dump [dict get $license licenseId] \
    [list name [dict get $license name] \
          spdx_uri [dict get $license reference] \
          see_also [dict get $license seeAlso] \
          text [dict get $details licenseText]]

  incr progress
  puts -nonewline "downloaded $progress / $lcount\r"
  flush stdout
}

puts "downloaded $lcount licenses"

set dumpfd [open $dump_path w]
zlib push gzip $dumpfd -level 9
puts $dumpfd $dump
flush $dumpfd
close $dumpfd

puts "saved to $dump_path"

A spdx/lib.tcl => spdx/lib.tcl +58 -0
@@ 0,0 1,58 @@
namespace eval spdx {
  set log [logger::init spdx]

  variable db

  proc load {} {
    variable log
    variable db

    if {[array exists db]} { return }

    ${log}::debug "loading database..."

    set dump_path [dict get [info frame [info frame]] file]
    set dump_path [file dirname $dump_path]
    set dump_path [file join $dump_path dump.tcldict.gz]

    set dump [open $dump_path]
    zlib push gunzip $dump
    set data [read $dump]
    close $dump
    unset dump
    unset dump_path

    foreach {name info} $data {
      set db($name) $info
    }
    unset data

    ${log}::debug "database loaded with [array size db] licenses!"
  }

  proc exists {id} {
    variable db
    ::spdx::load
    expr {$id in [array names db -exact $id]}
  }
  proc get_name {id} {
    variable db
    ::spdx::load
    dict get [set db($id)] name
  }
  proc get_text {id} {
    variable db
    ::spdx::load
    dict get [set db($id)] text
  }
  proc get_spdx_url {id} {
    variable db
    ::spdx::load
    dict get [set db($id)] spdx_url
  }
  proc get_see_also {id} {
    variable db
    ::spdx::load
    dict get [set db($id)] see_also
  }
}