def test_create_container_tree(self): '''test creation of filesystem tree function''' print("Testing collection tree creation.") from containertree import ContainerFileTree import requests # Path to database of container-api database = "https://singularityhub.github.io/api/files" containers = requests.get(database).json() entry = containers[0] # Google Container Diff Analysis Type "File" Structure tree = ContainerFileTree(entry['url']) # To find a node based on path node = tree.find('/etc/ssl') self.assertTrue(isinstance(node, containertree.tree.node.Node)) self.assertTrue(node.name == '/etc/ssl') # Trace a path, returning all nodes trace = tree.trace('/etc/ssl') self.assertEqual(len(trace), 3) names = [x.name for x in trace] # [Node<>, Node<etc>, Node<ssl>] self.assertEqual(names[0], '/') self.assertEqual(names[1], '/etc') self.assertEqual(names[2], '/etc/ssl') # Test node counter self.assertEqual(tree.get_count('/etc/ssl'), 1) # Insert a new node path self.assertEqual(tree.find('/etc/tomato'), None) tree.insert('/etc/tomato') self.assertTrue(tree.find('/etc/tomato') != None) trace = tree.trace('/etc/tomato') self.assertEqual(len(trace), 3) names = [x.name for x in trace] # [Node<>, Node<etc>, Node<tomato>] self.assertEqual(names[0], '/') self.assertEqual(names[1], '/etc') self.assertEqual(names[2], '/etc/tomato') # Get count of a node self.assertEqual(tree.get_count('/etc/tomato'), 1) tree.insert('/etc/tomato') self.assertEqual(tree.get_count('/etc/tomato'), 2) # Update the tree with a second container! self.assertEqual(tree.get_count('/etc/ssl'), 1) new_entry = containers[1] tree.update(new_entry['url']) # Test node counter self.assertEqual(tree.get_count('/etc/ssl'), 2)
def test_container_tree_tags(self): '''test adding tags to a container tree''' print("Testing creation with tags.") from containertree import ContainerFileTree import requests database = "https://singularityhub.github.io/api/files" containers = requests.get(database).json() tree = ContainerFileTree("vanessa/salad") entry1 = containers[0] entry2 = containers[1] tag1 = entry1['collection'] #'54r4/sara-server-vre' tag2 = entry2['collection'] #'A33a/sjupyter' tree = ContainerFileTree(entry1['url'], tag=tag1) self.assertTrue('54r4/sara-server-vre' in tree.root.tags) # Update the container tree with the second container tree.update(entry2['url'], tag=tag2) self.assertTrue('54r4/sara-server-vre' in tree.root.tags) self.assertTrue('A33a/sjupyter' in tree.root.tags) # calculate similarity between tags tags = tree.root.tags scores = tree.similarity_score(tree.root.tags) # {'diff': 44185, # 'same': 12201, # 'score': 0.21638349945021815, # 'tags': ['54r4/sara-server-vre', 'A33a/sjupyter'], # 'total': 56386} self.assertEqual(scores['diff'], 44185) self.assertEqual(scores['same'], 12201)
print('Generating comparison tree!') tree = ContainerFileTree(containers[0]['url'], tag=containers[0]['collection']) names = [] for c in range(len(containers)): container = containers[c] if len(container) > 0: # Some containers have different versions, thus have same collection name name = container['collection'] # If we already have the container, add based on hash if name in names: name = "%s@%s" % (name, container['hash']) try: print('Adding %s' % name) tree.update(container['url'], tag=name) names.append(name) except: print('Skipping %s, issue loading json!' % name) pass # Save to output file, if defined if len(sys.argv) > 1: import pickle output = sys.argv[1] print('Saving to %s' % output) pickle.dump(tree, open(output, 'wb'))
import requests import tempfile import json # Path to database of container-api database = "https://singularityhub.github.io/api/files" print('Selecting container from %s...' % database) containers = requests.get(database).json() container1 = containers[0] container2 = containers[1] # Google Container Diff Structure tree = ContainerFileTree(container1['url'], tag=container1['collection']) # Add the second container to the tree tree.update(container2['url'], tag=container2['collection']) # All the containers (tags) represented in the tree tags = tree.root.tags # Calculate the similarity scores = tree.similarity_score(tags) # {'diff': 44185, # 'same': 12201, # 'score': 0.21638349945021815, # 'tags': ['54r4/sara-server-vre', 'A33a/sjupyter'], # 'total': 56386} # We can now generate a matrix of similarity scores for containers! # Let's build from scratch
from containertree import ContainerFileTree from random import choice import requests import tempfile # Path to database of container-api database = "https://singularityhub.github.io/api/files" print('Selecting container from %s...' %database) containers = requests.get(database).json() container1 = containers[0] container2 = containers[1] # Google Container Diff Structure print('Generating files tree!') tree = ContainerFileTree(container1['url']) tree.update(container2['url']) print(tree) # ContainerTree<56386> # Create temporary directory and copy file there from containertree.utils import get_template from containertree.server import serve_template import shutil import tempfile # Copy the file to the webroot webroot = tempfile.mkdtemp() print('Webroot: %s' %webroot) template = get_template('files_tree') shutil.copyfile(template, "%s/index.html" %webroot)