예제 #1
0
def make_tree(containers):
  """
  Converts a list of containers into a tree represented by a zipper.
  see http://en.wikipedia.org/wiki/Zipper_(data_structure)

  >>> containers = [
  ...   Container('shipwright_test/2', 'path1/Dockerfile', 'shipwright_test/1'),
  ...   Container('shipwright_test/1', 'path1/Dockerfile', 'ubuntu'),
  ...   Container('shipwright_test/3', 'path1/Dockerfile', 'shipwright_test/2'),
  ...   Container('shipwright_test/independent', 'path1/Dockerfile', 'ubuntu'),
  ... ]

  >>> root = make_tree(containers)

  >>> root  # doctest: +ELLIPSIS
  <zipper.Loc(None) ...>

  >>> root.children()  # doctest: +ELLIPSIS
  [Container(name='shipwright_test/1', ...), Container(name='shipwright_test/independent', ...)]

  >>> root.down().node()  # doctest: +ELLIPSIS
  Container(name='shipwright_test/1', ...)

  >>> root.down().children()  # doctest: +ELLIPSIS
  [Container(name='shipwright_test/2', ...)]

  >>> root.down().down().node()  # doctest: +ELLIPSIS
  Container(name='shipwright_test/2', ...)

  >>> root.down().down().children()  # doctest: +ELLIPSIS
  [Container(name='shipwright_test/3', ...)]
  
  >>> root.down().right().node() # doctest: +ELLIPSIS
  Container(name='shipwright_test/independent', ...)

  """

  container_map = {c.container.name:c for c in containers}
  graph = defaultdict(list)

  # TODO: check for cycles
  for c in containers:
    graph[container_map.get(c.container.parent)].append(c)

  # sort children by name, to make testing eaiser
  for node, children in graph.items():
    graph[node] = sorted(children)

  def children(item):
    return graph[item]

  def is_branch(item):
    return len(children(item)) > 0

  def make_node(node, children):
    return node

  root_loc =  zipper.zipper(None, is_branch, children, make_node)

  return root_loc
예제 #2
0
def make_tree(containers):
    """
    Converts a list of containers into a tree represented by a zipper.
    see http://en.wikipedia.org/wiki/Zipper_(data_structure)

    >>> from .dependencies import targets

    >>> root = make_tree(targets)
    >>> root.node().name is None # doctest: +ELLIPSIS
    True

    >>> _names(root)  # doctest: +NORMALIZE_WHITESPACE
    ['shipwright_test/1', 'shipwright_test/independent', 'shipwright_test/2',
    'shipwright_test/3']

    >>> root.down().node()  # doctest: +ELLIPSIS
    Target(container=Container(name='shipwright_test/1', ...)

    >>> _names(root.down())  # doctest: +ELLIPSIS
    ['shipwright_test/2', 'shipwright_test/3']


    >>> root.down().down().node()  # doctest: +ELLIPSIS
    Target(container=Container(name='shipwright_test/2', ...)

    >>> _names(root.down().down())  # doctest: +ELLIPSIS
    ['shipwright_test/3']

    >>> root.down().right().node().name # doctest: +ELLIPSIS
    'shipwright_test/independent'

    """

    root = Root(None, ())
    tree = zipper.zipper(root, is_branch, children, make_node)

    for c in containers:

        branch_children, root_children = split(is_child(c), tree.children())
        t = c._replace(children=tuple(branch_children))

        if branch_children:
            tree = tree.edit(replace, tuple(root_children))

        loc = tree.find(fmap(is_target(t.parent)))
        if loc:
            tree = loc.insert(t).top()
        else:
            tree = tree.insert(t)

    return tree
예제 #3
0
def make_tree(containers):
    """
    Converts a list of containers into a tree represented by a zipper.
    see http://en.wikipedia.org/wiki/Zipper_(data_structure)

    >>> from .dependencies import targets

    >>> root = make_tree(targets)
    >>> root.node().name is None # doctest: +ELLIPSIS
    True

    >>> _names(root)  # doctest: +NORMALIZE_WHITESPACE
    ['shipwright_test/1', 'shipwright_test/independent', 'shipwright_test/2',
    'shipwright_test/3']

    >>> root.down().node()  # doctest: +ELLIPSIS
    Target(container=Container(name='shipwright_test/1', ...)

    >>> _names(root.down())  # doctest: +ELLIPSIS
    ['shipwright_test/2', 'shipwright_test/3']


    >>> root.down().down().node()  # doctest: +ELLIPSIS
    Target(container=Container(name='shipwright_test/2', ...)

    >>> _names(root.down().down())  # doctest: +ELLIPSIS
    ['shipwright_test/3']

    >>> root.down().right().node().name # doctest: +ELLIPSIS
    'shipwright_test/independent'

    """

    root = Root(None, ())
    tree = zipper.zipper(root, is_branch, children, make_node)

    for c in containers:

        branch_children, root_children = split(is_child(c), tree.children())
        t = c._replace(children=tuple(branch_children))

        if branch_children:
            tree = tree.edit(replace, tuple(root_children))

        loc = tree.find(fmap(is_target(t.parent)))
        if loc:
            tree = loc.insert(t).top()
        else:
            tree = tree.insert(t)

    return tree
예제 #4
0
def zipProcessedData(request):
  '''
  Compress data products to single zip for upload
  @param request: the request object
  '''
  print '\nBeginning file compression...'
  # Take dir with multiple scans, compress it & send it off

  ''' Zip it '''
  #temp = zipper.zipFilesFromFolders(dirName = request.session['usrDefProjDir'])
  temp = zipper.zipper(request.session['usrDefProjDir'], zip_file = request.session['usrDefProjDir'] + '.zip')
  ''' Wrap it '''
  wrapper = FileWrapper(temp)
  response = HttpResponse(wrapper, content_type='application/zip')
  response['Content-Disposition'] = ('attachment; filename='+ request.session['scanId'] +'.zip')
  response['Content-Length'] = temp.tell()
  temp.seek(0)

  # request.session.clear() # Very Important
  ''' Send it '''
  return response
예제 #5
0
def zipProcessedData(request):
    '''
  Compress data products to single zip for upload
  @param request: the request object
  '''
    print '\nBeginning file compression...'
    # Take dir with multiple scans, compress it & send it off
    ''' Zip it '''
    #temp = zipper.zipFilesFromFolders(dirName = request.session['usrDefProjDir'])
    temp = zipper.zipper(request.session['usrDefProjDir'],
                         zip_file=request.session['usrDefProjDir'] + '.zip')
    ''' Wrap it '''
    wrapper = FileWrapper(temp)
    response = HttpResponse(wrapper, content_type='application/zip')
    response['Content-Disposition'] = ('attachment; filename=' +
                                       request.session['scanId'] + '.zip')
    response['Content-Length'] = temp.tell()
    temp.seek(0)

    # request.session.clear() # Very Important
    ''' Send it '''
    return response
예제 #6
0
def query_zipper(operation):
  return zipper(operation, is_branch, children, make_node)