sitemap: adding sizecalculation
[treecutter.git] / treecutter / tools.py
1 #!/usr/bin/python
2 import os
3 import subprocess
4 import errno
5
6 def mkdir_p(path):
7     try:
8         os.makedirs(path)
9     except OSError as exc: # Python >2.5
10         if exc.errno == errno.EEXIST:
11             pass
12         else: raise
13
14 def publish(src,target):
15     cmd = ["rsync","-a","--delete",src,target]
16     retcode = subprocess.call(cmd)
17     if retcode:
18         print 'Error: '+' '.join(cmd)+' Returncode ['+str(retcode)+']'
19
20 def ssh_cmd(target, command):
21     t = target.split(":")
22     c = command.split()
23     if len(t)==1:
24         cmd = [c[0],c[1],t[0]]
25     else:
26         cmd = ["ssh",t[0],c[0],c[1],t[1]]
27     retcode = subprocess.call(cmd)
28     if retcode:
29         print 'Error: '+' '.join(cmd)+' Returncode ['+str(retcode)+']'
30
31 def sizeof_fmt(num, suffix='B'):
32     for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
33         if abs(num) < 1024.0:
34             return "%3.1f%s%s" % (num, unit, suffix)
35         num /= 1024.0
36     return "%.1f%s%s" % (num, 'Yi', suffix)
37
38 def get_folder_size(folder):
39     total_size = os.path.getsize(folder)
40     for item in os.listdir(folder):
41         itempath = os.path.join(folder, item)
42         if os.path.isfile(itempath):
43             total_size += os.path.getsize(itempath)
44         elif os.path.isdir(itempath):
45             total_size += get_folder_size(itempath)
46     return total_size