import httplib2 from googleapiclient.discovery import build from googleapiclient.http import MediaFileUpload from googleapiclient.errors import HttpError from backupme.configuration_parser import get_list_files from authorization import GoogleAuth from backupme.settings_parser import SettingsParser # get logger logger = logging.getLogger('backupme') # get environment variables parser = SettingsParser() PROJECT_DIR = parser.get('project_dir') MAIN_LOG_PATH = parser.get('main_log') CONFIG_FILE = parser.get('config_file') class GoogleDrive(object): """ Main Google Drive class. """ def __init__(self, parent_id=None, path_to_config=CONFIG_FILE): # Get credentials from GoogleAuth.get_credentials() auth = GoogleAuth() self.credentials = auth.get_credentials()
logger = logging.getLogger('backupme') # get environment variables PROJECT_DIR = SettingsParser().get('project_dir') MAIN_LOG_PATH = SettingsParser().get('main_log') if __name__ == '__main__': logging.config.fileConfig( os.path.join(PROJECT_DIR, 'google_api_driver/logging.conf'), defaults={'logfilename': MAIN_LOG_PATH}) logger.info('google api engine started') # Run the program # Try to obtain the name of directory in GoogleDrive to upload the file: parser = SettingsParser() dir_id = parser.get('dir_id') if len(dir_id) == 0: # if there is no upload directory_id - try to create one # and save it to settings.dir_id dir_id = dir_utilities.create_remote_folder() parser.set('dir_id', dir_id) # settings_changer.change_field("dir_id", dir_id) # TODO: 1) Investigate why it doesn't upload files in directory # TODO: with dir_id for the 1st time # TODO: 2) Investigate why everything is OK when dir_id is not empty in # TODO: settings but it doesn't exist in Google Drive engine = google_api_driver.GoogleDrive(parent_id=dir_id) files_to_upload = engine.get_files_to_upload() # Run the function to upload file
import unittest import os import logging import logging.config from backupme.settings_parser import SettingsParser from backupme.configuration_parser import validator from backupme.configuration_parser import get_list_files # get environment variables parser = SettingsParser() PROJECT_DIR = parser.get("project_dir") MAIN_LOG_PATH = parser.get("main_log") class TestValidator(unittest.TestCase): def test_file_list_with_missing_files(self): """ file_list_with_missing_files should result equal for result file_list after validation and list with only existent files. """ file_list = [] nonexistent_file = os.path.join(PROJECT_DIR, "nonexistent_file") existent_file = os.path.join(PROJECT_DIR, "README.md") file_list.append(nonexistent_file) file_list.append(existent_file) self.assertEqual(validator(file_list), [existent_file]) def test_file_list_with_all_existent_files(self): """
import os import unittest import logging import logging.config from googleapiclient.discovery import Resource from backupme.settings_parser import SettingsParser from google_api_driver.google_api_driver import GoogleDrive # get environment variables parser = SettingsParser() PROJECT_DIR = parser.get('project_dir') MAIN_LOG_PATH = parser.get('main_log') class TestGoogleDrive(unittest.TestCase): current_dir = os.path.dirname(os.path.realpath(__file__)) config_file_path = os.path.join(current_dir, 'config') def setUp(self): """ Function to create configuration file before every test case. """ open(self.config_file_path, 'w+').close() with open(self.config_file_path, 'a') as f: f.write(os.path.join(PROJECT_DIR, 'README.md') + '\n') f.write(os.path.join(PROJECT_DIR, '.gitignore') + '\n') f.write(os.path.join(PROJECT_DIR, 'LICENSE') + '\n') self.drive = GoogleDrive(path_to_config=self.config_file_path) def tearDown(self):