pyaptly

Aptly mirror/snapshot managment automation.

class pyaptly.Command(cmd)[source]

Repesents a system command and is used to resolve dependencies between such commands.

Parameters:cmd (list) – The command as list, one item per argument
append(argument)[source]

Append additional arguments to the command.

Parameters:argument (str) – String argument to append
static command_list_to_digraph(commands)[source]

Generate dot source for a digraph - suitable for generating diagrams.

The requires and provides from the commands build nodes, the commands themselves act as connectors.

Parameters:commands (list) – The commands to draw a diagram with
execute()[source]

Execute the command. Return the return value of the command.

Return type:integer
get_provides()[source]

Return all provides of this command.

Return type:set()
static order_commands(commands, has_dependency_cb=<function Command.<lambda>>)[source]

Order the commands according to the dependencies they provide/require.

Parameters:
  • commands (list) – The commands to order
  • has_dependency_cb (function) – Optional callback the resolve external dependencies
provide(type_, identifier)[source]

Provide a dependency for this command.

Parameters:
  • type (str) – Type or category of the dependency ie. snapshot
  • identifier (usually str) – Identifier of the dependency for example name of a snapshot
repr_cmd()[source]

Return repr of the command.

Return type:str
require(type_, identifier)[source]

Require a dependency for this command.

Parameters:
  • type (str) – Type or category of the dependency ie. snapshot
  • identifier (usually str) – Identifier of the dependency for example name of a snapshot
class pyaptly.FunctionCommand(func, *args, **kwargs)[source]

Repesents a function command and is used to resolve dependencies between such commands. This command executes the given function. *args and **kwargs are passed through.

Parameters:func (callable) – The function to execute
execute()[source]

Execute the command. (Call the function).

repr_cmd()[source]

Return repr of the command.

Return type:str
class pyaptly.SystemStateReader[source]

Reads the state from aptly and gpg to find out what operations have to be performed to reach the state defined in the yml config-file.

has_dependency(dependency)[source]

Check system state dependencies.

Parameters:dependency (list) – The dependency to check
read()[source]

Reads all available system states.

read_aptly_list(type_, list_)[source]

Generic method to read lists from aptly.

Parameters:
  • type (str) – The type of list to read ie. snapshot
  • list – Read into this list
  • list – list
read_gpg()[source]

Read all trusted keys in gpg.

read_mirror()[source]

Read all available mirrors.

read_publish_map()[source]

Create a publish map. publish -> snapshots

read_publishes()[source]

Read all available publishes.

read_repos()[source]

Read all available repos.

read_snapshot()[source]

Read all available snapshots.

read_snapshot_map()[source]

Create a snapshot map. snapshot -> snapshots. This is also called merge-tree.

pyaptly.add_gpg_keys(mirror_config)[source]

Uses the gpg command-line to download and add gpg keys needed to create mirrors.

Parameters:mirror_config (dict) – The configuration yml as dict
pyaptly.call_output(args, input_=None)[source]

Call command and return output.

Parameters:
  • args (list) – Command to execute
  • input (bytes) – Input to command
pyaptly.clone_snapshot(origin, destination)[source]

Creates a clone snapshot command with dependencies to be ordered and executed later.

Parameters:
  • origin (str) – The snapshot to clone
  • destination (str) – The new name of the snapshot
pyaptly.cmd_mirror_create(cfg, mirror_name, mirror_config)[source]

Create a mirror create command to be ordered and executed later.

Parameters:
  • cfg (dict) – The configuration yml as dict
  • mirror_name (str) – Name of the mirror to create
  • mirror_config (dict) – Configuration of the snapshot from the yml file.
pyaptly.cmd_mirror_update(cfg, mirror_name, mirror_config)[source]

Create a mirror update command to be ordered and executed later.

Parameters:
  • cfg (dict) – pyaptly config
  • mirror_name (str) – Name of the mirror to create
  • mirror_config (dict) – Configuration of the snapshot from the yml file.
