forked from holland-backup/holland
/
path.py
136 lines (110 loc) · 3.08 KB
/
path.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# $Id$
"""
Utility functions
"""
# Functions added here should really be as portable as possible
# and generally useful.
import os
import sys
import logging
LOGGER = logging.getLogger(__name__)
def ensure_dir(dir_path):
"""
Ensure a directory path exists (by creating it if it doesn't).
"""
if not os.path.exists(dir_path):
try:
os.makedirs(dir_path)
LOGGER.debug("created directory %s" % dir_path)
return True
except OSError, e:
# FIX ME: Need error codes/etc so this will exit(<code>) or raise
# an appropriate holland exception
LOGGER.error("os.makedirs(%s): %s" % (dir_path, e))
raise
return False
def protected_path(path):
"""
Take a path, and if the file/dir exist pass back a protected path
(suffixed).
Returns:
string = new file path
Example:
>>> mypath = '/tmp'
>>> new_path = helpers.protected_path(mypath)
>>> new_path
'/tmp.0'
"""
log = logging.getLogger(__name__)
safety = 0
safe_path = path
while True:
if os.path.exists(safe_path):
safe_path = "%s.%s" % (path, safety)
else:
break
safety = safety + 1
return safe_path
def format_bytes(bytes, precision=2):
"""
Format an integer number of bytes to a human
readable string.
If bytes is negative, this method raises ArithmeticError
"""
import math
if bytes < 0:
raise ArithmeticError("Only Positive Integers Allowed")
if bytes != 0:
exponent = math.floor(math.log(bytes, 1024))
else:
exponent = 0
return "%.*f%s" % (
precision,
bytes / (1024 ** exponent),
['B','KB','MB','GB','TB','PB','EB','ZB','YB'][int(exponent)]
)
def normpath(path):
from os.path import normpath, abspath
return abspath(normpath(path))
def relpath(origin, dest):
"""
Find the relative path between origin and dest
"""
parts = []
origin = os.path.normpath(origin)
dest = os.path.normpath(dest)
if origin == dest:
return ""
path = dest
while True:
head, tail = os.path.split(path)
if not tail:
break
if head == origin:
return os.path.join('', *([tail] + parts))
else:
parts.insert(0, tail)
path = head
return None
def disk_free(target_path):
"""
Find the amount of space free on a given path
Path must exist.
This method does not take into account quotas
returns the size in bytes potentially available
to a non privileged user
"""
info = os.statvfs(target_path)
return info.f_frsize*info.f_bavail
def directory_size(path):
"""
Find the size of all files in a directory, recursively
Returns the size in bytes on success
"""
from os.path import join, getsize
result = 0
for root, dirs, files in os.walk(path):
for name in files:
sz = getsize(join(root,name))
result = result + sz
return result