def lease_grant(tables, *, lease, expiration): ASSERT.greater(lease, 0) ASSERT.greater_or_equal(expiration, 0) return sqlite.upsert(tables.leases).values( lease=lease, expiration=expiration, )
def move(n): """Move ``n`` bytes from ``data`` to ``output``.""" nonlocal data ASSERT.greater_or_equal(self._chunk_remaining, n) output.append(data[:n]) data = data[n:] self._chunk_remaining -= n
def set_(tables, *, revision, key, value): ASSERT.greater_or_equal(revision, 0) ASSERT.true(key) ivs = {'revision': revision + 1, 'key': key, 'value': value} return [ sqlite.upsert(tables.keyspace).values(**ivs), tables.revisions.insert().values(**ivs), ]
def increment_revision(tables, *, revision): ASSERT.greater_or_equal(revision, 0) if revision == 0: return sqlite.upsert(tables.current_revision).values(revision=1) return ( tables.current_revision.update()\ .where(tables.current_revision.c.revision == revision) .values(revision=revision + 1) )
def maybe_remove(self, num_needed): """Remove the given number of tokens from the bucket. If the bucket has less tokens than the given number, this returns the estimated time to wait until the bucket is full. """ ASSERT.greater_or_equal(num_needed, 0) self._add_tokens() if self._num_tokens >= num_needed: self._num_tokens -= num_needed return None return self._bucket_size / self._token_rate
def init(self, index, size=None): if not 0 <= index < len(self): raise IndexError(index) if self.schema.element_type.is_list(): ASSERT.greater_or_equal(size, 0) return DynamicListBuilder( self._message, self.schema.element_type.as_list(), self._raw.init(index, size).asDynamicList(), ) else: # Although Builder::init does not support struct type, to # make interface consistent between list-of-struct and # struct-of-struct, let's return something here rather than # erring out. ASSERT.true(self.schema.element_type.is_struct()) return self[index]
def init(self, name, size=None): field = self.schema.fields[name] # For now let's only accept list and struct, but remember that # ``capnp::DynamicStruct::Builder::init`` actually supports more # types. if field.type.is_list(): ASSERT.greater_or_equal(size, 0) return DynamicListBuilder( self._message, field.type.as_list(), self._raw.init(field._raw, size).asDynamicList(), ) elif field.type.is_struct(): return DynamicStructBuilder( self._message, field.type.as_struct(), self._raw.init(field._raw).asDynamicStruct(), ) else: return ASSERT.unreachable('unexpected item type: {}', field.type)
def _scan( tables, make_columns, *, revision=0, key_start=b'', key_end=b'', sorts=(), limit=0, ): ASSERT.greater_or_equal(revision, 0) ASSERT.greater_or_equal(limit, 0) query, table = _make_keyspace_table(tables, revision) query = select(make_columns(table)).select_from(query) query = _add_range(query, table.c.key, key_start, key_end) if revision != 0: query = query.where(table.c.value != None) # pylint: disable=singleton-comparison for sort_ in sorts: query = _add_sort(table, query, sort_) query = _add_limit(query, limit) return query
def cmd_cleanup(args): ASSERT.greater_or_equal(args.keep, 0) envs_dir = repos.EnvsDir(args.release_repo) LOG.info('clean up pods') _cleanup( args.keep, envs_dir.get_current_pod_versions(), repos.PodDir.group_dirs(args.release_repo), ) LOG.info('clean up xars') _cleanup( args.keep, envs_dir.get_current_xar_versions(), repos.XarDir.group_dirs(args.release_repo), ) if args.also_builder: LOG.info('clean up builder images') _cleanup( args.keep, # Builder images are not referenced by pods and thus do not # have current versions. {}, repos.BuilderImageDir.group_dirs(args.release_repo), ) LOG.info('clean up images') groups = repos.ImageDir.group_dirs(args.release_repo) if not args.also_base: groups.pop(foreman.Label.parse('//bases:base'), None) _cleanup( args.keep, repos.get_current_image_versions(args.release_repo), groups, ) LOG.info('clean up volumes') _cleanup( args.keep, repos.PodDir.get_current_volume_versions(args.release_repo), repos.VolumeDir.group_dirs(args.release_repo), ) return 0
def lease_scan(tables, *, lease_start=0, lease_end=0, limit=0): ASSERT.greater_or_equal(lease_start, 0) ASSERT.greater_or_equal(lease_end, 0) ASSERT.greater_or_equal(limit, 0) query = select([ tables.leases.c.lease, tables.leases.c.expiration, ]) query = _add_range(query, tables.leases.c.lease, lease_start, lease_end) query = _add_limit(query, limit) query = query.alias('_leases') joined = join( query, tables.leases_key_ids, query.c.lease == tables.leases_key_ids.c.lease, isouter=True, ) joined = join( joined, tables.keyspace, tables.leases_key_ids.c.key_id == tables.keyspace.c.key_id, isouter=True, ) return ( select([ query.c.lease, query.c.expiration, tables.keyspace.c.key, ])\ .select_from(joined) # Order by lease ID so that we can itertools.groupby on it. .order_by(query.c.lease.asc()) # Order by key so that key order is deterministic. .order_by(tables.keyspace.c.key.asc()) )
def __post_init__(self): ASSERT.greater(self.lease, 0) ASSERT.greater_or_equal(self.expiration, 0)
] import inspect import logging import sys from g1.bases import classes from g1.bases.assertions import ASSERT from . import errors from . import traps LOG = logging.getLogger(__name__) # Python 3.4 implements PEP 442 for safe ``__del__``. ASSERT.greater_or_equal(sys.version_info, (3, 4)) class Task: """Task object. A ``Task`` object wraps an coroutine object, and is the basic unit of scheduling. It is modelled after ``Future` object, which is commonly used for wrapping a ``Thread`` object. There are a few notable differences between ``Task`` and ``Future``: * ``Task`` is cancellable due to its cooperative nature, but ``Future`` is not because threads in general are not cancellable. * ``get_result`` and ``get_exception`` does not take a ``timeout`` argument. While it is possible to add a ``timeout`` argument, as
def release(self, n=1): self._value += ASSERT.greater_or_equal(n, 1) self._gate.unblock()
def __init__(self, value=1): self._value = ASSERT.greater_or_equal(value, 0) self._gate = Gate()
def compact(tables, *, revision): ASSERT.greater_or_equal(revision, 0) table = tables.revisions return table.delete().where(table.c.revision < revision)
def lease_scan_expired(tables, *, current_time): ASSERT.greater_or_equal(current_time, 0) return ( select([tables.leases.c.lease])\ .where(tables.leases.c.expiration < current_time) )
def lease_count(tables, *, lease_start=0, lease_end=0): ASSERT.greater_or_equal(lease_start, 0) ASSERT.greater_or_equal(lease_end, 0) query = select([func.count()]).select_from(tables.leases) query = _add_range(query, tables.leases.c.lease, lease_start, lease_end) return query