Debian Codegraph @

guides/dev/README.md

raw symbols SCIP
1 # Development guide for aws-crt-python
2
3 This guide is for contributors to aws-crt-python's source code.
4 Familiarity (but not necessarily expertise) with Python and C is assumed.
5 This document covers basic setup and workflow.
6
7 For more advanced topics, see [writing-code.md](writing-code.md).
8
9 ### Table of Contents
10
11 * [Git](#git)
12 * [CMake](#cmake)
13 * [Set up a Virtual Environment](#set-up-a-virtual-environment)
14 * [Install](#install)
15 * [Run Tests](#run-tests)
16 * [Environment Variables for Tests](#environment-variables-for-tests)
17 * [Using an IDE](#using-an-ide)
18 * [Visual Studio Code](#using-visual-studio-code-vscode)
19 * [Debugging Python](#debugging-python-with-vscode)
20 * [Debugging C](#debugging-c-with-vscode)
21
22 ## Git
23
24 Clone to a development folder:
25 ```sh
26 $ git clone [email protected]:awslabs/aws-crt-python.git
27 $ cd aws-crt-python
28 $ git submodule update --init
29 ```
30
31 Note that you MUST manually update submodules any time you pull latest, or change branches:
32 ```sh
33 $ git submodule update
34 ```
35
36 ## CMake
37
38 CMake 3 is required to compile the C submodules. To install:
39
40 * On Mac, using homebrew:
41 ```sh
42 $ brew install cmake
43 ```
44 * On Linux: use your system's package manager (apt, yum, etc).
45 * On Windows: [download and install](https://cmake.org/download/)
46
47 ## Install Python Dev Libraries and Header files
48
49 Install the libraries and headers necessary for Python development.
50 These might already be installed. If you're not sure just skip ahead, and come back
51 to this step if you get build errors like: `Python.h: No such file or directory`
52
53 If you installed Python via [python.org](https://www.python.org/downloads/),
54 then you already have these files.
55
56 If you installed Python via a package manager, you might need the "dev" package.
57 i.e. `sudo apt install python3-dev` or `sudo yum install python3-devel`
58
59 ## Set up a Virtual Environment
60
61 Set up a [virtual environment](https://docs.python.org/3/library/venv.html)
62 for development. This guide suggests `aws-crt-python/.venv/` as a default location.
63 Create a virtual environment like so:
64 ```sh
65 $ python3 -m venv .venv/
66 ```
67
68 To activate the virtual environment in your current terminal:
69 * On Mac or Linux:
70 ```sh
71 $ source .venv/bin/activate
72 ```
73 * In Windows PowerShell:
74 ```pwsh
75 > .venv\Scripts\Activate.ps1
76 ```
77 * In Windows Command Prompt:
78 ```bat
79 > .venv\Scripts\Activate.bat
80 ```
81
82 Your terminal looks something like this when the virtual environment is active:
83 ```
84 (.venv) $
85 ```
86 Now any time you type `python3` or `pip` or even `python`, you are using the
87 one from your virtual environment.
88 To stop using the virtual environment, run `deactivate`.
89
90 ## Install
91
92 Ensure `pip` (Python's package manager) is installed and up to date:
93 ```sh
94 (.venv) $ python3 -m ensurepip --upgrade
95 ```
96
97 Install aws-crt-python, and its dev dependencies (helper script `python3 scripts/install-dev.py` does this):
98 ```sh
99 (.venv) $ python3 -m pip install --verbose --editable ".[dev]"
100 ```
101
102 You must re-run this command any time the C source code changes.
103 But you don't need to re-run it if .py files change
104 (thanks to the `--editable` aka "develop mode" flag)
105
106 Note that this takes about twice as long on Mac, which compiles C for both `x86_64` and `arm64`.
107 (TODO: in develop mode, for productivity's sake, only compile C for one architecture,
108 and ignore resulting warnings from the linker)
109
110 ## Run Tests
111
112 To run all tests:
113 ```sh
114 (.venv) $ python3 -m unittest discover --failfast --verbose
115 ```
116
117 `discover` automatically finds all tests to run
118
119 `--failfast` stops after one failed test.
120 This is useful because a failed test is likely to leak memory,
121 which will cause the rest of the tests to fail as well.
122
123 `--verbose` makes it easier to see which tests are skipped.
124
125 To run specific tests, specify a path. For example:
126 ```sh
127 (.venv) $ python3 -m unittest --failfast --verbose test.test_http_client.TestClient.test_connect_http
128 ```
129
130 More path examples:
131 * `test` - everything under `test/` folder
132 * `test.test_http_client` - every test in `test_http_client.py`
133 * `test.test_http_client.TestClient` - every test in `TestClient` class
134 * `test.test_http_client.TestClient.test_connect_http` - A single test
135
136 When creating new tests, note that the names of test files and test functions must be prefixed with `test_`.
137
138 ### Environment Variables for Tests
139 Many tests require an AWS account. These tests are skipped unless
140 specific environment variables are set:
141
142 * MQTT Tests
143 * `AWS_TEST_IOT_MQTT_ENDPOINT` - AWS IoT Core endpoint. This is specific to your account.
144 * `AWS_TEST_TLS_CERT_PATH` - file path to the certificate used to initialize the TLS context of the MQTT connection
145 * `AWS_TEST_TLS_KEY_PATH` - file path to the private key used to initialize the TLS context of the MQTT connection
146 * `AWS_TEST_TLS_ROOT_CERT_PATH` - file path to the root CA used to initialize the TLS context of the MQTT connection
147 * PKCS#11 Tests
148 * `AWS_TEST_PKCS11_LIB` - path to PKCS#11 library
149 * `AWS_TEST_PKCS11_PIN` - user PIN for logging into PKCS#11 token
150 * `AWS_TEST_PKCS11_TOKEN_LABEL` - label of PKCS#11 token
151 * `AWS_TEST_PKCS11_KEY_LABEL` - label of private key on PKCS#11 token, which must correspond to the cert at `AWS_TEST_TLS_CERT_PATH`.
152 * Proxy Tests
153 * TLS-protected connections to the proxy
154 * `AWS_TEST_HTTPS_PROXY_HOST` - proxy host address
155 * `AWS_TEST_HTTPS_PROXY_PORT` - proxy port
156 * Open connections to the proxy
157 * `AWS_TEST_HTTP_PROXY_HOST` - proxy host address
158 * `AWS_TEST_HTTP_PROXY_PORT` - port port
159 * Open connections to the proxy, with basic authentication
160 * `AWS_TEST_HTTP_PROXY_BASIC_HOST` - proxy host address
161 * `AWS_TEST_HTTP_PROXY_BASIC_PORT` - proxy port
162 * `AWS_TEST_BASIC_AUTH_USERNAME` - username
163 * `AWS_TEST_BASIC_AUTH_PASSWORD` - password
164 * S3 Tests
165 * `AWS_TEST_S3` - set to any value to enable S3 tests.
166 * **Unfortunately, at this time these tests can only be run by members of the
167 Common Runtime team, due to hardcoded paths.**
168 * TODO: alter tests so anyone can run them.
169
170 ## Code Formatting
171
172 We use automatic code formatters in this project and pull requests will fail unless
173 the code is formatted correctly.
174
175 `autopep8` is used for python code. You installed this earlier via `.[dev]` dependencies.
176
177 For C code `clang-format` is used. You need to install an exact version (see `CLANG_FORMAT_VERSION=...` line at the top of [format-check.py](../../format-check.py)) via [pipx](https://github.com/pypa/pipx). Doing this on MacOS looks like:
178 ```sh
179 (.venv) $ brew install pipx
180 (.venv) $ pipx ensurepath
181 (.venv) $ pipx install clang-format==<CLANG_FORMAT_VERSION from format-check.py>
182 ```
183
184 Use helper scripts to automatically format your code (or configure your IDE to do it):
185 ```sh
186 # format all code
187 (.venv) $ python3 scripts/format-all.py
188
189 # just format Python files
190 (.venv) $ python3 scripts/format-python.py
191
192 # just format C files
193 (.venv) $ python3 format-check.py -i
194 ```
195
196 ## Using an IDE
197 ### Using Visual Studio Code (VSCode)
198
199 1) Install the following extensions:
200 * Python (Microsoft)
201 * C/C++ (Microsoft)
202 * Code Spell Checker (Street Side Software) - optional
203
204 2) Open the `aws-crt-python/` folder.
205
206 3) Use your virtual environment: `cmd+shift+p -> Python: Select Interpreter`
207
208 4) Edit workspace settings: `cmd+shift+P -> Preferences: Open Workspace Settings`
209 * `Python > Terminal: Activate Env In Current Terminal` - ("python.terminal.activateEnvInCurrentTerminal" in json view)
210 * Set to true, so the VSCode terminal will always use your virtual environment.
211 * But your current terminal isn't using it. Kill the current terminal or reload the window to resolve this.
212 * `C_Cpp > Default: Include Path` - ("C_Cpp.default.includePath" in json view)
213 * Add item - set path to Python's C headers.
214 For example: `/Library/Frameworks/Python.framework/Versions/3.10/include/python3.10`
215 * This is optional, it helps IntelliSense when viewing C files.
216 * `Files: Insert Final Newline` - ("files.insertFinalNewline" in json view)
217 * Set to true. It's just good practice.
218 * `Files: Trim Trailing Whitespace` - ("files.trimTrailingWhitespace" in json view)
219 * Set to true. It's just good practice.
220
221 5) Add helpful tasks you can run via `cmd+shift+P -> Tasks: Run Task`
222 * Copy [this file](vscode/tasks.json) to `aws-crt-python/.vscode/tasks.json` for the following tasks:
223 * `install` - `pip install` in develop mode. `cmd+shift+B` is a special shortcut for this task
224 * `format` - format all code files
225
226 #### Debugging Python with VSCode
227 The VSCode `Testing` tab (lab flask/beaker icon) helps run and debug Python tests.
228 From this tab, click Configure Python Tests:
229 * Select a test framework/tool to enable - unittest
230 * Select the directory containing the tests - test
231 * Select the pattern to identify test files - test_*.py
232
233 Run tests by mousing over the name and clicking the PLAY button,
234 or use the DEBUG button to hit breakpoints in Python code, and see output in the DEBUG CONSOLE.
235
236 Note that many tests are skipped unless [specific environment variables](#environment-variables-for-tests) are set.
237 You can set these in a `aws-crt-python/.env` file (don't worry it's ignored by git. For example:
238 ```
239 AWS_TEST_IOT_MQTT_ENDPOINT=xxxxxxxxx-ats.iot.xxxxx.amazonaws.com
240 AWS_TEST_TLS_CERT_PATH=/Users/janedoe/iot/xxxxx-certificate.pem.crt
241 AWS_TEST_TLS_KEY_PATH=/Users/janedoe/iot/xxxxx-private.pem.key
242 ```
243
244 #### Debugging C with VSCode
245 Unfortunately, we haven't figured out how to do interactive debugging of the C code.
246 Python ultimately builds and links the C module together, and it seems to always strip out the debug info.
247 Please update this guide if you know how. For now, `printf()` is your best option.
248
249 If you suspect the bug is in the external C code (i.e. [aws-c-http](https://github.com/awslabs/aws-c-http))
250 and need to do interactive debugging, your best bet is cloning that external project,
251 build it in Debug, and step through its tests.
252
253 # TODO
254 * more about git submodules (latest-submodules.py and working from branches)
255 * more about logging. consider easy way to turn on logging in tests
256