Remove unused imagetree class
This commit is contained in:
parent
6f8ec1deac
commit
f9f47e68c3
3 changed files with 0 additions and 182 deletions
|
@ -8,7 +8,6 @@ from data import model
|
|||
from data.registry_model import registry_model
|
||||
from data.registry_model.datatypes import RepositoryReference
|
||||
from data.database import UseThenDisconnect
|
||||
from util.imagetree import ImageTree
|
||||
from util.morecollections import AttrDict
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
|
|
@ -1,99 +0,0 @@
|
|||
from collections import defaultdict
|
||||
|
||||
|
||||
class ImageTreeNode(object):
|
||||
""" A node in the image tree. """
|
||||
def __init__(self, image, child_map):
|
||||
self.image = image
|
||||
self.parent = None
|
||||
self.tags = []
|
||||
|
||||
self._child_map = child_map
|
||||
|
||||
@property
|
||||
def children(self):
|
||||
return self._child_map[self.image.id]
|
||||
|
||||
def add_tag(self, tag):
|
||||
self.tags.append(tag)
|
||||
|
||||
|
||||
class ImageTree(object):
|
||||
""" In-memory tree for easy traversal and lookup of images in a repository. """
|
||||
|
||||
def __init__(self, all_images, all_tags, base_filter=None):
|
||||
self._image_map = {}
|
||||
self._child_map = defaultdict(list)
|
||||
|
||||
self._build(all_images, all_tags, base_filter)
|
||||
|
||||
def _build(self, all_images, all_tags, base_filter=None):
|
||||
# Build nodes for each of the images.
|
||||
for image in all_images:
|
||||
ancestors = image.ancestor_id_list()
|
||||
|
||||
# Filter any unneeded images.
|
||||
if base_filter is not None:
|
||||
if image.id != base_filter and not base_filter in ancestors:
|
||||
continue
|
||||
|
||||
# Create the node for the image.
|
||||
image_node = ImageTreeNode(image, self._child_map)
|
||||
self._image_map[image.id] = image_node
|
||||
|
||||
# Add the node to the child map for its parent image (if any).
|
||||
parent_image_id = image.parent_id
|
||||
if parent_image_id is not None:
|
||||
self._child_map[parent_image_id].append(image_node)
|
||||
|
||||
# Build the tag map.
|
||||
for tag in all_tags:
|
||||
image_node = self._image_map.get(tag.image.id)
|
||||
if not image_node:
|
||||
continue
|
||||
|
||||
image_node.add_tag(tag.name)
|
||||
|
||||
def find_longest_path(self, image_id, checker):
|
||||
""" Returns a list of images representing the longest path that matches the given
|
||||
checker function, starting from the given image_id *exclusive*.
|
||||
"""
|
||||
start_node = self._image_map.get(image_id)
|
||||
if not start_node:
|
||||
return []
|
||||
|
||||
return self._find_longest_path(start_node, checker, -1)[1:]
|
||||
|
||||
def _find_longest_path(self, image_node, checker, index):
|
||||
found_path = []
|
||||
|
||||
for child_node in image_node.children:
|
||||
if not checker(index + 1, child_node.image):
|
||||
continue
|
||||
|
||||
found = self._find_longest_path(child_node, checker, index + 1)
|
||||
if found and len(found) > len(found_path):
|
||||
found_path = found
|
||||
|
||||
return [image_node.image] + found_path
|
||||
|
||||
def tag_containing_image(self, image):
|
||||
""" Returns the name of the closest tag containing the given image. """
|
||||
if not image:
|
||||
return None
|
||||
|
||||
# Check the current image for a tag.
|
||||
image_node = self._image_map.get(image.id)
|
||||
if image_node is None:
|
||||
return None
|
||||
|
||||
if image_node.tags:
|
||||
return image_node.tags[0]
|
||||
|
||||
# Check any deriving images for a tag.
|
||||
for child_node in image_node.children:
|
||||
found = self.tag_containing_image(child_node.image)
|
||||
if found is not None:
|
||||
return found
|
||||
|
||||
return None
|
|
@ -1,82 +0,0 @@
|
|||
from data import model
|
||||
from util.imagetree import ImageTree
|
||||
|
||||
from test.fixtures import *
|
||||
|
||||
NAMESPACE = 'devtable'
|
||||
SIMPLE_REPO = 'simple'
|
||||
COMPLEX_REPO = 'complex'
|
||||
|
||||
def _get_base_image(all_images):
|
||||
for image in all_images:
|
||||
if image.ancestors == '/':
|
||||
return image
|
||||
|
||||
return None
|
||||
|
||||
def test_longest_path_simple_repo(initialized_db):
|
||||
all_images = list(model.image.get_repository_images(NAMESPACE, SIMPLE_REPO))
|
||||
all_tags = list(model.tag.list_repository_tags(NAMESPACE, SIMPLE_REPO))
|
||||
tree = ImageTree(all_images, all_tags)
|
||||
|
||||
base_image = _get_base_image(all_images)
|
||||
tag_image = all_tags[0].image
|
||||
|
||||
def checker(index, image):
|
||||
return True
|
||||
|
||||
ancestors = tag_image.ancestors.split('/')[2:-1] # Skip the first image.
|
||||
result = tree.find_longest_path(base_image.id, checker)
|
||||
assert len(result) == 3
|
||||
for index in range(0, 2):
|
||||
assert result[index].id == int(ancestors[index])
|
||||
|
||||
assert tree.tag_containing_image(result[-1]) == 'latest'
|
||||
|
||||
def test_longest_path_complex_repo(initialized_db):
|
||||
all_images = list(model.image.get_repository_images(NAMESPACE, COMPLEX_REPO))
|
||||
all_tags = list(model.tag.list_repository_tags(NAMESPACE, COMPLEX_REPO))
|
||||
tree = ImageTree(all_images, all_tags)
|
||||
|
||||
base_image = _get_base_image(all_images)
|
||||
|
||||
def checker(index, image):
|
||||
return True
|
||||
|
||||
result = tree.find_longest_path(base_image.id, checker)
|
||||
assert len(result) == 5
|
||||
assert tree.tag_containing_image(result[-1]) == 'prod'
|
||||
|
||||
def test_filtering(initialized_db):
|
||||
all_images = list(model.image.get_repository_images(NAMESPACE, COMPLEX_REPO))
|
||||
all_tags = list(model.tag.list_repository_tags(NAMESPACE, COMPLEX_REPO))
|
||||
tree = ImageTree(all_images, all_tags, base_filter=1245)
|
||||
|
||||
base_image = _get_base_image(all_images)
|
||||
|
||||
def checker(index, image):
|
||||
return True
|
||||
|
||||
result = tree.find_longest_path(base_image.id, checker)
|
||||
assert len(result) == 0
|
||||
|
||||
def test_longest_path_simple_repo_direct_lookup(initialized_db):
|
||||
repository = model.repository.get_repository(NAMESPACE, SIMPLE_REPO)
|
||||
all_images = list(model.image.get_repository_images(NAMESPACE, SIMPLE_REPO))
|
||||
all_tags = list(model.tag.list_repository_tags(NAMESPACE, SIMPLE_REPO))
|
||||
|
||||
base_image = _get_base_image(all_images)
|
||||
|
||||
def checker(index, image):
|
||||
return True
|
||||
|
||||
filtered_images = model.image.get_repository_images_without_placements(
|
||||
repository,
|
||||
with_ancestor=base_image)
|
||||
assert set([a.id for a in all_images]) == set([f.id for f in filtered_images])
|
||||
|
||||
tree = ImageTree(filtered_images, all_tags)
|
||||
|
||||
result = tree.find_longest_path(base_image.id, checker)
|
||||
assert len(result) == 3
|
||||
assert tree.tag_containing_image(result[-1]) == 'latest'
|
Reference in a new issue