13. Module Reference

This chapter contains all the documentation auto-generated from the source code. It is probably not terribly useful for reading through, but may be useful as a searchable reference.

13.1. piwheels.master

13.2. piwheels.master.db

This module defines the low level database API, Database. This is a simple core SQLAlchemy affair which runs trivial queries against the PostgreSQL database. All the serious logic is defined within views in the database itself.

class piwheels.master.db.Database(dsn)[source]

PiWheels database connection class

add_new_package(package, skip='', description='')[source]

Insert a new package record into the database. Returns True if the row was inserted successfully, or False if a key violation occurred.

add_new_package_version(package, version, released=None, skip='')[source]

Insert a new package version record into the database. Returns True if the row was inserted successfully, or False if a key violation occurred.

add_package_name(package, name, seen=None)[source]

Add a new package alias or update the last seen timestamp.

delete_build(package, version)[source]

Remove all builds for the specified package and version, along with all files records.

delete_package(package)[source]

Remove the specified package, along with all builds and files.

delete_version(package, version)[source]

Remove the specified version of the specified package, along with all builds and files.

get_all_build_ids()[source]

Returns a set of all build IDs in the database. This is used by the piw-audit-logs script to check that all build logs are present.

get_all_package_versions()[source]

Returns the set of all known (package, version) tuples.

get_all_packages()[source]

Returns the set of all known package names.

get_build_abis(exclude_skipped=False)[source]

Return a set of ABIs. If exclude_skipped is False, return all ABIs from the build_abis table, otherwise return only active ABIs (not skipped).

get_build_queue(limit=1000)[source]

Returns a mapping of ABI tags to an ordered list of up to limit package version tuples which currently need building for that ABI.

get_initial_statistics()[source]

Return the initial (expensive to calculate) statistics from the database.

get_package_aliases(package)[source]

Retrieve all aliases for package (not including the canonical name itself).

get_package_files(package)[source]

Returns a mapping of filenames to file hashes; this is all the data required to build the simple index.html for the specified package.

get_project_data(package)[source]

Returns all details required to build the project page of the specified package.

get_pypi_serial()[source]

Return the serial number of the last PyPI event.

get_search_index()[source]

Return a mapping of all packages to their download count for the last month. This is used to construct the searchable package index.

get_statistics()[source]

Return various build related statistics from the database.

get_version_files(package, version)[source]

Returns the names of all files for version of package.

get_version_skip(package, version)[source]

Returns the reason for skipping version of package.

get_versions_deleted(package)[source]

Return any versions of package which have been marked for deletion.

load_rewrites_pending()[source]

Loads the rewrites-pending queue (the internal state of TheSecretary) from the database.

log_build(build)[source]

Log a build attempt in the database, including wheel info if successful.

log_download(download)[source]

Log a download in the database, including data derived from JSON in pip’s user-agent.

log_json(json)[source]

Log a project’s JSON page hit in the database.

log_metadata_download(download)[source]

Log a metadata file fetch in the database, including data derived from JSON in pip’s user-agent.

log_page(page)[source]

Log a web page hit in the database.

log_project(project)[source]

Log a project page hit in the database.

Log a search in the database, including data derived from JSON in pip’s user-agent.

package_marked_deleted(package)[source]

Check whether package has been marked for deletion.

save_rewrites_pending(queue)[source]

Save the rewrites-pending queue (the internal state of TheSecretary) in the database. The queue parameter is expected to be a list of RewritePendingRow tuples.

set_package_description(package, description)[source]

Update the description for package in the packages table.

set_pypi_serial(serial)[source]

Update the serial number of the last PyPI event.

skip_package(package, reason)[source]

Mark a package with a reason to prevent future builds of all versions (and all future versions).

skip_package_version(package, version, reason)[source]

Mark a version of a package with a reason to prevent future build attempts.

test_package(package)[source]

Check whether package already exists in the database. Returns a boolean.

test_package_version(package, version)[source]

Check whether version of package already exists in the database. Returns a boolean.

unyank_version(package, version)[source]

Mark the specified version of the specified package version as “unyanked”.

yank_version(package, version)[source]

Mark the specified version of the specified package version as “yanked”.

13.3. piwheels.master.cloud_gazer

13.4. piwheels.master.the_oracle

Defines TheOracle task and the DbClient RPC class for talking to it.

class piwheels.master.the_oracle.TheOracle(config)[source]

This task provides an RPC-like interface to the database; it handles requests such as registering a new package, version, or build, and answering queries about the hashes of files. The primary clients of this class are SlaveDriver, TheScribe, and CloudGazer.

Note that because database requests are notoriously variable in runtime the client RPC class (DbClient) doesn’t directly talk to TheOracle. Rather, multiple instances of TheOracle are spawned and Seraph sits in front of these acting as a simple load-sharing router for the RPC clients.

close()[source]

Close all registered queues. This should be overridden to close any additional queues the task holds which aren’t registered.

handle_db_request(queue)[source]

Handle incoming requests from DbClient instances.

class piwheels.master.the_oracle.DbClient(config, logger=None)[source]

RPC client class for talking to TheOracle.

13.5. piwheels.master.seraph

Defines the Seraph task; see class for more details.

class piwheels.master.seraph.Seraph(config)[source]

This task is a simple load-sharing router for TheOracle tasks.

handle_back(queue)[source]

Receive a response from an instance of TheOracle on the back queue. Strip off the worker’s address frame and add it back to the available queue then send the response back to the client that made the original request.

handle_front(queue)[source]

If any workers are currently available, receive DbClient requests from the front queue and send it on to the worker including the client’s address frame.

13.6. piwheels.master.the_architect

Defines TheArchitect task; see class for more details.

class piwheels.master.the_architect.TheArchitect(config)[source]

This task queries the backend database to determine which versions of packages have yet to be built (and aren’t marked to be skipped). It pushes the results to SlaveDriver to sort out.

