Rafael Aditama
Rafael Aditama

Reputation: 5

Iterate through each XML file

So currently i have a code that passed the information to Report Portal from a XML file, this xml file located on its own folder and it applies to many folder. Currently, the parser only pass the last xml data that are stored in the memory even though it recognize all the other file

this is my code for now:

launch = service.start_launch(name=launch_name,
                              attributes=rp_attributes,
                              start_time=timestamp(),
                              description=launch_doc)

r_path='\\\\esw-fs01\\esw_niagara_no_bck\\BuildResults\\master\\0.1.52.68_390534\\installation_area\\autotestlogs_top'

root = os.listdir(r_path)

for entry in root:
    subdir_path = os.path.join(r_path, entry) #  create the absolute path of the subdir
    if os.path.isdir(subdir_path):  # check if it is a folder
        subdir_entries = os.listdir(subdir_path)  # get the content of the subdir
        for subentry in subdir_entries:
            if subentry.endswith('.xml'):
                subentry_path = os.path.join(subdir_path, subentry)

                tree = ET.parse(subentry_path)
                root=tree.getroot()


for subentry_path in root.iter('entry'):
    if subentry_path.get('type') == "TM":
        if suite_item_id != None:
            service.finish_test_item(item_id=suite_item_id, end_time=timestamp(), status=tm_verdict)
        suite=subentry_path.find('name').text
        description=subentry_path.find('messages').text
        verdict=subentry_path.find('verdict').text
        if verdict=="True":
            tm_verdict="PASSED"
        elif verdict=="False":
            tm_verdict="FAILED"
        suite_item_id = service.start_test_item(name=suite,
                                  description=description,
                                  attributes=rp_attributes,                                  
                                  start_time=timestamp(),
                                  item_type="SUITE")
    if subentry_path.get('type') == "TR":
        name    = subentry_path.find('name').text 
        verdict = subentry_path.find('verdict').text
        link    = subentry_path.find('link').text
        duration = 10
        description = subentry_path.find('messages').text
        if verdict=="True":
            verdict="PASSED"
        elif verdict=="False":
            verdict="FAILED"
        start_time=timestamp()
        item_id = service.start_test_item(name=name,
                                        description=description,
                                        start_time=start_time,
                                        parent_item_id=suite_item_id,
                                        item_type="STEP",
                                        parameters={"key1": "val1",
                                                    "key2": "val2"})

the goal is to make it read all the files, any help will be apreciated

Upvotes: 0

Views: 1553

Answers (1)

fsimonjetz
fsimonjetz

Reputation: 5802

You could first build a list of paths, then in the second loop parse the files.

# don't use 'root' here
src = os.listdir(r_path)
files = list()

for entry in src:
    subdir_path = os.path.join(r_path, entry)
    if os.path.isdir(subdir_path):
        subdir_entries = os.listdir(subdir_path)
        for subentry in subdir_entries:
            if subentry.endswith('.xml'):
                subentry_path = os.path.join(subdir_path, subentry)
                files.append(subentry_path)


for f in files:
    tree = ET.parse(f)
    root = tree.getroot()

    for subentry_path in root.iter('entry'):
        ...

As a side note, it's advisable to use something more canonical to get all the files, like glob:

import glob

filelist = glob.glob(os.path.join(rpath, "**/*.xml"), recursive=True)

filelist is now a list of paths to all the xml files in the source directory. This will save you a couple of lines and indentations.

Upvotes: 1

Related Questions