tools: copy-link support and size calculations
[treecutter.git] / treecutter / tools.py
index f294c61106d8cfa025d8d660163001c6e9c7ddf4..3371a0c117ddbaa2ebc0b6a8a22cd7390edb714a 100644 (file)
@@ -1,7 +1,9 @@
 #!/usr/bin/python
+#from __future__ import print_function
 import os
 import subprocess
 import errno
+import sys
 
 def mkdir_p(path):
     try:
@@ -12,15 +14,49 @@ def mkdir_p(path):
         else: raise
 
 def publish(src,target):
-    cmd = ["rsync","-a","--delete",src,target]
+    cmd = ["rsync","-a","--copy-links","--partial",src,target]
     retcode = subprocess.call(cmd)
     if retcode:
-        print 'Error: '+' '.join(cmd)+' Returncode ['+str(retcode)+']'
+        error('%s : retruncode %s' % (' '.join(cmd),str(retcode)))
 
 def ssh_cmd(target, command):
     t = target.split(":")
     c = command.split()
-    cmd = ["ssh",t[0],c[0],c[1],t[1]]
+    if len(t)==1:
+        cmd = [c[0],c[1],t[0]]
+    else:
+        cmd = ["ssh",t[0],c[0],c[1],t[1]]
     retcode = subprocess.call(cmd)
     if retcode:
-        print 'Error: '+' '.join(cmd)+' Returncode ['+str(retcode)+']'
+        error('%s : retruncode %s' % (' '.join(cmd),str(retcode)))
+
+def sizeof_fmt(num, suffix='B'):
+    for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
+        if abs(num) < 1024.0:
+            return "%3.1f%s%s" % (num, unit, suffix)
+        num /= 1024.0
+    return "%.1f%s%s" % (num, 'Yi', suffix)
+
+def get_folder_size(folder):
+    total_size = os.path.getsize(folder)
+    if os.path.isfile(folder):
+        return total_size
+    for item in os.listdir(folder):
+        itempath = os.path.join(folder, item)
+        if os.path.isfile(itempath):
+            total_size += os.path.getsize(itempath)
+        elif os.path.isdir(itempath):
+            total_size += get_folder_size(itempath)
+    return total_size
+
+#proc = subprocess.Popen(args, env={'PATH': os.getenv('PATH')})
+def translate(files):
+    for f in files:
+        out = subprocess.check_output(["translate.sh", f])
+        sys.stdout.write('#')
+
+#def warning(*objs):
+#    print("WARNING: ", *objs, file=sys.stderr)
+
+#def error(*objs):
+#    print("ERROR: ", *objs, file=sys.stderr)