[go: up one dir, main page]

Menu

[r31]: / reverse / tools / clean-odf.py  Maximize  Restore  History

Download this file

89 lines (79 with data), 2.3 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/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()