bcb-0014
1.000
Proposed solution
```python
import configparser
import os
import shutil
def task_func(config_file_path, archieve_dir='/home/user/archive'):
"""
Archive a specified project directory into a ZIP file based on the configuration specified in a config file.
This function reads a configuration file to determine the project directory and archives this directory into a ZIP file.
The ZIP file's name will be the project directory's basename, stored in the specified archive directory.
Configuration File Format:
[Project]
directory=path_to_project_directory
Parameters:
- config_file_path (str): Path to the configuration file. The file must exist and be readable.
- archive_dir (str, optional): Path to the directory where the ZIP archive will be stored. Defaults to '/home/user/archive'.
Returns:
- bool: True if the ZIP archive is successfully created, otherwise an exception is raised.
Requirements:
- configparse
- os
- shutil
Raises:
- FileNotFoundError: If the `config_file_path` does not exist or the specified project directory does not exist.
- Exception: If the ZIP archive cannot be created.
Example:
>>> task_func("/path/to/config.ini")
True
"""
# Check if config file exists
if not os.path.exists(config_file_path):
raise FileNotFoundError(f"Config file not found: {config_file_path}")
# Read configuration file
config = configparser.ConfigParser()
config.read(config_file_path)
# Get project directory from config
project_dir = config.get('Project', 'directory')
# Check if project directory exists
if not os.path.exists(project_dir):
raise FileNotFoundError(f"Project directory not found: {project_dir}")
# Create archive directory if it doesn't exist
if not os.path.exists(archieve_dir):
os.makedirs(archieve_dir)
# Get basename of project directory
project_basename = os.path.basename(project_dir)
# Create ZIP file path
zip_file_path = os.path.join(archieve_dir, f"{project_basename}.zip")
try:
# Create ZIP archive
shutil.make_archive(
base_name=zip_file_path.replace('.zip', ''), # Remove .zip extension for make_archive
format='zip',
root_dir=os.path.dirname(project_dir),
base_dir=os.path.basename(project_dir)
)
return True
except Exception as e:
raise Exception(f"Failed to create ZIP archive: {str(e)}")
```Test output (stdout)
.... [100%] 4 passed in 0.03s
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.