WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: [Solved] script that generates .tree files  (Read 2357 times)

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 12744
Re: script that generates .tree files
« Reply #45 on: May 06, 2026, 12:19:15 AM »
Hi Paul_123
I saw the Dynamic format, I had missed you purposely removed it. ...
Technically, the formatting is still dynamic since it's controlled by
a variable. The implementation is just different to be compatible
with more versions of awk.

Quote
... Man your computer is slow :)
Thank you. It's plenty fast for most of my daily needs. Besides, its
slow speed makes it easier to spot when an optimization is really
effective, and not just noise.

This is a laptop with an external keyboard, monitor, and mouse that
I keep running for when I need a little more horsepower:
Code: [Select]
tc@HP-G62:~/TreeGen$ time ./treegen5.awk vlc-dev.tcz > vlc-dev.tcz.tree5
real    0m 1.03s
user    0m 0.74s
sys     0m 0.27s
tc@HP-G62:~/TreeGen$ time ./treegen6.awk vlc-dev.tcz > vlc-dev.tcz.tree6
real    0m 0.63s
user    0m 0.51s
sys     0m 0.11s

This is a desktop a buddy gave me because it was inadequate for newer
versions of Windows:
Code: [Select]
tc@box:~/TreeGen$ time ./treegen5.awk vlc-dev.tcz > vlc-dev.tcz.tree5
real    0m 0.61s
user    0m 0.46s
sys     0m 0.14s
tc@box:~/TreeGen$ time ./treegen6.awk vlc-dev.tcz > vlc-dev.tcz.tree6
real    0m 0.35s
user    0m 0.30s
sys     0m 0.04s

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1848
Re: script that generates .tree files
« Reply #46 on: May 06, 2026, 10:35:30 AM »
I hate to admit it, but the AI-generated version that Paul_123 shared is better than my version, and about twice as fast :-\ Using a cache to avoid reopening .dep files that have already been parsed is pretty clever. But it only worked for me (on TCL17 x86_64 with busybox awk) after I made Rich's edit:

After changing this:
Code: [Select]
printf("%*s%s\n", depth, "", tczname)To this:
Code: [Select]
printf("%"depth"s%s\n", "", tczname)

I think I've squeezed out all the fun there is to be had with this little project. Thank you, Rich and Paul_123, for your helpful feedback.

Rich, the thread can be safely marked as "Solved" :)
« Last Edit: May 06, 2026, 10:52:13 AM by GNUser »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 12744
Re: [Solved] script that generates .tree files
« Reply #47 on: May 06, 2026, 11:35:28 AM »
Hi GNUser
... But it only worked for me (on TCL17 x86_64 with busybox awk) after I made Rich's edit: ...
That's not surprising, considering I had to make those changes for TC10, TC14, and TC16.
The syntax the AI proposed is valid in ash and C. I know because I've used it.

Create this file:
Code: [Select]
#!/bin/sh
xyzzy=10
printf "%*s%s\n" "$xyzzy" "" "Hi there!"

Run it and you get:
Code: [Select]
tc@E310:~/TreeGen$ ./Printf
          Hi there!
tc@E310:~/TreeGen$

When I tried to use it to reduce the number of times you called printf, awk complained.
A search turned up the way around this is to replace the asterisk with the variables name
and add quote marks so the name isn't part of the formatting string, its value is.

Quote
... Rich, the thread can be safely marked as "Solved" :)
Done. ;D

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1848
Re: [Solved] script that generates .tree files
« Reply #48 on: May 06, 2026, 11:36:12 AM »
The AI version considers .dep file lines with length >= 4 to be relevant. I think this is an error. A line in .dep file with length == 4 (a line that contains, for example, nothing but ".tcz") should be ignored. I think testing for length > 4 is what we want.

Here's what I think is our final version:

Code: [Select]
#!/usr/bin/awk -f

# treegen.awk v3.5 (May 6, 2026)
# usage example: $ treegen.awk vlc-dev.tcz

BEGIN {
    LINUX_VERSION = "6.18.2-tinycore64"
    MIRROR_PATH   = "/mnt/usb/http/tinycorelinux/17.x/x86_64/tcz/"
    get_dependencies(ARGV[1], 0)
}

# Cache:
#   loaded[pkg]    = 1 once pkg.dep has been read (even if empty/missing)
#   ndeps[pkg]     = number of deps found
#   deps[pkg, i]   = ith dep (1..ndeps[pkg])

function load_depfile(pkg,    depfile, line, n) {
    if (loaded[pkg]) return
    loaded[pkg] = 1

    depfile = MIRROR_PATH pkg ".dep"
    n = 0

    while (getline line < depfile > 0) {
        gsub(/[ \t\r]+/, "", line)
        if (line == "") continue

        # endswith ".tcz" is faster/stricter than /\.tcz/
        L = length(line)
        if (L > 4 && substr(line, L-3) == ".tcz")
            deps[pkg, ++n] = line
    }

    close(depfile)
    ndeps[pkg] = n
}

function get_dependencies(tczname, depth,    i, n) {
    gsub(/KERNEL/, LINUX_VERSION, tczname)
    printf("%"depth"s%s\n", "", tczname)

    load_depfile(tczname)

    n = ndeps[tczname]
    for (i = 1; i <= n; i++) {
        get_dependencies(deps[tczname, i], depth + 3)
    }
}

Offline Paul_123

  • Administrator
  • Hero Member
  • *****
  • Posts: 1552
Re: [Solved] script that generates .tree files
« Reply #49 on: May 06, 2026, 12:17:39 PM »
These little exercises, while mundane in themselves, get you thinking, and learning. 

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1848
Re: [Solved] script that generates .tree files
« Reply #50 on: May 06, 2026, 12:21:35 PM »
These little exercises, while mundane in themselves, get you thinking, and learning.
Agreed. And that's what it's all about :)

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1848
Re: [Solved] script that generates .tree files
« Reply #51 on: May 06, 2026, 12:25:37 PM »
Paul_123, what AI tool did you use for the treegen.awk cleanup? Just curious.

Offline Paul_123

  • Administrator
  • Hero Member
  • *****
  • Posts: 1552
Re: [Solved] script that generates .tree files
« Reply #52 on: May 06, 2026, 12:33:05 PM »
It was GitHub Copilot,  which kinda mixes up the models used based on the request type.

It was either GPT5.2 or Sonnet 4.6

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1848
Re: [Solved] script that generates .tree files
« Reply #53 on: May 06, 2026, 12:45:07 PM »
Got it. The cache idea and its implementation were surprisingly good. It's kind of frightening, actually.
Thank you.