Created
May 7, 2018 11:29
-
-
Save arbakker/8c82133ff15373f60bb338b7827b4dda to your computer and use it in GitHub Desktop.
Python script to check GeoServer layergroups integrity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import glob | |
import lxml.etree as ET | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument("path", help="path to GeoServer data directory") | |
args = parser.parse_args() | |
# "/usr/local/apache-tomcat-8.0.33/webapps/geoserver_2.13/data/workspaces" | |
_path = args.path | |
_lyr_dic = {} | |
_lg_ids = [] | |
_lyr_ids = [] | |
def process_layergroup(path): | |
file = open(lg, "r") | |
s = file.read() | |
root = ET.fromstring(s) | |
layergroup_id = root.xpath('/layerGroup/id/text()')[0] | |
_lg_ids.append(layergroup_id) | |
layergroup_childs = [] | |
layer_childs = [] | |
for item in root.xpath("/layerGroup/publishables/published[@type='layerGroup']/id/text()"): | |
layergroup_childs.append(item) | |
for item in root.xpath("/layerGroup/publishables/published[@type='layer']/id/text()"): | |
layer_childs.append(item) | |
_lyr_dic[lg] = (layergroup_id, layergroup_childs, layer_childs) | |
def process_layer(path): | |
file = open(lyr, "r") | |
s = file.read() | |
root = ET.fromstring(s) | |
layer_id = root.xpath('/layer/id/text()')[0] | |
_lyr_ids.append(layer_id) | |
def proces_dictionary_item(key, value): | |
lg_id = value[0] | |
lg_childs = value[1] | |
l_childs = value[2] | |
for child in lg_childs: | |
if child not in _lg_ids: | |
raise AssertionError('Child layergroup {} of layergroup {} not found in data directory'.format(child, key)) | |
for child in l_childs: | |
if child not in _lyr_ids: | |
raise AssertionError('Child layer {} of layergroup {} not found in data directory'.format(child, key)) | |
if __name__ == '__main__': | |
for root, dirs, filelist in os.walk(_path): | |
if 'layergroups' in dirs: # insert logic to find the folder you want | |
lg = os.path.join(root, "layergroups") | |
# one layergroups directory can have multiple layergroup.xml files | |
glob_pattern = os.path.join(lg, "*.xml") | |
for lg in glob.glob(glob_pattern): | |
process_layergroup(lg) | |
if 'layer.xml' in filelist: | |
# layer directory can have only one layer.xml file | |
lyr = os.path.join(root, "layer.xml") | |
process_layer(lyr) | |
# python 3 | |
# for key, value in d.items(): | |
# python 2 | |
for key, value in _lyr_dic.iteritems(): | |
proces_dictionary_item(key,value) | |
print("All layergroup children are accounted for.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment