diff --git a/transfer_service/system_utils.py b/transfer_service/system_utils.py index d82bd86ba44283fea3fbf0456ed9ce3ddd8a1a6d..ac2aacf9b650fa2deccf9279eb798a574d7ad38e 100644 --- a/transfer_service/system_utils.py +++ b/transfer_service/system_utils.py @@ -6,20 +6,21 @@ # import os +import pwd import re import shutil import sys class SystemUtils(object): - + UNITS = { "B": 1, "KB": 1 << 10, "MB": 1 << 20, "GB": 1 << 30, "TB": 1 << 40, - "PB": 1 << 50, + "PB": 1 << 50, } def __init__(self): @@ -27,24 +28,15 @@ class SystemUtils(object): def userInfo(self, username): """ - Parses '/etc/passwd' and returns user, uid and gid associated to - a given username. + Returns username, UID and GID associated to a given user, + using the Python password database module (pwd). """ try: - fp = open("/etc/passwd", 'r') - except FileNotFoundError: - raise - else: - for line in fp: - info = line.split(':') - user = info[0] - uid = int(info[2]) - gid = int(info[3]) - if user == username: - fp.close() - return [ user, uid, gid ] - fp.close() + info = pwd.getpwnam(username) + except KeyError: return False + else: + return [ info[0], info[2], info[3] ] def findIllegalCharacters(self, name): """Checks for file/dir names containing illegal characters.""" @@ -53,7 +45,7 @@ class SystemUtils(object): return True else: return False - + def findInvalidFileAndDirNames(self, path): """ Scans 'path' recursively to search for file/dir names with @@ -108,7 +100,7 @@ class SystemUtils(object): # do nothing... pass return [ dirList, fileList ] - + def getSize(self, path): """ If 'path' is a file returns the file size in bytes, @@ -118,7 +110,7 @@ class SystemUtils(object): size = 0 if os.path.isfile(path) and not os.path.islink(path): size = os.path.getsize(path) - elif os.path.isdir(path) and not os.path.islink(path): + elif os.path.isdir(path) and not os.path.islink(path): for folder, subfolders, files in os.walk(path, topdown = True): cwd = os.path.basename(folder) parent = os.path.dirname(folder)