-
Notifications
You must be signed in to change notification settings - Fork 1.1k
micropython/utarfile: Support creating tar files. #659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
thank you @dpwe! this is very helpful |
Thanks @dpwe this looks useful. Something we need to consider is code size -- someone who just wants to read tar files shouldn't need to pay the extra flash cost. So to that end we've started splitting packages into base and extension packages. This might be a good candidate for that. The approach is:
Conceptually the idea is that installing the (Note: I'm using both meanings of "package" in the above... see https://github.com/micropython/micropython-lib/#notes-on-terminology) Unrelated to this PR... just a note while we're looking at |
Thanks. I converted to a package and split the tarfile writing into a separate utarfile-write extension that layers on top of utarfile, with minimal additions to the original module. Because the new methods are top-level members of the TarFile class, I had to monkeypatch them in in utarfile/init.py, let me know if this looks OK. I also added "append" mode support since it was a small change. |
Thanks @dpwe -- yeah there's basically only three ways to do it -- some form of monkeypatch, stubs, or the base-class method I described. Overall the implementation looks great, and thanks for adding the append and examples. The monkey-patching can be done a bit simpler though (which mostly results in less code size), and I think there are some useful additions that you put in write.py that are worth moving into the base (e.g. the context manager). Also, although it's neater to have the I had a quick go at implementing this (and a few other size optimisations along the way)... Overall this goes from 235+1316 for |
Yes, looks great. That's a nice size reduction! I was a bit uncomfortable with the ambiguity in how we represent if the TarInfo is a directory (between .type and .mode). Your unification is great. Do you want me to do anything more? Should I update my PR to use your version? |
I copied your changes into this branch and verified that everything still works. |
Thanks @dpwe, looks good. One thing that is worth pointing out is that as a result of it going from being a module to a package is that if someone already has it installed and then updates to the latest version, mip will not remove the old utarfile.py from the filesystem. However, packages of the same name always take precedence over .py files (it importer does dir, .py, .mpy in that order), so this should not be an issue. It will also need a version bump to manifest.py, but I will do that when I merge this (tomorrow). |
|
||
|
||
# Inject extra functionality into TarFile. | ||
from . import TarFile, TarInfo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a circular import, but I think it works because the package (__init__
) is already loaded by this point.
Would it be smaller code to do the following within the TarFile
class:
try:
from .write import _open_write, addfile, add, close
except:
pass
??
"typeflag": (uctypes.ARRAY | 156, uctypes.UINT8 | 1), | ||
} | ||
|
||
# Following https://github.com/python/cpython/blob/3.11/Lib/tarfile.py |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the code here copy anything verbatim from that Python file? Just need to be careful of licensing.
Thanks @dpwe , this is a really nice addition! |
Currently utarfile supports a small subset of the cpython tarfile functionality to read items from an existing tar archive.
This adds a
mode='w'
option to the constructor, and atarfile.add('filename')
method to support creating tar files.Only regular files and directories are supported.