def retrieve_all_files(service, currentFileIDs, currentGDriveState, downloadpath):

    logging.info("****************Performing Full backup of drive.****************")
    import getFiles

    succDnLds = 0
    for GDriveObject in currentGDriveState:
        if GDriveObject["mimeType"].find("folder") == -1:
            if GDriveObject["id"] in currentFileIDs:
                dFile = getFiles.get_download_url(GDriveObject)
                if dFile != None:
                    try:
                        content, filename = getFiles.download_file(service, dFile)
                        getFiles.write_file(content, filename, downloadpath)
                        succDnLds += 1
                        logging.debug("Downloading %s of %s", succDnLds, len(currentFileIDs))
                    except Exception as e:
                        logging.error(
                            """Failed to download or write the file.
                                     \nERROR: %s""",
                            e,
                        )
    rsync_rm()
    message = "%s of %s files have downloaded and saved" % (succDnLds, len(currentFileIDs))
    logging.info(message)
    send_email(message, configData, False)
def download_diff(
    service,
    archivedFileIDs,
    currentFileIDs,
    archivedGDriveState,
    currentGDriveState,
    archivedGDriveStateFolderIds,
    currentGDriveStateFolderIds,
):

    logging.info("Performing Diff backup")
    # Must check both directions just incase one is empty
    if len(currentFileIDs.difference(archivedFileIDs)) == 0 and len(archivedFileIDs.difference(currentFileIDs)) == 0:
        import os.path, time

        message = "PeaceGeeks Google Drive auditor ran successfully:\n"
        message += "There have been no changes to you Google Drive since %s" % time.ctime(
            os.path.getmtime(archivedGDriveStateFilename)
        )
        try:
            send_email(message, configData, False)
            logger.info('"No updates needed." email sent.')
        except Exception as e:
            message = 'Failed to send "No updates needed." email. %s %s'
            logger.error(message, "ERROR: ", e)

        return False

    else:
        removedFileIDs = get_difference(archivedFileIDs, currentFileIDs)
        logger.debug("Retrieved set of removed file IDs.")
        addedFileIDs = get_difference(currentFileIDs, archivedFileIDs)
        logger.debug("Retrieved set of added file IDs.")

        reformatCurrent = reformatDrive(currentGDriveState, currentGDriveStateFolderIds)
        reformatArchive = reformatDrive(archivedGDriveState, archivedGDriveStateFolderIds)

        modfiedFileIDs = getModifiedFiles(reformatCurrent, reformatArchive)

        #  Download added Files
        import getFiles

        succDnLds = 0
        for GDriveObject in currentGDriveState:
            # XXX This will be if added or if changed
            if GDriveObject["id"] in modfiedFileIDs or GDriveObject["id"] in addedFileIDs:
                if GDriveObject["mimeType"].find("folder") == -1:
                    dFile = getFiles.get_download_url(GDriveObject)
                    if dFile == None:
                        dFile = getFiles.get_export_link(GDriveObject)
                    filename = ""
                    content = ""
                    try:
                        content, filename = getFiles.download_file(service, dFile)
                    except Exception as e:
                        logger.error("%s: ERROR: %s", GDriveObject["title"], e)
                    # if this failed filename will be blank and an error was logged in logs
                    if filename != "":
                        try:
                            getFiles.write_file(content, filename, datebackuppath)
                            succDnLds += 1
                            logger.debug(
                                "Downloaded and saved %s of %s. Retrieved: %s", succDnLds, len(currentFileIDs), filename
                            )
                        except Exception as e:
                            logger.error(e)
        rsync_rm()
        downloadsMessage = ("%s of %s files have downloaded and saved") % (
            succDnLds,
            (len(addedFileIDs) + len(modfiedFileIDs)),
        )
        message = generate_added_removed_modifed_message(
            removedFileIDs, addedFileIDs, archivedGDriveState, currentGDriveState, modfiedFileIDs
        )
        message += downloadsMessage
        try:
            send_email(message, configData, False)
            logger.info(message)
            logger.info(downloadsMessage)
        except Exception as e:
            message = "Failed to send Auditor report email.  Error: %s" % e
            logger.error(message)
        return True