close()[source]

Close all registered queues. This should be overridden to close any additional queues the task holds which aren’t registered.

quit()[source]

Overridden to cancel any existing long-running query.

update_build_queue()[source]

The architect simply runs the build queue query repeatedly, with a break of a minute between each execution.

All entries found within this limit are sorted into per-ABI queues and pushed to SlaveDriver which queues and dispatches jobs to build ABI-matched slaves as they become available.

13.7. piwheels.master.slave_driver

Defines the SlaveDriver task; see class for more details.

class piwheels.master.slave_driver.SlaveDriver(config)[source]

This task handles interaction with the build slaves using the slave protocol. Interaction is driven by the slaves (i.e. the master doesn’t push jobs, rather the slaves request a job and the master replies with the next (package, version) tuple from the internal “builds” queue).

The task also incidentally interacts with several other queues: the internal “status” queue is sent details of every reply sent to a build slave (the main_loop() method passes this information on to any listening monitors). Also, the internal “indexes” queue is informed of any packages that need web page indexes re-building (as a result of a successful build).

close()[source]

Close all registered queues. This should be overridden to close any additional queues the task holds which aren’t registered.

do_built(slave)[source]

Handler for the build slave’s “BUILT” message, which is sent after an attempted package build succeeds or fails. The handler logs the result in the database and, if files have been generated by the build, informs the FileJuggler task to expect a file transfer before sending “SEND” back to the build slave with the required filename.

If no files were generated (e.g. in the case of a failed build, or a degenerate success), “DONE” is returned indicating that the build slave is free to discard all resources generated during the build and return to its idle state.

do_busy(slave)[source]

Handler for the build slave’s “BUSY” message, which is sent periodically during package builds. If the slave fails to respond with a BUSY ping for a duration longer than SlaveState.busy_timeout then the master will assume the slave has died and remove it from the internal state mapping (if the slave happens to resurrect itself later the master will simply treat it as a new build slave).

In response to “BUSY” the master can respond “CONT” to indicate the build should continue processing, or “DONE” to indicate that the build slave should immediately terminate and discard the build and return to “IDLE” state.

do_bye(slave)[source]

Handler for the build slave’s final “BYE” message upon shutdown. This removes the associated state from the internal slaves dict.

Parameters:

slave (SlaveState) – The object representing the current status of the build slave.

do_hello(slave)[source]

Handler for the build slave’s initial “HELLO” message. This associates the specified slave state with the slave’s address and returns “HELLO” with the master’s id for the slave (the id communicated back simply for consistency of logging; administrators can correlate master log messages with slave log messages when both have the same id number; we can’t use IP address for this as multiple slaves can run on one machine).

Parameters:

slave (SlaveState) – The object representing the current status of the build slave.

do_idle(slave)[source]

Handler for the build slave’s “IDLE” message (which is effectively the slave requesting work). If the master wants to terminate the slave, it sends back “BYE”. If the build queue (for the slave’s ABI) is empty or the task is currently paused, “SLEEP” is returned indicating the slave should wait a while and then try again.

If a job can be retrieved from the (ABI specific) build queue, then a “BUILD” message is sent back with the required package and version.

Parameters:

slave (SlaveState) – The object representing the current status of the build slave.

do_sent(slave)[source]

Handler for the build slave’s “SENT” message indicating that it’s finished sending the requested file to FileJuggler. The FsClient RPC mechanism is used to ask FileJuggler to verify the transfer against the stored hash and, if this is successful, a message is sent to TheScribe to regenerate the package’s index.

If further files remain to be transferred, another “SEND” message is returned to the build slave. Otherwise, “DONE” is sent to free all build resources.

If a transfer fails to verify, another “SEND” message with the same filename is returned to the build slave.

handle_build(queue)[source]

Refresh the ABI-specific queues of package versions waiting to be built. The queues are limited to 1000 packages per ABI, and are kept as lists ordered by release date. When a message arrives from TheArchitect it refreshes (replaces) all current queues. There is, however, still a duplication possibility as TheArchitect doesn’t know what packages are actively being built; this method handles filtering out such packages.

Even if the active builds fail (because build slaves crash, or the network dies) this doesn’t matter as a future re-run of the build queue query will return these packages again, and if no build slaves are actively working on them at that time they will then be retried.

handle_control(queue)[source]

Handle incoming requests to the internal control queue.

This class understands a couple of extra control messages unique to it, specifically “KILL” to tell a build slave to terminate, “SKIP” to tell a build slave to terminate its current build immediately, and “HELLO” to cause all “HELLO” messages from build slaves to be replayed (for the benefit of a newly attached monitor process).

handle_delete(queue)[source]

Handle package or version deletion requests.

When the PyPI upstream deletes a version or package, the CloudGazer task requests that other tasks perform the deletion on its behalf. In the case of this task, this involves cancelling any pending builds of that package (version), and ignoring any builds involving that package (version) in the next queue update from TheArchitect.

handle_slave(queue)[source]

Handle requests from build slaves.

See the piw-slave chapter for an overview of the protocol for messages between build slaves and SlaveDriver. This method retrieves the message from the build slave, finds the associated SlaveState and updates it with the message, then calls the appropriate message handler. The handler will be expected to return a reply (in the usual form of a list of strings) or None if no reply should be sent (e.g. for a final “BYE” message).

kill_slave(slave_id)[source]

Additional task control method to trigger a “KILL” message to the internal control queue. See handle_control() for more information.

list_slaves()[source]

Additional task control method to trigger a “HELLO” message to the internal control queue. See quit() for more information.

remove_expired()[source]

Remove slaves which have exceeded their timeout.

skip_slave(slave_id)[source]

Additional task control method to trigger a “SKIP” message to the internal control queue. See handle_control() for more information.

sleep_slave(slave_id)[source]

Additional task control method to trigger a “SLEEP” message to the internal control queue. See handle_control() for more information.

