# For versioni-registro.sh, with json.awk: the versions of one package of Gitea's registry in the pages of # GET /api/v1/packages/{owner}/{type}/{name} of Gitea 28, given as files: # # LC_ALL=C awk -v tipo=container -v nome=febe -f json.awk -f versioni-registro.awk ... # # A page is a list of versions with «type», «name», «version», «created_at» and «id»; an empty page may be «[]» or # «null». Only the versions whose type and name are those asked count, and a version found in two pages counts once # (the last one read wins, in the place of the first). For each it prints, in the order first met: # # # # # created_at is read as ISO 8601 / RFC 3339 like JavaScript's Date.parse: a date, or a date and time with «Z» or an # offset (+02:00), seconds and fractions optional. A time without a zone is local time for JavaScript and would depend # on the machine: here it is not a date, and in doubt nothing is removed. If a file is not JSON, or not a list (nor # null), it prints nothing, says why on stderr and exits with 2. function json_begin(file) { root = "" inside = 0 } function json_open(kind) { if (json_depth == 1) { root = kind } else if (json_depth == 2 && root == "a" && kind == "o") { inside = 1 split("", field) split("", seen) } } function json_scalar(kind, value) { if (json_depth == 0) { root = kind == "z" ? "null" : "valore" } else if (json_depth == 2 && inside) { field[json_at[2]] = value seen[json_at[2]] = kind } } function json_close(kind) { if (json_depth == 2 && inside) { inside = 0 keep() } } function json_end(file, error) { if (error != "") { problems = problems file ": " error "\n" } else if (root != "a" && root != "null") { problems = problems file ": non è un elenco di versioni\n" } } function keep( version, created, id) { if (seen["type"] != "s" || field["type"] != tipo || seen["name"] != "s" || field["name"] != nome) return if (seen["version"] != "s") return version = field["version"] if (version ~ control) return if (!("created_at" in seen)) created = "undefined" else if (seen["created_at"] == "z") created = "null" else if (seen["created_at"] == "t") created = "true" else if (seen["created_at"] == "f") created = "false" else created = field["created_at"] gsub(control, "?", created) id = 0 if (seen["id"] == "n" || (seen["id"] == "s" && field["id"] ~ /^[0-9]+$/)) id = field["id"] + 0 if (!(version in order)) order[version] = ++count when[version] = seen["created_at"] == "s" ? milliseconds(field["created_at"]) : "" number[version] = id written[version] = created } # Milliseconds since 1970 UTC of an ISO 8601 date as Date.parse reads it, or "" if it is not one. function milliseconds(text, y, mo, d, h, mi, s, ms, rest, offset, fraction) { if (text !~ /^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/) return "" y = substr(text, 1, 4) + 0 mo = substr(text, 6, 2) + 0 d = substr(text, 9, 2) + 0 h = mi = s = ms = offset = 0 rest = substr(text, 11) if (rest != "") { if (rest !~ /^T[0-9][0-9]:[0-9][0-9]/) return "" h = substr(rest, 2, 2) + 0 mi = substr(rest, 5, 2) + 0 rest = substr(rest, 7) if (rest ~ /^:[0-9][0-9]/) { s = substr(rest, 2, 2) + 0 rest = substr(rest, 4) if (match(rest, /^[.][0-9]+/)) { fraction = substr(rest, 2, RLENGTH - 1) "00" ms = substr(fraction, 1, 3) + 0 rest = substr(rest, RLENGTH + 1) } } if (rest == "Z") offset = 0 else if (rest ~ /^[-+][0-9][0-9]:[0-9][0-9]$/) { offset = substr(rest, 2, 2) * 60 + substr(rest, 5, 2) if (substr(rest, 2, 2) + 0 > 23 || substr(rest, 5, 2) + 0 > 59) return "" if (substr(rest, 1, 1) == "-") offset = -offset } else return "" } if (mo < 1 || mo > 12 || d < 1 || d > days_in_month(y, mo)) return "" if (h > 24 || mi > 59 || s > 59 || (h == 24 && (mi > 0 || s > 0 || ms > 0))) return "" return ((days_from_civil(y, mo, d) * 86400 + h * 3600 + mi * 60 + s - offset * 60) * 1000 + ms) } function leap(y) { return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0 } function days_in_month(y, m) { if (m == 2) return leap(y) ? 29 : 28 return (m == 4 || m == 6 || m == 9 || m == 11) ? 30 : 31 } # Days from 1970-01-01 to a date of the proleptic Gregorian calendar (years 0 to 9999 here). function days_from_civil(y, m, d, era, yoe, doy, doe) { if (m <= 2) y-- era = int(y / 400) if (y < 0 && y % 400 != 0) era-- yoe = y - era * 400 doy = int((153 * (m > 2 ? m - 3 : m + 9) + 2) / 5) + d - 1 doe = yoe * 365 + int(yoe / 4) - int(yoe / 100) + doy return era * 146097 + doe - 719468 } BEGIN { control = "[" sprintf("%c", 1) "-" sprintf("%c", 31) "]" if (tipo == "" || nome == "") { print "versioni-registro.awk: servono -v tipo=… -v nome=…" > "/dev/stderr" failed = 1 exit 2 } } END { if (failed) exit 2 if (problems != "") { printf "%s", problems > "/dev/stderr" exit 2 } for (version in order) { printf "%s\t%.0f\t%d\t%s\t%s\n", when[version] == "" ? "-" : sprintf("%.0f", when[version]), number[version], order[version], version, written[version] } }