pyaptly.cmd_snapshot_create(cfg, snapshot_name, snapshot_config, ignore_existing=False)[source]

Create a snapshot create command to be ordered and executed later.

Parameters:
  • cfg (dict) – pyaptly config
  • snapshot_name (str) – Name of the snapshot to create
  • snapshot_config (dict) – Configuration of the snapshot from the yml file.
  • ignore_existing (dict) – Optional, defaults to False. If set to True, still return a command object even if the requested snapshot already exists
Return type:

Command

pyaptly.cmd_snapshot_update(cfg, snapshot_name, snapshot_config)[source]

Create commands to update all rotating snapshots.

Parameters:
  • cfg (dict) – pyaptly config
  • snapshot_name (str) – Name of the snapshot to update/rotate
  • snapshot_config (dict) – Configuration of the snapshot from the yml file.
pyaptly.date_round_daily(date, time=None)[source]

Round datetime to day back (floor) to the roundpoint (time) in the day

THIS FUNCTION IGNORES THE TZINFO OF TIME and assumes it is the same tz as the date.

Parameters:
Return type:

datetime.datetime

pyaptly.date_round_weekly(date, day_of_week=1, time=None)[source]

Round datetime back (floor) to a given the of the week.

THIS FUNCTION IGNORES THE TZINFO OF TIME and assumes it is the same tz as the date.

Parameters:
  • date (datetime.datetime) – Datetime object to round
  • day_of_week (int) – ISO day of week: monday is 1 and sunday is 7
  • time (datetime.time) – Roundpoint in the day (tzinfo ignored)
Return type:

datetime.datetime

pyaptly.dependents_of_snapshot(snapshot_name)[source]

Yield a flat list of dependents from the current state.

Return type:generator
pyaptly.expand_timestamped_name(name, timestamp_config, date=None)[source]

Expand a timestamped name using round_timestamp.

Parameters:
pyaptly.format_timestamp(timestamp)[source]

Wrapper for strftime, to ensure we’re all using the same format.

Parameters:timestamp (datetime.datetime) – The timestamp to format
pyaptly.get_logger()[source]

Get the logger.

Return type:logging.Logger
pyaptly.init_hypothesis()[source]

Initialize hypothesis profile if hypothesis is available

pyaptly.iso_first_week_start(iso_year, tzinfo=None)[source]

The gregorian calendar date of the first day of the given ISO year

Parameters:iso_year (int) – Year to find the date of the first week.
pyaptly.iso_to_gregorian(iso_year, iso_week, iso_day, tzinfo=None)[source]

Gregorian calendar date for the given ISO year, week and day

Parameters:
  • iso_year (int) – ISO year
  • iso_week (int) – ISO week
  • iso_day (int) – ISO day
pyaptly.main(argv=None)[source]

Called by command-line, defines parsers and executes commands.

Parameters:argv (list) – Arguments usually taken from sys.argv
pyaptly.mirror(cfg, args)[source]

Creates mirror commands, orders and executes them.

Parameters:
  • cfg (dict) – The configuration yml as dict
  • args (namespace) – The command-line arguments read with argparse
pyaptly.publish(cfg, args)[source]

Creates publish commands, orders and executes them.

Parameters:
  • cfg (dict) – The configuration yml as dict
  • args (namespace) – The command-line arguments read with argparse
pyaptly.publish_cmd_create(cfg, publish_name, publish_config, ignore_existing=False)[source]

Creates a publish command with its dependencies to be ordered and executed later.

Parameters:
  • cfg (dict) – pyaptly config
  • publish_name (str) – Name of the publish to create
  • publish_config (dict) – Configuration of the publish from the yml file.
pyaptly.publish_cmd_update(cfg, publish_name, publish_config, ignore_existing=False)[source]

Creates a publish command with its dependencies to be ordered and executed later.

Parameters:
  • cfg (dict) – pyaptly config
  • publish_name (str) – Name of the publish to update
  • publish_config (dict) – Configuration of the publish from the yml file.
pyaptly.repo(cfg, args)[source]