wake_slave(slave_id)[source]

Additional task control method to trigger a “WAKE” message to the internal control queue. See handle_control() for more information.

13.8. piwheels.master.mr_chase

13.9. piwheels.master.file_juggler

Defines the FileJuggler task and the FsClient RPC class for interacting with it.

exception piwheels.master.file_juggler.TransferError[source]

Base class for errors raised during a file transfer.

exception piwheels.master.file_juggler.TransferIgnoreChunk[source]

Exception raised when a build slave sends CHUNK instead of HELLO as the first message (see FileJuggler.new_transfer()).

exception piwheels.master.file_juggler.TransferDone[source]

Exception raised when a transfer is complete. It may seem a little odd to use an exception for this, but it is “exceptional” behaviour to terminate the file transfer.

class piwheels.master.file_juggler.FileJuggler(config)[source]

This task handles file transfers from the build slaves. The specifics of the file transfer protocol are best understood from the implementation of the FileState class.

However, to detail how a file transfer begins: when a build slave has successfully completed a build it informs the master via the SlaveDriver task. That task replies with a “SEND” instruction to the slave (including a filename). The slave then initiates the transfer with a “HELLO” message to this task. Once transfers are complete the slave sends a “SENT” message to the SlaveDriver task which verifies the transfer and either retries it (when verification fails) or sends back “DONE” indicating the slave can wipe the source file.

current_transfer(transfer, msg, *args)[source]

Called for messages associated with an existing file transfer.

Usually this is “CHUNK” indicating another chunk of data. Rarely, it can be “HELLO” if the master has fallen silent and dropped tons of packets.

Parameters:
  • transfer (TransferState) – The object representing the state of the transfer.

  • msg (str) – The message sent during the transfer.

  • *args – All additional arguments; for “CHUNK” the first must be the file offset and the second the data to write to that offset.

do_expect(slave_id, file_state)[source]

Message sent by FsClient to inform file juggler that a build slave is about to start a file transfer. The message includes the full FileState. The state is stored in the pending map.

Parameters:
  • slave_id (int) – The identity of the build slave about to begin the transfer.

  • file_state (list) – The details of the file to be transferred including the expected hash.

do_verify(slave_id, package)[source]

Message sent by FsClient to request that juggler verify a file transfer against the expected hash and, if it matches, rename the file into its final location.

Parameters:
  • slave_id (int) – The identity of the build slave that sent the file.

  • package (str) – The name of the package that the file is to be committed to, if valid.

handle_file(queue)[source]

Handle incoming file-transfer messages from build slaves.

The file transfer protocol is in some ways very simple (see the chart in the piw-slave chapter for an overview of the message sequence) and in some ways rather complex (read the ZeroMQ guide chapter on file transfers for more detail on why multiple messages must be allowed in flight simultaneously).

The “normal” state for a file transfer is to be requesting and receiving chunks. Anything else, including redundant re-sends, and transfer completion is handled as an exceptional case.

handle_fs_request(queue)[source]

Handle incoming messages from FsClient instances.

new_transfer(msg, *args)[source]

Called for messages initiating a new file transfer.

The first message must be HELLO along with the id of the slave starting the transfer. The metadata for the transfer will be looked up in the pending list (which is written to by do_expect()).

Parameters:
  • msg (str) – The message sent to start the transfer (must be “HELLO”)

  • *args – All additional arguments (expected to be an integer slave id).

once()[source]

This method is called once before the task loop starts. It the task needs to do some initialization or setup within the task thread, this is the place to do it.

class piwheels.master.file_juggler.FsClient(config, logger=None)[source]

RPC client class for talking to FileJuggler.

expect(slave_id, file_state)[source]

See FileJuggler.do_expect().

verify(slave_id, package)[source]

See FileJuggler.do_verify().

13.10. piwheels.master.big_brother

Defines the BigBrother task; see class for more details.

class piwheels.master.big_brother.BigBrother(config)[source]

This task periodically queries the database and output file-system for various statistics like the number of packages known to the system, the number built, the number of packages built in the last hour, the remaining file-system space, etc. These statistics are written to the internal “status” queue which main_loop() uses to pass statistics to any listening monitors.

close()[source]

Close all registered queues. This should be overridden to close any additional queues the task holds which aren’t registered.

handle_control(queue)[source]

Handle incoming requests to the internal control queue.

This just adds handling for the custom STATS verb to replay the master stats history.

13.11. piwheels.master.the_secretary

Defines the TheSecretary task; see class for more details.

class piwheels.master.the_secretary.TheSecretary(config)[source]

This task buffers requests for the scribe, for the purpose of consolidating multiple consecutive (duplicate) requests.

Requests to write the project page for a package (which is a relatively expensive operation in terms of database accesses) can come in thick and fast, particularly when a new version is being registered with lots of files. There’s little point in writing the project page 5 times in as many seconds, or writing the project page, then the index and project page immediately afterward. This class is used to buffer requests for up to a minute, allowing us to eliminate many of the duplicate requests.

close()[source]

Close all registered queues. This should be overridden to close any additional queues the task holds which aren’t registered.

handle_input(queue)[source]

Handle incoming write requests with buffering and de-dupe.

Some incoming requests (currently “HOME”, “SEARCH”, “LOG”, “DELPKG”, and “DELVER”) are passed directly through to TheScribe as these are either sufficiently rare (“HOME”, “SEARCH”) that no benefit is gained by buffering them or sufficiently urgent (“DELPKG”, “DELVER”, “LOG”) that they must be acted on immediately.

For other requests (“PROJECT” and “BOTH”), requests can come thick and fast in the case of multiple file registrations picked up by CloudGazer. In this case, requests are buffered for a minute and de-duplicated; e.g. if several requests are made to re-write the project page for package “foo” within that period, they will be combined into a single request. After the minute of buffering, the request is passed down to TheScribe.

handle_output()[source]

