def testSortFilePatternDirTupleLst_2_items(self): if os.name == 'posix': configFilePathName = '/sdcard/transfiles.ini' else: configFilePathName = 'c:\\temp\\transfiles.ini' cm = ConfigManager(configFilePathName) fl = FileLister(cm) if os.name == 'posix': filePatternDirTupleLst = [('*.py', ''), ('test*.py', '/test')] self.assertEqual( [('test*.py', '/test'), ('*.py', '')], fl.sortFilePatternDirTupleLst(filePatternDirTupleLst)) filePatternDirTupleLst = [('test*.py', '/test'), ('*.py', '')] self.assertEqual( [('test*.py', '/test'), ('*.py', '')], fl.sortFilePatternDirTupleLst(filePatternDirTupleLst)) else: filePatternDirTupleLst = [('*.py', ''), ('test*.py', '\\test')] self.assertEqual( [('test*.py', '\\test'), ('*.py', '')], fl.sortFilePatternDirTupleLst(filePatternDirTupleLst)) filePatternDirTupleLst = [('test*.py', '\\test'), ('*.py', '')] self.assertEqual( [('test*.py', '\\test'), ('*.py', '')], fl.sortFilePatternDirTupleLst(filePatternDirTupleLst))
def testConvertWildcardExprStrToRegexpStr(self): if os.name == 'posix': configFilePathName = '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/transfiles.ini' else: configFilePathName = 'D:\\Development\\Python\\trans_file_cloud\\test\\transfiles.ini' cm = ConfigManager(configFilePathName) fl = FileLister(cm) wildchardLst = [ '*Solemne*.mp3', 'test*.py', '/excldir/subdir/*.py', 'd:\\excldir\\subdir\\*.py', '/excldir/subdir/*.*', 'd:\\excldir\\subdir\\*.*' ] expectedRegexpLst = [ '.*Solemne.*\.mp3\Z', 'test.*\.py\Z', '/excldir/subdir/.*\.py\Z', 'd:\\\\excldir\\\\subdir\\\\.*\.py\Z', '/excldir/subdir/.*\..*\Z', 'd:\\\\excldir\\\\subdir\\\\.*\..*\Z' ] for wildchardExpr, expectedRegexp in zip(wildchardLst, expectedRegexpLst): self.assertEqual( expectedRegexp, fl.convertWildcardExprStrToRegexpStr(wildchardExpr))
def testCreateRegexpPatternLstFromWildchardExprLst(self): if os.name == 'posix': configFilePathName = '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/transfiles.ini' else: configFilePathName = 'D:\\Development\\Python\\trans_file_cloud\\test\\transfiles.ini' cm = ConfigManager(configFilePathName) fl = FileLister(cm) wildchardLst = [ '*Solemne*.mp3', 'test*.py', '/excldir/subdir/*.py', 'd:\\excldir\\subdir\\*.py', '/excldir/subdir/*.*', 'd:\\excldir\\subdir\\*.*' ] expectedPatternLst = [ re.compile('.*Solemne.*\.mp3\Z'), re.compile('test.*\.py\Z'), re.compile('/excldir/subdir/.*\.py\Z'), re.compile('d:\\\\excldir\\\\subdir\\\\.*\.py\Z'), re.compile('/excldir/subdir/.*\..*\Z'), re.compile('d:\\\\excldir\\\\subdir\\\\.*\..*\Z') ] self.assertEqual( expectedPatternLst, fl.createRegexpPatternLstFromWildchardExprLst(wildchardLst))
def testSortFilePatternDirTupleLst_2_items_one_start_With_star(self): if os.name == 'posix': configFilePathName = '/sdcard/transfiles.ini' else: configFilePathName = 'c:\\temp\\transfiles.ini' cm = ConfigManager(configFilePathName) fl = FileLister(cm) if os.name == 'posix': filePatternDirTupleLst = [('*.mp3', ''), ('*Rimsky-Korsakov*.mp3', '/rim')] self.assertEqual( [('*Rimsky-Korsakov*.mp3', '/rim'), ('*.mp3', '')], fl.sortFilePatternDirTupleLst(filePatternDirTupleLst)) filePatternDirTupleLst = [('*Rimsky-Korsakov*.mp3', '/rim'), ('*.mp3', '')] self.assertEqual( [('*Rimsky-Korsakov*.mp3', '/rim'), ('*.mp3', '')], fl.sortFilePatternDirTupleLst(filePatternDirTupleLst)) else: filePatternDirTupleLst = [('*.mp3', ''), ('*Rimsky-Korsakov*.mp3', '\\rim')] self.assertEqual( [('*Rimsky-Korsakov*.mp3', '\\rim'), ('*.mp3', '')], fl.sortFilePatternDirTupleLst(filePatternDirTupleLst)) filePatternDirTupleLst = [('*Rimsky-Korsakov*.mp3', '\\rim'), ('*.mp3', '')] self.assertEqual( [('*Rimsky-Korsakov*.mp3', '\\rim'), ('*.mp3', '')], fl.sortFilePatternDirTupleLst(filePatternDirTupleLst))
def testGetFilesByOrderedTypes(self): if os.name == 'posix': configFilePathName = '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/transfiles.ini' else: configFilePathName = 'D:\\Development\\Python\\trans_file_cloud\\test\\transfiles.ini' cm = ConfigManager(configFilePathName) fl = FileLister(cm) cloudFileLst = [ 'aa_current.jpg', 'constants_2.py', 'current_state_21.jpg', 'current_state_22.jpg', 'doc_21.docx', 'doc_22.docx', 'filelister_2.py', 'filemover_2.py', 'README_2.md', 'testfilelister_2.py', 'testfilemover_2.py' ] orderedFileTypeWildchardExprLst, fileTypeDic = fl.getFilesByOrderedTypes( 'transFileCloudTestProject', cloudFileLst=cloudFileLst) self.assertEqual([ 'aa*.jpg', '*Solemne*.mp3', 'test*.py', '*.mp3', '*.jpg', '*.docx', '*.py', '*.md' ], orderedFileTypeWildchardExprLst) if os.name == 'posix': self.assertEqual( { '*.jpg': ('/images', ['current_state_21.jpg', 'current_state_22.jpg']), '*.docx': ('/doc', ['doc_21.docx', 'doc_22.docx']), '*.md': ('', ['README_2.md']), '*.mp3': ('/mp3', []), 'test*.py': ('/test', ['testfilelister_2.py', 'testfilemover_2.py']), '*.py': ('', ['constants_2.py', 'filelister_2.py', 'filemover_2.py']), '*Solemne*.mp3': ('/mp3/solemne', []), 'aa*.jpg': ('/images/aa', ['aa_current.jpg']) }, fileTypeDic) else: self.assertEqual( { '*.jpg': ('\\images', ['current_state_21.jpg', 'current_state_22.jpg']), '*.docx': ('\\doc', ['doc_21.docx', 'doc_22.docx']), '*.md': ('', ['README_2.md']), '*.mp3': ('\\mp3', []), 'test*.py': ('\\test', ['testfilelister_2.py', 'testfilemover_2.py']), '*.py': ('', ['constants_2.py', 'filelister_2.py', 'filemover_2.py']), '*Solemne*.mp3': ('\\mp3\\solemne', []), 'aa*.jpg': ('\\images\\aa', ['aa_current.jpg']) }, fileTypeDic)
def testSortDirTupleListUsingComputeMoveOrder_empty_root_dir(self): if os.name == 'posix': configFilePathName = '/sdcard/transfiles.ini' filePatternDirDic = { 'test*.py': '/test', '*.py': '', '*.rd': '', '*.docx': '/doc', '*.jpg': '/images', 'aacc*.mp3': '/aaccmp3', 'cc*.mp3': '/ccmp3', '*.mp3': '/mp3', 'aa*.mp3': '/aamp3', 'bb*.mp3': '/bbmp3', ' *Rimsky-Korsakov*.mp3': '/mp3/Rimsky Korsakov' } else: configFilePathName = 'c:\\temp\\transfiles.ini' filePatternDirDic = { 'test*.py': '\\test', '*.py': '', '*.rd': '', '*.docx': '\\doc', '*.jpg': '\\images', 'aacc*.mp3': '\\aaccmp3', 'cc*.mp3': '\\ccmp3', '*.mp3': '\\mp3', 'aa*.mp3': '\\aamp3', 'bb*.mp3': '\\bbmp3', ' *Rimsky-Korsakov*.mp3': '\\mp3\\Rimsky Korsakov' } filePatternDirTupleLst = [item for item in filePatternDirDic.items()] cm = ConfigManager(configFilePathName) fl = FileLister(cm) if os.name == 'posix': self.assertEqual( [(' *Rimsky-Korsakov*.mp3', '/mp3/Rimsky Korsakov'), ('test*.py', '/test'), ('cc*.mp3', '/ccmp3'), ('bb*.mp3', '/bbmp3'), ('aacc*.mp3', '/aaccmp3'), ('aa*.mp3', '/aamp3'), ('*.mp3', '/mp3'), ('*.jpg', '/images'), ('*.docx', '/doc'), ('*.rd', ''), ('*.py', '')], sorted(filePatternDirTupleLst, key=functools.cmp_to_key(fl.computeMoveOrder))) else: self.assertEqual( [(' *Rimsky-Korsakov*.mp3', '\\mp3\\Rimsky Korsakov'), ('test*.py', '\\test'), ('cc*.mp3', '\\ccmp3'), ('bb*.mp3', '\\bbmp3'), ('aacc*.mp3', '\\aaccmp3'), ('aa*.mp3', '\\aamp3'), ('*.mp3', '\\mp3'), ('*.jpg', '\\images'), ('*.docx', '\\doc'), ('*.rd', ''), ('*.py', '')], sorted(filePatternDirTupleLst, key=functools.cmp_to_key(fl.computeMoveOrder)))
def __init__(self, configManager, projectName): """ FileMover constructor. @param configManager: ConfigManager giving access to the local configuration file data @param projectName: project name as defined in the local configuration file """ self.projectName = projectName self.downloadDir = configManager.downloadPath self.projectDir = configManager.getProjectLocalDir(projectName) self.fileNameLister = FileLister(configManager)
def testGetModifiedAndNotExcludedFileLst(self): if os.name == 'posix': configFilePathName = '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/transfiles.ini' projectDir = '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_3/projectdir' else: configFilePathName = 'D:\\Development\\Python\\trans_file_cloud\\test\\transfiles.ini' projectDir = 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_3\\projectdir' cm = ConfigManager(configFilePathName) fl = FileLister(cm) expectedAllFileNameLst = [ 'constants_2.py', 'filelister_2.py', 'filemover_2.py', 'README_2.md', 'testfilelister_2.py', 'testfilemover_2.py' ] if os.name == 'posix': expectedAllFilePathNameLst = [ '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_3/projectdir/constants_2.py', '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_3/projectdir/filelister_2.py', '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_3/projectdir/filemover_2.py', '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_3/projectdir/README_2.md', '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_3/projectdir/test/testfilelister_2.py', '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_3/projectdir/test/testfilemover_2.py' ] else: expectedAllFilePathNameLst = [ 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_3\\projectdir\\constants_2.py', 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_3\\projectdir\\filelister_2.py', 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_3\\projectdir\\filemover_2.py', 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_3\\projectdir\\README_2.md', 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_3\\projectdir\\test\\testfilelister_2.py', 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_3\\projectdir\\test\\testfilemover_2.py' ] excludedDirLst = [] excludedFileTypeWildchardLst = ['*.ini', '*.tmp', '*.jpg', '*.docx'] excludedFileTypePatternLst = fl.createRegexpPatternLstFromWildchardExprLst( excludedFileTypeWildchardLst) lastSyncTime = datetime.datetime.strptime( '15/06/2020 08:45:23', DATE_TIME_FORMAT_CONFIG_FILE) actualAllFileNameLst, actualAllFilePathNameLst = fl.getModifiedAndNotExcludedFileLst( projectDir, lastSyncTime, excludedDirLst, excludedFileTypePatternLst) self.assertEqual(sorted(expectedAllFileNameLst), sorted(actualAllFileNameLst)) self.assertEqual(sorted(expectedAllFilePathNameLst), sorted(actualAllFilePathNameLst))
def testSortFilePatternDirTupleLst_n_items(self): if os.name == 'posix': configFilePathName = '/sdcard/transfiles.ini' else: configFilePathName = 'c:\\temp\\transfiles.ini' cm = ConfigManager(configFilePathName) fl = FileLister(cm) if os.name == 'posix': filePatternDirTupleLst = [('*.py', ''), ('test*.py', '/test'), ('*.jpg', '/images'), ('sub*.jpg', '/images/sub'), ('*.docx', '/doc')] self.assertEqual( [('sub*.jpg', '/images/sub'), ('test*.py', '/test'), ('*.jpg', '/images'), ('*.docx', '/doc'), ('*.py', '')], fl.sortFilePatternDirTupleLst(filePatternDirTupleLst)) filePatternDirTupleLst = [('sub*.jpg', '/images/sub'), ('*.jpg', '/images'), ('*.docx', '/doc'), ('aa*.docx', '/doc/aa_sub_dir'), ('test*.py', '/test'), ('*.py', '')] self.assertEqual( [('sub*.jpg', '/images/sub'), ('aa*.docx', '/doc/aa_sub_dir'), ('test*.py', '/test'), ('*.jpg', '/images'), ('*.docx', '/doc'), ('*.py', '')], fl.sortFilePatternDirTupleLst(filePatternDirTupleLst)) else: filePatternDirTupleLst = [('*.py', ''), ('test*.py', '\\test'), ('*.jpg', '\\images'), ('sub*.jpg', '\\images\\sub'), ('*.docx', '\\doc')] self.assertEqual( [('sub*.jpg', '\\images\\sub'), ('test*.py', '\\test'), ('*.jpg', '\\images'), ('*.docx', '\\doc'), ('*.py', '')], fl.sortFilePatternDirTupleLst(filePatternDirTupleLst)) filePatternDirTupleLst = [('sub*.jpg', '\\images\\sub'), ('*.jpg', '\\images'), ('*.docx', '\\doc'), ('aa*.docx', '\\doc\\aa_sub_dir'), ('test*.py', '\\test'), ('*.py', '')] self.assertEqual( [('sub*.jpg', '\\images\\sub'), ('aa*.docx', '\\doc\\aa_sub_dir'), ('test*.py', '\\test'), ('*.jpg', '\\images'), ('*.docx', '\\doc'), ('*.py', '')], fl.sortFilePatternDirTupleLst(filePatternDirTupleLst))
def testGetModifiedFileLst_invalid_local_dir(self): ''' Tests that the getModifiedFileLst() method raises a NotADirectoryError if the project path specified in the transfiles.ini does not exist. ''' if os.name == 'posix': configFilePathName = '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/transfiles.ini' else: configFilePathName = 'D:\\Development\\Python\\trans_file_cloud\\test\\transfiles.ini' cm = ConfigManager(configFilePathName) fl = FileLister(cm) # project name which has an invalid (not existing) project path in the # transfiles.ini file invalidProjectName = 'transFileCloudInvalidProject' self.assertRaises(NotADirectoryError, fl.getModifiedFileLst, invalidProjectName)
def initTransferFileOnProject(self, configFilePath=None, projectName=None): """ This method enables the transfer files utility to be executed in a loop. It initializes the TransferFile class with the data specific to the project selected by the user. @param configFilePath: used for unit testing only @param projectName used for unit testing only @return: True if the user selects another project, False otherwise """ if projectName == None: # we are not unit testing ... projectName = self.requester.getProjectName(commandLineArgs=None) if projectName == None: # user did choose Quit self.projectName = None return False self.projectName = projectName self.localProjectDir = None try: self.localProjectDir = self.configManager.getProjectLocalDir( self.projectName) except KeyError as e: # this happens only when TransferFiles is launched from the command line # with an invalid project name passed as -p command line parm print( '\nProject {} not defined in configuration file {}. Program closed.\n' .format(str(e), configFilePath)) self.projectName = None return False # currently, only Dropbox as cloud space is implemented self.cloudAccess = DropboxAccess(self.configManager, self.projectName) self.fileLister = FileLister(self.configManager) return True
def testExcludeFile(self): if os.name == 'posix': configFilePathName = '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/transfiles.ini' else: configFilePathName = 'D:\\Development\\Python\\trans_file_cloud\\test\\transfiles.ini' cm = ConfigManager(configFilePathName) fl = FileLister(cm) excludedFileSpecLst = [ '*.ini', '*.temp', 'help*.*', 'modified*', '*.pyc' ] excludedPatternLst = fl.createRegexpPatternLstFromWildchardExprLst( excludedFileSpecLst) self.assertTrue(fl.excludeFile('transfiles.ini', excludedPatternLst)) self.assertFalse(fl.excludeFile('transfiles.py', excludedPatternLst)) self.assertTrue(fl.excludeFile('transfiles.temp', excludedPatternLst)) self.assertFalse(fl.excludeFile('transfiles.tmp', excludedPatternLst)) self.assertTrue(fl.excludeFile('helpMe.txt', excludedPatternLst)) self.assertTrue(fl.excludeFile('modified_no_type', excludedPatternLst)) self.assertTrue(fl.excludeFile('transfiles.pyc', excludedPatternLst))
def testIsRootAsDirOrSubDirInExcludedDirLst(self): if os.name == 'posix': configFilePathName = '/sdcard/transfiles.ini' excludedDirLst = [ '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_1/fromdir', '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_1/fromdir_saved' ] subDir1 = '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_1/fromdir/fromSubDir' subDir2 = '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_1/fromdir_saved/fromSubDir' subDir3 = '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_2/fromdir_saved/fromSubDir' dir1 = '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_1/fromdir' dir2 = '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_2' else: configFilePathName = 'c:\\temp\\transfiles.ini' excludedDirLst = [ 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_1\\fromdir', 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_1\\fromdir_saved' ] subDir1 = 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_1\\fromdir\\fromSubDir' subDir2 = 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_1\\fromdir_saved\\fromSubDir' subDir3 = 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_2\\fromdir_saved\\fromSubDir' dir1 = 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_1\\fromdir' dir2 = 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_2' cm = ConfigManager(configFilePathName) fl = FileLister(cm) self.assertTrue( fl.isRootAsDirOrSubDirInExcludedDirLst(subDir1, excludedDirLst)) self.assertTrue( fl.isRootAsDirOrSubDirInExcludedDirLst(subDir2, excludedDirLst)) self.assertTrue( fl.isRootAsDirOrSubDirInExcludedDirLst(dir1, excludedDirLst)) self.assertFalse( fl.isRootAsDirOrSubDirInExcludedDirLst(subDir3, excludedDirLst)) self.assertFalse( fl.isRootAsDirOrSubDirInExcludedDirLst(dir2, excludedDirLst))
def testSortFilePatternDirTupleLst_n_items_one_start_With_star(self): if os.name == 'posix': configFilePathName = '/sdcard/transfiles.ini' else: configFilePathName = 'c:\\temp\\transfiles.ini' cm = ConfigManager(configFilePathName) fl = FileLister(cm) if os.name == 'posix': filePatternDirTupleLst = [('test*.py', '/test'), ('*.py', ''), ('*.md', ''), ('*.docx', '/doc'), ('*.jpg', '/images'), ('sub*.jpg', '/images/sub'), ('*.mp3', '/mp3'), ('*Rimsky-Korsakov*.mp3', '/mp3/Rimsky-Korsakov')] self.assertEqual( [('sub*.jpg', '/images/sub'), ('*Rimsky-Korsakov*.mp3', '/mp3/Rimsky-Korsakov'), ('test*.py', '/test'), ('*.mp3', '/mp3'), ('*.jpg', '/images'), ('*.docx', '/doc'), ('*.py', ''), ('*.md', '')], fl.sortFilePatternDirTupleLst(filePatternDirTupleLst)) else: filePatternDirTupleLst = [('test*.py', '\\test'), ('*.py', ''), ('*.md', ''), ('*.docx', '\\doc'), ('*.jpg', '\\images'), ('sub*.jpg', '\\images\\sub'), ('*.mp3', '\\mp3'), ('*Rimsky-Korsakov*.mp3', '\\mp3\\Rimsky-Korsakov')] self.assertEqual( [('sub*.jpg', '\\images\\sub'), ('*Rimsky-Korsakov*.mp3', '\\mp3\\Rimsky-Korsakov'), ('test*.py', '\\test'), ('*.mp3', '\\mp3'), ('*.jpg', '\\images'), ('*.docx', '\\doc'), ('*.py', ''), ('*.md', '')], fl.sortFilePatternDirTupleLst(filePatternDirTupleLst))
def testGetModifiedFileLst(self): if os.name == 'posix': configFilePathName = '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/transfiles.ini' else: configFilePathName = 'D:\\Development\\Python\\trans_file_cloud\\test\\transfiles.ini' cm = ConfigManager(configFilePathName) fl = FileLister(cm) allFileNameLst, allFilePathNameLst, lastSyncTimeStr = fl.getModifiedFileLst( 'transFileCloudTestProject') self.assertEqual( sorted([ 'constants_2.py', 'filelister_2.py', 'filemover_2.py', 'testfilelister_2.py', 'testfilemover_2.py', 'README_2.md' ]), sorted(allFileNameLst)) if os.name == 'posix': self.assertEqual( sorted([ '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_3/projectdir/constants_2.py', '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_3/projectdir/filelister_2.py', '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_3/projectdir/filemover_2.py', '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_3/projectdir/test/testfilelister_2.py', '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_3/projectdir/test/testfilemover_2.py', '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_3/projectdir/README_2.md' ]), sorted(allFilePathNameLst)) else: self.assertEqual( sorted([ 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_3\\projectdir\\constants_2.py', 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_3\\projectdir\\filelister_2.py', 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_3\\projectdir\\filemover_2.py', 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_3\\projectdir\\test\\testfilelister_2.py', 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_3\\projectdir\\test\\testfilemover_2.py', 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_3\\projectdir\\README_2.md' ]), sorted(allFilePathNameLst)) self.assertEqual('15/06/2020 08:45:23', lastSyncTimeStr)
def testMoveFilesToLocalDirs(self): if os.name == 'posix': downloadDir = '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_1/fromdir' downloadDirSaved = '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_1/fromdir_saved' projectDir = '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_1/projectdir' projectDirEmpty = '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_1/projectdir_empty' TEST_SUB_DIR = '/test' IMG_SUB_DIR = '/images' DOC_SUB_DIR = '/doc' else: # Windows downloadDir = 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_1\\fromdir' downloadDirSaved = 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_1\\fromdir_saved' projectDir = 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_1\\projectdir' projectDirEmpty = 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_1\\projectdir_empty' TEST_SUB_DIR = '/test' IMG_SUB_DIR = '/images' DOC_SUB_DIR = '/doc' configManager = ConfigManager(CONFIG_FILE_PATH_NAME) # deleting downloadDir (dir and content) if os.path.exists(downloadDir): shutil.rmtree(downloadDir) # restoring downloadDir from its saved version shutil.copytree(downloadDirSaved, downloadDir) # deleting projectDir if os.path.exists(projectDir): shutil.rmtree(projectDir) # restoring a directory structure only project dir (no files) from its saved empty version shutil.copytree(projectDirEmpty, projectDir) # ensuring downloadDir contains the required files fl = FileLister(configManager) projectName = 'transFileCloudTestProject' cloudFileLst = [ 'constants_1.py', 'current_state_11.jpg', 'current_state_12.jpg', 'doc_11.docx', 'doc_12.docx', 'filelister_1.py', 'filemover_1.py', 'README_1.md', 'testfilelister_1.py', 'testfilemover_1.py' ] _, fileTypeDic = fl.getFilesByOrderedTypes(projectName, cloudFileLst=cloudFileLst) self.assertEqual( sorted(['filelister_1.py', 'filemover_1.py', 'constants_1.py']), fileTypeDic['*.py'][1]) self.assertEqual(sorted(['testfilelister_1.py', 'testfilemover_1.py']), fileTypeDic['test*.py'][1]) self.assertEqual( sorted(['current_state_12.jpg', 'current_state_11.jpg']), fileTypeDic['*.jpg'][1]) self.assertEqual(sorted(['doc_12.docx', 'doc_11.docx']), fileTypeDic['*.docx'][1]) self.assertEqual(sorted(['README_1.md']), fileTypeDic['*.md'][1]) fm = FileMover(configManager, projectName) fm.projectDir = projectDir # capturing stdout into StringIO to avoid outputing in terminal # window while unit testing stdout = sys.stdout outputCapturingString = StringIO() sys.stdout = outputCapturingString fm.moveFilesToLocalDirs(cloudFileLst) sys.stdout = stdout if os.name == 'posix': self.assertEqual([ 'moving test/testproject_1/fromdir/testfilelister_1.py to ' 'testproject_1/projectdir/test/testfilelister_1.py', 'moving test/testproject_1/fromdir/testfilemover_1.py to ' 'testproject_1/projectdir/test/testfilemover_1.py', 'moving test/testproject_1/fromdir/current_state_11.jpg to ' 'testproject_1/projectdir/images/current_state_11.jpg', 'moving test/testproject_1/fromdir/current_state_12.jpg to ' 'testproject_1/projectdir/images/current_state_12.jpg', 'moving test/testproject_1/fromdir/doc_11.docx to ' 'testproject_1/projectdir/doc/doc_11.docx', 'moving test/testproject_1/fromdir/doc_12.docx to ' 'testproject_1/projectdir/doc/doc_12.docx', 'moving test/testproject_1/fromdir/constants_1.py to ' 'test/testproject_1/projectdir/constants_1.py', 'moving test/testproject_1/fromdir/filelister_1.py to ' 'test/testproject_1/projectdir/filelister_1.py', 'moving test/testproject_1/fromdir/filemover_1.py to ' 'test/testproject_1/projectdir/filemover_1.py', 'moving test/testproject_1/fromdir/README_1.md to ' 'test/testproject_1/projectdir/README_1.md', '' ], outputCapturingString.getvalue().split('\n')) else: self.assertEqual([ 'moving test\\testproject_1\\fromdir\\testfilelister_1.py to ' 'testproject_1\\projectdir\\test\\testfilelister_1.py', 'moving test\\testproject_1\\fromdir\\testfilemover_1.py to ' 'testproject_1\\projectdir\\test\\testfilemover_1.py', 'moving test\\testproject_1\\fromdir\\current_state_11.jpg to ' 'testproject_1\\projectdir\\images\\current_state_11.jpg', 'moving test\\testproject_1\\fromdir\\current_state_12.jpg to ' 'testproject_1\\projectdir\\images\\current_state_12.jpg', 'moving test\\testproject_1\\fromdir\\doc_11.docx to ' 'testproject_1\\projectdir\\doc\\doc_11.docx', 'moving test\\testproject_1\\fromdir\\doc_12.docx to ' 'testproject_1\\projectdir\\doc\\doc_12.docx', 'moving test\\testproject_1\\fromdir\\constants_1.py to ' 'test\\testproject_1\\projectdir\\constants_1.py', 'moving test\\testproject_1\\fromdir\\filelister_1.py to ' 'test\\testproject_1\\projectdir\\filelister_1.py', 'moving test\\testproject_1\\fromdir\\filemover_1.py to ' 'test\\testproject_1\\projectdir\\filemover_1.py', 'moving test\\testproject_1\\fromdir\\README_1.md to ' 'test\\testproject_1\\projectdir\\README_1.md', '' ], outputCapturingString.getvalue().split('\n')) # verifying project dir fileNameLst = [ x.split(sep)[-1] for x in glob.glob(projectDir + sep + '*.*') ] self.assertEqual( sorted([ 'filelister_1.py', 'filemover_1.py', 'constants_1.py', 'README_1.md' ]), sorted(fileNameLst)) # verifying project test sub dir fileNameLst = [ x.split(sep)[-1] for x in glob.glob(projectDir + TEST_SUB_DIR + sep + '*.*') ] self.assertEqual(sorted(['testfilelister_1.py', 'testfilemover_1.py']), sorted(fileNameLst)) # verifying project images sub dir fileNameLst = [ x.split(sep)[-1] for x in glob.glob(projectDir + IMG_SUB_DIR + sep + '*.*') ] self.assertEqual( sorted(['current_state_12.jpg', 'current_state_11.jpg']), sorted(fileNameLst)) # verifying project doc sub dir fileNameLst = [ x.split(sep)[-1] for x in glob.glob(projectDir + DOC_SUB_DIR + sep + '*.*') ] self.assertEqual(sorted(['doc_12.docx', 'doc_11.docx']), sorted(fileNameLst)) # testing that download no longer contains the files defined in cloudFileLst fileNameLst = [ x.split(sep)[-1] for x in glob.glob(downloadDir + sep + '*.*') ] self.assertEqual( sorted([ 'constants_1.mp3', 'Nikolay Rimsky-Korsakov - Отче наш Notre Père Our Father - Cep.mp3' ]), sorted(fileNameLst))
def testTransferFilesFromCloudToLocalDirs_filePath(self): # avoid warning resourcewarning unclosed ssl.sslsocket due to Dropbox warnings.filterwarnings(action="ignore", message="unclosed", category=ResourceWarning) if os.name == 'posix': localProjectDir = '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_2/projectdir' localProjectDirSaved = '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/testproject_2/projectdir_saved' configFilePathName = '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/trans_file_cloud/test/test_TransferFiles.ini' else: localProjectDir = 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_2\\projectdir' localProjectDirSaved = 'D:\\Development\\Python\\trans_file_cloud\\test\\testproject_2\\projectdir_saved' configFilePathName = 'D:\\Development\\Python\\trans_file_cloud\\test\\test_TransferFiles.ini' cm = ConfigManager(configFilePathName) projectName = 'TransferPathFilesTestProject' # storing the last synch update time to compare it to the new update # time once download and move has been performed storedLastSynchTimeStr = cm.getLastSynchTime(projectName) storedLastSyncTime = datetime.datetime.strptime( storedLastSynchTimeStr, DATE_TIME_FORMAT_CONFIG_FILE) # cleaning up the cloud folder before uploading the test files drpa = DropboxAccess(cm, projectName) cloudFileLst = drpa.getCloudFilePathNameList() for file in cloudFileLst: drpa.deleteFile(file) self.assertEqual([], drpa.getCloudFileNameList()) # listing the test files which wiLl be uploaded to # the cloud in order to be available for download # and move to local dirs tstFileToUploadLst = ['testfilemover_2.py'] pythonFileToUploadLst = ['filemover_2.py', 'filelister_2.py'] docFileToUploadLst = ['doc_21.docx', 'doc_22.docx'] imgFileToUploadLst = ['current_state_21.jpg'] fileNameToUploadLst = tstFileToUploadLst + pythonFileToUploadLst + docFileToUploadLst + imgFileToUploadLst tstFilePathNameToUploadLst = [ localProjectDir + sep + 'test' + sep + x for x in tstFileToUploadLst ] pythonFilePathNameToUploadLst = [ localProjectDir + sep + x for x in pythonFileToUploadLst ] docFilePathNameToUploadLst = [ localProjectDir + sep + 'doc' + sep + x for x in docFileToUploadLst ] imgFilePathNameToUploadLst = [ localProjectDir + sep + 'images' + sep + x for x in imgFileToUploadLst ] filePathNameToUploadLst = tstFilePathNameToUploadLst + pythonFilePathNameToUploadLst + docFilePathNameToUploadLst + imgFilePathNameToUploadLst # uploading the test files which will then be downloaded and moved to # local dirs drpa = DropboxAccess(cm, projectName) for filePathName in filePathNameToUploadLst: drpa.uploadFilePathName(filePathName) # simulating user input stdin = sys.stdin # selecting project 2 (the test project 'TransferPathFilesTestProject' is # the second project defined in test_TransferFiles.ini !) sys.stdin = StringIO('2') # TransferPathFilesTestProject print('\nstdout temporarily captured. Test is running ...') stdout = sys.stdout outputCapturingString = StringIO() sys.stdout = outputCapturingString # now asking TransferFiles to download the cloud files and move them # to the local dirs tf = TransferFiles(configFilePath=configFilePathName) # confirming cloud files download sys.stdin = StringIO('Y') tf.transferFilesFromCloudToLocalDirs(drpa.getCloudFilePathNameList()) sys.stdin = stdin sys.stdout = stdout # testing that the last synch time is after the stored synch time cm_reloaded = ConfigManager(configFilePathName) newLastSynchTimeStr = cm_reloaded.getLastSynchTime(projectName) newLastSyncTime = datetime.datetime.strptime( newLastSynchTimeStr, DATE_TIME_FORMAT_CONFIG_FILE) self.assertTrue(newLastSyncTime > storedLastSyncTime) # now testing that the files downloaded from the cloud and moved to # the local dirs are the expected ones # first, reset the last synch time to the stored one so that FileLister # will list files whose modification date is oreater than this time cm.updateLastSynchTime(projectName, storedLastSynchTimeStr) fl = FileLister(cm) allFileNameLst, allFilePathNameLst, lastSyncTimeStr = fl.getModifiedFileLst( projectName) self.assertEqual(sorted(fileNameToUploadLst), sorted(allFileNameLst)) self.assertEqual(sorted(filePathNameToUploadLst), sorted(allFilePathNameLst)) # now restoring the modified files dir to its saved version shutil.rmtree(localProjectDir) dir_util.copy_tree(localProjectDirSaved, localProjectDir)
def __init__(self): self.fileLister = FileLister()