bcb-0016
1.000
Proposed solution
```python
import os
import glob
import subprocess
import tarfile
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'
"""
if not os.path.exists(directory):
raise FileNotFoundError(f"The specified directory {directory} does not exist.")
log_files = glob.glob(os.path.join(directory, '*.log'))
if not log_files:
return 'No logs found to backup'
backup_file = os.path.join(backup_dir, 'logs_backup.tar.gz')
with tarfile.open(backup_file, 'w:gz') as tar:
for log_file in log_files:
tar.add(log_file, arcname=os.path.basename(log_file))
os.remove(log_file)
return backup_file
```Test output (stdout)
.... [100%] 4 passed in 0.02s
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.