Fork me on GitHub
#babashka
<
2022-08-09
>
lepistane13:08:08

Hello, i am super new to scripting but i wanna do something. So i have a video stream that i am storing using ffmpeg and i would like to use babashka to cut that ongoing stream by hour. So each hour i stop the recording and i create new folder and continue storing in that new folder. Idea is to have stream segmented by hour. /videos/13:00/stream (lenght 1h) /videos/14:00/stream (length 1h) ... Currently i have everything setup with clojure.java.shell but the main problem is that i don't know how to stop the process after i initiate it with sh.

(sh "ffmpeg" ...  file-name)
This is blocking and i don't know how to interrupt it. Maybe i am doing this wrong way? Any suggestions ?

borkdude14:08:14

@U45SLGVHV Hey welcome! I recommend taking a look at the built-in babashka.process library

borkdude14:08:06

I see there's also an ffmpeg library which does this for you in bb: https://github.com/luissantos/ffclj

lepistane14:08:23

Thanks! So stream is 24/7 and i want to chunk it. i couldn't find a way to stop ffclj. wait task - will just hang because i am recording a stream process - i also couldn't fin a way to stop the process. Did i miss anything in the documentation? My idea was to have a loop, read the stream and store it to file, after 1h stop and repeat (but to new file).

borkdude14:08:01

do you want to kill the ffmpeg process?

borkdude14:08:26

you can do this using fs/destroy

lepistane14:08:53

yes i want to kill ffmpeg process periodically. Aha! Alright i will try it out

hlship15:08:53

Is there a way to get the Babashka version of the stack trace at runtime? I'd like to make https://github.com/hlship/trace work with BB, but it uses Thread.getStackTrace and a small amount of guesswork to approximate where the code is called from https://github.com/hlship/trace/blob/main/src/net/lewisship/trace.clj#L65 ... I tried (-> (Thread/currentThread) .getStackTrace sci.core/format-stacktrace) but it returns [].

borkdude15:08:55

you can try:

(catch ^:sci/error Exception e)
This is a new experimental feature which puts the interpreter stacktrace info as ex-data on e which can be processed with sci/stacktrace and formatted with sci/format-stacktrace

borkdude15:08:37

Trying to come up with an example, hold on

borkdude15:08:13

(require '[sci.core :as sci])


(defn foo [x]
  (/ 1 x))

(defn bar []
  (foo 0))

(try (bar)
     (catch ^:sci/error Exception e
       (->> e
            (sci/stacktrace)
            (sci/format-stacktrace)
            (run! prn))))

borkdude15:08:36

Use the newest version if this doesn't work for you

hlship17:08:57

I'll give this a try when I can; it may involve throwing an exception just to catch it for its stack trace, which is not great performance-wise, but that's a lesser concern. I'm sure even in the JVM getting the current stack trace is relatively expensive.

hlship17:08:33

Remember back in the day where people convinced themselves that the best way to write an efficient loop was to write an infinite loop that throws an exception, and to catch the exception outside the loop? I try not to be that person. One of my big Tapestry regrets is that I used a little bit of throwing exceptions for flow of control, something I would never do now.

borkdude17:08:51

The above is just to expose the error handling that SCI normally offers outside of the interpreter to within the interpreter e.g. in a -main function. Throwing and immediately catching probably results in an empty stack trace due to how this is implemented.

hlship17:08:58

I'll have to experiment; alternately, is there a way to know the namespace and fn name in Babashka at macro expansion time? I wish Clojure exposed this, but of course due to order of operations, it might not even know.

borkdude17:08:58

> is there a way to know the namespace and fn name in Babashka at macro expansion time? Not sure what you mean here, got an example?

hlship21:08:09

Calls to trace output something like:

{:in my.example.ring-handler/handle-request,
 :line 6,
 :thread "nREPL-session-c3dde1ce-ca19-4e78-95ad-d0e4beda61eb",
 :method :get,
 :uri "/status"}
I'm doing some tricks with stack traces and name de-mangling to get the :in key, which represents the namespace and function (possibly nested) where the trace macro is expanded. I was wondering if sci exposes something extra that would make it easier to determine this inside my macro, but failing that, I want to adapt the same technique to BB.

borkdude21:08:26

Well, the line and column are available as metadata on &form and *file* has the current file, I guess the only thing missing is the parent function then right?

borkdude21:08:57

we could stuff something into &env like ClojureScript does

Cora (she/her)16:08:39

what do you think about including this in babashka? https://clj-commons.org/camel-snake-kebab/

Cora (she/her)16:08:06

since inevitably every project contains an adhoc and buggy version of one of the features of this

Cora (she/her)16:08:42

only 163 lines according to tokei

hlship17:08:13

That raises the larger question of what is the cutoff for any library being baked in; I would say criteria involve universality and how useful it would be baked into the implicit user namespace and whether it requires Java classes be baked in; for camel-snake-kebab that isn't so useful in user namespace (this is very subjective) and the library can otherwise just be added as a dependency in bb.edn. I mean, I can definitely see using it or https://github.com/trhura/clojure-humanize in tools I write, but baking it in also means locking down the version.

borkdude08:08:54

I tested humanize and it doesn't work due to the dependency on joda time. If the project maintainer can be convinced to java.time it might work.

borkdude09:08:07

Commenting out the date-related functions, then it seems to work:

$ bb --classpath $(lein classpath) -e "(require '[clojure.contrib.humanize :as h]) (h/intcomma 1000) "
"1,000"
$ bb --classpath $(lein classpath) -e "(require '[clojure.contrib.humanize :as h]) (h/numberword 1000) "
"one thousand"

Cora (she/her)17:08:00

first I've heard of that library, it looks great! stars it

borkdude17:08:05

I prefer to keep libraries that work perfectly fine as a library out as the binary image size can then be used for something else. There are exceptions but those exceptions are probably already built-in: tools.cli, data.csv :)

