Skip to content

Commit 9ef5766

Browse files
authored
Merge branch 'micropython:master' into master
2 parents bf5f853 + c860319 commit 9ef5766

File tree

114 files changed

+5865
-374
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+5865
-374
lines changed

.github/workflows/build_packages.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Build all packages
2+
3+
on: [push, pull_request]
4+
5+
env:
6+
PACKAGE_INDEX_PATH: /tmp/micropython-lib-deploy
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v2
13+
- uses: actions/setup-python@v1
14+
- name: Setup environment
15+
run: source tools/ci.sh && ci_build_packages_setup
16+
- name: Check manifest files
17+
run: source tools/ci.sh && ci_build_packages_check_manifest
18+
- name: Compile package index
19+
run: source tools/ci.sh && ci_build_packages_compile_index
20+
- name: Publish packages for branch
21+
if: vars.MICROPY_PUBLISH_MIP_INDEX && github.event_name == 'push' && ! github.event.deleted
22+
run: source tools/ci.sh && ci_push_package_index
23+
- name: Upload packages as artifact
24+
uses: actions/upload-artifact@v3
25+
with:
26+
name: packages-${{ github.sha }}
27+
path: ${{ env.PACKAGE_INDEX_PATH }}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: Cleanup published packages
2+
3+
on: delete
4+
5+
jobs:
6+
cleanup:
7+
runs-on: ubuntu-latest
8+
if: vars.MICROPY_PUBLISH_MIP_INDEX
9+
steps:
10+
- uses: actions/checkout@v2
11+
- name: Clean up published files
12+
run: source tools/ci.sh && ci_cleanup_package_index ${{ github.event.ref }}

