Hacking Python SDK » History » Version 23

Bryan Cosca, 08/04/2015 06:54 PM

1 1 Tom Clegg
h1. Hacking Python SDK
2 1 Tom Clegg
3 1 Tom Clegg
{{toc}}
4 1 Tom Clegg
5 1 Tom Clegg
h2. Prerequisites
6 1 Tom Clegg
7 6 Brett Smith
The FUSE driver requires associated libraries to build:
8 4 Tom Clegg
9 4 Tom Clegg
<pre>
10 5 Tom Clegg
sudo apt-get install libattr1-dev libfuse-dev pkg-config fuse
11 5 Tom Clegg
sudo adduser "$USER" fuse
12 5 Tom Clegg
sudo chmod g+rw /dev/fuse
13 5 Tom Clegg
sudo chown root:fuse /dev/fuse
14 1 Tom Clegg
</pre>
15 5 Tom Clegg
16 5 Tom Clegg
After installing @fuse@ and adding yourself to the @fuse@ group, you need to start a new login session. Make sure the @groups@ command reports that you're in the @fuse@ group.
17 4 Tom Clegg
18 1 Tom Clegg
h2. Get the source code
19 1 Tom Clegg
20 1 Tom Clegg
<pre>
21 1 Tom Clegg
cd
22 1 Tom Clegg
git clone https://github.com/curoverse/arvados.git
23 1 Tom Clegg
</pre>
24 1 Tom Clegg
25 6 Brett Smith
h2. virtualenv
26 6 Brett Smith
27 6 Brett Smith
virtualenv helps you isolate the dependencies for a specific package or environment, much like Bundler does for our Rails applications.  The recommended way to deploy is to build a virtualenv for Arvados development.
28 6 Brett Smith
29 6 Brett Smith
To build the virtualenv, run:
30 6 Brett Smith
31 6 Brett Smith
<pre>
32 23 Bryan Cosca
$ sudo apt-get install python-virtualenv
33 6 Brett Smith
$ virtualenv --setuptools VENVDIR
34 6 Brett Smith
</pre>
35 6 Brett Smith
36 6 Brett Smith
(@VENVDIR@ can be a directory anywhere you like, although best practice is to keep it outside your source directory.)
37 6 Brett Smith
38 6 Brett Smith
To set up the shell to use the isolated virtualenv environment, run:
39 6 Brett Smith
40 6 Brett Smith
<pre>
41 6 Brett Smith
$ source VENVDIR/bin/activate
42 6 Brett Smith
</pre>
43 6 Brett Smith
44 6 Brett Smith
To learn more about using and configuring virtualenv, read the "virtualenv usage documentation":https://virtualenv.pypa.io/en/latest/virtualenv.html#usage.
45 6 Brett Smith
46 1 Tom Clegg
h2. Run tests
47 1 Tom Clegg
48 1 Tom Clegg
Strategy:
49 6 Brett Smith
# Set up the environment to use a dedicated virtualenv
50 1 Tom Clegg
# Run the client library test suite
51 1 Tom Clegg
# Build a client library package and install it to the virtualenv
52 1 Tom Clegg
# Run the FUSE driver test suite
53 1 Tom Clegg
# Build a FUSE driver package and install it to the virtualenv
54 1 Tom Clegg
55 1 Tom Clegg
Note: The test suite brings up a Keep server and an API server to run tests against. For best results:
56 1 Tom Clegg
* Try [[Hacking Keep]] and [[Hacking API Server]] to make sure you have all the right dependencies for running the Keep and API servers.
57 1 Tom Clegg
* Make sure you have a blob_signing_key in services/api/config/application.yml
58 11 Tom Clegg
* Install the keepstore binary.
59 11 Tom Clegg
** Make sure your GOPATH points somewhere, e.g.: @export GOPATH=~/gocode; mkdir -p $GOPATH@
60 11 Tom Clegg
** Install keepstore: @go get git.curoverse.com/arvados.git/services/keepstore@
61 11 Tom Clegg
** (if you don't do anything special, this fetches "master" from git.curoverse.com -- if you want to build a version of keepstore with local modifications, see [[Hacking Keep]])
62 4 Tom Clegg
63 6 Brett Smith
Script (make sure to edit the first line to refer to your virtualenv):
64 1 Tom Clegg
65 1 Tom Clegg
<pre>
66 22 Nico César
apt-get install libcurl4-openssl-dev python-dev libssl-dev
67 6 Brett Smith
source VENVDIR/bin/activate
68 1 Tom Clegg
69 1 Tom Clegg
cd ~/arvados/sdk/python
70 9 Tom Clegg
python setup.py test
71 18 Brett Smith
python setup.py install
72 1 Tom Clegg
73 1 Tom Clegg
cd ~/arvados/services/fuse
74 7 Brett Smith
python setup.py test
75 18 Brett Smith
python setup.py install
76 1 Tom Clegg
</pre>
77 10 Brett Smith
78 12 Tom Clegg
h3. Run a single test or test class
79 12 Tom Clegg
80 12 Tom Clegg
<pre>
81 12 Tom Clegg
source VENVDIR/bin/activate
82 12 Tom Clegg
cd ~/arvados/sdk/python
83 12 Tom Clegg
84 13 Tom Clegg
# One test module
85 12 Tom Clegg
python setup.py test --test-suite tests.test_keep_locator
86 1 Tom Clegg
87 13 Tom Clegg
# One test class
88 13 Tom Clegg
python setup.py test --test-suite tests.test_keep_locator.ArvadosKeepLocatorTest
89 13 Tom Clegg
90 13 Tom Clegg
# One test case
91 12 Tom Clegg
python setup.py test --test-suite tests.test_keep_locator.ArvadosKeepLocatorTest.base_locators
92 1 Tom Clegg
</pre>
93 12 Tom Clegg
94 12 Tom Clegg
h2. Builds and versioning
95 15 Brett Smith
96 18 Brett Smith
When we build each Python package, the version number is generated from the most recent commit that affected the package.  The format is @0.1.[commit's timestamp formatted as %Y%m%d%H%M%S].[commit's short hash]@.
97 15 Brett Smith
98 18 Brett Smith
If you want to make changes in one package and refer to it somewhere else (e.g., have the FUSE package depend on a specific SDK version), you should commit your changes to the original package before anything else.  Then you can consistently refer to the metadata from that dedicated commit in subsequent changes.
99 15 Brett Smith
100 10 Brett Smith
h2. Logging
101 10 Brett Smith
102 10 Brett Smith
The Python SDK uses Python's built-in logging module to log errors, warnings, and debug messages.  The arvados module sets up logging for messages under "arvados" based on local configuration (e.g., the @ARVADOS_DEBUG@ setting).  Other SDK modules and command-line tools should @import arvados@ and then send messages to a logger under "arvados" to ensure consistent log handling.  Typical setup looks like this:
103 10 Brett Smith
104 10 Brett Smith
<pre><code class="python">
105 10 Brett Smith
import arvados
106 10 Brett Smith
import logging
107 10 Brett Smith
108 10 Brett Smith
logger = logging.getLogger('arvados.YOURTHING')
109 10 Brett Smith
</code></pre>
110 10 Brett Smith
111 10 Brett Smith
Once you've set this up, you can send messages to the logger using methods like @logger.debug()@ and @logger.error()@.  See the "Logger class documentation":https://docs.python.org/2/library/logging.html#logger-objects for full details.
112 10 Brett Smith
113 10 Brett Smith
Command-line scripts may reconfigure the @arvados.logger@ object based on additional configuration like command-line switches.  @services/fuse/bin/arv-mount@ demonstrates adjusting the level and setting a custom log handler.
114 14 Peter Amstutz
115 20 Peter Amstutz
h2. Documenting your code
116 19 Peter Amstutz
117 19 Peter Amstutz
See "PEP 257":https://www.python.org/dev/peps/pep-0257/ and "PEP 287":https://www.python.org/dev/peps/pep-0287/
118 19 Peter Amstutz
119 14 Peter Amstutz
h2. Python buffer protocol
120 14 Peter Amstutz
121 14 Peter Amstutz
Notes on managing buffers efficiently in Python, we don't use this in the python sdk as of this writing (but we might).
122 14 Peter Amstutz
123 14 Peter Amstutz
http://eli.thegreenplace.net/2011/11/28/less-copies-in-python-with-the-buffer-protocol-and-memoryviews/
124 14 Peter Amstutz
125 14 Peter Amstutz
Example using bytearray() to allocate a buffer, memoryview() to create a writable slice, and readinto() to write directly to the buffer slice:
126 14 Peter Amstutz
127 14 Peter Amstutz
<pre>
128 14 Peter Amstutz
>>> b = bytearray(20)
129 14 Peter Amstutz
>>> c = memoryview(b)
130 14 Peter Amstutz
>>> f = open("python.txt", "r")
131 14 Peter Amstutz
>>> f.readinto(c[5:10])
132 14 Peter Amstutz
5
133 14 Peter Amstutz
>>> b
134 14 Peter Amstutz
bytearray(b"\x00\x00\x00\x00\x00I\'ve \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
135 14 Peter Amstutz
</pre>