Passes buffered requests downstream.

This sub-task runs periodically to pluck things from the internal buffer that have reached the minute delay, and passes them downstream to TheScribe. The process stops when we run out of things that have expired.

once()[source]

This method is called once before the task loop starts. It the task needs to do some initialization or setup within the task thread, this is the place to do it.

13.12. piwheels.master.the_scribe

13.13. piwheels.slave

Defines the PiWheelsSlave class. An instance of this is the entry-point for the piw-slave script.

class piwheels.slave.PiWheelsSlave[source]

This is the main class for the piw-slave script. It connects (over 0MQ sockets) to a master (see piw-master) then loops around the slave protocol (see the piw-slave chapter). It retrieves source packages directly from PyPI, attempts to build a wheel in a sandbox directory and, if successful, transmits the results to the master.

clean_up_build(timeout=datetime.timedelta(seconds=60))[source]

Terminate any existing build and clean up its temporary storage. Raises an exception if termination does not occur in a reasonable time.

do_ack(new_id, pypi_url)[source]

In response to our initial “HELLO” (detailing our various PEP 425 tags), the master is expected to send “ACK” back with an integer identifier and the URL of the PyPI repository to download from. We use the identifier in all future log messages for the ease of the administrator.

We reply with “IDLE” to indicate we’re ready to accept a build job.

do_build(package, version)[source]

Alternatively, in response to “IDLE”, the master may send “BUILD” package version. We should then attempt to build the specified wheel and send back a “BUSY” message with more status info.

do_cont()[source]

Once we’re busy building something the master will periodically ping us with “CONT” if it wishes us to continue building. We wait up to 10 seconds for the build to finish and reply with “BUILT” (and a full status report on the build) if it finishes in that time, or “BUSY” and more status info otherwise.

If the master wishes to terminate a build prior to completion, it’ll send “DONE” instead of “CONT” and we move straight to clean-up.

do_die()[source]

The master may respond with “DIE” at any time indicating we should immediately terminate. We raise SystemExit to cause main_loop() to exit. Clean-up of any extant build is handled by our caller.

do_done()[source]

The master can send “DONE” at any point during a built to terminate it prematurely. Alternately, this is also the standard response after a successful build has finished and all files have been sent (and successfully verified).

In response we must clean-up all resources associated with the build (including terminating an on-going build) and return “IDLE” with the usual stats.

do_send(filename)[source]

If a build succeeds and generates files (detailed in a “BUILT” message), the master will reply with “SEND” filename indicating we should transfer the specified file (this is done on a separate socket with a different protocol; see builder.Wheel.transfer() for more details). Once the transfers concludes, reply to the master with “SENT”.

do_sleep(paused)[source]

If, in response to an “IDLE” message we receive “SLEEP” this indicates the master has nothing for us to do currently. Sleep for a little while then try “IDLE” again.

get_status()[source]

Returns the set of statistics periodically required by the master when reporting our status.

handle_reply(msg, data)[source]

Dispatch a message from the master to an appropriate handler method.

main_loop(queue, master_timeout=datetime.timedelta(seconds=300))[source]

The main messaging loop. Sends the initial request, and dispatches replies via handle_reply(). Implements a timeout for responses from the master and raises MasterTimeout if timeout seconds are exceeded.

piwheels.slave.duration(s)[source]

Convert s, a string representing a duration, into a datetime.timedelta.

13.14. piwheels.slave.builder

Defines the classes which use pip to build wheels.

class piwheels.slave.builder.Wheel(path, apt_dependencies=None)[source]

Records the state of a build artifact, i.e. a wheel package. The filename is deconstructed into the fields specified by PEP 425.

Parameters:
  • path (pathlib.Path) – The path to the wheel on the local filesystem.

  • apt_dependencies (set or None) – A set of apt dependencies that are required to use this particular wheel file. Defaults to None (no dependencies).

as_message()[source]

Return the state as a list suitable for use in the BUILT message of piw-slave.

calc_pip_dependencies()[source]

Return the pip dependencies required by the wheel, as a set of package names. This is derived from the Requires-Dist metadata field, and only includes mandatory dependencies (not extras).

open()[source]

Open the wheel in binary mode and return the open file object.

transfer(queue, slave_id)[source]

Transfer the wheel via the specified queue. This is the client side implementation of the file_juggler.FileJuggler protocol.

property abi_tag

Return the ABI part of the wheel’s filename (the penultimate “-” separated element).

property apt_dependencies

Return the apt dependencies required by the wheel.

property build_tag

Return the optional build part of the wheel’s filename (the third “-” separated element when 6 elements exist in total).

property dependencies

Return the dependencies required by the wheel as a mapping of dependency system (e.g. “apt”, “pip”, etc.) to set of package names for that system.

property filehash

Return an SHA256 digest of the wheel’s contents.

property filename

Return the filename of the wheel as a simple string (with no path components).

property filesize

Return the size of the wheel in bytes.

property metadata

Return the contents of the METADATA file inside the wheel.

property orphan_dependencies

Return the orphan dependencies required by the wheel.

property package_canon

Return the package part of the wheel’s filename, canonicalized according to PyPI’s rules.

property package_tag

Return the package part of the wheel’s filename (the first “-” separated element).

property package_version_tag

Return the version part of the wheel’s filename (the second “-” separated element).

property pip_dependencies

Return the pip dependencies required by the wheel.

property platform_tag

Return the platform part of the wheel’s filename (the last “-” separated element).

property py_version_tag

Return the python version part of the wheel’s filename (third from last “-” separated element).

property requires_python

Return the contents of the Requires-Python specification from the wheel metadata.

class piwheels.slave.builder.Builder(package, version, *, timeout=datetime.timedelta(seconds=300), index_url='https://pypi.python.org/simple', extra_index_urls={'https://www.piwheels.org/simple'}, dir=None)[source]

