Project

General

Profile

Hacking Python SDK » History » Version 21

Nico César, 06/05/2015 07:34 PM

1 1 Tom Clegg
h1. Hacking Python SDK
2
3
{{toc}}
4
5
h2. Prerequisites
6
7 6 Brett Smith
The FUSE driver requires associated libraries to build:
8 4 Tom Clegg
9
<pre>
10 5 Tom Clegg
sudo apt-get install libattr1-dev libfuse-dev pkg-config fuse
11
sudo adduser "$USER" fuse
12
sudo chmod g+rw /dev/fuse
13
sudo chown root:fuse /dev/fuse
14 1 Tom Clegg
</pre>
15 5 Tom Clegg
16
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
20
<pre>
21
cd
22
git clone https://github.com/curoverse/arvados.git
23
</pre>
24
25 6 Brett Smith
h2. virtualenv
26
27
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
29
To build the virtualenv, run:
30
31
<pre>
32 21 Nico César
$ sudo apt-get install virtualenv
33 6 Brett Smith
$ virtualenv --setuptools VENVDIR
34
</pre>
35
36
(@VENVDIR@ can be a directory anywhere you like, although best practice is to keep it outside your source directory.)
37
38
To set up the shell to use the isolated virtualenv environment, run:
39
40
<pre>
41
$ source VENVDIR/bin/activate
42
</pre>
43
44
To learn more about using and configuring virtualenv, read the "virtualenv usage documentation":https://virtualenv.pypa.io/en/latest/virtualenv.html#usage.
45
46 1 Tom Clegg
h2. Run tests
47
48
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
# Build a client library package and install it to the virtualenv
52
# Run the FUSE driver test suite
53
# Build a FUSE driver package and install it to the virtualenv
54
55
Note: The test suite brings up a Keep server and an API server to run tests against. For best results:
56
* Try [[Hacking Keep]] and [[Hacking API Server]] to make sure you have all the right dependencies for running the Keep and API servers.
57
* Make sure you have a blob_signing_key in services/api/config/application.yml
58 11 Tom Clegg
* Install the keepstore binary.
59
** Make sure your GOPATH points somewhere, e.g.: @export GOPATH=~/gocode; mkdir -p $GOPATH@
60
** Install keepstore: @go get git.curoverse.com/arvados.git/services/keepstore@
61
** (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
<pre>
66 6 Brett Smith
source VENVDIR/bin/activate
67 1 Tom Clegg
68
cd ~/arvados/sdk/python
69 9 Tom Clegg
python setup.py test
70 18 Brett Smith
python setup.py install
71 1 Tom Clegg
72
cd ~/arvados/services/fuse
73 7 Brett Smith
python setup.py test
74 18 Brett Smith
python setup.py install
75 1 Tom Clegg
</pre>
76 10 Brett Smith
77 12 Tom Clegg
h3. Run a single test or test class
78
79
<pre>
80
source VENVDIR/bin/activate
81
cd ~/arvados/sdk/python
82
83 13 Tom Clegg
# One test module
84 12 Tom Clegg
python setup.py test --test-suite tests.test_keep_locator
85 1 Tom Clegg
86 13 Tom Clegg
# One test class
87
python setup.py test --test-suite tests.test_keep_locator.ArvadosKeepLocatorTest
88
89
# One test case
90 12 Tom Clegg
python setup.py test --test-suite tests.test_keep_locator.ArvadosKeepLocatorTest.base_locators
91 1 Tom Clegg
</pre>
92 12 Tom Clegg
93
h2. Builds and versioning
94 15 Brett Smith
95 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]@.
96 15 Brett Smith
97 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.
98 15 Brett Smith
99 10 Brett Smith
h2. Logging
100
101
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:
102
103
<pre><code class="python">
104
import arvados
105
import logging
106
107
logger = logging.getLogger('arvados.YOURTHING')
108
</code></pre>
109
110
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.
111
112
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.
113 14 Peter Amstutz
114 20 Peter Amstutz
h2. Documenting your code
115 19 Peter Amstutz
116
See "PEP 257":https://www.python.org/dev/peps/pep-0257/ and "PEP 287":https://www.python.org/dev/peps/pep-0287/
117
118 14 Peter Amstutz
h2. Python buffer protocol
119
120
Notes on managing buffers efficiently in Python, we don't use this in the python sdk as of this writing (but we might).
121
122
http://eli.thegreenplace.net/2011/11/28/less-copies-in-python-with-the-buffer-protocol-and-memoryviews/
123
124
Example using bytearray() to allocate a buffer, memoryview() to create a writable slice, and readinto() to write directly to the buffer slice:
125
126
<pre>
127
>>> b = bytearray(20)
128
>>> c = memoryview(b)
129
>>> f = open("python.txt", "r")
130
>>> f.readinto(c[5:10])
131
5
132
>>> b
133
bytearray(b"\x00\x00\x00\x00\x00I\'ve \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
134
</pre>