borkdude17:08:48

Btw, camel-snake-kebab is built into #jet

borkdude17:08:27

Today marks babashka's 3rd birthday!

🎉 19
🍰 13
littleli18:08:52

Hey Look Ma eql works natively on Windows too 😎 https://github.com/littleli/scoop-clojure/pull/180 @lilactown

🎉 4
lilactown18:08:41

woah!! thanks!

littleli18:08:11

You're welcome! Nice project

Cora (she/her)18:08:05

I hadn't considered json to edn and then using eql

borkdude20:08:42

@lilactown Why does your example contain --no-pretty?

borkdude20:08:03

I haven't used eql that much, but it's good, maybe it's also an option to bake into jet eventually

lilactown21:08:33

The reason I use —no-pretty is because the CLI I wrote expects the EDN passed via stdin to be on one line

👍 1
lilactown21:08:05

It does (edn/read *in*)

borkdude21:08:33

That should work with multiline strings as well?

Cora (she/her)21:08:56

it should read until stdin is closed right?

borkdude21:08:11

$ echo '[\n\n1 2 3]' | bb -e '(edn/read *in*)'
[1 2 3]

lilactown21:08:23

I thought so too but recall running into issues. I'll try again when I'm back at my computer

lilactown21:08:42

oh yeah it works without --no-pretty

😎 1
clojure-spin 1
🎉 1
frankitox19:08:05

Is there a canonical way to merge bb configs (`bb.edn`)?

frankitox19:08:40

For now I'm doing this

cat ~/.config/bb.edn my-bb.edn | jet --collect --func "(fn [edns] (apply merge edns))"
I wanted some sort of global config support

borkdude07:08:59

$ bb -e "(babashka.deps/merge-deps ['{:deps {a 1}} '{:deps {a 2 b 3}}])"
{:deps {a 2, b 3}}

frankitox13:08:21

Oh I see, thank you your dudeness

😆 1
zane19:08:00

Anyone have a sense of what’s going on with Babashka on nixpkgs master?

[nix-shell:~]$ hydra-check babashka --arch=aarch64-darwin --channel=master
Build Status for babashka.aarch64-darwin on master
⚠ This job is not a member of the latest evaluation of its jobset. This means it was removed or had an evaluation error.

ericdallo20:08:41