Class responsible for building wheels for a given version of a package. Note that this class derives from Thread and hence is expected to run in the background after calling start().

Parameters:
  • package (str) – The name of the package to attempt to build wheels for.

  • version (str) – The version of the package to attempt to build.

  • timeout (datetime.timedelta) – The number of seconds to wait for pip to finish before raising subprocess.TimeoutExpired.

  • index_url (str) – The URL of the PEP 503 compliant repository from which to fetch packages for building.

  • extra_index_urls (set) – The URLs of any additional PEP 503 compliant repositories from which to fetch packages.

  • dir (str) – The directory in which to store wheel and log output.

as_message()[source]

Return the state as a list suitable for use in the BUILT message of piw-slave.

build_command(log_file)[source]

Generate the pip command line used to run the build.

build_environment()[source]

Configure the environment for the build.

build_wheel(log_file)[source]

Call pip and attempt to build the wheel; handle killing the subprocess if termination is requested, and watch the clock for a build timeout.

calc_apt_dependencies(wheel)[source]

Calculate the apt dependencies of wheel (which is a Wheel instance representing a built wheel).

close()[source]

Remove the temporary build directory and all its contents.

run()[source]

Attempt to build the package within the configured timeout.

stop()[source]

Tell the build to stop prematurely.

property duration

The timedelta indicating how long the actual build took (without any extraneous tasks like dependency calculation). This is an indication of how long a user would spend installing the package without piwheels.

property extra_index_urls

The URLs of any additional indexes from which the builder will also check when retrieving packages. This is intended to be used for fetching compiled platform wheels for specified build dependencies.

property index_url

The URL of primary index from which the builder will attempt to obtain the source to build.

property output

The log output from the build.

property package

The package that the builder will attempt to build.

property status

A bool indicating if the build succeeded or failed. If the build is still on-going, returns None.

property timeout

The timedelta after which the builder will assume the build has failed.

property version

The version of package that the builder will attempt to build.

property wheels

A list of Wheel instances generated by the build.

13.15. piwheels.initdb

Contains the functions that make up the piw-initdb script.

piwheels.initdb.main(args=None)[source]

This is the main function for the piw-initdb script. It creates the piwheels database required by the master or, if it already exists, upgrades it to the current version of the application.

piwheels.initdb.detect_users(conn, test_user)[source]

Test that the user for conn is a cluster superuser (so we can drop and create anything we want in the database), and that test_user (which will be granted limited rights to various objects for the purposes of the piw-master script) exists and is not a cluster superuser.

piwheels.initdb.detect_version(conn)[source]

Detect the version of the database. This is typically done by reading the contents of the configuration table, but before that was added we can guess a couple of versions based on what tables exist (or don’t). Returns None if the database appears uninitialized, and raises RuntimeError is the version is so ancient we can’t do anything with it.

piwheels.initdb.get_connection(dsn)[source]

Return an SQLAlchemy connection to the specified dsn or raise RuntimeError if the database doesn’t exist (the administrator is expected to create the database before running this script).

piwheels.initdb.get_script(version=None)[source]

Generate the script to get the database from version (the result of detect_version()) to the current version of the software. If version is None, this is simply the contents of the sql/create_piwheels.sql script. Otherwise, it is a concatenation of various update scripts.

piwheels.initdb.parse_statements(script)[source]

This is an extremely crude statement splitter for PostgreSQL’s dialect of SQL. It understands --comments, "quoted identifiers", 'string literals' and $delim$ extended strings $delim$, but not E'\escaped strings' or /* C-style comments */. If you start using such things in the update scripts, you’ll need to extend this function to accommodate them.

It returns a generator which yields individiual statements from script, delimited by semi-colon terminators.

13.16. piwheels.importer

Contains the functions that implement the piw-import script.

piwheels.importer.main(args=None)[source]

This is the main function for the piw-import script. It uses some bits of the piw-slave script to deconstruct the filenames passed to it in order to build all the required information that MrChase needs.

piwheels.importer.print_state(state)[source]

Dumps a human-readable description of the state to the log / console.

Parameters:

state (BuildState) – The build state to print the description of.

piwheels.importer.do_import(config, packages, state)[source]

Handles constructing and sending the initial “IMPORT” message to master.mr_chase.MrChase. If “SEND” is then received, uses do_send() to handle transmitting files.

Parameters:
  • config – The configuration obtained from parsing the command line.

  • packages (list) – A sequence of Wheel objects corresponding to files in the state.

  • state (BuildState) – The object representing the state of the build.

piwheels.importer.do_send(packages, filename)[source]

Handles sending files when requested by do_import().

13.17. piwheels.add

Contains the functions that implement the piw-add script.

piwheels.add.main(args=None)[source]

This is the main function for the piw-add script. It uses MrChase to add mew packages and versions to the system.

piwheels.add.do_add(config)[source]

Handles constructing and sending the ADDPKG/ADDVER message to MrChase.

Parameters:

config – The configuration obtained from parsing the command line.

13.18. piwheels.remove

Contains the functions that implement the piw-remove script.

piwheels.remove.main(args=None)[source]

This is the main function for the piw-remove script. It uses MrChase to remove built packages from the system.

piwheels.remove.do_remove(config)[source]

Handles constructing and sending the REMPKG/REMVER message to MrChase.

Parameters:

config – The configuration obtained from parsing the command line.

13.19. piwheels.transport

This module augments the classes provided by pyzmq (the 0MQ Python bindings) to use CBOR encoding, and voluptuous for message validation. It also tweaks a few minor things like using seconds for timeouts.

class piwheels.transport.Context[source]

Wrapper for 0MQ zmq.Context. This extends the socket() method to include parameters for the socket’s protocol and logger.

class piwheels.transport.Socket(socket, protocol=None, logger=None)[source]

Wrapper for zmq.Socket. This extends 0MQ’s sockets to include a protocol which will be used to validate messages that are sent and received (via a voluptuous schema), and a logger which can be used to debug socket behaviour.

