Skip to content

Commit 8fdb117

Browse files
authored
Merge pull request #18 from chnyda/master
Keep publishes and snapshots variable to get rid of http requests
2 parents c00bb51 + feed6b5 commit 8fdb117

3 files changed

Lines changed: 49 additions & 8 deletions

File tree

aptly/decorators.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
3+
4+
class CachedMethod(object):
5+
"""
6+
Decorator for caching of function results
7+
"""
8+
def __init__(self, function):
9+
self.function = function
10+
self.mem = {}
11+
12+
def __call__(self, *args, **kwargs):
13+
cached = kwargs.pop('cached', True)
14+
if cached is True:
15+
if (args, str(kwargs)) in self.mem:
16+
return self.mem[args, str(kwargs)]
17+
18+
tmp = self.function(*args, **kwargs)
19+
self.mem[args, str(kwargs)] = tmp
20+
return tmp
21+
22+
def __get__(self, obj, objtype):
23+
""" Support instance methods """
24+
import functools
25+
return functools.partial(self.__call__, obj)

aptly/publisher/__init__.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import yaml
77
import apt_pkg
88
from aptly.exceptions import AptlyException, NoSuchPublish
9+
from aptly.decorators import CachedMethod
910

1011
lg = logging.getLogger(__name__)
1112

@@ -150,7 +151,7 @@ def get_repo_information(config, client, fill_repo=False, components=[]):
150151
if components and repo.get('component') not in components:
151152
continue
152153
if fill_repo and origin == 'repo':
153-
packages = client.do_get('/{}/{}/{}'.format("repos", name, "packages"))
154+
packages = Publish._get_packages("repos", name)
154155
repo_dict[name] = packages
155156
for distribution in repo.get('distributions'):
156157
publish_name = str.join('/', distribution.split('/')[:-1])
@@ -281,11 +282,26 @@ def compare(self, other, components=[]):
281282

282283
return (diff, equal)
283284

285+
@staticmethod
286+
@CachedMethod
287+
def _get_packages(client, source_type, source_name):
288+
return client.do_get('/{}/{}/packages'.format(source_type, source_name))
289+
290+
@staticmethod
291+
@CachedMethod
292+
def _get_publishes(client):
293+
return client.do_get('/publish')
294+
295+
@staticmethod
296+
@CachedMethod
297+
def _get_snapshots(client):
298+
return client.do_get('/snapshots', {'sort': 'time'})
299+
284300
def _get_publish(self):
285301
"""
286302
Find this publish on remote
287303
"""
288-
publishes = self.client.do_get('/publish')
304+
publishes = self._get_publishes(self.client)
289305
for publish in publishes:
290306
if publish['Distribution'] == self.distribution and \
291307
publish['Prefix'].replace("/", "_") == (self.prefix or '.') and \
@@ -350,7 +366,7 @@ def purge_publish(self, repo_dict, publish_dict, components=[], publish=False):
350366
repo_dict[repo_name] = []
351367
continue
352368

353-
packages = self.client.do_get('/{}/{}/{}'.format("snapshots", name, "packages"))
369+
packages = self._get_packages(self.client, "snapshots", name)
354370
packages = sorted(packages, key=lambda x: self.parse_package_ref(x)[2], reverse=True, cmp=apt_pkg.version_compare)
355371

356372
for package in packages:
@@ -503,7 +519,7 @@ def get_packages(self, component=None, components=[], packages=None):
503519
# We don't want packages for this component
504520
continue
505521

506-
component_refs = self.client.do_get('/snapshots/%s/packages' % snapshot['Name'])
522+
component_refs = self._get_packages(self.client, "snapshots", snapshot['Name'])
507523
if packages:
508524
# Filter package names
509525
for ref in component_refs:
@@ -536,7 +552,7 @@ def _find_snapshot(self, name):
536552
"""
537553
Find snapshot on remote by name or regular expression
538554
"""
539-
remote_snapshots = self.client.do_get('/snapshots', {'sort': 'time'})
555+
remote_snapshots = self._get_snapshots(self.client)
540556
for remote in reversed(remote_snapshots):
541557
if remote["Name"] == name or \
542558
re.match(name, remote["Name"]):
@@ -597,7 +613,7 @@ def merge_snapshots(self):
597613
package_refs = []
598614
for snapshot in snapshots:
599615
# Get package refs from each snapshot
600-
packages = self.client.do_get('/snapshots/%s/packages' % snapshot)
616+
packages = self._get_packages(self.client, "snapshots", snapshot)
601617
package_refs.extend(packages)
602618

603619
try:

aptly/publisher/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def find_publishes(client, source, target):
205205
lg.error("Source publish is regular expression but target does not refer any match groups. See help for more info.")
206206
sys.exit(1)
207207
lg.debug("Looking for source publishes matching regular expression: {0}".format(source))
208-
publishes = client.do_get('/publish')
208+
publishes = Publish._get_publishes(client)
209209
re_source = re.compile(source)
210210
for publish in publishes:
211211
name = "{}{}{}".format(publish['Storage']+":" if publish['Storage']
@@ -308,7 +308,7 @@ def action_publish(client, publishmgr, config_file, recreate=False,
308308
only_latest=False, components=[]):
309309
if not architectures:
310310
architectures = []
311-
snapshots = client.do_get('/snapshots', {'sort': 'time'})
311+
snapshots = Publish._get_snapshots(client)
312312

313313
config = load_config(config_file)
314314
for name, repo in config.get('mirror', {}).items():

0 commit comments

Comments
 (0)