def test_history_get(self):
     file_1 = os.path.join(self.test_bin_path, 'file_1')
     file_1_name = os.path.basename(file_1)
     bin_config_manager.history_add(file_1)
     self.assertEqual(
         bin_config_manager.history_get(file_1_name)['src_dir'],
         os.path.dirname(file_1))
     self.assertEqual(
         bin_config_manager.history_get(file_1_name)['bin_name'],
         os.path.basename(file_1))
Beispiel #2
0
 def test_restore_with_file(self):
     file_1 = os.path.join(self.test_folder_path, 'file_1')
     os.mknod(file_1)
     self.assertTrue(os.path.exists(file_1))
     delete_command.delete([file_1])
     self.assertFalse(os.path.exists(file_1))
     self.assertEqual(
         restore_command.restore(
             [os.path.join(
                 user_config_manager.get_property('bin_path'),
                 bin_config_manager.history_get('file_1')['bin_name']
             )]
         ),
         0
     )
     self.assertTrue(os.path.exists(file_1))
     self.assertFalse(
         os.path.exists(
             os.path.join(
                 user_config_manager.get_property('bin_path'),
                 os.path.basename(file_1)
             )
         )
     )
     self.assertEqual(
         len(bin_config_manager.get_property('history')),
         0,
         msg="While restore, fail occupies."
     )
Beispiel #3
0
    def test_empty_bin(self):
        file_1 = os.path.join(self.test_folder_path, 'file_1')
        os.mknod(file_1)
        bin_config_manager.history_add(file_1)
        clean_path.move(file_1, user_config_manager.get_property('bin_path'))
        self.assertTrue(
            os.path.exists(
                os.path.join(user_config_manager.get_property('bin_path'),
                             'file_1')))

        self.assertFalse(os.path.exists(file_1), msg="File should be in bin.")
        self.assertEqual(bin_config_manager.history_get('file_1')['src_dir'],
                         os.path.dirname(file_1),
                         msg="Initial preparations fails.")
        self.assertEqual(bin_command.empty_bin(),
                         0,
                         msg='Bin command should return success code: 0.')
        self.assertTrue(os.path.isdir(
            user_config_manager.get_property('bin_path')),
                        msg="Binempty should not delete existing bin folder.")
        self.assertFalse(
            os.path.exists(
                os.path.join(user_config_manager.get_property('bin_path'),
                             'file_1')))
        self.assertEqual(len(bin_config_manager.get_property('history')), 0)
 def test_history_get_with_date(self):
     file_1 = os.path.join(self.test_bin_path, 'file_1')
     file_1_name = os.path.basename(file_1)
     cur_datetime = datetime.strptime(
         datetime.strftime(datetime.now(), HISTORY_DATETIME_FORMAT),
         HISTORY_DATETIME_FORMAT)
     bin_config_manager.history_add(file_1)
     self.assertEqual(
         bin_config_manager.history_get(file_1_name)['date'], cur_datetime)
Beispiel #5
0
def _move_from_bin(paths, options=None):
    """Copy From Bin Function

    Do not use this function outside of module.

    Function allow copy bin files to destination.

    Parameters:
        paths: what to copy.
        options: list of copy politics.


    """
    # Go through every path in paths and move every element
    # from bin folder
    for path in paths:
        # Move obj to src location.
        clean_path.move(
            os.path.join(
                user_config_manager.get_property('bin_path'),
                bin_config_manager.history_get(path, options)['bin_name']
            ),
            os.path.join(
                bin_config_manager.history_get(path, options)['src_dir'],
                bin_config_manager.history_get(path, options)['src_name'],
            ),
            options
        )

        # Delete element from history list
        bin_config_manager.history_del(path, options)

        # Print Progress bar of operation
        get_log().info(
            INFO_MESSAGES['progress_res'].format(
                ascii_bar.get_progress_bar(
                    paths.index(path) + 1,
                    len(paths)
                ),
                path
            )
        )
