Skip to content

Commit

Permalink
script
Browse files Browse the repository at this point in the history
  • Loading branch information
BostX committed Dec 11, 2023
1 parent de4ab44 commit 97a2274
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 52 deletions.
19 changes: 12 additions & 7 deletions script/cleanup-checkout-cache.fish
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,22 @@ set checkoutCache ~/.cache/guix/checkouts
set regexp "\(https\|http\|file\)\?://[^ ]\+"
# printf "[DEBUG] regexp:\n%s\n" $regexp

### Be away that printing arrays in Fish can be tricky

# find items under $checkoutCache which are directories
set cacheDirs (find $checkoutCache -mindepth 1 -maxdepth 1 -type d)
# printf "[DEBUG] %s cacheDirs:\n$cacheDirs\n" (count $cacheDirs)
# printf "[DEBUG] %s cacheDirs:\n" (count $cacheDirs)
# printf "%s\n" $cacheDirs

# find items under $checkoutCache which are NOT directories
set cacheNonDirs (find $checkoutCache -mindepth 1 -maxdepth 1 -not -type d)
# printf "[DEBUG] %s cacheNonDirs:\n$cacheNonDirs\n" (count $cacheNonDirs)
# printf "[DEBUG] %s cacheNonDirs:\n" (count $cacheNonDirs)
# printf "%s\n" $cacheNonDirs

set allChannelRepos (guix system describe | \
grep repository | grep --only-matching "$regexp")
# printf "[DEBUG] %s allChannelRepos:\n$allChannelRepos\n" (count $allChannelRepos)
# printf "[DEBUG] %s allChannelRepos:\n" (count $allChannelRepos)
# printf "%s\n" (string join \n $allChannelRepos)

# Define empty variables which will serve as arrays. Can't use:
# set <variable> "" # some comment
Expand Down Expand Up @@ -63,7 +68,7 @@ end
# a void value?

if test -n "$removeDirs"
printf "### Cached checkouts to delete:\n"
printf "### %s Cached checkouts to delete:\n" (count $removeDirs)
# The array size of $removeDirs and $removeURLs are the same
for i in (seq (count $removeDirs))
# printf "%s # %s\n" $removeDirs[$i] $removeURLs[$i]
Expand All @@ -75,21 +80,21 @@ if test -n "$removeDirs"
end

if test -n "$keepDirs"
printf "\n### Cached checkouts to keep:\n"
printf "\n### %s Cached checkouts to keep:\n" (count $keepDirs)
# The array size of $keepDirs and $keepURLs are the same
for i in (seq (count $keepDirs))
printf "# %s # %s\n" $keepDirs[$i] $keepURLs[$i]
end
end

