bcb-0014
1.000
Proposed solution
```python
import configparser
import os
import shutil
def task_func(config_file_path, archive_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
"""
# Read the configuration file
config = configparser.ConfigParser()
config.read(config_file_path)
# Get the project directory from the configuration file
try:
project_dir = config.get('Project', 'directory')
except (KeyError, configparser.NoSectionError, configparser.NoOptionError) as e:
raise Exception("Invalid configuration file format") from e
# Check if the project directory exists
if not os.path.exists(project_dir):
raise FileNotFoundError(f"The specified project directory does not exist: {project_dir}")
# Create the archive directory if it does not exist
os.makedirs(archive_dir, exist_ok=True)
# Define the name of the ZIP file
zip_filename = os.path.basename(project_dir) + '.zip'
zip_filepath = os.path.join(archive_dir, zip_filename)
# Create the ZIP archive
try:
shutil.make_archive(zip_filepath.replace('.zip', ''), 'zip', project_dir)
except Exception as e:
raise Exception("Failed to create ZIP archive") from e
return True
```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.