#!/usr/bin/env python
# clean-odf.py
# By Joseph Colton
# Used to prep an ODF document for looking at
import sys
import os
import commands
def usage():
print "Usage:"
print "\t%s <FILENAME>" % (sys.argv[0])
sys.exit()
# Make sure we have the right nimber of args
if (len(sys.argv) < 2): usage()
# Make sure the file is a file
filename = sys.argv[1]
if (not os.path.isfile(filename)):
print "Invalid filename"
usage()
# Create Expand Directory
newdir = "temp-%s" % os.path.basename(filename)
try:
os.mkdir(newdir)
except:
print "Unable to create directory '%s'" % newdir
sys.exit()
print "Creating directory %s" % newdir
# Copy file into directory and expand it
newfile = os.path.join(newdir, os.path.basename(filename))
os.link(filename, newfile)
os.chdir(newdir)
print "Expanding ODF file"
output = commands.getoutput('unzip %s' % os.path.basename(filename))
print output
# Set permissions correctly
print "Setting Permissions Correctly"
for root, dirs, files in os.walk('.'):
for dir in dirs:
dname = os.path.join(root, dir)
cmd = "chmod 755 %s" % dname
print cmd
commands.getoutput(cmd)
for file in files:
fname = os.path.join(root, file)
cmd = "chmod 644 %s" % fname
print cmd
commands.getoutput(cmd)
# Clean the XML
print "Cleaning XML"
for root, dirs, files in os.walk('.'):
for file in files:
fname = os.path.join(root, file)
parts = fname.split('.')
ext = parts[-1].lower()
# See if it is xml
if (ext == 'xml'):
print "Cleaning '%s'" % fname
outlines = []
# Read in file
f = open(fname, 'r')
while 1:
line = f.readline()
if not line: break
line = line.replace("><", ">\n<")
outlines.append(line)
f.close()
# Write out file
f = open(fname, 'w')
while outlines:
line = outlines.pop(0)
f.write(line)
f.close()
# Create a Makefile
print "Creating Makefile"
f = open("Makefile", "w")
f.write("XML=content.xml meta.xml settings.xml\n")
f.write("\n")
f.write("%s: $(XML)\n" % os.path.basename(filename))
f.write("\tzip -r %s .\n\n" % os.path.basename(filename))
f.close()