Beispiel #6
0
 def test__copy_to_bin(self):
     file_1 = os.path.join(self.test_folder_path, 'file_1')
     os.mknod(file_1)
     delete_command._copy_to_bin([file_1])
     self.assertTrue(os.path.exists(file_1),
                     msg='Should not remove current file.')
     self.assertEqual(bin_config_manager.history_get('file_1')['src_dir'],
                      os.path.dirname(file_1),
                      msg='Should copy file to bin.')
     self.assertTrue(os.path.exists(
         os.path.join(user_config_manager.get_property('bin_path'),
                      'file_1')),
                     msg='File should be coped to bin.')
 def test_execution_manager_with_delete_folder(self):
     dir_1 = os.path.join(self.test_folder_path, 'dir_1')
     file_1 = os.path.join(dir_1, 'file_1')
     os.mkdir(dir_1)
     os.mknod(file_1)
     self.assertTrue(os.path.exists(file_1))
     self.options.update({'mods': ['remove']})
     self.paths = [dir_1]
     self.assertEqual(execution_manager.execute_command(
         self.paths, self.options),
                      0,
                      msg="Execute command should return success code: 0.")
     self.assertFalse(os.path.exists(dir_1))
     self.assertEqual(len(bin_config_manager.get_property('history')), 1)
     self.assertEqual(
         bin_config_manager.history_get(os.path.basename(dir_1))['src_dir'],
         os.path.dirname(dir_1))
     self.assertTrue(
         os.path.exists(
             os.path.join(
                 user_config_manager.get_property('bin_path'),
                 bin_config_manager.history_get(
                     os.path.basename(dir_1))['bin_name'])))
Beispiel #8
0
    def test_restore_with_different_src_bin_names(self):
        file_1 = os.path.join(self.test_folder_path, 'file_1')
        file_new_base_name = 'file_2'
        os.mknod(file_1)
        delete_command.delete([file_1])
        self.assertFalse(os.path.exists(file_1))
        self.assertTrue(os.path.exists(os.path.join(
            user_config_manager.get_property('bin_path'),
            os.path.basename(file_1)
        )))
        history_item = bin_config_manager.history_get(os.path.basename(file_1))
        history_item['src_name'] = 'file_2'
        history_item['date'] = '1997.12.17'
        bin_config_manager.set_property('history', [history_item])

        self.assertEqual(
            restore_command.restore(
                [os.path.join(
                    user_config_manager.get_property('bin_path'),
                    bin_config_manager.history_get('file_1')['bin_name']
                )]
            ),
            0
        )
        self.assertTrue(
            os.path.isfile(
                os.path.join(
                    os.path.dirname(file_1),
                    file_new_base_name
                )
            )
        )
        self.assertFalse(
            os.path.exists(
                file_1
            )
        )
Beispiel #9
0
def _copy_by_rename_way(path, options):
    """Copy By Renam Way
    
    Function allow to delete file/dir... to bin by rename way if name
    collision exists.
    
    Arguments:
        path: path to source.
        options: list of optional parameters.
    
    """
    src_name = os.path.basename(path)
    src_dir = os.path.dirname(path)
    bin_name = os.path.basename(path)
    if bin_config_manager.history_get(bin_name, options):

        count = 0
        while True:

            tmp_bin_name = HISTORY_COPY_NAME_FORMAT.format(bin_name, count)
            if not bin_config_manager.history_get(tmp_bin_name, options):
                break
            count += 1
        bin_name = tmp_bin_name

    clean_path.copy(
        path, os.path.join(user_config_manager.get_property('bin_path'), bin_name),
        options
    )
    bin_config_manager.history_add(
        {
            'src_name': src_name,
            'src_dir': src_dir,
            'bin_name': bin_name
        },
        options
    )
Beispiel #10
0
 def test_delete_with_file(self):
     file_1 = os.path.join(self.test_folder_path, 'file_1')
     os.mknod(file_1)
     delete_command.delete([file_1])
     self.assertEqual(delete_command.delete([file_1]),
                      0,
                      msg='Should return success code: 0.')
     self.assertFalse(os.path.exists(file_1))
     self.assertTrue(
         os.path.exists(
             os.path.join(user_config_manager.get_property('bin_path'),
                          'file_1')))
     self.assertEqual(
         bin_config_manager.history_get('file_1')['src_dir'],
         os.path.dirname(file_1))
    def test_execution_manager_with_move_bin(self):
        file_1 = os.path.join(self.test_folder_path, 'file_1')
        os.mknod(file_1)
        delete_command.delete([file_1])
        new_bin_location = os.path.join(self.test_folder_path, 'new_bin')

        self.options.update({'mods': ['move_bin'], 'path': new_bin_location})
        self.assertEqual(execution_manager.execute_command(
            self.paths, self.options),
                         0,
                         msg="Execute command should return success code: 0.")
        self.assertEqual(user_config_manager.get_property('bin_path'),
                         new_bin_location)
        self.assertTrue(
            os.path.exists(
                os.path.join(new_bin_location, os.path.basename(file_1))))
        self.assertEqual(
            bin_config_manager.history_get(
                os.path.basename(file_1))['src_dir'], os.path.dirname(file_1))