I have no idea why :(

❤️ 1
kokada20:08:47

@U050CT4HR Looks fine for me, at least looking at the hydra logs

zane20:08:06

Maybe I’m using hydra-check wrong? I’m pretty new to this.

kokada20:08:23

It did fail the staging-next-22.05 build, but I would say this is unrelated

kokada20:08:03

Please check the unstable channel, it doesn't make much sense to check master

kokada20:08:01

Well, this is also failing for me, but: https://hydra.nixos.org/build/186540565

kokada20:08:07

This is the last build I think

kokada20:08:10

So it should be fine

zane20:08:41

Yeah, I was going to say — I think it’s also broken on unstable.

zane20:08:15

How did you find that build?

zane20:08:17

This question might belong in #nix, but I don’t yet understand how to find a working nixpkgs SHA for n packages and m architectures.

kokada20:08:02

> How did you find that build? I generally just search in the Web interface directly. It list the latest builds

zane20:08:06

Works for one individual package, but not for n packages. We’re just using Nix to create reproducible development environments. Should I be pinning different versions of nixpkgs for each package we need?

zane20:08:16

(I realize this conversation has gone off topic.) simple_smile

kokada20:08:30

Let me move the conversation to #nix

1
lread20:08:01

The interesting case of Path vs PATH on Windows 🧵

lread20:08:59

I was confused as to why bb test was failing for me on babashka/fs on Windows. The which tests rely on on-path being on the PATH. It seemed that the bb.edn test tasks was https://github.com/babashka/fs/blob/111894217039261c45e4b9f415e6371ae41cf148/bb.edn#L8-L9:

:tasks
 {:requires ([babashka.fs :as fs])
  test (clojure {:extra-env {"PATH" (str "on-path" fs/path-separator (System/getenv "PATH"))}}
                "-M:test")
But then I noticed the https://github.com/babashka/fs/blob/111894217039261c45e4b9f415e6371ae41cf148/appveyor.yml#L49:
set PATH=on-path;%PATH%;
“Huh”, I thought, why is that needed? I typed set from a cmd prompt and saw that path was showing under environment var Path. If I change the bb.edn above to use Path instead of PATH:
:tasks
 {:requires ([babashka.fs :as fs])
  test (clojure {:extra-env {"Path" (str "on-path" fs/path-separator (System/getenv "PATH"))}}
                "-M:test")
the path is updated appropriately on Windows and the test passes.

lread20:08:56

This is a bit confusing.

lread20:08:12

Can someone with a Windows box/vm try a bb test on babashka/fs and report back?

borkdude21:08:03

I can try this tomorrow. You could also make a change in the Github action right?

borkdude21:08:06

or appveyor

lread21:08:25

Yah could do…

littleli21:08:20

Running tests in #{"test"}

Testing babashka.fs-test

FAIL in (list-dirs-and-which-test) (fs_test.clj:292)
expected: (fs/which "foo.foo")
  actual: (not (fs/which "foo.foo"))

Ran 42 tests containing 144 assertions.
1 failures, 0 errors.
Error while executing task: test
^Before
Running tests in #{"test"}

Testing babashka.fs-test

Ran 42 tests containing 144 assertions.
0 failures, 0 errors.
^After So basically confirmed?

lread21:08:59

Huh! Yes! Thanks @UBLU3FQRZ!

lread21:08:15

Ok… here’s an interesting test on Windows:

bb
Babashka v0.9.161 REPL.
Use :repl/quit or :repl/exit to quit the REPL.
Clojure rocks, Bash reaches.

user=> (require '[babashka.tasks :as t])
nil
user=> (t/shell {:extra-env {"pAtH" "foo"}} "clojure")
Clojure 1.11.0
user=> (System/getenv "PATH")
"C:\\Program Files\\Parallels\\Parallels Tools\\Applications;...etc...etc..."
user=> (System/getenv "pAtH")
"C:\\Program Files\\Parallels\\Parallels Tools\\Applications;...etc...etc..."

user=> (get (System/getenv) "PATH")
nil
user=> (get (System/getenv) "Path")
"C:\\Program Files\\Parallels\\Parallels Tools\\Applications;...etc...etc..."
user=> (get (System/getenv) "pAtH")
"foo"

lread21:08:18

From https://docs.oracle.com/javase/tutorial/essential/environment/env.html: > There are many subtle differences between the way environment variables are implemented on different systems. For example, Windows ignores case in environment variable names, while UNIX does not.

lread21:08:10

Ok, so maybe the way babashka/process is merging :extra-env environment vars needs to be tweaked for Windows. It seems that the Windows shells match case insensitively when overriding: cmd.exe:

C:\Users\lee>set foo=bar
C:\Users\lee>echo %FOO%
bar
C:\Users\lee>echo %fOo%
bar
C:\Users\lee>set FOO=gum
C:\Users\lee>echo %fOo%
gum
C:\Users\lee>echo %FOO%
gum
C:\Users\lee>set | grep -i FOO
foo=gum
Powershell:
PS C:\Users\lee> $env:foo = "bar"
PS C:\Users\lee> echo $env:foo
bar
PS C:\Users\lee> echo $env:FoO
bar
PS C:\Users\lee> $env:FOO = "gum"
PS C:\Users\lee> echo $env:foo
gum
PS C:\Users\lee> echo $env:FoO
gum
PS C:\Users\lee> Get-ChildItem Env: | grep -i FOO
foo                            gum

lread21:08:16

Happy to do an issue/PR if this seems right.

lread21:08:14

Should probably do a look-see on macOS and Linux too….

lread22:08:27

macOS on default zsh:

❯ foo=bar
❯ echo $foo
bar
❯ echo $FoO

❯ FOO=gum
❯ echo $FOO
gum
❯ echo $foo
bar
is case sensitive. Same idea for bash on Linux.

lread14:08:57

@U04V15CAJ, when you have a moment to read the above, lemme know if you'd like an issue/PR on babashka/process

borkdude14:08:04

I don't think fs has to account for Windows flakes this much, Java also doesn't do that right?

lread14:08:09

Java does not seem to handle this well for process builder environment on Windows. Behaviour does not match Windows shell behaviour. System/getenv seems to behave like you'd expect though.

borkdude14:08:16

This is the first time I hear about this. Perhaps with Path/PATH it's more common, but I'd rather just fix our tests than having to do a str/lower comparison etc.

borkdude14:08:44

Might change my mind if this comes up more often

lread14:08:50

Well not many Clojure users use Windows, so there's that too... Path on Windows is normally updated via PATH when setting, but not for :extra-env . I personally think it is worth fixing, but of course, your call. I could do a little poking around to see examples of what others have done around process builder environment for Windows.

borkdude14:08:18

Can you formulate a clear problem statement and a proposed solution? I'm not sure I get it entirely.

borkdude14:08:54

Isn't the environment variable in Windows called PATH? Is it always called Path?

lread14:08:11

It's Path on my Windows VM, and apparently on @UBLU3FQRZ's Windows too.

borkdude14:08:57

Then maybe we should update Path and not PATH in the programs on Windows

lread14:08:02

I'm assuming Path is the default name of path on Windows. Could double-check that, but it seems to be the case.

borkdude14:08:29

If you do extra-env then you automatically merge the already set Path, so I don't see how that could go wrong

lread14:08:35

But technically (I'll also have to double-check this), PATH would be valid on Windows.

littleli14:08:04

This is a snapshot of Control Panel (system environment setup) if it helps a bit

littleli14:08:19

sorry for :flag-cz: UI but I don't have a different one 😄

lread14:08:29

Yah, thanks @UBLU3FQRZ, that helps.

lread14:08:54

Here's an openjdk bug someone raised around the issue: https://bugs.openjdk.org/browse/JDK-8245431

borkdude14:08:56

Windows on brand again

borkdude14:08:29

yeah ok. So I'm not going to compensate for JDK bugs more than I already have ;)

lread14:08:29

It might be practical to do so in this case?

littleli14:08:33

but to add more confusion... if you run env command from powershell or cmd.exe there is PATH not Path... :thinking_face:

lread14:08:14

But if you run set in cmd.exe you'll see Path.

lread14:08:13

So I think the JDK does not handle this well for Windows.

littleli14:08:45

well, in such case I think well documented way to run programs. I mean to communicate requirements is a way to go.

borkdude14:08:22

Problem statement + proposed solution + considered alternatives welcome. My favorite alternative is "leave it" right now, unless I learn more in a digestible format that isn't a long-threaded conversation. ❤️

lread14:08:15

Ok, I think it is worth raising as an issue to consider in babashka/process. Will invest the time to do so.

littleli14:08:55

Case sensitivity is the endless source of unexpected behaviour, not just on Windows, but also on Mac.

littleli14:08:01

In a company where I work we have a 20+GB repo, that you cannot clone with Git on Mac because case sensitivity issue.

lread14:08:53

Ya, lotsa time spent on these inconsistencies and nuances.

lread21:08:58

It’s never exciting to raise an issue about a Windows quirk nor an issue the project owner is not excited about! But I felt I should put in the work.

lread22:08:18

For interested lurkers, we decided to simply document the behaviour for now.