0

My goal: search webapps folder to find all .war files and make sure a folder exists with the same name(indicating .war expansion)

My Code:

import os
import stat

file = open("hosts.txt","r")

for servername in file:
    path = '\\\\'+servername.strip()+'\\d$\\tomcat\\Servers\\'
    server_name = servername.strip()
    for r,d,f in os.walk(path, topdown=False):

        for files in f:
            print os.path.join(r,files)
            if files.endswith(".war"):
                statinfo=os.stat(os.path.join(r,files))
                print "FILE LOCATION: " + os.path.join(r,files)+ " FILE SIZE: " +str(statinfo.st_size)
                print os.path.join(r,files)
                folder= os.path.join(r,files).replace('.war','')
                if os.path.isdir(folder):
                    None
                else:
                    print "folder missing:",folder

My problem:
on each server i'm searching the directory structure is such:
\server\d$\tomcat\servers\servername(random)\webapps

my search is taking forever because i can't put a wildcard in at \servername\ to skip down to the webapps folder where the .war files exist. If i do this with 200 vm's then the search will be quite expensive.

the folder missing print indicates to me the .war file did not expand because there's no folder matching.

Would someone know of a way to search the specific directory or now to skip a directory?

Thank you

2
Contributors
1
Reply
26
Views
2 Years
Discussion Span
Last Post by Gribouillis
1

You can use a breadth-first traversal to find the webapps folders first, then walk these folders

import collections as col
import itertools as itt
import os

def breadth_first(root, subnodes_func, max_depth=None):
    """Yield pairs (depth, node) in the breadth-first traversal of a tree

        Args:
            root: root node of the tree
            subnodes_func: function of one node argument returning
                a sequence of subnodes
            max_depth: maximum depth of generated nodes (defaults to None)
    """
    fifo = col.deque([(0, root)])
    limited = (max_depth is not None)
    while fifo:
        depth, node = fifo.popleft()
        yield depth, node
        if limited and depth >= max_depth:
            continue
        depth += 1
        for subnode in subnodes_func(node):
            fifo.append((depth, subnode))

def subdirs(dir):
    """Generates subfolders of a given folder"""
    try:
        r, d, f = next(os.walk(dir))
        for x in d:
            yield os.path.join(r, x)
    except StopIteration:
        return

def gen_webapps_dirs(root, max_depth=None):
    """Generate folders named 'webapps' while traversing root folder breadth-first"""
    for d, dir in breadth_first(root, subdirs, max_depth=max_depth):
        if os.path.basename(dir) == 'webapps':
            yield dir

if __name__ == '__main__':
    file = open("hosts.txt","r")

    for servername in file:
        path = '\\\\'+servername.strip()+'\\d$\\tomcat\\Servers\\'
        for webapps in gen_webapps_dirs(path, max_depth=2):
            print(webapps)

`

Edited by Gribouillis

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.