summaryrefslogtreecommitdiffstats
path: root/reduce.py
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2011-07-14 14:43:03 +0800
committerPeng Wu <alexepico@gmail.com>2011-07-14 14:43:03 +0800
commite037a2db71289c97d382311b6cfc450157a5bc1a (patch)
treea37e190c21271a28bf315ec752f9ee2afd44fc55 /reduce.py
parent24cadd56e4a93daf93e47287fd929b4d25a7a948 (diff)
downloadtrainer-e037a2db71289c97d382311b6cfc450157a5bc1a.zip
trainer-e037a2db71289c97d382311b6cfc450157a5bc1a.tar.gz
trainer-e037a2db71289c97d382311b6cfc450157a5bc1a.tar.xz
begin to write reduce.py
Diffstat (limited to 'reduce.py')
-rw-r--r--reduce.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/reduce.py b/reduce.py
new file mode 100644
index 0000000..1aa0f39
--- /dev/null
+++ b/reduce.py
@@ -0,0 +1,50 @@
+#!/usr/bin/python3
+import os
+import os.path
+import sys
+from myconfig import MyConfig
+
+
+config = MyConfig()
+
+def read_file(infile):
+ with open(infile, 'r') as f:
+ data = ''.join(f.readlines())
+ f.close()
+ return data
+
+def handleError(error):
+ sys.exit(error)
+
+def mergeSubIndex(output, path):
+ for root, dirs, files in os.walk(path, topdown=True, onerror=handleError):
+ for onefile in files:
+ filepath = os.path.join(root, onefile)
+ if onefile.endswith(config.getIndexPostfix()):
+ data = read_file(filepath)
+ output.writelines([data, os.linesep])
+ else:
+ print('Unexpected file:' + filepath)
+
+
+def iterateSubDirectory(oldroot, newroot, level):
+ #Merge the index in oldroot
+ if level <= 0 :
+ newindex = newroot + config.getIndexPostfix()
+ os.makedirs(os.path.dirname(newindex), exist_ok=True)
+ newindexfile = open(newindex, 'a')
+ mergeSubIndex(newindexfile, oldroot)
+ newindexfile.close()
+ #Recursive into the sub directories
+ for onedir in os.listdir(oldroot):
+ olddir = os.path.join(oldroot, onedir)
+ newdir = os.path.join(newroot, onedir)
+ if not os.path.isdir(olddir):
+ sys.exit('Un-expected file:' + olddir)
+ else:
+ iterateSubDirectory(olddir, newdir, level - 1)
+
+
+#test case
+if __name__ == '__main__':
+ pass