tsutsumu


Nametsutsumu JSON
Version 0.1.0 PyPI version JSON
download
home_pageNone
SummaryPython module bundles
upload_time2023-07-07 20:00:25
maintainerNone
docs_urlNone
authorNone
requires_python>=3.7
licenseNone
keywords bundler source code bundle module bundle
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Tsutsumu: A Python Module Bundler and Runtime

> つつむ (tsutsumu), Japanese for bundle

Tsutsumu creates Python module bundles, that is, scripts that contain many more
modules and supporting resources, and imports modules from bundles. That way,
Tsutsumu enables self-contained scripts that can run anywhere a suitable Python
interpreter is available—*without* creating a virtual environment or installing
packages first.

Having said that, Tsutsumu isn't the only option for more easily distributing
Python code and it may very well not be the right option for your use case.
Notably, the standard library's
[`zipapp`](https://docs.python.org/3/library/zipapp.html) also compresses
bundled files and [pex](https://github.com/pantsbuild/pex) files further combine
bundling with virtual environments. That makes them more sophisticated but also
significantly more heavyweight. Tsutsumu's simplicity makes it best suited to
scripts that import a few dozen modules at most and should remain easily
readable and inspectable before execution.

The rest of this document covers Tsutsumu thusly:

 1. [Just Download and
    Run!](https://github.com/apparebit/tsutsumu#1-just-download-and-run)
 2. [Make a Bundle](https://github.com/apparebit/tsutsumu#2-make-a-bundle)
 3. [The Workings of a
    Bundle](https://github.com/apparebit/tsutsumu#3-the-workings-of-a-bundle)
     1. [Layout of Bundled
        Files](https://github.com/apparebit/tsutsumu#31-layout-of-bundled-files)
     2. [A Bundle's Manifest: Offsets and
        Lengths](https://github.com/apparebit/tsutsumu#32-a-bundles-manifest-offsets-and-lengths)
     3. [On-Disk vs
        In-Memory](https://github.com/apparebit/tsutsumu#33-on-disk-vs-in-memory)
     4. [Meta-Circular
        Bundling](https://github.com/apparebit/tsutsumu#34-meta-circular-bundling)
     5. [importlib.resources Considered
        Harmful](https://github.com/apparebit/tsutsumu#35-importlibresources-considered-harmful)
     6. [Add a Resource Manifest
        Instead](https://github.com/apparebit/tsutsumu#36-add-a-resource-manifest-instead)
 4. [Still Missing](https://github.com/apparebit/tsutsumu#4-still-missing)


## 1. Just Download and Run!

There is nothing to install. There is no virtual environment to set up. Just
download [this one Python
script](https://raw.githubusercontent.com/apparebit/tsutsumu/boss/bundles/bundler.py)
and run it:

```sh
% curl -o tsutsumu.py \
    "https://raw.githubusercontent.com/apparebit/tsutsumu/boss/bundles/bundler.py"
% python tsutsumu.py -h
usage: tsutsumu [-h] [-b] [-m MODULE] [-o FILENAME] [-r] [-v]
                PKGROOT [PKGROOT ...]

Combine Python modules and related resources into a single,
...
```

Yup. I used Tsutsumu to bundle its own modules into `bundler.py`. As a result,
getting started with Tsutsumu is as easy as downloading a file and running it.
Bundled scripts can be this easy and convenient!


### The Complicated Route Still Works

But just in case that you prefer to take the slow and familiar route, you can do
that, too. It just requires 3.5 times more command invocations and takes quite a
bit longer. But sure, here you go:

```sh
% mkdir tsutsumu
% cd tsutsumu
% python -m venv .venv
% source .venv/bin/activate
(.venv) % pip install --upgrade pip
(.venv) % pip install tsutsumu
(.venv) % python -m tsutsumu -h
usage: tsutsumu [-h] [-b] [-m MODULE] [-o FILENAME] [-r] [-v]
                PKGROOT [PKGROOT ...]

Combine Python modules and related resources into a single,
...
```

So how about bundling your Python modules?


## 2. Make a Bundle

The only challenge in making a bundle is in selecting the right directories for
inclusion. Right now, you need to list every package that should be included in
the bundle as a separate directory argument to Tsutsumu. Alas, for most Python
tools and applications, that's just the list of regular dependencies. While
module-level tree-shaking might still be desirable, automating package selection
based on a project's `pyproject.toml` is an obvious next step.

When Tsutsumu traverses provided directories, it currently limits itself to a
few textual formats based on file extension. In particular, it includes plain
text, Markdown, ReStructured Text, HTML, CSS, JavaScript, and most importantly
Python sources. Out of these, Tsutsumu only knows how to execute Python code.
The other files serve as resources. Adding support for Base85-encoded binary
formats seems like another obvious next step.


## 3. The Workings of a Bundle

This section is a hands-on exploration of Tsutsumu's inner workings. Its
implementation is split across the following modules:

  * `tsutsumu` for Tsutsumu's `__version__` and nothing else;
  * `tsutsumu.__main__` for the `main()` entry point and command line interface;
  * `tsutsumu.debug` for validating the manifest and contents of bundle scripts;
  * `tsutsumu.maker` for generating bundles with the `BundleMaker` class;
  * `tsutsumu.bundle` for importing from bundles with the `Bundle` class.

As that breakdown should make clear, `tsutsumu.maker` and `tsutsumu.bundle`
provide the critical two classes that do all the heavy lifting. Hence I'll be
focusing on them in this section. To illustrate their workings, I rely on the
`spam` package also contained in Tsutsumu's source repository. In addition to
its own `__init__` package module, the package contains two Python modules,
`__main__` and `bacon`, as well as a very stylish webpage, `ham.html`.

All subsequent code examples have been validated with Python's `doctest` tool.
Running the tool over this file is part of Tsutsumu's [test
suite](https://github.com/apparebit/tsutsumu/blob/boss/test.py).

Let's get started making a bundle with the contents of the `spam` directory:

```py
>>> import tsutsumu.maker
>>> maker = tsutsumu.maker.BundleMaker(['spam'])
>>> maker
<tsutsumu-maker spam>
>>>
```

The bundle maker ultimately needs to produce a Python script. To get there, the
bundle maker processes data from byte to file granularity, which is quite the
spread. At the same time, it's easy enough to format strings that are entire
lines and, similarly, break down larger blobs into individual lines. Hence, most
bundle maker methods treat the source line as the common unit of abstraction.
However, since files are stored as byte string, not character strings, and byte
counts do matter, those source lines are `bytes`, *not* `str`, and include
newlines, *just* `\n`.

Having said that, the bundle maker starts out by iterating over the contents of
directories, yielding the files to be bundled. For each such file, it yields an
operating-system-specific `Path`—suitable for reading the file's contents from
the local file system—as well as a relative `str` key with forward slashes—for
identifying the file in the bundle's manifest. Here are the bundle maker's keys
for `spam`:

```py
>>> files = list(sorted(maker.list_files(), key=lambda f: f.key))
>>> for file in files:
...     print(file.key)
...
spam/__init__.py
spam/__main__.py
spam/bacon.py
spam/ham.html
>>>
```

Those are just the four files we expect:

  * `spam/__init__.py` contains `spam`'s package module;
  * `spam/__main__.py` is the package's main entry point;
  * `spam/bacon.poy` contains the `spam.bacon` submodule;
  * `spam/ham.html` is a package resource.


### 3.1 Layout of Bundled Files

Now that we know which files to include in the bundle, we can turn to their
layout in bundle scripts. The current format tries to reconcile two
 contradictory requirements: First, the layout must be valid Python source code.
That pretty much limits us to string literals for file names and contents.
Furthermore, since the collection of file names and contents obviously forms a
mapping, we might as well use a `dict` literal for the file data.

Second, the code must not retain the bundled data. Otherwise, all bundled files
are loaded into memory at startup and remain there for the duration of the
application's runtime. Ideally, the Python runtime doesn't even instantiate the
`dict` literal and just scans for its end. To facilitate that, the bundle script
does not assign the `dict` literal to a variable and, on top of that, includes
it only inside an `if False:` branch.

```py
>>> writeall = tsutsumu.maker.BundleMaker.writeall
>>> writeall(tsutsumu.maker._BUNDLE_START_IFFY.splitlines(keepends=True))
if False: {
>>>
```

I'm not sure whether CPython actually takes this use case into account during
parsing. In fact, I'd be surprised if it did. However, I also do know that
Donald Knuth's TeX (which dates back to the late 1970s) does optimize just this
case: Once TeX knows that a conditional branch is not taken, it scans upcoming
tokens, taking only `\if` (and variations thereof), `\else`, and `\fi` into
account, until it has found the end of the branch, resuming regular processing
thereafter.

In the hope that Python is just as clever, we next emit the dictionary contents
as file name, content pairs for each bundled file. We start with
`spam/__init__.py`:

```py
>>> writeall(maker.emit_text_file(*files[0]))
# ------------------------------------------------------------------------------
"spam/__init__.py": b"print('spam/__init__.py')\n",
>>>
```

As illustrated above, the file name or key is a `str` literal, whereas the file
contents are a `bytes` literal. The latter is more appropriate for file contents
because files store bytestrings, too. That means that bundle maker is yielding
lines of bytestrings that contain string and bytestring literals both. Ooh...

Let's process the other three files:

```py
>>> writeall(maker.emit_text_file(*files[1]))
# ------------------------------------------------------------------------------
"spam/__main__.py":
b"""print('spam/__main__.py')
import spam.bacon
<BLANKLINE>
print('also:', __file__)
""",
>>> writeall(maker.emit_text_file(*files[2]))
# ------------------------------------------------------------------------------
"spam/bacon.py": b"print('spam/bacon.py')\n",
>>> writeall(maker.emit_text_file(*files[3]))
# ------------------------------------------------------------------------------
"spam/ham.html":
b"""<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<title>Ham?</title>
<style>
* {
    margin: 0;
    padding: 0;
}
html {
    height: 100%;
}
body {
    min-height: 100%;
    display: grid;
    justify-content: center;
    align-content: center;
}
p {
    font-family: system-ui, sans-serif;
    font-size: calc(32vmin + 4vmax);
    font-weight: bolder;
}
</style>
<p>Ham!
""",
>>>
```

Now we can close the dictionary again:

```py
>>> writeall(tsutsumu.maker._BUNDLE_STOP.splitlines(keepends=True))
}
<BLANKLINE>
>>>
```


### 3.2 A Bundle's Manifest: Offsets and Lengths

A bundle's files are encoded as a `dict` literal by design—so that the script
parses—but are *not* assigned to any variable by design as well—so that the
script does not retain access to the data, which would only increase memory
pressure. So if the script doesn't retain a reference to the data, how does
it access the data when it's needed?

I've already hinted at the solution: While turning file names and contents into
yielded lines of the bundle script, the bundle maker tracks the byte offset and
length of each content literal. It helps that the bundle maker is implemented as
a class with several methods that are generators instead of as a bunch of
generator functions. That way, accumulating state while yielding lines only
requires another method call, with the state stored by the bundle maker
instance. It also helps that the bundle maker emits bundle contents first, at
the beginning of the content script and that it relies on named string constants
for the boilerplate before, between, and right after the file contents
dictionary.

Once the bundle maker is done with the file contents, it emits the manifest
with the offset and length for each file included in the bundle:

```py
>>> writeall(maker.emit_manifest())
# ==============================================================================
<BLANKLINE>
__manifest__ = {
    "spam/__init__.py": (305, 30),
    "spam/__main__.py": (438, 77),
    "spam/bacon.py": (615, 27),
    "spam/ham.html": (742, 382),
}
>>>
```

The data collected while yielding the file contents is one datum more granular
than offset and length. But the generator for the manifest consumes the output
of another generator that accumulates the original three length values per file.
As you can see, Tsutsumu's not so secret sauce are generator functions and
methods!

Tsutsumu's source repository does not just include the `spam` package. But its
so far tiny collection of [prebundled
scripts](https://github.com/apparebit/tsutsumu/tree/boss/bundles) includes
`can.py`, which already bundles the package. If you check `can.py`'s contents,
you should see the exact same files in the same order with the same offsets and
lengths. That means that we can use the bundle to illustrate how the bundle
runtime reads a file such as `spam/bacon.py`:

```py
>>> with open('bundles/can.py', mode='rb') as file:
...     _ = file.seek(615)
...     data = file.read(27)
...
>>> data
b'b"print(\'spam/bacon.py\')\\n"'
>>>
```

As you can see, the returned bytes aren't just the file contents, but also the
leading and trailing characters necessary for turning the contents into a valid
Python bytestring literal. We need those "decorations" in the script, so that
Python knows to parse the bytestring. But why read those extra characters?

Python bytestring literals represent 256 values per byte with ASCII characters
only. As a result, some code points necessarily require escape sequences. In
fact, there are more code points that require escaping than printable ASCII
characters. Nonetheless, this is a reasonable encoding for this domain because
Python source code draws on ASCII mostly and remains human-readable under the
encoding.

Still, we can't escape escape sequences—as the above example illustrates. Notice
the trailing `\\n`? That's an escaped newline taking up two bytes in the
bytestring. So why read a bytestring, as indicated by the leading `b'`,
containing a bytestring literal, as indicated by the subsequent `b"`, when we
really want proper bytes?

Here's why:

```py
>>> eval(data)
b"print('spam/bacon.py')\n"
>>>
```

It only takes an `eval` to turn two consecutive bytestring prefixes and
backslash characters into one each, producing real `bytes`.


### 3.3 On-Disk vs In-Memory

As presented so far, **bundled files are named by relative paths with forward
slashes**. That makes sense for bundle scripts while they are inert and being
distributed. After all, the raison d'être for Tsutsumu's bundle scripts is to be
easily copied to just about any computer and run right there. That wouldn't be
practical if the names used in the bundle were tied to the originating file
system or limited to some operating system only.

However, the naming requirements change fundamentally the moment a bundle starts
to execute on some computer. That instance should seamlessly integrate with the
local Python runtime and operating system, while also tracking provenance, i.e.,
whether modules originate from the bundle or from the local machine. In other
words, a **running bundle uses absolute paths with the operating system's path
segment separator**. Sure enough, the constructor for `tsutsumu.bundle.Bundle`
performs the translation from relative, system-independent paths to absolute,
system-specific paths by joining the absolute path to the bundle script with
each key.

Let's see how that plays out in practice on the example of the `can.py` bundle:

```py
>>> import bundles.can
>>> manifest = bundles.can.__manifest__
>>> for key in manifest.keys():
...     print(key)
...
spam/__init__.py
spam/__main__.py
spam/bacon.py
spam/ham.html
>>>
```

Clearly, the `__manifest__` is using relative paths.

Since `bundles.can` isn't `__main__`, importing the bundle resulted in the
definition of the `__manifest__` dictionary and the `Bundle` class but it did
not install a new `Bundle` instance in the module loading machinery. Before we
manually install the bundle, there's a bit of housekeeping to do. We need to cut
off our ability to load modules from the regular file system. Otherwise, we
might inadvertently import the `spam` package from its sources and get mightily
confused. (Not that that ever happened to me...)

```py
>>> bundles.can.Bundle.restrict_sys_path()
>>> import spam
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'spam'
>>>
```

No more readily available `spam`? Time to open `bundles.can`:

```py
>>> from pathlib import Path
>>> can_path = Path('.').absolute() / 'bundles' / 'can.py'
>>> version = bundles.can.__version__
>>> can_content = bundles.can.Bundle.install(can_path, manifest, version)
>>> import spam
spam/__init__.py
>>>
```

W00t, our supply of spam is secured. That's great. But how does it work? What
did `Bundle.install()` do exactly?

Well, a `Bundle` is what `importlib`'s documentation calls an *importer*, a
class that is both a *meta path finder* and a *loader*. When Python tries to
load a module that hasn't yet been loaded, it (basically) invokes
`find_spec(name)` on each object in `sys.meta_path`, asking that meta path
finder whether it recognizes the module. If the meta path finder does, it
returns a description of the module. Most fields of that *spec* are just
informative, i.e., strings, but one field, surprisingly called `loader`, is an
object with methods for loading and executing the module's Python code. It just
happens that `Bundle` does not delegate to a separate class for loading but does
all the work itself.

In short, `Bundle.install()` creates a new `Bundle()` and makes that bundle the
first entry of `sys.meta_path`.

Ok. But what about the bundle using absolute paths?

```py
>>> for key in can_content._manifest.keys():
...     path = Path(key)
...     assert path.is_absolute()
...     print(str(path.relative_to(can_path)).replace('\\', '/'))
...
spam/__init__.py
spam/__main__.py
spam/bacon.py
spam/ham.html
>>>
```

Clearly, the installed `can_content` bundle is using absolute paths. Also, each
key now starts with the bundle script's path, which we recreated in `CAN`. While
we usually don't worry much about these paths when importing modules in Python,
we do need to use them when loading resources from a package:

```py
>>> data = can_content.get_data(can_path / 'spam' / 'ham.html')
>>> data[-5:-1]
b'Ham!'
>>>
```

Ham! it is.

My apologies to vegetarians. You probably are tired of all this ham-fisted humor
by now. So let's make sure we stop right here:

```py
>>> can_content.uninstall()
>>> import spam.bacon
Traceback (most recent call last):
  ...
ModuleNotFoundError: No module named 'spam.bacon'
>>>
```

Alas, already imported modules are much harder to expunge. In fact, it may just
be impossible. In this case, however, it is feasible:

```py
>>> import sys
>>> 'spam' in sys.modules
True
>>> import spam
>>> del sys.modules['spam']
>>> 'spam' in sys.modules
False
>>> import spam
Traceback (most recent call last):
  ...
ModuleNotFoundError: No module named 'spam'
>>>
```


### 3.4 Meta-Circular Bundling

Tsutsumu can bundle any application that is not too large and written purely in
Python. That includes itself. Tsutsumu can bundle itself because it avoids the
file system when including its own `tsutsumu/bundle.py` in the bundle script.
Instead, it uses the module loader's `get_data()` method, which is designed for
accessing packaged resources and whose use I just demonstrated.

One drawback of Tsutsumu treating its own source code just like other Python
files is the effective duplication of `tsutsumu/bundle.py`, once as part of the
bundled files and once as part of the bundle script itself. While that may be
desirable, for example, when experimenting with a new version of Tsutsumu, it
also wastes almost 8 kb. To avoid that overhead, you can use the
`-r`/`--repackage` command line option when bundling Tsutsumu. Under that
option, Tsutsumu special cases the `tsutsumu` and `tsutsumu.bundle` modules and
recreates them during startup—with `tsutsumu`'s `bundle` attribute referencing
the `tsutsumu.bundle` module and `tsutsumu.bundle`'s `Bundle` attribute
referencing the corresponding class.


## 3.5 importlib.resources Considered Harmful

While Tsutsumu does support `Loader.get_data()`, it does *not* support the more
recent `Loader.get_resource_reader()` and probably never will. The API simply is
too complex for what it does, i.e., providing yet another way of traversing a
hierarchy of directory-like and file-like entities. Furthermore, the
documentation's claims about the benefits of integration with Python's import
machinery seem farfetched at best.

A look at the 8 (!) modules implementing `importlib.resources` in the standard
library bears this out: In addition to the documented `ResourceReader`,
`Traversable`, and `TraversableReader` abstract classes, there are undocumented
`FileReader`, `ZipReader`, and `NamespaceReader` implementations, the
`SimpleReader` fallback implementation, and the `CompatibilityFiles` adapter.
Furthermore, since `Traversable` is designed to have "a subset of `pathlib.Path`
methods," the code in `importlib.resources` makes heavy use of the `Path`
implementations provided by `pathlib` and `zipfile`. Taken together, that's a
lot of code for exposing a hierarchy of directory- and file-like entities.
Worse, despite the documentation's claims to the contrary, none of this code
leverages core `importlib` machinery—besides hanging off loaders and hence
touching on `ModuleType` and `ModuleSpec`. In fact, it doesn't even integrate
with the previous resource API, the much simpler `get_data()` method on loaders.
In summary, `importlib.resources` does not offer what it claims and is far too
complex for what it offers. It should be scrapped!


### 3.6 Add a Resource Manifest Instead

When you compare the two ways of accessing resources, `Loader.get_data()` and
`Loader.get_resource_reader()`, the latter obviously wins on traversing a
package's namespace. But that's a non-feature when it comes to resource access.
When code needs a resource, it shouldn't need to search for the resource by
searching them all, it should be able to just access the resource, possibly
through one level of indirection. In other words, if a package's resources may
vary, the package should include a resource manifest at a well-known location,
say, `manifest.toml` relative to the package's path. Once the package includes a
manifest, `Loader.get_data()` more than suffices for retrieving resources.
`Loader.get_resource_reader()` only adds useless complexity.


## 4. Still Missing

I believe that Tsutsumu is ready for real-world use. However, since it hasn't
seen wide usage, I'd hold off on mission-critical deployments for now.
Meanwhile, Tsutsumu could use a few more features. I can think of three:

  * [ ] Automatically determine module dependencies
  * [ ] Support inclusion of binary files in bundles
  * [ ] Support the bundling of namespace packages

What else?

---

Tsutsumu is © 2023 [Robert Grimm](https://apparebit.com) and has been released
under the Apache 2.0 license.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "tsutsumu",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": "bundler,source code bundle,module bundle",
    "author": null,
    "author_email": "Robert Grimm <rgrimm@alum.mit.edu>",
    "download_url": "https://files.pythonhosted.org/packages/49/98/715b802e4ef376aca7cca4bac6dacf06e1f195807e213aeb496d3b44ba44/tsutsumu-0.1.0.tar.gz",
    "platform": null,
    "description": "# Tsutsumu: A Python Module Bundler and Runtime\n\n> \u3064\u3064\u3080 (tsutsumu), Japanese for bundle\n\nTsutsumu creates Python module bundles, that is, scripts that contain many more\nmodules and supporting resources, and imports modules from bundles. That way,\nTsutsumu enables self-contained scripts that can run anywhere a suitable Python\ninterpreter is available\u2014*without* creating a virtual environment or installing\npackages first.\n\nHaving said that, Tsutsumu isn't the only option for more easily distributing\nPython code and it may very well not be the right option for your use case.\nNotably, the standard library's\n[`zipapp`](https://docs.python.org/3/library/zipapp.html) also compresses\nbundled files and [pex](https://github.com/pantsbuild/pex) files further combine\nbundling with virtual environments. That makes them more sophisticated but also\nsignificantly more heavyweight. Tsutsumu's simplicity makes it best suited to\nscripts that import a few dozen modules at most and should remain easily\nreadable and inspectable before execution.\n\nThe rest of this document covers Tsutsumu thusly:\n\n 1. [Just Download and\n    Run!](https://github.com/apparebit/tsutsumu#1-just-download-and-run)\n 2. [Make a Bundle](https://github.com/apparebit/tsutsumu#2-make-a-bundle)\n 3. [The Workings of a\n    Bundle](https://github.com/apparebit/tsutsumu#3-the-workings-of-a-bundle)\n     1. [Layout of Bundled\n        Files](https://github.com/apparebit/tsutsumu#31-layout-of-bundled-files)\n     2. [A Bundle's Manifest: Offsets and\n        Lengths](https://github.com/apparebit/tsutsumu#32-a-bundles-manifest-offsets-and-lengths)\n     3. [On-Disk vs\n        In-Memory](https://github.com/apparebit/tsutsumu#33-on-disk-vs-in-memory)\n     4. [Meta-Circular\n        Bundling](https://github.com/apparebit/tsutsumu#34-meta-circular-bundling)\n     5. [importlib.resources Considered\n        Harmful](https://github.com/apparebit/tsutsumu#35-importlibresources-considered-harmful)\n     6. [Add a Resource Manifest\n        Instead](https://github.com/apparebit/tsutsumu#36-add-a-resource-manifest-instead)\n 4. [Still Missing](https://github.com/apparebit/tsutsumu#4-still-missing)\n\n\n## 1. Just Download and Run!\n\nThere is nothing to install. There is no virtual environment to set up. Just\ndownload [this one Python\nscript](https://raw.githubusercontent.com/apparebit/tsutsumu/boss/bundles/bundler.py)\nand run it:\n\n```sh\n% curl -o tsutsumu.py \\\n    \"https://raw.githubusercontent.com/apparebit/tsutsumu/boss/bundles/bundler.py\"\n% python tsutsumu.py -h\nusage: tsutsumu [-h] [-b] [-m MODULE] [-o FILENAME] [-r] [-v]\n                PKGROOT [PKGROOT ...]\n\nCombine Python modules and related resources into a single,\n...\n```\n\nYup. I used Tsutsumu to bundle its own modules into `bundler.py`. As a result,\ngetting started with Tsutsumu is as easy as downloading a file and running it.\nBundled scripts can be this easy and convenient!\n\n\n### The Complicated Route Still Works\n\nBut just in case that you prefer to take the slow and familiar route, you can do\nthat, too. It just requires 3.5 times more command invocations and takes quite a\nbit longer. But sure, here you go:\n\n```sh\n% mkdir tsutsumu\n% cd tsutsumu\n% python -m venv .venv\n% source .venv/bin/activate\n(.venv) % pip install --upgrade pip\n(.venv) % pip install tsutsumu\n(.venv) % python -m tsutsumu -h\nusage: tsutsumu [-h] [-b] [-m MODULE] [-o FILENAME] [-r] [-v]\n                PKGROOT [PKGROOT ...]\n\nCombine Python modules and related resources into a single,\n...\n```\n\nSo how about bundling your Python modules?\n\n\n## 2. Make a Bundle\n\nThe only challenge in making a bundle is in selecting the right directories for\ninclusion. Right now, you need to list every package that should be included in\nthe bundle as a separate directory argument to Tsutsumu. Alas, for most Python\ntools and applications, that's just the list of regular dependencies. While\nmodule-level tree-shaking might still be desirable, automating package selection\nbased on a project's `pyproject.toml` is an obvious next step.\n\nWhen Tsutsumu traverses provided directories, it currently limits itself to a\nfew textual formats based on file extension. In particular, it includes plain\ntext, Markdown, ReStructured Text, HTML, CSS, JavaScript, and most importantly\nPython sources. Out of these, Tsutsumu only knows how to execute Python code.\nThe other files serve as resources. Adding support for Base85-encoded binary\nformats seems like another obvious next step.\n\n\n## 3. The Workings of a Bundle\n\nThis section is a hands-on exploration of Tsutsumu's inner workings. Its\nimplementation is split across the following modules:\n\n  * `tsutsumu` for Tsutsumu's `__version__` and nothing else;\n  * `tsutsumu.__main__` for the `main()` entry point and command line interface;\n  * `tsutsumu.debug` for validating the manifest and contents of bundle scripts;\n  * `tsutsumu.maker` for generating bundles with the `BundleMaker` class;\n  * `tsutsumu.bundle` for importing from bundles with the `Bundle` class.\n\nAs that breakdown should make clear, `tsutsumu.maker` and `tsutsumu.bundle`\nprovide the critical two classes that do all the heavy lifting. Hence I'll be\nfocusing on them in this section. To illustrate their workings, I rely on the\n`spam` package also contained in Tsutsumu's source repository. In addition to\nits own `__init__` package module, the package contains two Python modules,\n`__main__` and `bacon`, as well as a very stylish webpage, `ham.html`.\n\nAll subsequent code examples have been validated with Python's `doctest` tool.\nRunning the tool over this file is part of Tsutsumu's [test\nsuite](https://github.com/apparebit/tsutsumu/blob/boss/test.py).\n\nLet's get started making a bundle with the contents of the `spam` directory:\n\n```py\n>>> import tsutsumu.maker\n>>> maker = tsutsumu.maker.BundleMaker(['spam'])\n>>> maker\n<tsutsumu-maker spam>\n>>>\n```\n\nThe bundle maker ultimately needs to produce a Python script. To get there, the\nbundle maker processes data from byte to file granularity, which is quite the\nspread. At the same time, it's easy enough to format strings that are entire\nlines and, similarly, break down larger blobs into individual lines. Hence, most\nbundle maker methods treat the source line as the common unit of abstraction.\nHowever, since files are stored as byte string, not character strings, and byte\ncounts do matter, those source lines are `bytes`, *not* `str`, and include\nnewlines, *just* `\\n`.\n\nHaving said that, the bundle maker starts out by iterating over the contents of\ndirectories, yielding the files to be bundled. For each such file, it yields an\noperating-system-specific `Path`\u2014suitable for reading the file's contents from\nthe local file system\u2014as well as a relative `str` key with forward slashes\u2014for\nidentifying the file in the bundle's manifest. Here are the bundle maker's keys\nfor `spam`:\n\n```py\n>>> files = list(sorted(maker.list_files(), key=lambda f: f.key))\n>>> for file in files:\n...     print(file.key)\n...\nspam/__init__.py\nspam/__main__.py\nspam/bacon.py\nspam/ham.html\n>>>\n```\n\nThose are just the four files we expect:\n\n  * `spam/__init__.py` contains `spam`'s package module;\n  * `spam/__main__.py` is the package's main entry point;\n  * `spam/bacon.poy` contains the `spam.bacon` submodule;\n  * `spam/ham.html` is a package resource.\n\n\n### 3.1 Layout of Bundled Files\n\nNow that we know which files to include in the bundle, we can turn to their\nlayout in bundle scripts. The current format tries to reconcile two\n contradictory requirements: First, the layout must be valid Python source code.\nThat pretty much limits us to string literals for file names and contents.\nFurthermore, since the collection of file names and contents obviously forms a\nmapping, we might as well use a `dict` literal for the file data.\n\nSecond, the code must not retain the bundled data. Otherwise, all bundled files\nare loaded into memory at startup and remain there for the duration of the\napplication's runtime. Ideally, the Python runtime doesn't even instantiate the\n`dict` literal and just scans for its end. To facilitate that, the bundle script\ndoes not assign the `dict` literal to a variable and, on top of that, includes\nit only inside an `if False:` branch.\n\n```py\n>>> writeall = tsutsumu.maker.BundleMaker.writeall\n>>> writeall(tsutsumu.maker._BUNDLE_START_IFFY.splitlines(keepends=True))\nif False: {\n>>>\n```\n\nI'm not sure whether CPython actually takes this use case into account during\nparsing. In fact, I'd be surprised if it did. However, I also do know that\nDonald Knuth's TeX (which dates back to the late 1970s) does optimize just this\ncase: Once TeX knows that a conditional branch is not taken, it scans upcoming\ntokens, taking only `\\if` (and variations thereof), `\\else`, and `\\fi` into\naccount, until it has found the end of the branch, resuming regular processing\nthereafter.\n\nIn the hope that Python is just as clever, we next emit the dictionary contents\nas file name, content pairs for each bundled file. We start with\n`spam/__init__.py`:\n\n```py\n>>> writeall(maker.emit_text_file(*files[0]))\n# ------------------------------------------------------------------------------\n\"spam/__init__.py\": b\"print('spam/__init__.py')\\n\",\n>>>\n```\n\nAs illustrated above, the file name or key is a `str` literal, whereas the file\ncontents are a `bytes` literal. The latter is more appropriate for file contents\nbecause files store bytestrings, too. That means that bundle maker is yielding\nlines of bytestrings that contain string and bytestring literals both. Ooh...\n\nLet's process the other three files:\n\n```py\n>>> writeall(maker.emit_text_file(*files[1]))\n# ------------------------------------------------------------------------------\n\"spam/__main__.py\":\nb\"\"\"print('spam/__main__.py')\nimport spam.bacon\n<BLANKLINE>\nprint('also:', __file__)\n\"\"\",\n>>> writeall(maker.emit_text_file(*files[2]))\n# ------------------------------------------------------------------------------\n\"spam/bacon.py\": b\"print('spam/bacon.py')\\n\",\n>>> writeall(maker.emit_text_file(*files[3]))\n# ------------------------------------------------------------------------------\n\"spam/ham.html\":\nb\"\"\"<!DOCTYPE html>\n<html lang=en>\n<meta charset=utf-8>\n<title>Ham?</title>\n<style>\n* {\n    margin: 0;\n    padding: 0;\n}\nhtml {\n    height: 100%;\n}\nbody {\n    min-height: 100%;\n    display: grid;\n    justify-content: center;\n    align-content: center;\n}\np {\n    font-family: system-ui, sans-serif;\n    font-size: calc(32vmin + 4vmax);\n    font-weight: bolder;\n}\n</style>\n<p>Ham!\n\"\"\",\n>>>\n```\n\nNow we can close the dictionary again:\n\n```py\n>>> writeall(tsutsumu.maker._BUNDLE_STOP.splitlines(keepends=True))\n}\n<BLANKLINE>\n>>>\n```\n\n\n### 3.2 A Bundle's Manifest: Offsets and Lengths\n\nA bundle's files are encoded as a `dict` literal by design\u2014so that the script\nparses\u2014but are *not* assigned to any variable by design as well\u2014so that the\nscript does not retain access to the data, which would only increase memory\npressure. So if the script doesn't retain a reference to the data, how does\nit access the data when it's needed?\n\nI've already hinted at the solution: While turning file names and contents into\nyielded lines of the bundle script, the bundle maker tracks the byte offset and\nlength of each content literal. It helps that the bundle maker is implemented as\na class with several methods that are generators instead of as a bunch of\ngenerator functions. That way, accumulating state while yielding lines only\nrequires another method call, with the state stored by the bundle maker\ninstance. It also helps that the bundle maker emits bundle contents first, at\nthe beginning of the content script and that it relies on named string constants\nfor the boilerplate before, between, and right after the file contents\ndictionary.\n\nOnce the bundle maker is done with the file contents, it emits the manifest\nwith the offset and length for each file included in the bundle:\n\n```py\n>>> writeall(maker.emit_manifest())\n# ==============================================================================\n<BLANKLINE>\n__manifest__ = {\n    \"spam/__init__.py\": (305, 30),\n    \"spam/__main__.py\": (438, 77),\n    \"spam/bacon.py\": (615, 27),\n    \"spam/ham.html\": (742, 382),\n}\n>>>\n```\n\nThe data collected while yielding the file contents is one datum more granular\nthan offset and length. But the generator for the manifest consumes the output\nof another generator that accumulates the original three length values per file.\nAs you can see, Tsutsumu's not so secret sauce are generator functions and\nmethods!\n\nTsutsumu's source repository does not just include the `spam` package. But its\nso far tiny collection of [prebundled\nscripts](https://github.com/apparebit/tsutsumu/tree/boss/bundles) includes\n`can.py`, which already bundles the package. If you check `can.py`'s contents,\nyou should see the exact same files in the same order with the same offsets and\nlengths. That means that we can use the bundle to illustrate how the bundle\nruntime reads a file such as `spam/bacon.py`:\n\n```py\n>>> with open('bundles/can.py', mode='rb') as file:\n...     _ = file.seek(615)\n...     data = file.read(27)\n...\n>>> data\nb'b\"print(\\'spam/bacon.py\\')\\\\n\"'\n>>>\n```\n\nAs you can see, the returned bytes aren't just the file contents, but also the\nleading and trailing characters necessary for turning the contents into a valid\nPython bytestring literal. We need those \"decorations\" in the script, so that\nPython knows to parse the bytestring. But why read those extra characters?\n\nPython bytestring literals represent 256 values per byte with ASCII characters\nonly. As a result, some code points necessarily require escape sequences. In\nfact, there are more code points that require escaping than printable ASCII\ncharacters. Nonetheless, this is a reasonable encoding for this domain because\nPython source code draws on ASCII mostly and remains human-readable under the\nencoding.\n\nStill, we can't escape escape sequences\u2014as the above example illustrates. Notice\nthe trailing `\\\\n`? That's an escaped newline taking up two bytes in the\nbytestring. So why read a bytestring, as indicated by the leading `b'`,\ncontaining a bytestring literal, as indicated by the subsequent `b\"`, when we\nreally want proper bytes?\n\nHere's why:\n\n```py\n>>> eval(data)\nb\"print('spam/bacon.py')\\n\"\n>>>\n```\n\nIt only takes an `eval` to turn two consecutive bytestring prefixes and\nbackslash characters into one each, producing real `bytes`.\n\n\n### 3.3 On-Disk vs In-Memory\n\nAs presented so far, **bundled files are named by relative paths with forward\nslashes**. That makes sense for bundle scripts while they are inert and being\ndistributed. After all, the raison d'\u00eatre for Tsutsumu's bundle scripts is to be\neasily copied to just about any computer and run right there. That wouldn't be\npractical if the names used in the bundle were tied to the originating file\nsystem or limited to some operating system only.\n\nHowever, the naming requirements change fundamentally the moment a bundle starts\nto execute on some computer. That instance should seamlessly integrate with the\nlocal Python runtime and operating system, while also tracking provenance, i.e.,\nwhether modules originate from the bundle or from the local machine. In other\nwords, a **running bundle uses absolute paths with the operating system's path\nsegment separator**. Sure enough, the constructor for `tsutsumu.bundle.Bundle`\nperforms the translation from relative, system-independent paths to absolute,\nsystem-specific paths by joining the absolute path to the bundle script with\neach key.\n\nLet's see how that plays out in practice on the example of the `can.py` bundle:\n\n```py\n>>> import bundles.can\n>>> manifest = bundles.can.__manifest__\n>>> for key in manifest.keys():\n...     print(key)\n...\nspam/__init__.py\nspam/__main__.py\nspam/bacon.py\nspam/ham.html\n>>>\n```\n\nClearly, the `__manifest__` is using relative paths.\n\nSince `bundles.can` isn't `__main__`, importing the bundle resulted in the\ndefinition of the `__manifest__` dictionary and the `Bundle` class but it did\nnot install a new `Bundle` instance in the module loading machinery. Before we\nmanually install the bundle, there's a bit of housekeeping to do. We need to cut\noff our ability to load modules from the regular file system. Otherwise, we\nmight inadvertently import the `spam` package from its sources and get mightily\nconfused. (Not that that ever happened to me...)\n\n```py\n>>> bundles.can.Bundle.restrict_sys_path()\n>>> import spam\nTraceback (most recent call last):\n  File \"<stdin>\", line 1, in <module>\nModuleNotFoundError: No module named 'spam'\n>>>\n```\n\nNo more readily available `spam`? Time to open `bundles.can`:\n\n```py\n>>> from pathlib import Path\n>>> can_path = Path('.').absolute() / 'bundles' / 'can.py'\n>>> version = bundles.can.__version__\n>>> can_content = bundles.can.Bundle.install(can_path, manifest, version)\n>>> import spam\nspam/__init__.py\n>>>\n```\n\nW00t, our supply of spam is secured. That's great. But how does it work? What\ndid `Bundle.install()` do exactly?\n\nWell, a `Bundle` is what `importlib`'s documentation calls an *importer*, a\nclass that is both a *meta path finder* and a *loader*. When Python tries to\nload a module that hasn't yet been loaded, it (basically) invokes\n`find_spec(name)` on each object in `sys.meta_path`, asking that meta path\nfinder whether it recognizes the module. If the meta path finder does, it\nreturns a description of the module. Most fields of that *spec* are just\ninformative, i.e., strings, but one field, surprisingly called `loader`, is an\nobject with methods for loading and executing the module's Python code. It just\nhappens that `Bundle` does not delegate to a separate class for loading but does\nall the work itself.\n\nIn short, `Bundle.install()` creates a new `Bundle()` and makes that bundle the\nfirst entry of `sys.meta_path`.\n\nOk. But what about the bundle using absolute paths?\n\n```py\n>>> for key in can_content._manifest.keys():\n...     path = Path(key)\n...     assert path.is_absolute()\n...     print(str(path.relative_to(can_path)).replace('\\\\', '/'))\n...\nspam/__init__.py\nspam/__main__.py\nspam/bacon.py\nspam/ham.html\n>>>\n```\n\nClearly, the installed `can_content` bundle is using absolute paths. Also, each\nkey now starts with the bundle script's path, which we recreated in `CAN`. While\nwe usually don't worry much about these paths when importing modules in Python,\nwe do need to use them when loading resources from a package:\n\n```py\n>>> data = can_content.get_data(can_path / 'spam' / 'ham.html')\n>>> data[-5:-1]\nb'Ham!'\n>>>\n```\n\nHam! it is.\n\nMy apologies to vegetarians. You probably are tired of all this ham-fisted humor\nby now. So let's make sure we stop right here:\n\n```py\n>>> can_content.uninstall()\n>>> import spam.bacon\nTraceback (most recent call last):\n  ...\nModuleNotFoundError: No module named 'spam.bacon'\n>>>\n```\n\nAlas, already imported modules are much harder to expunge. In fact, it may just\nbe impossible. In this case, however, it is feasible:\n\n```py\n>>> import sys\n>>> 'spam' in sys.modules\nTrue\n>>> import spam\n>>> del sys.modules['spam']\n>>> 'spam' in sys.modules\nFalse\n>>> import spam\nTraceback (most recent call last):\n  ...\nModuleNotFoundError: No module named 'spam'\n>>>\n```\n\n\n### 3.4 Meta-Circular Bundling\n\nTsutsumu can bundle any application that is not too large and written purely in\nPython. That includes itself. Tsutsumu can bundle itself because it avoids the\nfile system when including its own `tsutsumu/bundle.py` in the bundle script.\nInstead, it uses the module loader's `get_data()` method, which is designed for\naccessing packaged resources and whose use I just demonstrated.\n\nOne drawback of Tsutsumu treating its own source code just like other Python\nfiles is the effective duplication of `tsutsumu/bundle.py`, once as part of the\nbundled files and once as part of the bundle script itself. While that may be\ndesirable, for example, when experimenting with a new version of Tsutsumu, it\nalso wastes almost 8 kb. To avoid that overhead, you can use the\n`-r`/`--repackage` command line option when bundling Tsutsumu. Under that\noption, Tsutsumu special cases the `tsutsumu` and `tsutsumu.bundle` modules and\nrecreates them during startup\u2014with `tsutsumu`'s `bundle` attribute referencing\nthe `tsutsumu.bundle` module and `tsutsumu.bundle`'s `Bundle` attribute\nreferencing the corresponding class.\n\n\n## 3.5 importlib.resources Considered Harmful\n\nWhile Tsutsumu does support `Loader.get_data()`, it does *not* support the more\nrecent `Loader.get_resource_reader()` and probably never will. The API simply is\ntoo complex for what it does, i.e., providing yet another way of traversing a\nhierarchy of directory-like and file-like entities. Furthermore, the\ndocumentation's claims about the benefits of integration with Python's import\nmachinery seem farfetched at best.\n\nA look at the 8 (!) modules implementing `importlib.resources` in the standard\nlibrary bears this out: In addition to the documented `ResourceReader`,\n`Traversable`, and `TraversableReader` abstract classes, there are undocumented\n`FileReader`, `ZipReader`, and `NamespaceReader` implementations, the\n`SimpleReader` fallback implementation, and the `CompatibilityFiles` adapter.\nFurthermore, since `Traversable` is designed to have \"a subset of `pathlib.Path`\nmethods,\" the code in `importlib.resources` makes heavy use of the `Path`\nimplementations provided by `pathlib` and `zipfile`. Taken together, that's a\nlot of code for exposing a hierarchy of directory- and file-like entities.\nWorse, despite the documentation's claims to the contrary, none of this code\nleverages core `importlib` machinery\u2014besides hanging off loaders and hence\ntouching on `ModuleType` and `ModuleSpec`. In fact, it doesn't even integrate\nwith the previous resource API, the much simpler `get_data()` method on loaders.\nIn summary, `importlib.resources` does not offer what it claims and is far too\ncomplex for what it offers. It should be scrapped!\n\n\n### 3.6 Add a Resource Manifest Instead\n\nWhen you compare the two ways of accessing resources, `Loader.get_data()` and\n`Loader.get_resource_reader()`, the latter obviously wins on traversing a\npackage's namespace. But that's a non-feature when it comes to resource access.\nWhen code needs a resource, it shouldn't need to search for the resource by\nsearching them all, it should be able to just access the resource, possibly\nthrough one level of indirection. In other words, if a package's resources may\nvary, the package should include a resource manifest at a well-known location,\nsay, `manifest.toml` relative to the package's path. Once the package includes a\nmanifest, `Loader.get_data()` more than suffices for retrieving resources.\n`Loader.get_resource_reader()` only adds useless complexity.\n\n\n## 4. Still Missing\n\nI believe that Tsutsumu is ready for real-world use. However, since it hasn't\nseen wide usage, I'd hold off on mission-critical deployments for now.\nMeanwhile, Tsutsumu could use a few more features. I can think of three:\n\n  * [ ] Automatically determine module dependencies\n  * [ ] Support inclusion of binary files in bundles\n  * [ ] Support the bundling of namespace packages\n\nWhat else?\n\n---\n\nTsutsumu is \u00a9 2023 [Robert Grimm](https://apparebit.com) and has been released\nunder the Apache 2.0 license.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Python module bundles",
    "version": "0.1.0",
    "project_urls": {
        "Repository": "https://github.com/apparebit/tsutsumu"
    },
    "split_keywords": [
        "bundler",
        "source code bundle",
        "module bundle"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "2f43a26276d7a1b9725e020a7563b2a87619cd72a75191cda26c7e452e71ca23",
                "md5": "5d3713f92ee812388afd639132553cfc",
                "sha256": "51e4512ede4a791a4e011b5e8c666c78de28adeb6faae24c5a098409e22ff3d5"
            },
            "downloads": -1,
            "filename": "tsutsumu-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5d3713f92ee812388afd639132553cfc",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 19646,
            "upload_time": "2023-07-07T20:00:23",
            "upload_time_iso_8601": "2023-07-07T20:00:23.174884Z",
            "url": "https://files.pythonhosted.org/packages/2f/43/a26276d7a1b9725e020a7563b2a87619cd72a75191cda26c7e452e71ca23/tsutsumu-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "4998715b802e4ef376aca7cca4bac6dacf06e1f195807e213aeb496d3b44ba44",
                "md5": "68fa5338fa4736d4e11535e8af27e6bc",
                "sha256": "a40a27266f25b045a3394e0afc8f143ae674974fee2e4b391c01dd98bcc90b19"
            },
            "downloads": -1,
            "filename": "tsutsumu-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "68fa5338fa4736d4e11535e8af27e6bc",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 35805,
            "upload_time": "2023-07-07T20:00:25",
            "upload_time_iso_8601": "2023-07-07T20:00:25.263696Z",
            "url": "https://files.pythonhosted.org/packages/49/98/715b802e4ef376aca7cca4bac6dacf06e1f195807e213aeb496d3b44ba44/tsutsumu-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-07 20:00:25",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "apparebit",
    "github_project": "tsutsumu",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "tsutsumu"
}
        
Elapsed time: 0.08731s