Skip to content

Instantly share code, notes, and snippets.

@ebot
Created January 18, 2012 20:50
Show Gist options
  • Save ebot/1635515 to your computer and use it in GitHub Desktop.
Save ebot/1635515 to your computer and use it in GitHub Desktop.
Merges separate Soarian XML files into one xml
#!/usr/bin/python
#
# Soarian Merge Script
# Created By Ed Botzum
#
# This script reads the specified input xml files in the specified
# directory and merges them into one mod file. It then deletes the
# specified files from the processing directory.
import glob
import os
import sys
import xml.dom.minidom
mod_file = open( 'soar_' + sys.argv[1] + '.xml', 'w' )
directory = sys.argv[2]
input_files = sys.argv[3]
if not os.path.exists( directory + '/backup' ): os.makedirs( directory + '/backup' )
if len(sys.argv) == 5:
delete_files = sys.argv[4]
files = delete_files.split( '-' )
for delete_file in files:
print '\nRemoving ' + delete_file + ' files'
print '-----------------------------------------------'
for filename in glob.glob( directory + "/" + delete_file + "*" ):
print ' Deleting ' + filename
os.remove( filename )
print '\nDeleting old backup files'
print '-----------------------------------------------'
for filename in glob.glob( directory + "/backup/" + input_files + "*" ):
print ' Deleting ' + filename
os.remove( filename)
mod_file.write( "<?xml version='1.0' encoding='utf-8'?>" )
mod_file.write( '<Batches>' )
print '\nMerging ' + input_files + ' files'
print '-----------------------------------------------'
for filename in glob.glob( directory + "/" + input_files + "*" ):
print ' Appending ' + filename
input_file = open( filename, 'r' )
input_xml = input_file.read()
first_statement_loc = input_xml.index( '<serialization' )
input_xml = input_xml[first_statement_loc:-10]
input_file.close
mod_file.write( input_xml )
mod_file.flush
os.rename( filename, directory + '/backup/' + os.path.basename( filename ) )
mod_file.write( '</Batches>' )
mod_file.close
print '\nExecution Complete'
print ' Merged file written to soar_' + sys.argv[1] + '.xml\n'
python merge_files.py hhrr input NSB GPS-GBD-NP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment