File size: 5,599 Bytes
0544243
1
2
{"task_id":"BigCodeBench\/16","complete_prompt":"import os\nimport glob\nimport subprocess\n\ndef task_func(directory, backup_dir='\/path\/to\/backup'):\n    \"\"\"\n    Backup all '.log' files in a specified directory to a tar.gz file and delete the original files after backup.\n    The backup file is named 'logs_backup.tar.gz' and placed in the specified backup directory.\n    \n    Parameters:\n    - directory (str): The directory that contains the log files to be backed up.\n    - backup_dir (str, optional): The directory where the backup file will be saved.\n                                  Default is '\/path\/to\/backup'.\n    \n    Returns:\n    - str: The path to the backup file if logs are found, otherwise returns a message 'No logs found to backup'.\n    \n    Raises:\n    - FileNotFoundError: If the specified directory does not exist.\n    \n    Requirements:\n    - subprocess\n    - glob\n    - os\n    \n    Example:\n    >>> task_func('\/path\/to\/logs')\n    '\/path\/to\/backup\/logs_backup.tar.gz'\n    >>> task_func('\/path\/to\/logs', '\/alternative\/backup\/dir')\n    '\/alternative\/backup\/dir\/logs_backup.tar.gz'\n    \"\"\"\n","instruct_prompt":"Backup all '.log' files in a specified directory to a tar.gz file and delete the original files after backup. The backup file is named 'logs_backup.tar.gz' and placed in the specified backup directory.\nThe function should raise the exception for: FileNotFoundError: If the specified directory does not exist.\nThe function should output with:\n    str: The path to the backup file if logs are found, otherwise returns a message 'No logs found to backup'.\nYou should write self-contained code starting with:\n```\nimport os\nimport glob\nimport subprocess\ndef task_func(directory, backup_dir='\/path\/to\/backup'):\n```","canonical_solution":"    if not os.path.exists(directory):\n        raise FileNotFoundError(f\"Directory '{directory}' not found.\")\n\n    log_files = glob.glob(os.path.join(directory, '*.log'))\n    if not log_files:\n        return \"No logs found to backup\"\n\n    if not os.path.exists(backup_dir):\n        os.makedirs(backup_dir)\n\n    backup_file = os.path.join(backup_dir, 'logs_backup.tar.gz')\n    subprocess.call(['tar', '-czvf', backup_file] + log_files)\n\n    for file in log_files:\n        os.remove(file)\n\n    return backup_file","code_prompt":"import os\nimport glob\nimport subprocess\ndef task_func(directory, backup_dir='\/path\/to\/backup'):\n","test":"import unittest\nimport tempfile\nimport os\nimport subprocess\nimport glob\nimport shutil\nclass TestCases(unittest.TestCase):\n    def setUp(self):\n        self.temp_dir = tempfile.mkdtemp()\n        self.temp_backup_dir = tempfile.mkdtemp()\n        \n        # Create some log files and some non-log files\n        for i in range(5):\n            with open(os.path.join(self.temp_dir, f\"file_{i}.log\"), \"w\") as f:\n                f.write(f\"Mock log content for file_{i}\")\n            with open(os.path.join(self.temp_dir, f\"file_{i}.txt\"), \"w\") as f:\n                f.write(f\"Mock content for file_{i}.txt\")\n    def tearDown(self):\n        shutil.rmtree(self.temp_dir)\n        shutil.rmtree(self.temp_backup_dir)\n    def test_backup_creation_and_log_file_deletion(self):\n        # Test the creation of the backup file and deletion of original log files.\n        backup_path = task_func(self.temp_dir, self.temp_backup_dir)\n        self.assertTrue(os.path.exists(backup_path))\n        self.assertEqual(backup_path, os.path.join(self.temp_backup_dir, 'logs_backup.tar.gz'))\n        self.assertFalse(any(file.endswith('.log') for file in os.listdir(self.temp_dir)))\n    def test_no_log_files_to_backup(self):\n        # Test behavior when no log files are present in the directory.\n        empty_dir = tempfile.mkdtemp()\n        result = task_func(empty_dir, self.temp_backup_dir)\n        self.assertEqual(result, \"No logs found to backup\")\n        shutil.rmtree(empty_dir)\n    def test_non_log_files_remain(self):\n        # Ensure that non-log files are not deleted or included in the backup.\n        backup_path = task_func(self.temp_dir, self.temp_backup_dir)\n        self.assertEqual(len(glob.glob(os.path.join(self.temp_dir, '*.txt'))), 5)  # Check only non-log files remain\n    def test_handle_non_existing_directory(self):\n        # Verify that a FileNotFoundError is raised for a non-existing source directory.\n        with self.assertRaises(FileNotFoundError):\n            task_func('\/non\/existing\/directory', self.temp_backup_dir)","entry_point":"task_func","doc_struct":"{\"description\": [\"Backup all '.log' files in a specified directory to a tar.gz file and delete the original files after backup.\", \"The backup file is named 'logs_backup.tar.gz' and placed in the specified backup directory.\"], \"notes\": [], \"params\": [\"directory (str): The directory that contains the log files to be backed up.\", \"backup_dir (str, optional): The directory where the backup file will be saved.\", \"Default is '\/path\/to\/backup'.\"], \"returns\": [\"str: The path to the backup file if logs are found, otherwise returns a message 'No logs found to backup'.\"], \"reqs\": [\"subprocess\", \"glob\", \"os\"], \"raises\": [\"FileNotFoundError: If the specified directory does not exist.\"], \"examples\": [\">>> task_func('\/path\/to\/logs')\", \"'\/path\/to\/backup\/logs_backup.tar.gz'\", \">>> task_func('\/path\/to\/logs', '\/alternative\/backup\/dir')\", \"'\/alternative\/backup\/dir\/logs_backup.tar.gz'\"]}","libs":"['glob', 'subprocess', 'os']"}