bind(address)[source]

Binds the socket to listen on the specified address.

close(linger=None)[source]

Closes the socket. If linger is specified, it is the number of seconds to wait for pending messages to be flushed.

connect(address)[source]

Connects the socket to the listening socket at address.

drain()[source]

Receives all pending messages in the queue and discards them. This is typically useful during shutdown routines or for testing.

poll(timeout=None, flags=zmq.POLLIN)[source]

Polls the socket for pending data (by default, when flags is POLLIN). If no data is available after timeout seconds, returns False. Otherwise returns True.

If flags is POLLOUT instead, tests whether the socket has available slots for queueing new messages.

recv(flags=0)[source]

Receives the next message as a bytes string.

recv_addr_msg(flags=0)[source]

Receive a CBOR-encoded message (and associated data) along with the address it came from (represented as a bytes string).

recv_msg(flags=0)[source]

Receive a CBOR-encoded message, returning a tuple of the unicode message string and its associated data. This is the primary method used in piwheels for receiving information into a task.

The message, and its associated data, will be validated against the protocol associated with the socket on construction.

recv_multipart(flags=0)[source]

Receives a multi-part message, returning its content as a list of bytes strings.

send(buf, flags=0)[source]

Send buf (a bytes string).

send_addr_msg(addr, msg, data=NoData, flags=0)[source]

Send a CBOR-encoded message (and associated data) to addr, a bytes string.

send_msg(msg, data=NoData, flags=0)[source]

Send the unicode string msg with its associated data as a CBOR-encoded message. This is the primary method used in piwheels for sending information between tasks.

The message, and its associated data, must validate against the protocol associated with the socket on construction.

send_multipart(msg_parts, flags=0)[source]

Send msg_parts, a list of bytes strings as a multi-part message which can be received intact with recv_multipart().

subscribe(topic)[source]

Subscribes SUB type sockets to the specified topic (a string prefix).

unsubscribe(topic)[source]

Unsubscribes SUB type sockets from the specified topic (a string prefix).

property hwm

The high-water mark of the socket, i.e. the number of messages that can be queued before the socket blocks (or drops, depending on the socket type) messages.

class piwheels.transport.Poller[source]

Wrapper for 0MQ zmq.Poller. This simply tweaks 0MQ’s poller to use seconds for timeouts, and to return a dict by default from poll().

poll(timeout=None)[source]

Poll all registered sockets for the events they were registered with, for timeout seconds. Returns a dictionary mapping sockets to events or an empty dictinoary if the timeout elapsed with no events occurring.

register(sock, flags=None)[source]

Register sock with the poller, watching for events as specified by flags (which defaults to POLLIN and POLLOUT events).

unregister(sock)[source]

Unregister sock from the poller. After this, calls to poll() will never return references to sock.

13.20. piwheels.tasks

Implements the base classes (Task and its derivative PauseableTask) which form the basis of all the tasks in the piwheels master.

exception piwheels.tasks.TaskQuit[source]

Exception raised when the “QUIT” message is received by the internal control queue.

class piwheels.tasks.Task(config, control_protocol=({'PAUSE': NoData, 'QUIT': NoData, 'RESUME': NoData}, {}))[source]

The Task class is a Thread derivative which is the base for all tasks in the piwheels master. The run() method is overridden to perform a simple task loop which calls poll() once a cycle to react to any messages arriving into queues, and to dispatch any periodically executed methods.

Queues are associated with handlers via the register() method. Periodic methods are associated with an interval via the every() method. These should be called during initialization (don’t attempt to register handlers from within the thread itself).

Generally this shouldn’t be used as a base-class. Use one of the descendents that implements a pausing mechanism, NonStopTask, PauseableTask, or PausingTask.

close()[source]

Close all registered queues. This should be overridden to close any additional queues the task holds which aren’t registered.

every(interval, handler)[source]

Register handler to be called every interval periodically.

Parameters:
  • interval (timedelta) – The time interval between each run of handler.

  • handler – The function or method to call periodically.

force(handler)[source]

Force handler to run next time its interval is polled.

handle_control(queue)[source]

Default handler for the internal control queue. In this base class it simply handles the “QUIT” message by raising TaskQuit (which the run() method will catch and use as a signal to end).

Messages other than QUIT, PAUSE and RESUME raise TaskControl which can be caught in descendents to implement custom control messages.

once()[source]

This method is called once before the task loop starts. It the task needs to do some initialization or setup within the task thread, this is the place to do it.

pause()[source]

Requests that the task pause itself. This is an idempotent method; it’s always safe to call repeatedly and even if the task isn’t pauseable it’ll simply be ignored.

poll(timeout=1)[source]

This method is called once per loop of the task’s run() method. It runs all periodic handlers, then polls all registered queues and calls their associated handlers if the poll is successful.

quit()[source]

Requests that the task terminate at its earliest convenience. To wait until the task has actually closed, call join() afterwards.

register(queue, handler, flags=zmq.POLLIN)[source]

Register queue to be polled on each cycle of the task. Any messages with the relevant flags (defaults to POLLIN) will trigger the specified handler method which is expected to take a single argument which will be queue.

Parameters:
  • queue (transport.Socket) – The queue to poll.

  • handler – The function or method to call when a message with matching flags arrives in queue.

  • flags (int) – The flags to match in the queue poller (defaults to POLLIN).

resume()[source]

Requests that the task resume itself. This is an idempotent method; it’s safe to call repeatedly and even if the task isn’t pauseable it’ll simply be ignored.

run()[source]

This method is the main task loop. Override this to perform one-off startup processing within the task’s background thread, and to perform any finalization required.

socket(sock_type, protocol=None)[source]

Construct a socket and link it to the logger for this task. This is primarily useful for debugging purposes, but also ensures that the task will implicitly close and clean up the socket when it closes.

