* doc strings for util module
This commit is contained in:
+17
-15
@@ -13,21 +13,20 @@ class FileError(Exception):
|
|||||||
class UtilError(Exception):
|
class UtilError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# shorthand to check if a file exists
|
|
||||||
def check_file(file, mode = os.F_OK):
|
def check_file(file, mode = os.F_OK):
|
||||||
|
"shorthand to check if a file exists"
|
||||||
if not os.access(file, mode):
|
if not os.access(file, mode):
|
||||||
raise FileError("File " + file + " not found")
|
raise FileError("File " + file + " not found")
|
||||||
|
|
||||||
# check if directory exists, and create if it doesn't
|
|
||||||
# works recursively
|
|
||||||
# FIXME: could have a better name
|
|
||||||
def check_dir(dir):
|
def check_dir(dir):
|
||||||
|
"""check if directory exists, and create if it doesn't.
|
||||||
|
works recursively"""
|
||||||
dir = dir.strip().rstrip("/")
|
dir = dir.strip().rstrip("/")
|
||||||
if not os.access(dir, os.F_OK):
|
if not os.access(dir, os.F_OK):
|
||||||
os.makedirs(dir)
|
os.makedirs(dir)
|
||||||
|
|
||||||
def clean_dir(top):
|
def clean_dir(top):
|
||||||
"""Remove all content of a directory (top)"""
|
"Remove all content of a directory (top)"
|
||||||
for root, dirs, files in os.walk(top, topdown=False):
|
for root, dirs, files in os.walk(top, topdown=False):
|
||||||
for name in files:
|
for name in files:
|
||||||
os.remove(os.path.join(root, name))
|
os.remove(os.path.join(root, name))
|
||||||
@@ -35,9 +34,9 @@ def clean_dir(top):
|
|||||||
os.rmdir(os.path.join(root, name))
|
os.rmdir(os.path.join(root, name))
|
||||||
|
|
||||||
|
|
||||||
# calculate the size of files under a dir
|
|
||||||
# based on the os module example
|
|
||||||
def dir_size(dir):
|
def dir_size(dir):
|
||||||
|
""" calculate the size of files under a dir
|
||||||
|
based on the os module example"""
|
||||||
from os.path import join, getsize
|
from os.path import join, getsize
|
||||||
def sizes():
|
def sizes():
|
||||||
for root, dirs, files in os.walk(dir):
|
for root, dirs, files in os.walk(dir):
|
||||||
@@ -45,26 +44,29 @@ def dir_size(dir):
|
|||||||
yield sum(getsize(join(root, name)) for name in files)
|
yield sum(getsize(join(root, name)) for name in files)
|
||||||
return sum( sizes() )
|
return sum( sizes() )
|
||||||
|
|
||||||
def copy_file(s,d):
|
def copy_file(src,dest):
|
||||||
check_file(s)
|
"""copy source file to destination file"""
|
||||||
check_dir(os.path.dirname(d))
|
check_file(src)
|
||||||
fs = file(s, 'rb')
|
check_dir(os.path.dirname(dest))
|
||||||
fd = file(d, 'wb')
|
fs = file(src, 'rb')
|
||||||
|
fd = file(dest, 'wb')
|
||||||
for l in fs:
|
for l in fs:
|
||||||
fd.write(l)
|
fd.write(l)
|
||||||
|
|
||||||
def copy_dir():
|
def copy_dir(src, dest):
|
||||||
|
"""copy source dir to destination dir recursively"""
|
||||||
raise UtilError("not implemented")
|
raise UtilError("not implemented")
|
||||||
|
|
||||||
def md5_file(filename):
|
def md5_file(filename):
|
||||||
|
"""calculate md5 hash of filename"""
|
||||||
m = md5.new()
|
m = md5.new()
|
||||||
f = file(filename, 'rb')
|
f = file(filename, 'rb')
|
||||||
for l in f:
|
for l in f:
|
||||||
m.update(l)
|
m.update(l)
|
||||||
return m.hexdigest()
|
return m.hexdigest()
|
||||||
|
|
||||||
# run a command non-interactively
|
|
||||||
def run_batch(cmd):
|
def run_batch(cmd):
|
||||||
|
"""run command non-interactively and report return value and output"""
|
||||||
ui.info('running ' + cmd)
|
ui.info('running ' + cmd)
|
||||||
a = os.popen(cmd)
|
a = os.popen(cmd)
|
||||||
lines = a.readlines()
|
lines = a.readlines()
|
||||||
@@ -75,6 +77,6 @@ def run_batch(cmd):
|
|||||||
ui.error('ERROR: executing command: ' + cmd + '\n' + strlist(lines))
|
ui.error('ERROR: executing command: ' + cmd + '\n' + strlist(lines))
|
||||||
return (successful,lines)
|
return (successful,lines)
|
||||||
|
|
||||||
# print a list
|
|
||||||
def strlist(l):
|
def strlist(l):
|
||||||
|
"""concatenate string reps of l's elements"""
|
||||||
return string.join(map(lambda x: str(x) + ' ', l))
|
return string.join(map(lambda x: str(x) + ' ', l))
|
||||||
|
|||||||
Reference in New Issue
Block a user