Coverage for org_fedora_oscap/utils.py : 69%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# # Copyright (C) 2013 Red Hat, Inc. # # This copyrighted material is made available to anyone wishing to use, # modify, copy, or redistribute it subject to the terms and conditions of # the GNU General Public License v.2, or (at your option) any later version. # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY expressed or implied, including the implied warranties of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General # Public License for more details. You should have received a copy of the # GNU General Public License along with this program; if not, write to the # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA # 02110-1301, USA. Any Red Hat trademarks that are incorporated in the # source code or documentation are not subject to the GNU General Public # License and may only be used or replicated with the express permission of # Red Hat, Inc. # # Red Hat Author(s): Vratislav Podzimek <vpodzime@redhat.com> #
""" Checks if a given directory exists and if not, it creates the directory as well as all the nonexisting directories in its path.
:param dirpath: path to the directory to be checked/created :type dirpath: str
"""
# nothing can be done for an empty string
""" Function that copies the files or directories specified by the src argument to the destination given by the dst argument. It should follow the same rules as the standard 'cp' utility.
:param src: source to copy -- may be a glob, file path or a directory path :type src: str :param dst: destination to copy to :type src: str
"""
if glob.has_magic(src): # src is a glob sources = glob.glob(src) else: # not a glob sources = [src]
for item in sources: if os.path.isdir(item): if os.path.isdir(dst): item = item.rstrip("/") dirname = item.rsplit("/", 1)[-1] shutil.copytree(item, join_paths(dst, dirname)) else: shutil.copytree(item, dst) else: shutil.copy2(item, dst)
""" Function that maps the given function to items in the given iterable keeping the type of the iterable.
:param func: function to be mapped on the items in the iterable :type func: in_item -> out_item :param iterable: iterable providing the items the function should be mapped on :type iterable: iterable :return: iterable providing items produced by the function mapped on the input items :rtype: the same type as input iterable or generator if the iterable is not of any basic Python types
"""
else: else:
""" Joins two paths as one would expect -- i.e. just like the os.path.join function except for doing crazy things when the second argument is an absolute path.
:param path1: first path :type path1: str :param path2: second path :type path2: str :return: path1 and path2 joined with the file separator :rtype: str
"""
# os.path.normpath doesn't squash two starting slashes
""" Get hashing algorithm for the given fingerprint or None if fingerprint of unsupported length is given.
:param fingerprint: hexa fingerprint to get the hashing algorithm for :type fingerprint: hexadecimal str :return: one of the hashlib.* hash objects :rtype: hashlib.HASH object
"""
hashlib.sha256(), hashlib.sha384(), hashlib.sha512())
# pylint: disable-msg=E1103
""" Get fingerprint of the given file with the given hashing algorithm.
:param fpath: path to the file to get fingerprint for :type fpath: str :param hash_obj: hashing algorithm to get fingerprint with :type hash_obj: hashlib.HASH :return: fingerprint of the given file with the given algorithm :rtype: hexadecimal str
"""
with open(fpath, "r") as fobj: bsize = 4*1024 # process file as 4 KB blocks buf = fobj.read(bsize) while buf: hash_obj.update(buf) buf = fobj.read(bsize)
return hash_obj.hexdigest() |