class piwheels.tasks.PauseableTask(config, control_protocol=({'PAUSE': NoData, 'QUIT': NoData, 'RESUME': NoData}, {}))[source]

Derivative of Task that implements a rudimentary pausing mechanism. When the “PAUSE” message is received on the internal control queue, the task will enter a loop which simply polls the control queue waiting for “RESUME” or “QUIT”. No other work will be done (Task.loop() and Task.poll() will not be called) until the task is resumed (or terminated).

If you need a more complex pausing implementation which can still do some work while paused (to drain incoming queues for instance), use PausingTask instead.

handle_control(queue)[source]

Default handler for the internal control queue. In this base class it simply handles the “QUIT” message by raising TaskQuit (which the run() method will catch and use as a signal to end).

Messages other than QUIT, PAUSE and RESUME raise TaskControl which can be caught in descendents to implement custom control messages.

13.21. piwheels.states

This module defines several classes which permit interested tasks to track the state of build slaves (SlaveState), file transfers (TransferState), build attempts (BuildState), build artifacts (FileState) and various loggers.

class piwheels.states.FileState(filename, filesize, filehash, package_tag, package_version_tag, py_version_tag, abi_tag, platform_tag, requires_python, dependencies, location='/simple', transferred=False)[source]

Represents the state of an individual build artifact (a package file, or wheel) including its filename, filesize, the SHA256 filehash, and various tags extracted from the build. Also tracks whether or not the file has been transferred.

Parameters:
  • filename (str) – The original filename of the build artifact.

  • filesize (int) – The size of the file in bytes.

  • filehash (str) – The SHA256 hash of the file contents.

  • package_tag (str) – The package tag extracted from the filename (first “-” separated component).

  • package_version_tag (str) – The package version tag extracted from the filename (second “-” separated component).

  • py_version_tag (str) – The python version tag extracted from the filename (third from last “-” separated component).

  • abi_tag (str) – The python ABI tag extracted from the filename (second from last “-” separated component).

  • platform_tag (str) – The platform tag extracted from the filename (last “-” separated component).

  • requires_python (str) – The Requires-Python specification for the file.

  • dependencies (set) – The set of dependencies that are required to use this particular wheel.

  • location (str) – The location the file is stored (default is /simple i.e. the same server as the master but could be the URL of an external archive server).

  • transferred (bool) – True if the file has been transferred from the build slave that generated it to the file server.

classmethod from_message(value)[source]

Convert the output from as_message() back into a BuildState.

as_message()[source]

Convert the FileState object into a simpler list for serialization and transport.

verified()[source]

Called to set transferred to True after a file transfer has been successfully verified.

class piwheels.states.BuildState(slave_id, package, version, abi_tag, status, duration, output, files, build_id=None)[source]

Represents the state of a package build including the package, version, status, build duration, and all the lines of output. The files attribute is a mapping containing details of each successfully built package file.

Parameters:
  • slave_id (int) – The master’s identifier for the build slave.

  • package (str) – The name of the package to build.

  • version (str) – The version number of the package to build.

  • abi_tag (str) – The ABI for which the build was attempted (must not be 'none').

  • status (bool) – True if the build succeeded, False if it failed.

  • duration (timedelta) – The amount of time (in seconds) it took to complete the build.

  • output (str) – The log output of the build.

  • files (dict) – A mapping of filenames to FileState objects for each artifact produced by the build.

  • build_id (int) – The integer identifier generated for the build by the database (None until the build has been inserted into the database).

classmethod from_message(value)[source]

Convert the output from as_message() back into a BuildState.

as_message()[source]

Convert the BuildState, and its nested FileState objects into simpler lists for serialization and transport.

logged(build_id)[source]

Called to fill in the build’s ID in the backend database.

property files

A mapping of filename to FileState instances.

property next_file

Returns the filename of the next file that needs transferring or None if all files have been transferred.

property transfers_done

Returns True if all files have been transferred.

class piwheels.states.SlaveState(address, build_timeout, busy_timeout, native_py_version, native_abi, native_platform, label, os_name, os_version, board_revision, board_serial)[source]

Tracks the state of a build slave. The master updates this state with each request and reply sent to and received from the slave, and this class in turn manages the associated BuildState (accessible from build) and TransferState (accessible from transfer). The class also tracks the time a request was last seen from the build slave, and includes a kill() method.

Parameters:
  • address (bytes) –

    The slave’s ephemeral 0MQ address.

    Note

    This is not the slave’s IP address; it’s a unique identifier generated on connection to the master’s ROUTER socket. It will be different each time the slave re-connects (due to timeout, reboot, etc).

  • timeout (int) – The number of seconds after which any build will be considered to have timed out (and the slave will be assumed crashed).

  • native_py_version (str) – The slave’s native Python version.

  • native_abi (str) – The slave’s native Python ABI.

  • native_platform (str) – The slave’s native platform.

  • label (str) – A label representing the slave.

class piwheels.states.TransferState(slave_id, file_state)[source]

Tracks the state of a file transfer. All file transfers are held in temporary locations until verify() indicates the transfer was successful, at which point they are atomically renamed into their final location.

The state is intimately tied to the file transfer protocol and includes methods to write a received chunk(), and to determine the next chunk to fetch(), as well as a property to determine when the transfer is done.

Parameters:
  • slave_id (str) – The ID number of the slave which built the file.

  • file_state (FileState) – The details of the file to be transferred (filename, size, hash, etc.)

class piwheels.states.DownloadState(filename, host, timestamp, arch, distro_name, distro_version, os_name, os_version, py_name, py_version, installer_name, installer_version, setuptools_version)[source]

Represents the state of the log entry for a download of a package wheel file, including its filename, the user’s host IP, access timestamp and information about the operating system and installer.

