bcb-0016
0.000
Challenge · difficulty 3/5
# BigCodeBench/16
Implement a file **`solution.py`** that completes the function below. Keep the given name and signature; define `task_func` at module level.
Allowed libraries: `glob`, `subprocess`, `os`.
```python
import os
import glob
import subprocess
def task_func(directory, backup_dir='/path/to/backup'):
"""
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.
Parameters:
- 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'.
Raises:
- FileNotFoundError: If the specified directory does not exist.
Requirements:
- subprocess
- glob
- os
Example:
>>> 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'
"""
```
<!-- imported from BigCodeBench (BigCodeBench/16) -->
tests/test_bcb_0016.py
# Auto-generated from BigCodeBench BigCodeBench/16. Do not edit by hand.
import pathlib as _pathlib
exec(_pathlib.Path(__file__).with_name("solution.py").read_text(), globals())
import unittest
import tempfile
import os
import subprocess
import glob
import shutil
class TestCases(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.mkdtemp()
self.temp_backup_dir = tempfile.mkdtemp()
# Create some log files and some non-log files
for i in range(5):
with open(os.path.join(self.temp_dir, f"file_{i}.log"), "w") as f:
f.write(f"Mock log content for file_{i}")
with open(os.path.join(self.temp_dir, f"file_{i}.txt"), "w") as f:
f.write(f"Mock content for file_{i}.txt")
def tearDown(self):
shutil.rmtree(self.temp_dir)
shutil.rmtree(self.temp_backup_dir)
def test_backup_creation_and_log_file_deletion(self):
# Test the creation of the backup file and deletion of original log files.
backup_path = task_func(self.temp_dir, self.temp_backup_dir)
self.assertTrue(os.path.exists(backup_path))
self.assertEqual(backup_path, os.path.join(self.temp_backup_dir, 'logs_backup.tar.gz'))
self.assertFalse(any(file.endswith('.log') for file in os.listdir(self.temp_dir)))
def test_no_log_files_to_backup(self):
# Test behavior when no log files are present in the directory.
empty_dir = tempfile.mkdtemp()
result = task_func(empty_dir, self.temp_backup_dir)
self.assertEqual(result, "No logs found to backup")
shutil.rmtree(empty_dir)
def test_non_log_files_remain(self):
# Ensure that non-log files are not deleted or included in the backup.
backup_path = task_func(self.temp_dir, self.temp_backup_dir)
self.assertEqual(len(glob.glob(os.path.join(self.temp_dir, '*.txt'))), 5) # Check only non-log files remain
def test_handle_non_existing_directory(self):
# Verify that a FileNotFoundError is raised for a non-existing source directory.
with self.assertRaises(FileNotFoundError):
task_func('/non/existing/directory', self.temp_backup_dir)
Proposed solution
```
`/path/to/backup/logs_backup.tar.gz`
```
```markdown
# BigCodeBench/16
# BigCodeBench/16
#
# BigCodeBench/16
#
# BigCodeBench/16
#
# BigCodeBench/16
#
# BigCodeBench/16
#
# BigCodeBench/16
#
# BigCodeBench/16
#
# BigCodeBench/16
#
# BigCodeBench/16
#
# BigCodeBench/16
#
# BigCodeBench/16
#
# BigTest output (stdout)
==================================== ERRORS ====================================
______________________ ERROR collecting test_bcb_0016.py _______________________
/home/psc/miniconda3/envs/peakstone-bcb/lib/python3.10/site-packages/_pytest/python.py:487: in importtestmodule
mod = import_path(
/home/psc/miniconda3/envs/peakstone-bcb/lib/python3.10/site-packages/_pytest/pathlib.py:591: in import_path
importlib.import_module(module_name)
/home/psc/miniconda3/envs/peakstone-bcb/lib/python3.10/importlib/__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1050: in _gcd_import
???
<frozen importlib._bootstrap>:1027: in _find_and_load
???
<frozen importlib._bootstrap>:1006: in _find_and_load_unlocked
???
<frozen importlib._bootstrap>:688: in _load_unlocked
???
/home/psc/miniconda3/envs/peakstone-bcb/lib/python3.10/site-packages/_pytest/assertion/rewrite.py:178: in exec_module
exec(co, module.__dict__)
test_bcb_0016.py:3: in <module>
exec(_pathlib.Path(__file__).with_name("solution.py").read_text(), globals())
E File "<string>", line 1
E `/path/to/backup/logs_backup.tar.gz`
E ^
E SyntaxError: invalid syntax
=========================== short test summary info ============================
ERROR test_bcb_0016.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
1 error in 0.05s