CODEOFCONDUCT.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Please see the [MicroPython Code of Conduct](https://github.com/micropython/micropython/blob/master/CODEOFCONDUCT.md).

CONTRIBUTING.md

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,117 @@
1-
If you submit a pull request, please adhere to Contributor Guidelines:
1+
## Contributor's Guidelines & Code Conventions
22

3-
https://github.com/micropython/micropython-lib/wiki/ContributorGuidelines
3+
micropython-lib follows the same general conventions as the [main MicroPython
4+
repository](https://github.com/micropython/micropython). Please see
5+
[micropython/CONTRIBUTING.md](https://github.com/micropython/micropython/blob/master/CONTRIBUTING.md)
6+
and [micropython/CODECONVENTIONS.md](https://github.com/micropython/micropython/blob/master/CODECONVENTIONS.md).
7+
8+
### Raising issues
9+
10+
Please include enough information for someone to reproduce the issue you are
11+
describing. This will typically include:
12+
13+
* The version of MicroPython you are using (e.g. the firmware filename, git
14+
hash, or version info printed by the startup message).
15+
* What board/device you are running MicroPython on.
16+
* Which package you have installed, how you installed it, and what version.
17+
When installed via `mip`, all packages will have a `__version__`
18+
attribute.
19+
* A simple code snippet that demonstrates the issue.
20+
21+
If you have a how-to question or are looking for help with using MicroPython
22+
or packages from micropython-lib, please post at the
23+
[discussion forum](https://github.com/orgs/micropython/discussions) instead.
24+
25+
### Pull requests
26+
27+
The same rules for commit messages, signing-off commits, and commit structure
28+
apply as for the main MicroPython repository. All Python code is formatted
29+
using `black`. See [`tools/codeformat.py`](tools/codeformat.py) to apply
30+
`black` automatically before submitting a PR.
31+
32+
There are some specific conventions and guidelines for micropython-lib:
33+
34+
* The first line of the commit message should start with the name of the
35+
package, followed by a short description of the commit. Package names are
36+
globally unique in the micropython-lib directory structure.
37+
38+
For example: `shutil: Add disk_usage function.`
39+
40+
* Although we encourage keeping the code short and minimal, please still use
41+
comments in your code. Typically, packages will be installed via
42+
`mip` and so they will be compiled to bytecode where comments will
43+
_not_ contribute to the installed size.
44+
45+
* All packages must include a `manifest.py`, including a `metadata()` line
46+
with at least a description and a version.
47+
48+
* Prefer to break larger packages up into smaller chunks, so that just the
49+
required functionality can be installed. The way to do this is to have a
50+
base package, e.g. `mypackage` containing `mypackage/__init__.py`, and then
51+
an "extension" package, e.g. `mypackage-ext` containing additional files
52+
e.g. `mypackage/ext.py`. See
53+
[`collections-defaultdict`](python-stdlib/collections-defaultdict) as an
54+
example.
55+
56+
* If you think a package might be extended in this way in the future, prefer
57+
to create a package directory with `package/__init__.py`, rather than a
58+
single `module.py`.
59+
60+
* Packages in the python-stdlib directory should be CPython compatible and
61+
implement a subset of the CPython equivalent. Avoid adding
62+
MicroPython-specific extensions. Please include a link to the corresponding
63+
CPython docs in the PR.
64+
65+
* Include tests (ideally using the `unittest` package) as `test_*.py`.
66+
Otherwise, provide examples as `example_*.py`. When porting CPython
67+
packages, prefer to use the existing tests rather than writing new ones
68+
from scratch.
69+
70+
* When porting an existing third-party package, please ensure that the source
71+
license is compatible.
72+
73+
* To make it easier for others to install packages directly from your PR before
74+
it is merged, consider opting-in to automatic package publishing (see
75+
[Publishing packages from forks](#publishing-packages-from-forks)). If you do
76+
this, consider quoting the [commands to install
77+
packages](README.md#installing-packages-from-forks) in your Pull Request
78+
description.
79+
80+
### Publishing packages from forks
81+
82+
You can easily publish the packages from your micropython-lib
83+
[fork](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/about-forks)
84+
by opting in to a system based on [GitHub
85+
Actions](https://docs.github.com/en/actions) and [GitHub
86+
Pages](https://docs.github.com/en/pages):
87+
88+
1. Open your fork's repository in the GitHub web interface.
89+
2. Navigate to "Settings" -> "Secrets and variables" -> "Actions" -> "Variables".
90+
3. Click "New repository variable"
91+
4. Create a variable named `MICROPY_PUBLISH_MIP_INDEX` with value `true` (or any
92+
"truthy" value).
93+
5. The settings for GitHub Actions and GitHub Pages features should not need to
94+
be changed from the repository defaults, unless you've explicitly disabled
95+
them.
96+
97+
The next time you push commits to a branch in your fork, GitHub Actions will run
98+
an additional step in the "Build All Packages" workflow named "Publish Packages
99+
for branch".
100+
101+
Anyone can then install these packages as described under [Installing packages
102+
from forks](README.md#installing-packages-from-forks). The exact commands are also
103+
quoted in the GitHub Actions log for the "Publish Packages for branch" step.
104+
105+
#### Opting Back Out
106+
107+
To opt-out again, delete the `MICROPY_PUBLISH_MIP_INDEX` variable and
108+
(optionally) delete the `gh-pages` branch from your fork.
109+
110+
*Note*: While enabled, all micropython-lib packages will be published each time
111+
a change is pushed to any branch in your fork. A commit is added to the
112+
`gh-pages` branch each time. In a busy repository, the `gh-pages` branch may
113+
become quite large. The actual `.git` directory size on disk should still be
114+
quite small, as most of the content will be duplicated. If you're worried that
115+
the `gh-pages` branch has become too large then you can always delete this
116+
branch from GitHub. GitHub Actions will create a new `gh-pages` branch the next
117+
time you push a change.

README.md

Lines changed: 161 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,172 @@
1-
micropython-lib
2-
===============
1+
# micropython-lib
32

4-
This is a repository of libraries designed to be useful for writing
5-
MicroPython applications.
3+
This is a repository of packages designed to be useful for writing MicroPython
4+
applications.
65

7-
The libraries here fall into four categories corresponding to the four top-level directories:
6+
The packages here fall into categories corresponding to the four top-level
7+
directories:
88

9-
* **python-stdlib**: Compatible versions of modules from the [Python Standard Library](https://docs.python.org/3/library/). These should be drop-in replacements for the Python libraries, although many have reduced functionality or missing methods or classes (which may not be an issue for many most cases).
9+
* **python-stdlib**: Compatible versions of modules from [The Python Standard
10+
Library](https://docs.python.org/3/library/). These should be drop-in
11+
replacements for the corresponding Python modules, although many have
12+
reduced functionality or missing methods or classes (which may not be an
13+
issue for most cases).
1014

11-
* **python-ecosys**: Compatible, but reduced-functionality versions of modules from the larger Python ecosystem, for example that might be found in the [Python Package Index](https://pypi.org/).
15+
* **python-ecosys**: Compatible, but reduced-functionality versions of
16+
packages from the wider Python ecosystem. For example, a package that
17+
might be found in the [Python Package Index](https://pypi.org/).
1218

13-
* **micropython**: MicroPython-specific modules that do not have equivalents in other Python environments. These are typically hardware drivers or highly-optimised alternative implementations of functionality available in other Python modules.
19+
* **micropython**: MicroPython-specific packages that do not have equivalents
20+
in other Python environments. This includes drivers for hardware
21+
(e.g. sensors, peripherals, or displays), libraries to work with
22+
embedded functionality (e.g. bluetooth), or MicroPython-specific
23+
packages that do not have equivalents in CPython.
1424

15-
* **unix-ffi**: These modules are specifically for the MicroPython Unix port and provide access to operating-system and third-party libraries via FFI.
25+
* **unix-ffi**: These packages are specifically for the MicroPython Unix port
26+
and provide access to operating-system and third-party libraries via FFI,
27+
or functionality that is not useful for non-Unix ports.
1628

17-
Usage
18-
-----
29+
## Usage
1930

20-
Many libraries are self contained modules, and you can quickly get started by
21-
copying the relevant Python file to your device. For example, to add the
22-
`base64` library, you can directly copy `python-stdlib/base64/base64.py` to the `lib`
23-
directory on your device.
31+
To install a micropython-lib package, there are four main options. For more
32+
information see the [Package management documentation](https://docs.micropython.org/en/latest/reference/packages.html)
33+
documentation.
2434

25-
Other libraries are packages, in which case you'll need to copy the directory instead. For example, to add `collections.defaultdict`, copy `collections/collections/__init__.py` and `collections.defaultdict/collections/defaultdict.py` to a directory named `lib/collections` on your device.
35+
### On a network-enabled device
2636

27-
Future plans (and new contributor ideas)
28-
----------------------------------------
37+
As of MicroPython v1.20 (and nightly builds since October 2022), boards
38+
with WiFi and Ethernet support include the `mip` package manager.
39+
40+
```py
41+
>>> import mip
42+
>>> mip.install("package-name")
43+
```
44+
45+
### Using `mpremote` from your PC
46+
47+
`mpremote` is the officially-supported tool for interacting with a MicroPython
48+
device and, since v0.4.0, support for installing micropython-lib packages is
49+
provided by using the `mip` command.
50+
51+
```bash
52+
$ mpremote connect /dev/ttyUSB0 mip install package-name
53+
```
54+
55+
See the [mpremote documentation](https://docs.micropython.org/en/latest/reference/mpremote.html).
56+
57+
### Freeze into your firmware
58+
59+
If you are building your own firmware, all packages in this repository include
60+
a `manifest.py` that can be included into your board manifest via the
61+
`require()` command. See [Manifest files](https://docs.micropython.org/en/latest/reference/manifest.html#require) for
62+
more information.
63+
64+
### Copy the files manually
65+
66+
Many micropython-lib packages are just single-file modules, and you can
67+
quickly get started by copying the relevant Python file to your device. For
68+
example, to add the `base64` library, you can directly copy
69+
`python-stdlib/base64/base64.py` to the `lib` directory on your device.
70+
71+
This can be done using `mpremote`, for example:
72+
73+
```bash
74+
$ mpremote connect /dev/ttyUSB0 cp python-stdlib/base64/base64.py :/lib
75+
```
76+
77+
For packages that are implemented as a package directory, you'll need to copy
78+
the directory instead. For example, to add `collections.defaultdict`, copy
79+
`collections/collections/__init__.py` and
80+
`collections-defaultdict/collections/defaultdict.py` to a directory named
81+
`lib/collections` on your device.
82+
83+
Note that unlike the other three approaches based on `mip` or `manifest.py`,
84+
you will need to manually resolve dependencies. You can inspect the relevant
85+
`manifest.py` file to view the list of dependencies for a given package.
86+
87+
## Installing packages from forks
88+
89+
It is possible to use the `mpremote mip install` or `mip.install()` methods to
90+
install packages built from a
91+
[fork](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/about-forks)
92+
of micropython-lib, if the fork's owner has opted in.
93+
94+
This can be useful to install packages from a pending Pull Request, for example.
95+
96+
First, the owner of the fork must opt-in as described under
97+
[Publishing packages from forks](CONTRIBUTING.md#publishing-packages-from-forks).
98+
99+
After this has happened, each time someone pushes to a branch in that fork then
100+
GitHub Actions will automatically publish the packages to a GitHub Pages site.
101+
102+
To install these packages, use commands such as:
103+
104+
```bash
105+
$ mpremote connect /dev/ttyUSB0 mip install --index https://USERNAME.github.io/micropython-lib/mip/BRANCH_NAME PACKAGE_NAME
106+
```
107+
108+
Or from a networked device:
109+
110+
```py
111+
import mip
112+
mip.install(PACKAGE_NAME, index="https://USERNAME.github.io/micropython-lib/mip/BRANCH_NAME")
113+
```
114+
115+
(Where `USERNAME`, `BRANCH_NAME` and `PACKAGE_NAME` are replaced with the owner
116+
of the fork, the branch the packages were built from, and the package name.)
117+
118+
## Contributing
119+
120+
We use [GitHub Discussions](https://github.com/micropython/micropython/discussions)
121+
as our forum, and [Discord](https://micropython.org/discord) for chat. These
122+
are great places to ask questions and advice from the community or to discuss your
123+
MicroPython-based projects.
124+
125+
The [MicroPython Wiki](https://github.com/micropython/micropython/wiki) is
126+
also used for micropython-lib.
127+
128+
For bugs and feature requests, please [raise an issue](https://github.com/micropython/micropython-lib/issues/new).
129+
130+
We welcome pull requests to add new packages, fix bugs, or add features.
131+
Please be sure to follow the
132+
[Contributor's Guidelines & Code Conventions](CONTRIBUTING.md). Note that
133+
MicroPython is licensed under the [MIT license](LICENSE) and all contributions
134+
should follow this license.
135+
136+
### Future plans (and new contributor ideas)
137+
138+
* Develop a set of example programs using these packages.
139+
* Develop more MicroPython packages for common tasks.
140+
* Expand unit testing coverage.
141+
* Add support for referencing remote/third-party repositories.
142+
143+
## Notes on terminology
144+
145+
The terms *library*, *package*, and *module* are overloaded and lead to some
146+
confusion. The interpretation used in by the MicroPython project is that:
147+
148+
A *library* is a collection of installable packages, e.g. [The Python Standard
149+
Library](https://docs.python.org/3/library/), or micropython-lib.
150+
151+
A *package* can refer to two things. The first meaning, "library package", is
152+
something that can be installed from a library, e.g. via `mip` (or `pip` in
153+
CPython/PyPI). Packages provide *modules* that can be imported. The ambiguity
154+
here is that the module provided by the package does not necessarily have to
155+
have the same name, e.g. the `pyjwt` package provides the `jwt` module. In
156+
CPython, the `pyserial` package providing the `serial` module is another
157+
common example.
158+
159+
A *module* is something that can be imported. For example, "the *os* module".
160+
161+
A module can be implemented either as a single file, typically also called
162+
a *module* or "single-file module", or as a *package* (the second meaning),
163+
which in this context means a directory containing multiple `.py` files
164+
(usually at least an `__init__.py`).
165+
166+
In micropython-lib, we also have the concept of an *extension package* which
167+
is a library package that extends the functionality of another package, by
168+
adding additional files to the same package directory. These packages have
169+
hyphenated names. For example, the `collections-defaultdict` package extends
170+
the `collections` package to add the `defaultdict` class to the `collections`
171+
module.
29172

30-
* Develop a set of example programs using these libraries.
31-
* Develop more MicroPython libraries for common tasks.

micropython/README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
MicroPython-specific libraries
2-
==============================
1+
## MicroPython-specific packages
32

4-
These are libraries that have been written specifically for use on MicroPython.
3+
These are packages that have been written specifically for use on MicroPython.
54

6-
In some cases, the libraries are inspired by or based on equivalent CPython standard libraries, but compatibility varies. The libraries are often named with a "u" prefix.
5+
Packages in this directory should not have the same name as modules from the Python Standard Library.
76

8-
Other libraries have been written specifically for MicroPython use cases.
7+
### Future plans
98

10-
Future plans
11-
------------
12-
13-
* More organised directory structure based on library purpose (e.g. drivers, network, etc).
9+
* More organised directory structure based on purpose (e.g. drivers, network, etc).

micropython/aiorepl/aiorepl.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,18 @@ async def task(g=None, prompt="--> "):
104104
cmd = ""
105105
while True:
106106
b = await s.read(1)
107-
c = ord(b)
108107
pc = c # save previous character
108+
c = ord(b)
109109
pt = t # save previous time
110110
t = time.ticks_ms()
111111
if c < 0x20 or c > 0x7E:
112112
if c == 0x0A:
113-
# CR
113+
# LF
114+
# If the previous character was also LF, and was less
115+
# than 20 ms ago, this was likely due to CRLF->LFLF
116+
# conversion, so ignore this linefeed.
117+
if pc == 0x0A and time.ticks_diff(t, pt) < 20:
118+
continue
114119
sys.stdout.write("\n")
115120
if cmd:
116121
# Push current command.

0 commit comments

Comments
 (0)