def test_create_from_uri(self): '''test creation of tree from unique resource identifier''' from containertree import ContainerFileTree print("Testing creation from unique resource identifier.") tree = ContainerFileTree("vanessa/salad") self.assertTrue(tree.find('/code/salad') != None) bins = tree.search('bin') self.assertEqual(len(bins), 5)
def main(args): # No image or uri provided, print help if len(args.image) == 0: subparser.print_help() sys.exit(1) image = args.image.pop(0) # Step 1: Generate container tree object from Docker URI tree = ContainerFileTree(image) bot.debug(tree) # Step 2: Create a webroot to serve from webroot = args.output if args.output is None: webroot = get_tmpdir('containertree-') if not os.path.exists(webroot): os.mkdir(webroot) bot.debug('Webroot: %s' % webroot) # Copy the template to the webroot template = get_template(args.template) shutil.copyfile(template, "%s/index.html" % webroot) # If the user wants to print to terminal, we don't save filename = None if args.printout == None: filename = '%s/data.json' % webroot # Export data.json bot.debug('Exporting data for d3 visualization') data = tree.export_tree(filename) # Does the user want to view the tree? if args.view: # Generate the data.json serve_template(webroot, 9779) # Does the user want to print output? elif args.printout != None: if args.printout == "data.json": print(data) elif args.printout == "index.html": template = read_file(template) print(template) return webroot
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)
# This scrips will produce a massive summary tree (and save to file, if # argument is provided from containertree import ContainerFileTree from random import choice import requests import tempfile import sys # Path to database of container-api database = "https://singularityhub.github.io/api/files" print('Selecting container from %s...' % database) containers = requests.get(database).json() 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)
# along with this program. If not, see <https://www.gnu.org/licenses/>. 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() entry = containers[0] # Google Container Diff Structure print('Generating tree!') tree = ContainerFileTree(entry['url']) print(tree) # ContainerTree<38008> # 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('tree') shutil.copyfile(template, "%s/index.html" %webroot)
from containertree import ContainerFileTree from random import choice 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}
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')
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)