util: Raise FilePermissionDeniedError if the file cannot be

read by the current user.

Whitespace, variable name cleanups
This commit is contained in:
Ozan Çağlayan
2011-06-07 07:43:37 +00:00
parent 7f5d97a984
commit ff87a7716d
+24 -21
View File
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2005-2010, TUBITAK/UEKAE
# Copyright (C) 2005-2011, TUBITAK/UEKAE
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
@@ -53,6 +53,9 @@ class Error(pisi.Error):
class FileError(Error):
pass
class FilePermissionDeniedError(Error):
pass
#########################
# spec validation utility #
@@ -84,9 +87,9 @@ def strlist(l):
"""Concatenate string reps of l's elements."""
return "".join(map(lambda x: str(x) + ' ', l))
def multisplit(str, chars):
def multisplit(_str, chars):
"""Split str with any of the chars."""
l = [str]
l = [_str]
for c in chars:
l = concat(map(lambda x:x.split(c), l))
return l
@@ -305,7 +308,6 @@ def parentpath(a, sep = os.path.sep):
def parenturi(a):
return parentpath(a, '/')
# but this one is necessary
def subpath(a, b):
"""Find if path a is before b in the directory tree."""
return prefix(splitpath(a), splitpath(b))
@@ -320,9 +322,7 @@ def removepathprefix(prefix, path):
def join_path(a, *p):
"""Join two or more pathname components.
Python os.path.join cannot handle '/' at the start of latter components.
"""
path = a
for b in p:
@@ -337,10 +337,10 @@ def join_path(a, *p):
# File/Directory Related Functions #
####################################
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):
raise FileError("File " + file + " not found")
if not os.access(_file, mode):
raise FileError("File " + _file + " not found")
return True
def ensure_dirs(path):
@@ -353,14 +353,14 @@ def clean_dir(path):
if os.path.exists(path):
shutil.rmtree(path)
def creation_time(file):
def creation_time(_file):
"""Return the creation time of the given file."""
if check_file(file):
if check_file(_file):
import time
st = os.stat(file)
st = os.stat(_file)
return time.localtime(st.st_ctime)
def dir_size(dir):
def dir_size(_dir):
"""Calculate the size of files under a directory."""
# It's really hard to give an approximate value for package's
# installed size. Gettin a sum of all files' sizes if far from
@@ -368,15 +368,15 @@ def dir_size(dir):
# better solution :(.
# Not really, du calculates size on disk, this is much better -- exa
if os.path.exists(dir) and (not os.path.isdir(dir) and not os.path.islink(dir)):
if os.path.exists(_dir) and (not os.path.isdir(_dir) and not os.path.islink(_dir)):
#so, this is not a directory but file..
return os.path.getsize(dir)
return os.path.getsize(_dir)
if os.path.islink(dir):
return long(len(read_link(dir)))
if os.path.islink(_dir):
return long(len(read_link(_dir)))
def sizes():
for root, dirs, files in os.walk(dir):
for root, dirs, files in os.walk(_dir):
yield sum([os.path.getsize(join_path(root, name)) for name in files if not os.path.islink(join_path(root, name))])
return sum(sizes())
@@ -495,7 +495,7 @@ def sha1_file(filename):
# Broken links can cause problem!
try:
m = hashlib.sha1()
f = file(filename, 'rb')
f = open(filename, 'rb')
while True:
# 256 KB seems ideal for speed/memory tradeoff
# It wont get much faster with bigger blocks, but
@@ -510,7 +510,11 @@ def sha1_file(filename):
# we wont have two allocated blocks with same size
del block
return m.hexdigest()
except IOError:
except IOError, e:
if e.errno == 13:
# Permission denied, the file doesn't have read permissions, skip
raise FilePermissionDeniedError(_("You don't have necessary permissions to read %s") % filename)
else:
raise FileError(_("Cannot calculate SHA1 hash of %s") % filename)
def sha1_data(data):
@@ -657,7 +661,6 @@ def parse_package_name_legacy(package_name):
"""Separate package name and version string for package formats <= 1.1.
example: tasma-1.0.3-5-2 -> (tasma, 1.0.3-5-2)
"""
# We should handle package names like 855resolution
name = []