"""YuanRong datasystem CLI command to generate C++ code template."""
import os
import shutil
from pathlib import Path
import yr.datasystem.cli.common.util as util
from yr.datasystem.cli.command import BaseCommand
class Command(BaseCommand):
"""
Generate C++ code template for yuanrong datasystem.
"""
name = 'generate_cpp_template'
description = 'generate C++ code template for yuanrong datasystem'
@staticmethod
def add_arguments(parser):
"""
Add arguments to parser.
Args:
parser (ArgumentParser): Specify parser to which arguments are added.
"""
parser.add_argument(
'-o', '--output_path',
type=str,
metavar='OUTPUT_PATH',
default=os.getcwd(),
help='path to save the generated C++ code template, default path is the current directory. \
Example: dscli generate_cpp_template --output_path /home/user/cppTemplates'
)
def run(self, args):
"""
Execute for generate_cpp_template command.
Args:
args (Namespace): Parsed command-line arguments.
"""
output_path = Path(args.output_path) / 'cpp_template'
output_path = output_path.resolve()
util.valid_safe_path(output_path)
output_path.mkdir(parents=True, exist_ok=True)
cpp_template_path = os.path.join(self._base_dir, 'cpp_template')
try:
shutil.copytree(cpp_template_path, output_path, dirs_exist_ok=True)
except shutil.Error as e:
self.logger.error(f"Error copying files: {e}")
return self.FAILURE
except OSError as e:
self.logger.error(f"OS error occurred: {e}")
return self.FAILURE
src_lib_path = os.path.join(self._base_dir, 'lib')
dest_lib_path = os.path.join(output_path, 'lib')
try:
shutil.copytree(src_lib_path, dest_lib_path, dirs_exist_ok=True)
except shutil.Error as e:
self.logger.error(f"Error copying files: {e}")
return self.FAILURE
except OSError as e:
self.logger.error(f"OS error occurred: {e}")
return self.FAILURE
src_include_path = os.path.join(self._base_dir, 'include')
dest_include_path = os.path.join(output_path, 'include')
try:
shutil.copytree(src_include_path, dest_include_path, dirs_exist_ok=True)
except shutil.Error as e:
self.logger.error(f"Error copying files: {e}")
return self.FAILURE
except OSError as e:
self.logger.error(f"OS error occurred: {e}")
return self.FAILURE
self.logger.info(f"\nC++ code template generated successfully at: {output_path}")
self.logger.info("You can now use this template for your C++ development.")
return self.SUCCESS