if test -n "$cacheNonDirs"
printf "\n### Non-directory items in the checkout cache directory:\n"
printf "\n### %s Non-directory items in the checkout cache directory:\n" (count $cacheNonDirs)
for elem in $cacheNonDirs
printf "# %s\n" $elem
end
end
if test -n "$others"
printf "\n### Other items in the checkout cache directory:\n"
printf "\n### %s Other items in the checkout cache directory:\n" (count $others)
for elem in $others
printf "# %s\n" $elem
end
Expand Down
21 changes: 10 additions & 11 deletions script/cleanup-checkout-cache.scm
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@ containing its output."
(define cacheDirs
(exec
(format #f "find ~a -mindepth 1 -maxdepth 1 -type d" checkoutCache)))
;; (format #t "[DEBUG] cacheDirs:\n~a\n" (string-join cacheDirs "\n"))
;; (format #t "[DEBUG] ~a cacheDirs:\n~a\n" (length cacheDirs) (string-join cacheDirs "\n"))

;; find items under $checkoutCache which are NOT directories
(define cacheNonDirs
(exec
(format #f "find ~a -mindepth 1 -maxdepth 1 -not -type d" checkoutCache)))
;; (format #t "[DEBUG] cacheNonDirs:\n~a\n" (string-join cacheNonDirs "\n"))
;; (format #t "[DEBUG] ~a cacheNonDirs:\n~a\n" (length cacheNonDirs) (string-join cacheNonDirs "\n"))

(define allChannelRepos
(exec
(format
#f "guix system describe | grep repository | grep --only-matching \"~a\""
regexp)))
;; (format #t "[DEBUG] allChannelRepos:\n~a\n" (string-join allChannelRepos "\n"))
;; (format #t "[DEBUG] ~a allChannelRepos:\n~a\n" (length allChannelRepos) (string-join allChannelRepos "\n"))

;; define empty lists
(define keepDirs (list)) ; local clones of git repositories actively used & cached
Expand Down Expand Up @@ -95,34 +95,33 @@ containing its output."
(set! others (append others (list dir))))))
cacheDirs)

;; `(iota N)` returns a list of numbers (0 ... N-1)
(if (> (length removeDirs) 0)
(begin
(format #t "### Cached checkouts to delete:\n")
(format #t "### ~a Cached checkouts to delete:\n" (length removeDirs))
(for-each
(lambda (i)
(let ((removeCheckoutsI (list-ref removeDirs i))
(removeURLsI (list-ref removeURLs i)))
(format #t "mv ~a{,.deleteMe} # ~a\n" removeCheckoutsI removeURLsI)))
;; `(iota N)` returns a list of numbers (0 ... N-1)
(iota (length removeDirs)))))

(if (> (length keepDirs) 0)
(begin
(format #t "\n### Cached checkouts to keep:\n")
(format #t "\n### ~a Cached checkouts to keep:\n" (length keepDirs))
(for-each
(lambda (i)
(let ((keepCheckoutsI (list-ref keepDirs i))
(keepURLsI (list-ref keepURLs i)))
(format #t "# ~a # ~a\n" keepCheckoutsI keepURLsI)))
;; `(iota N)` returns a list of numbers (0 ... N-1)
(iota (length keepDirs)))))

(if (> (length cacheNonDirs) 0)
(begin
"\n### Non-directory items in the checkout cache directory:\n"
(for-each (lambda (ndir) (format #t "# ~a\n" ndir)) cacheNonDirs)))
(format #t "\n### ~a Non-directory items in the checkout cache directory:\n" (length cacheNonDirs))
(for-each (lambda (elem) (format #t "# ~a\n" elem)) cacheNonDirs)))

(if (> (length others) 0)
(begin
(format #t "\n### Other items in the checkout cache directory:\n")
(for-each (lambda (dir) (format #t "# ~a\n" dir)) others)))
(format #t "\n### ~a Other items in the checkout cache directory:\n" (length others))
(for-each (lambda (elem) (format #t "# ~a\n" elem)) others)))
29 changes: 4 additions & 25 deletions script/part-1.org
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ rapidly piecing scripts together.
If quickly writing Guile scripts is demanding, why not try to develop a
prototype iteratively[fn:lp1] in a simpler environment? An environment with an
easy-to-read scripting language would be ideal. One such environment is the
[[https://fishshell.com/][fish shell]], which features a scripting language
similar to Python.
[[https://fishshell.com/][Fish shell]], which features a scripting language similar to Python.

Returning to our original goal of writing a script for cleaning the checkout
cache: Your Guix system might use fewer channels than the number of items in
Expand Down Expand Up @@ -178,31 +177,11 @@ printf "%s %s\n" (contains $repo $checkoutCache && echo "Yes" || echo "No ") $c
Okay. Now, let's cobble all the pieces together, implementing multiple
improvements, strongly(!) preferring readability over elegance, etc.:

#+BEGIN_SRC fish :exports both :results replace output
#+INCLUDE: "./cleanup-checkout-cache.fish" src fish
#+BEGIN_SRC fish :exports results :results replace output
source "./cleanup-checkout-cache.fish"
#+END_SRC

#+RESULTS:
#+begin_example
mv /home/bost/.cache/guix/checkouts/zs6zmevqvbzp76b5g35peosmi6a6szehu3rvisbmfxclfwhyyida{,.deleteMe} # https://gitlab.inria.fr/guix-hpc/guix-past
mv /home/bost/.cache/guix/checkouts/qrpa3yqjnryavq6ke65ui2wfhxxce6yhkjjljfjwjnzxwrzir2kq{,.deleteMe} # https://git.sr.ht/~abcdw/rde
mv /home/bost/.cache/guix/checkouts/lzgohq5qx5m3joiq7hpwhifey5cjhzdjh7ftgqf3zf6yscgpwmha{,.deleteMe} # file:///home/bost/dev/andrew-rde
mv /home/bost/.cache/guix/checkouts/xiy6tmf5c47m4mbsgcyscbv54puep4fsa4cxjvakkhvnwv5lhqma{,.deleteMe} # https://codeberg.org/SystemCrafters/crafted-guix.git
mv /home/bost/.cache/guix/checkouts/lfimhcs6pzfafrqnk5pkwua2o7xromo7q233lmyeynaqragzj7ya{,.deleteMe} # file:///home/bost/dev/haskell-guix
mv /home/bost/.cache/guix/checkouts/q34vpblad6qjkemghxeewwgkxzw6b77mas5mipiiym7cjfph3s2q{,.deleteMe} # https://gitlab.com/virt-viewer/virt-viewer
mv /home/bost/.cache/guix/checkouts/63g3ea4meam7lf5mkodnht3ilpw6f2c7ptksrpy4ivkvcmez2voq{,.deleteMe} # https://gitlab.com/guix-gaming-channels/games.git
mv /home/bost/.cache/guix/checkouts/xsxs5jlzxqsz5a7r3mygoglnval3uum5umz6jfmiwo3t7ltndu5a{,.deleteMe} # https://github.com/engstrand-config/home-service-dwl-guile
mv /home/bost/.cache/guix/checkouts/sbukdbdulb5sf3fmyaj7h2t3g3qd2tvif3p6fuw5rxvmbh5ocrxq{,.deleteMe} # file:///home/bost/dev/guix-packages

# Cached checkouts to keep:
/home/bost/.cache/guix/checkouts/l74zueb3lgylhgxnuzx3d5fzraztxvzu2s4466wlqqvmz7hdct3a # https://gitlab.com/nonguix/nonguix
/home/bost/.cache/guix/checkouts/nvfgx2qw6oh46gsdlp4tqi7dpxqecwz5rxx3dako5hgvxm3t4emq # https://github.com/Bost/haskell-guix
/home/bost/.cache/guix/checkouts/pjmkglp4t7znuugeurpurzikxq3tnlaywmisyr27shj7apsnalwq # https://git.savannah.gnu.org/git/guix.git
/home/bost/.cache/guix/checkouts/ssrrbehkhpdmb76h67z3j7zsfqgh2uuku2p5fpb75tndxp3uf4qa # https://github.com/Bost/guix-packages

# Other items in the checkout cache directory:
/home/bost/.cache/guix/checkouts/fooo
#+end_example

It wasn't too complicated, although I admit it took me quite some time to work
it out. Here's a breakdown of the script's key points:

Expand Down
23 changes: 14 additions & 9 deletions script/part-2.markdown.org
Original file line number Diff line number Diff line change
Expand Up @@ -223,21 +223,26 @@ As of now(!), December 2023:
or asking ChatGPT to create a download link for the Python code and then
editing and developing it yourself without involving ChatGPT anymore.

[fn:scheme1] Here is the final result - the Guile Scheme script. Compare it
with its Fish version. And as usual - read and understand the code before you
run it.
#+BEGIN_SRC scheme :exports code
;; the code
#+END_SRC

insert and verify fish and guile script
in the next blog we go over the top
analyze the code
will use the go hard core,
implement the script / functionality properly
with some higher order functions,
and functional programming paradigm
eventually hash maps - like in clojure
eventually hash maps - like in Clojure
try to find some Category Theory common things


[fn:scheme1] Here is the final result - the Guile Scheme script. Compare it
with its Fish version. And as usual - read and understand the code before you
run it.
#+INCLUDE: "./cleanup-checkout-cache.scm" src scheme
#+BEGIN_SRC fish :exports results :results replace output
guile ./cleanup-checkout-cache.scm
#+END_SRC
Compare the results
#+BEGIN_SRC fish :exports both :results replace output
source "./cleanup-checkout-cache.fish" > fish.log
guile ./cleanup-checkout-cache.scm > guile.log
diff --report-identical-files --brief --side-by-side fish.log guile.log
#+END_SRC

0 comments on commit 97a2274

Please sign in to comment.