def testListRecursivelyForEmptyDirectory(self):
        empty_dir = tempfile.mkdtemp(prefix=self.get_temp_dir())
        subdirectory_entries = list(io_wrapper.ListRecursively(empty_dir))
        self.assertEqual(1, len(subdirectory_entries))

        entry = subdirectory_entries[0]
        self.assertEqual(empty_dir, entry[0])
        self.assertItemsEqual((), entry[1])
Exemple #2
0
def GetLogdirSubdirectories(path):
    """Returns subdirectories with event files on path."""
    if gfile.Exists(path) and not gfile.IsDirectory(path):
        raise ValueError('GetLogdirSubdirectories: path exists and is not a '
                         'directory, %s' % path)

    # ListRecursively just yields nothing if the path doesn't exist.
    return (subdir for (subdir, files) in io_wrapper.ListRecursively(path)
            if list(filter(event_accumulator.IsTensorFlowEventsFile, files)))
    def testListRecursivelyForNestedFiles(self):
        temp_dir = tempfile.mkdtemp(prefix=self.get_temp_dir())

        # Add a few subdirectories.
        directory_names = (
            'foo',
            'bar',
            'bar/baz',
        )
        for directory_name in directory_names:
            os.makedirs(os.path.join(temp_dir, directory_name))

        # Add a few files to the directory.
        file_names = (
            'events.out.tfevents.1473720381.meep.com',
            'model.ckpt',
            'bar/events.out.tfevents.1473720382.bar.com',
            'bar/red_herring.txt',
            'bar/baz/events.out.tfevents.1473720383.baz.com',
            'bar/baz/events.out.tfevents.1473720384.baz.com',
        )
        for file_name in file_names:
            open(os.path.join(temp_dir, file_name), 'w').close()

        # There were 4 subdirectories in total.
        listing = io_wrapper.ListRecursively(temp_dir)
        directory_to_listing = {
            dir: list(generator)
            for (dir, generator) in listing
        }
        expected = ('foo', 'bar', 'bar/baz')
        self.assertItemsEqual([os.path.join(temp_dir, f)
                               for f in expected] + [temp_dir],
                              directory_to_listing.keys())

        # Test for the listings of individual directories.
        expected = (
            'events.out.tfevents.1473720381.meep.com',
            'model.ckpt',
        )
        self.assertItemsEqual((os.path.join(temp_dir, f) for f in expected),
                              directory_to_listing[temp_dir])

        expected = ()
        self.assertItemsEqual(
            (os.path.join(temp_dir, 'foo', f) for f in expected),
            directory_to_listing[os.path.join(temp_dir, 'foo')])

        expected = (
            'events.out.tfevents.1473720382.bar.com',
            'red_herring.txt',
        )
        self.assertItemsEqual(
            (os.path.join(temp_dir, 'bar', f) for f in expected),
            directory_to_listing[os.path.join(temp_dir, 'bar')])

        expected = (
            'events.out.tfevents.1473720383.baz.com',
            'events.out.tfevents.1473720384.baz.com',
        )
        self.assertItemsEqual(
            (os.path.join(temp_dir, 'bar/baz', f) for f in expected),
            directory_to_listing[os.path.join(temp_dir, 'bar/baz')])