๓
Vพ๗Xc           @` sา   d  Z  d d l m Z d d l m Z d d l m Z d d l Z d d l Z d d l Z d d l Z d d l	 m
 Z
 d d l m Z e j d d	  Z d
   Z d   Z d   Z d   Z d   Z d   Z d S(   s|  System for specifying garbage collection (GC) of path based data.

This framework allows for GC of data specified by path names, for example files
on disk.  gc.Path objects each represent a single item stored at a path and may
be a base directory,
  /tmp/exports/0/...
  /tmp/exports/1/...
  ...
or a fully qualified file,
  /tmp/train-1.ckpt
  /tmp/train-2.ckpt
  ...

A gc filter function takes and returns a list of gc.Path items.  Filter
functions are responsible for selecting Path items for preservation or deletion.
Note that functions should always return a sorted list.

For example,
  base_dir = "/tmp"
  # create the directories
  for e in xrange(10):
    os.mkdir("%s/%d" % (base_dir, e), 0o755)

  # create a simple parser that pulls the export_version from the directory
  def parser(path):
    match = re.match("^" + base_dir + "/(\d+)$", path.path)
    if not match:
      return None
    return path._replace(export_version=int(match.group(1)))

  path_list = gc.get_paths("/tmp", parser)  # contains all ten Paths

  every_fifth = gc.mod_export_version(5)
  print every_fifth(path_list) # shows ["/tmp/0", "/tmp/5"]

  largest_three = gc.largest_export_versions(3)
  print largest_three(all_paths)  # shows ["/tmp/7", "/tmp/8", "/tmp/9"]

  both = gc.union(every_fifth, largest_three)
  print both(all_paths)  # shows ["/tmp/0", "/tmp/5",
                         #        "/tmp/7", "/tmp/8", "/tmp/9"]
  # delete everything not in 'both'
  to_delete = gc.negation(both)
  for p in to_delete(all_paths):
    gfile.DeleteRecursively(p.path)  # deletes:  "/tmp/1", "/tmp/2",
                                     # "/tmp/3", "/tmp/4", "/tmp/6",
i    (   t   absolute_import(   t   division(   t   print_functionN(   t   xrange(   t   gfilet   Paths   path export_versionc         ` s     f d   } | S(   sซ   Creates a filter that keeps the largest n export versions.

  Args:
    n: number of versions to keep.

  Returns:
    A filter function that keeps the n largest paths.
  c         ` s   g  } xE t  |   D]7 \ } } | j d  k	 r t j | | j | f  q q Wg  t j   |  D] \ } } |  | ^ qa } t |  S(   N(   t	   enumeratet   export_versiont   Nonet   heapqt   heappusht   nlargestt   sorted(   t   pathst   heapt   idxt   patht   _t   it   keepers(   t   n(    sf   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/gc.pyt   keepX   s     /(    (   R   R   (    (   R   sf   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/gc.pyt   largest_export_versionsO   s    	c         ` s     f d   } | S(   s  Creates a filter that keeps one of every n export versions.

  Args:
    n: interval size.

  Returns:
    A filter function that keeps exactly one path from each interval
    [0, n], (n, 2n], (2n, 3n], etc...  If more than one path exists in an
    interval the largest is kept.
  c         ` s   i  } x |  D]| } | j  d  k r( q n  | j  rK t j | j  d    n d } | j | d   } | s| | j  | j  k  r | | | <q q Wt | j    S(   Ni   i    (   R   R   t   matht   floort   getR   t   values(   R   t
   keeper_mapt   pt   intervalt   existing(   R   (    sf   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/gc.pyR   n   s    )(    (   R   R   (    (   R   sf   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/gc.pyt   one_of_every_n_export_versionsc   s    c         ` s     f d   } | S(   sฑ   Creates a filter that keeps every export that is a multiple of n.

  Args:
    n: step size.

  Returns:
    A filter function that keeps paths where export_version % n == 0.
  c         ` sD   g  } x1 |  D]) } | j    d k r | j |  q q Wt |  S(   Ni    (   R   t   appendR   (   R   R   R   (   R   (    sf   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/gc.pyR      s
    (    (   R   R   (    (   R   sf   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/gc.pyt   mod_export_version   s    	c         ` s      f d   } | S(   sฎ   Creates a filter that keeps the union of two filters.

  Args:
    lf: first filter
    rf: second filter

  Returns:
    A filter function that keeps the n largest paths.
  c         ` s8   t    |    } t   |    } t t | | B  S(   N(   t   setR   t   list(   R   t   lt   r(   t   lft   rf(    sf   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/gc.pyR      s    (    (   R&   R'   R   (    (   R&   R'   sf   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/gc.pyt   union   s    
c         ` s     f d   } | S(   s   Negate a filter.

  Args:
    f: filter function to invert

  Returns:
    A filter function that returns the negation of f.
  c         ` s2   t  |   } t    |    } t t | |   S(   N(   R"   R   R#   (   R   R$   R%   (   t   f(    sf   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/gc.pyR   ฌ   s    (    (   R)   R   (    (   R)   sf   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/gc.pyt   negationฃ   s    	c         C` sj   t  j |   } g  } xH | D]@ } | t t j j |  |  d   } | r | j |  q q Wt |  S(   sค  Gets a list of Paths in a given directory.

  Args:
    base_dir: directory.
    parser: a function which gets the raw Path and can augment it with
      information such as the export_version, or ignore the path by returning
      None.  An example parser may extract the export version from a path
      such as "/tmp/exports/100" an another may extract from a full file
      name such as "/tmp/checkpoint-99.out".

  Returns:
    A list of Paths contained in the base directory with the parsing function
    applied.
    By default the following fields are populated,
      - Path.path
    The parsing function is responsible for populating,
      - Path.export_version
  N(	   R   t   ListDirectoryR   t   osR   t   joinR   R    R   (   t   base_dirt   parsert	   raw_pathsR   R%   R   (    (    sf   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/gc.pyt	   get_pathsณ   s    $(   t   __doc__t
   __future__R    R   R   t   collectionsR	   R   R,   t	   six.movesR   t   tensorflow.python.platformR   t
   namedtupleR   R   R   R!   R(   R*   R1   (    (    (    sf   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/gc.pyt   <module>>   s    					