Creates repository commands, orders and executes them.

Parameters:
  • cfg (dict) – The configuration yml as dict
  • args (namespace) – The command-line arguments read with argparse
pyaptly.repo_cmd_create(cfg, repo_name, repo_config)[source]

Create a repo create command to be ordered and executed later.

Parameters:
  • cfg (dict) – pyaptly config
  • repo_name (str) – Name of the repo to create
  • repo_config (dict) – Configuration of the repo from the yml file.
pyaptly.rotate_snapshot(cfg, snapshot_name)[source]

Creates a command to rotate a snapshot in order to be able to update a current publish.

Parameters:
  • cfg (dict) – pyaptly config
  • snapshot_name (str) – the snapshot to rotate
pyaptly.round_timestamp(timestamp_config, date=None)[source]

Round the given name by adding a timestamp.

The contents of the timestamp is configured by the given timestamp_config dict, which MUST contain a “time” key, and MAY contain a “repeat-weekly” key.

If the key “repeat-weekly” is given, it is expected to contain a three-letter weekday name (mon, tue, thu, ...). The “time” key is expected to be a 24 hour HH:MM time specification.

Timestamps are rounded down to the nearest time as specified (which may be on the previous day. If repeat-weekly is specified, it is rounded down (back in time) to the given weekday.)

The name parameter may be a simple string. If it contains the marker “%T”, then this placeholder will be replaced by the timestamp. If it does NOT contain that marker, then nothing happens (and the timestamp_config is not evaluated at all)

If a datetime object is given as third parameter, then it is used to generate the timestamp. If it is omitted, the current date/time is used.

Example: >>> expand_timestamped_name( ... ‘foo-%T’, ... {‘timestamp’: {‘time’: ‘00:00’}}, ... datetime.datetime(2015,10,7, 15,30) # A Wednesday ... ) ‘foo-20151007T0000Z’

>>> expand_timestamped_name(
...     'foo-%T',
...     {'timestamp': {'time': '00:00', 'repeat-weekly': 'mon'}},
...     datetime.datetime(2015,10,8, 15,30)  # A Thursday
... )
'foo-20151005T0000Z'
>>> expand_timestamped_name(
...     'foo',  # No %T placeholder, timestamp info is ignored
...     {'timestamp': {'time': '00:00', 'repeat-weekly': 'mon'}},
...     datetime.datetime(2015,10,8, 15,30)
... )
'foo'
Parameters:
  • timestamp_config (dict) – Contains the recurrence specification for the timestamp.
  • date (datetime.datetime) – The date to expand the timestamp with.
pyaptly.snapshot(cfg, args)[source]

Creates snapshot commands, orders and executes them.

Parameters:
  • cfg (dict) – The configuration yml as dict
  • args (namespace) – The command-line arguments read with argparse
pyaptly.snapshot_spec_to_name(cfg, snapshot)[source]

Converts a given snapshot short spec to a name.

A short spec is a value that may either be a string or a dict.

If it’s a string, everything is fine and we just use that as a snapshot name.

However if it’s a dict, we assume it has the following keys:

  • name: template for the snapshot
  • timestamp: information on how to generate the timestamp.

For further information regarding the timestamp’s data structure, consult the documentation of expand_timestamped_name().

Parameters:
  • cfg (dict) – Complete yaml config
  • snapshot (dict) – Config of the snapshot
pyaptly.time_delta_helper(time)[source]

Convert a :py:class`datetime.time` to :py:class`datetime.datetime` to calculate deltas

Parameters:time (datetime.time) – Time to convert
Return type:datetime.datetime
pyaptly.time_remove_tz(time)[source]

Convert a :py:class`datetime.time` to :py:class`datetime.time` to without tzinfo.

Parameters:time (datetime.time) – Time to convert
Return type:datetime.time
pyaptly.unit_or_list_to_list(thingy)[source]

Ensures that a yml entry is always a list. Used to allow lists and single units in the yml file.

Parameters:thingy (list, tuple or other) – The data to ensure it is a list