Parameters:
  • filename (str) – The filename of the downloaded wheel file.

  • host – The hostname or IP address of the user.

  • timestamp (datetime.datetime) – The timestamp at which the file was downloaded.

  • arch (str or None) – The architecture of the user’s computer system (usually armv6 or armv7).

  • distro_name (str or None) – The user’s operating system distribution name (e.g. Raspbian).

  • distro_version (str or None) – The version of the user’s operating system distribution.

  • os_name (str or None) – The name of the user’s operating system (e.g. Linux).

  • os_version (str or None) – The version of the user’s operating system (e.g. Linux kernel version).

  • py_name (str or None) – The Python implementation used (e.g. CPython).

  • py_version (str or None) – The Python version used (e.g. 3.7.3).

  • installer_name (str or None) – The name of the tool used to install the file (e.g. pip).

  • installer_version (str or None) – The version of the tool (e.g. pip) used to install the file.

  • setuptools_version (str or None) – The version of setuptools used.

class piwheels.states.SearchState(package, host, timestamp, arch, distro_name, distro_version, os_name, os_version, py_name, py_version, installer_name, installer_version, setuptools_version)[source]

Represents the state of the log entry for an instance of a package search, including the package name, user’s host IP, access timestamp and information about the operating system and installer.

Parameters:
  • package (str) – The name of the package searched for.

  • host (str) – The hostname or IP address of the user.

  • timestamp (datetime.datetime) – The timestamp at which the search occurred.

  • arch (str or None) – The architecture of the user’s computer system (usually armv6 or armv7).

  • distro_name (str or None) – The user’s operating system distribution name (e.g. Raspbian).

  • distro_version (str or None) – The version of the user’s operating system distribution.

  • os_name (str or None) – The name of the user’s operating system (e.g. Linux).

  • os_version (str or None) – The version of the user’s operating system (e.g. Linux kernel version).

  • py_name (str or None) – The Python implementation used (e.g. CPython).

  • py_version (str or None) – The Python version used (e.g. 3.7.3).

  • installer_name (str or None) – The name of the tool used (e.g. pip).

  • installer_version (str or None) – The version of the tool (e.g. pip) used.

  • setuptools_version (str or None) – The version of setuptools used.

class piwheels.states.ProjectState(package, host, timestamp, user_agent)[source]

Represents the state of the log entry for an instance of project page hit, including the page name, the user’s host IP, access timestamp and the user’s user_agent.

Parameters:
  • package (str) – The name of the package searched for.

  • host (str) – The hostname or IP address of the user.

  • timestamp (datetime.datetime) – The timestamp at which the page was accessed.

  • user_agent (str) – The user agent of the page request.

class piwheels.states.JSONState(package, host, timestamp, user_agent)[source]

Represents the state of the log entry for an instance of project JSON download, including the page name, the user’s host IP, access timestamp and the user’s user_agent.

Parameters:
  • package (str) – The name of the package whose JSON file was accessed.

  • host (str) – The hostname or IP address of the user.

  • timestamp (datetime.datetime) – The timestamp at which the page was accessed.

  • user_agent (str) – The user agent of the request.

class piwheels.states.PageState(page, host, timestamp, user_agent)[source]

Represents the state of the log entry for an instance of web page hit, including the page name, the user’s host IP, access timestamp and the user’s user_agent.

Parameters:
  • page (str) – The name of the page accessed.

  • host (str) – The IP address of the user.

  • timestamp (datetime.datetime) – The timestamp at which the page was accessed.

  • user_agent (str) – The user agent of the page request.

class piwheels.states.SlaveStats(timestamp, disk_size, disk_free, mem_size, mem_free, swap_size, swap_free, load_average, cpu_temp)[source]
class piwheels.states.MasterStats(timestamp, packages_built, builds_last_hour, builds_time, builds_size, builds_pending, new_last_hour, files_count, downloads_last_hour, downloads_last_month, downloads_all, disk_size, disk_free, mem_size, mem_free, swap_size, swap_free, load_average, cpu_temp)[source]

Make pkg_dir, replacing any existing symlink in its place. See the notes in TheScribe.write_package_index() for more information.

13.22. piwheels.ranges

A set of utility routines for efficiently tracking byte ranges within a stream. These are used to track which chunks of a file have been received during file transfers from build slaves.

See FileJuggler for the usage of these functions.

piwheels.ranges.consolidate(ranges)[source]

Given a list of ranges in ascending order, this generator function returns the list with any overlapping ranges consolidated into individual ranges. For example:

>>> list(consolidate([range(0, 5), range(4, 10)]))
[range(0, 10)]
>>> list(consolidate([range(0, 5), range(5, 10)]))
[range(0, 10)]
>>> list(consolidate([range(0, 5), range(6, 10)]))
[range(0, 5), range(6, 10)]
piwheels.ranges.exclude(ranges, ex)[source]

Given a list of non-overlapping ranges in ascending order, and a range ex to exclude, this generator function returns ranges with all values covered by ex removed from any contained ranges. For example:

>>> list(exclude([range(10)], range(2)))
[range(2, 10)]
>>> list(exclude([range(10)], range(2, 4)))
[range(0, 2), range(4, 10)]
piwheels.ranges.intersect(range1, range2)[source]

Given two ranges range1 and range2 (which must both have a step of 1), returns the range formed by the intersection of the two ranges, or None if the ranges do not overlap. For example:

>>> intersect(range(10), range(5))
range(0, 5)
>>> intersect(range(10), range(10, 2))
>>> intersect(range(10), range(2, 5))
range(2, 5)
piwheels.ranges.split(ranges, i)[source]

Given a list of non-overlapping ranges in ascending order, this generator function returns the list with the range containing i split into two ranges, one ending at i and the other starting at i. If i is not contained in any of the ranges, then ranges is returned unchanged. For example:

>>> list(split([range(10)], 5))
[range(0, 5), range(5, 10)]
>>> list(split([range(10)], 0))
[range(0, 10)]
>>> list(split([range(10)], 20))
[range(0, 10)]