diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ec9eff1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +# We don't track Python virtual environments +build/ +__pycache__/ +libufps.egg-info/ +venv/ +docs/build +docs/source/_autosummary +/key.secret +index.ufps diff --git a/libufps/ufps.py b/libufps/ufps.py index 114c718..e4655c1 100644 --- a/libufps/ufps.py +++ b/libufps/ufps.py @@ -2,6 +2,7 @@ import os import stat import pwd import grp +import subprocess class ufps: """ @@ -73,10 +74,12 @@ class ufps: return isdir - def parsePerms(self, path): + def parsePath(self, path): """ Parses octal permissions stats into a string. Also determines other characteristics of the path: if it's a directory, and if it's a link (and where it goes). Returns this information as well. - :param oct_perm: Permissions of path in octal form (i.e. 0o0100644) + :param path: Path to file or directory. + :type path: str + :return pPerms: Path permissions :rtype pPerms: str @@ -89,9 +92,10 @@ class ufps: :return pLinkTarget: Where the link points to (if applicable) :rtype pLinkTarget: str """ + realpath = self.basedir+path # Get stats of the file/directory - stats = os.stat(self.basedir+path) + stats = os.stat(realpath) # Invoke a magic spell # Magic number -3 is used as for this we only need the last 3 digits. @@ -99,14 +103,14 @@ class ufps: pPerms = oct(stats.st_mode)[-3:] # Get whether or not the path is a dir - pIsDir = os.path.isdir(path) + pIsDir = os.path.isdir(realpath) # Get whether or not the path is a link. - pIsLink = os.path.islink(path) + pIsLink = os.path.islink(realpath) pLinkTarget = None if pIsLink: - pLinkTarget = os.readlink(path) + pLinkTarget = os.readlink(realpath) return pPerms, pIsDir, pIsLink, pLinkTarget @@ -114,16 +118,25 @@ class ufps: """ Gets Unix file permissions of a directory or file. :param path: Path to directory or file - :type path: str + :type permStr: str + + :return pPerms: Path permissions + :rtype pPerms: str + + :return pIsDir: Whether or not the path is a directory + :rtype pIsDir: bool + + :return pIsLink: Whether or not the path is a link + :rtype pIsLink: bool + + :return pLinkTarget: Where the link points to (if applicable) + :rtype pLinkTarget: str """ - # Parse oct_perm - permStr, isDir, isLink, linkTarget = self.parsePerms(path) + # TODO: Consider just moving parsePerms to getFilePermissions - # If it's a directory, we also want to figure out if it's a link. - #isDirectoryLink + pPerms, pIsDir, pIsLink, pLinkTarget = self.parsePath(path) - D_userFriendlyString = permStr + " | " + str(isDir) + " | " + str(isLink) + " | " + str(linkTarget) - return D_userFriendlyString + return pPerms, pIsDir, pIsLink, pLinkTarget def getFileOwner(self, path): """ @@ -137,7 +150,9 @@ class ufps: :return groupname: Group name :rtype groupname: str """ - stats = os.stat(path) + realpath = self.basedir+path + + stats = os.stat(realpath) uid = stats.st_uid gid = stats.st_gid @@ -145,3 +160,48 @@ class ufps: groupname = grp.getgrgid(stats.st_gid).gr_name return ownername, groupname + +# def getDirectoryContents(self, path): +# realpath = self.basedir + path +# contents = os.listdir(path) +# return contents + +# def getInformationR(self, path): +# for root, dirs, files in os.walk(path): +# for dir in dirs: +# +# for file in files: + + + def createIndex(self, path): + """ + Create an idex by delving into subdirectories and recording information. + :rtype path: Path to directory or file + :type path: str + + :return outputIndex: Returns file permission, owner, directory, and link information in a string to be written to a file. + :rtype outputIndex: str + """ + outputIndex = "" + + realpath = self.basedir+path + #realpath = path + findCommand = "find " + realpath + " -printf '%P\n'" + + fileList = subprocess.check_output(findCommand, shell=True, text=True) + fileList = fileList.split('\n') + + # Remove tailing newline which causes the root directory to be indexed twice. + fileList = fileList[:-1] + + for p in fileList: + pPerms, pIsDir, pIsLink, pLinkTarget = self.getFilePermissions(p) + ownername, groupname = self.getFileOwner(p) + + D_userFriendlyString = pPerms + " | " + ownername + " | " + groupname + " | " + str(pIsDir) + " | " + str(pIsLink) + " | " + p + " | " + str(pLinkTarget) + "\n" + outputIndex+=D_userFriendlyString + + # Remove tailing newline once again + outputIndex = outputIndex[:-1] + + return outputIndex