summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrederic Back <fredericback@gmail.com>2006-09-23 23:55:44 +0000
committerFrederic Back <fredericback@gmail.com>2006-09-23 23:55:44 +0000
commit0e811da0ac511c492c645b65c83015191c200aec (patch)
tree2ecebc2dfdf45d38701185aa36454b54c4942db0
parent722dacb7745d60f17a2010632eec74b5fe68ba90 (diff)
* Added experimental build_tree in c++
git-svn-id: file:///home/lennart/svn/public/fring/trunk@16 d0d2c35f-0a1e-0410-abeb-dabff30a67ee
-rw-r--r--setup.py8
-rw-r--r--src/extension/extensiontest.py23
-rw-r--r--src/extension/fringtools.cpp97
3 files changed, 126 insertions, 2 deletions
diff --git a/setup.py b/setup.py
index c33fde8..a734775 100644
--- a/setup.py
+++ b/setup.py
@@ -1,9 +1,11 @@
#!/usr/bin/python
from os import sep
-from distutils.core import setup
+from distutils.core import setup, Extension
if __name__ == '__main__' :
+
+ module = Extension('fringtools',sources = ['src'+sep+'extension'+sep+'fringtools.cpp'])
setup(\
name = "fring",
@@ -13,5 +15,7 @@ if __name__ == '__main__' :
packages = ['fringlib'],
package_dir = {'fringlib': 'src'+sep+'fringlib'},
- scripts = ['src'+sep+'fring.py']
+ scripts = ['src'+sep+'fring.py'],
+
+ ext_modules = [ module ]
)
diff --git a/src/extension/extensiontest.py b/src/extension/extensiontest.py
new file mode 100644
index 0000000..e95c7a3
--- /dev/null
+++ b/src/extension/extensiontest.py
@@ -0,0 +1,23 @@
+#!/usr/bin/env python
+
+import sys, os
+
+try: import fringtools
+except:
+ print "Install along with the normal setup.py script before testing"
+ sys.exit()
+
+if len(sys.argv) >= 2: tree_path = sys.argv[1]
+else: tree_path = os.path.expanduser("~")
+
+def read( t, tab=0 ):
+ fn, data, size = t
+ print " "*tab,fn,size
+ if not data: return
+ for e in data:
+ read(e,tab+1)
+
+print 'getting "%s":'%tree_path
+i = fringtools.build_tree(tree_path, True)
+print "done."
+read(i)
diff --git a/src/extension/fringtools.cpp b/src/extension/fringtools.cpp
new file mode 100644
index 0000000..577a486
--- /dev/null
+++ b/src/extension/fringtools.cpp
@@ -0,0 +1,97 @@
+#include <Python.h>
+
+#include <dirent.h>
+#include <iostream>
+#include <string>
+#include <vector>
+
+/*
+
+==============================================================================
+This is the original function in python
+
+def _build_tree(self,path):
+ l = []
+ for fn in os.listdir(path):
+ try: p = os.path.join(path, fn)
+ except: continue
+ s = os.lstat(p)
+ if stat.S_ISDIR(s.st_mode): l.append( (fn, self._build_tree(p)) )
+ elif stat.S_ISREG(s.st_mode): l.append( (fn, s.st_size) )
+ return sum_list(l, os.path.split(path)[-1])
+
+==============================================================================
+
+Instead of using SumList, the new build_tree will use a tuple
+
+*/
+
+/// recursive function to build a list system with directory information
+PyObject* build_tree( std::string path, const bool& skipHidden) {
+
+ PyObject* l = PyList_New(0);
+
+ DIR* d = opendir(path.c_str());
+ struct dirent *e;
+
+ int total = 0;
+
+ while((e = readdir(d)) != NULL) {
+
+ std::string fn = e->d_name;
+ if(skipHidden) if(e->d_name[0] == '.') continue;
+ if(fn == "." || fn == "..") continue;
+ fn = path+"/" +fn;
+
+ struct stat s;
+ lstat(fn.c_str(), &s);
+
+ if(S_ISDIR(s.st_mode)) {
+ PyObject* sub = build_tree(fn,skipHidden);
+ PyList_Append(l, sub );
+ total += PyInt_AsLong( PyTuple_GET_ITEM(sub,2) );
+ } else if (S_ISREG(s.st_mode)) {
+ PyList_Append(l, Py_BuildValue("(sOi)",fn.c_str(),Py_None,s.st_size) );
+ total += s.st_size;
+ }
+
+
+ }
+ closedir(d);
+
+ return Py_BuildValue("(sOi)", path.c_str(), l, total );
+}
+
+/// initial function call.
+static PyObject* fringtools_build_tree(PyObject *self, PyObject *args) {
+ const char *path;
+ int skip_hidden = 1;
+ if (!PyArg_ParseTuple(args, "si", &path, &skip_hidden)) return NULL;
+
+ PyObject* o;
+ //Py_BEGIN_ALLOW_THREADS
+ o = build_tree(std::string(path),(skip_hidden==1));
+ //Py_END_ALLOW_THREADS
+
+ return o;
+}
+
+//==============================================================================
+// module init
+//==============================================================================
+
+static PyMethodDef FringtoolsMethods[] = {
+ {"build_tree", fringtools_build_tree, METH_VARARGS, "Walk Tree."},
+ {NULL, NULL, 0, NULL} /* Sentinel */
+};
+
+PyMODINIT_FUNC initfringtools(void) {
+ (void) Py_InitModule("fringtools", FringtoolsMethods);
+}
+
+int main(int argc, char *argv[]) {
+ Py_SetProgramName(argv[0]);
+ Py_Initialize();
+ initfringtools();
+ return 0;
+}