babashka-sci-dev

borkdude 2022-07-18T09:03:39.167099Z

If someone here wants to help with some research: https://github.com/babashka/fs/issues/67 The issue is that fs/zip should be able to add directories while omitting part of the the directory structure. So `foo/bar/ -> bar/ for example. The homework is to find out how other platforms (Ruby, Python, Node and possible other libs do this.

lispyclouds 2022-07-18T09:57:16.375229Z

In Python you need to use the lower level zipfile module and recurse down the dir structure and manually supply the filename in the write method: https://docs.python.org/3.10/library/zipfile.html#zipfile.ZipFile.write The dir walk is bit eased with Pathlib:

from pathlib import Path 

def zipdir(parent_dir : str , ziph : ZipFile) -> None:
    
    for file in Path(parent_dir).rglob('*'): # gets all child items
        ziph.write(file, file.name) 
the file.name 2nd arg to ziph.write is whats flattening it

borkdude 2022-07-18T09:57:45.709329Z

yeah, this is too hard in Python if you ask me :)

borkdude 2022-07-18T09:57:55.436199Z

We could just provide a function to do the renaming

lispyclouds 2022-07-18T09:58:12.843739Z

theres no abstracted way to do this in py afaik

lispyclouds 2022-07-18T09:59:44.283009Z

maybe an option like {:flatten true} or {:preserve-heirarchy true}

borkdude 2022-07-18T10:00:45.447829Z

flatten true would not cover the use case of adding foo/bar/baz and then have bar/baz in the zip

lispyclouds 2022-07-18T10:01:32.008259Z

so its more of a selective filter?

lispyclouds 2022-07-18T10:02:03.452389Z

hierarchal filter of sorts?

borkdude 2022-07-18T10:02:29.808839Z

you don't know this use case? you have a directory src/foo/bar.clj but you would like to have foo/bar.clj in the zip

lispyclouds 2022-07-18T10:04:30.693229Z

yeah i get around it with zipping src/* and others manually, havent had a need to just stripping one level yet

borkdude 2022-07-18T10:04:53.892899Z

how do you "zip manually"

lispyclouds 2022-07-18T10:05:35.096789Z

manually as in add all the needed parts step by step, not specifying what i need in a full dir

lispyclouds 2022-07-18T10:07:06.120919Z

(zip archive "src/foo/*") (zip archive "src/bar/*") and more such calls

borkdude 2022-07-18T10:07:38.492039Z

this is not using fs/zip which is what the issue is about :)

lispyclouds 2022-07-18T10:08:05.366819Z

yep got it, just never thought of a need for this 😛

lispyclouds 2022-07-18T10:16:07.530389Z

Ruby has a python like api too, seeing these two i think a :rename-fn would be the most similar

borkdude 2022-07-18T10:17:26.184189Z

yeah, seems good to me