def test_reset(self, d): os.chdir(d.path) current_dir = FileUtil.get_current_dir() process_data = ProcessData() process_data.reset() process_data.project_name = Constants.PROJECT_NAME self.assertEqual(process_data.project_name, Constants.PROJECT_NAME) self.assertEqual(process_data.project_home_dir, current_dir) self.assertEqual(process_data.dependency_name, '') self.assertEqual(process_data.dependency_source_dir, '') self.assertEqual(process_data.dependency_build_dir, '') self.assertEqual(process_data.dependency_temp_dir, '') self.assertEqual(process_data.dependency_vendor_dir, '') self.assertEqual(process_data.target_name, '') self.assertEqual(process_data.target_source_dir, '') self.assertEqual(process_data.target_build_dir, '') self.assertEqual(process_data.target_temp_dir, '') self.assertEqual(process_data.target_vendor_dir, '') self.assertEqual(process_data.temp_dir, os.path.join(current_dir, Constants.TEMP_DIR)) self.assertEqual(process_data.build_dir, os.path.join(current_dir, Constants.BUILD_DIR)) self.assertEqual(process_data.vendor_dir, os.path.join(current_dir, Constants.VENDOR_DIR))
def test_merge_target_data(self, d): os.chdir(d.path) # create project project = Project() project.config['name'] = Constants.PROJECT_NAME # create dependency dep_repository = Repository( rep_type=Constants.REPOSITORY_TYPE_LOCAL, rep_path='/tmp/dep-repository-test', rep_version='1.0.0', ) dependency = Dependency(name='repository-test', repository=dep_repository) project.dependencies = [dependency] # create target target_repository = Repository( rep_type=Constants.REPOSITORY_TYPE_LOCAL, rep_path='/tmp/target-repository-test', rep_version='1.0.0', ) target1 = Target(name='test', repository=target_repository) target2 = Target(name='test', repository=target_repository) project.targets = [target1, target2] # process data process_data = ProcessData() process_data.reset() process_data.project_name = project.get_config_value('name') # process target data for target in project.targets: # get all target data from project dependencies print('Who is the target:') print(target.get_name()) print("") target_data = TargetData() for dependency in project.dependencies: print('Who is the dependency:') print(dependency.get_name()) print("") dependency.prepare_from_process_data(process_data) new_target_data = TargetData() new_target_data.c_flags.extend(['flag']) target_data.merge(new_target_data) self.assertEqual(len(target_data.c_flags), 1)
def install(self, dependency_name): from ezored.models.logger import Logger from ezored.models.project import Project project = Project.create_from_project_file() if dependency_name: Logger.i('Install dependency "{0}"'.format(dependency_name)) else: Logger.i('Install all dependencies') dependency_found = False total_deps = len(project.dependencies) if total_deps > 0: for dependency in project.dependencies: process_data = ProcessData() process_data.reset() process_data.project_name = project.get_config_value('name') can_build = False if not dependency_name: can_build = True elif dependency.get_name() == dependency_name: can_build = True if can_build: Logger.i('Installing dependency "{0}"...'.format( dependency.get_name())) dependency_found = True dependency.prepare_from_process_data(process_data) dependency.repository.download() dependency.repository.build(project=project, process_data=process_data) Logger.i('Dependency "{0}" installed'.format( dependency.get_name())) if not dependency_found: Logger.f('Dependency not found: {0}'.format(dependency_name)) else: Logger.i('Your project does not have dependencies')
def update(self): from ezored.models.logger import Logger from ezored.models.project import Project Logger.d('Updating all dependencies...') project = Project.create_from_project_file() total_deps = len(project.dependencies) if total_deps > 0: Logger.i('Updating {0} dependencies...'.format(total_deps)) process_data = ProcessData() process_data.reset() process_data.project_name = project.get_config_value('name') for dependency in project.dependencies: dependency.prepare_from_process_data(process_data) dependency.repository.download() dependency.repository.build(process_data) else: Logger.i('Your project does not have dependencies')
def build(self, target_name): from ezored.models.logger import Logger from ezored.models.project import Project project = Project.create_from_project_file() process_data = ProcessData() process_data.reset() process_data.project_name = project.get_config_value('name') if target_name: Logger.i('Build only target: {0}'.format(target_name)) else: Logger.i('Build all targets') target_found = False for target in project.targets: can_build = False if not target_name: can_build = True elif target.get_name() == target_name: can_build = True if can_build: Logger.d('Getting target data by target name: {0}...'.format( target_name)) target_found = True # targets need be deleted to be always fresh with target data from dependencies target.remove() # build the target repository after download target.prepare_from_process_data(process_data) target.repository.download() target.repository.build(process_data) # get all target data from project dependencies target_data = TargetData() target_data.project_config = project.config for dependency in project.dependencies: dependency.prepare_from_process_data(process_data) new_target_data = dependency.get_target_data_by_target_name_and_parse( target.get_name(), process_data) target_data.merge(new_target_data) # back to target data target.prepare_from_process_data(process_data) # copy files from dependencies to target directory FileUtil.copy_files_from_list(target_data.copy_files) # parse files path and it content target_project_file_data = target.load_target_project_file_data( ) if 'target' in target_project_file_data: target_project_data = target_project_file_data['target'] # parse files if 'parse_files' in target_project_data: target_project_data_parse_files = target_project_data[ 'parse_files'] if target_project_data_parse_files: Logger.d('Files to parse from target: {0}'.format( len(target_project_data_parse_files))) target_project_data_parse_files = process_data.parse_text_list( target_project_data_parse_files) for target_project_data_parse_file in target_project_data_parse_files: template_loader = jinja2.FileSystemLoader( searchpath='/') template_env = jinja2.Environment( loader=template_loader) template_file = target_project_data_parse_file template = template_env.get_template( template_file) templ_result = template.render( target=target_data) FileUtil.write_to_file( os.path.dirname( target_project_data_parse_file), os.path.basename( target_project_data_parse_file), str(templ_result)) else: Logger.d('No files need to parse from target: {0}'. format(target.get_name())) # build target if 'build' in target_project_data: Logger.i('Building target: {0}...'.format( target.get_name())) target_project_data_build = target_project_data[ 'build'] exitcode, stderr, stdout = FileUtil.run( target_project_data_build, target.repository.get_vendor_dir(), process_data.get_environ()) if exitcode == 0: Logger.i('Build finished for target: {0}'.format( target.get_name())) else: if stdout: Logger.i('Build output for target: {0}'.format( target.get_name())) Logger.clean(stdout) if stderr: Logger.i( 'Error output while build target: {0}'. format(target.get_name())) Logger.clean(stderr) Logger.f('Failed to build target: {0}'.format( target.get_name())) if not target_found: Logger.f('Target not found: {0}'.format(target_name))
def execute_command(self, target_command, target_name): from ezored.models.logger import Logger from ezored.models.project import Project import importlib import sys project = Project.create_from_project_file() if target_name: Logger.i('Execute command "{0}" only on target "{1}"'.format( target_command, target_name)) else: Logger.i( 'Execute command "{0}" on all targets'.format(target_command)) target_found = False total_targets = len(project.targets) if total_targets > 0: for target in project.targets: process_data = ProcessData() process_data.reset() process_data.project_name = project.get_config_value('name') can_build = False if not target_name: can_build = True elif target.get_name() == target_name: can_build = True if can_build: Logger.d( 'Getting target data by target name "{0}"...'.format( target.get_name())) target_found = True # targets need be deleted to be always fresh with target data from dependencies target.remove() # build the target repository after download target.prepare_from_process_data(process_data) target.repository.download() target.repository.build(project=project, process_data=process_data) # get all target data from project dependencies target_data = TargetData() target_data.project_home = target.repository.get_vendor_dir( ) target_data.project_config = project.config for dependency in project.dependencies: dependency.prepare_from_process_data(process_data) new_target_data = dependency.get_target_data_by_target_name_and_parse( target.get_name(), process_data) target_data.merge(new_target_data) # back to target data target.prepare_from_process_data(process_data) # process target data and execute required command target_data_file = target.repository.load_target_data_file( ) if 'target' in target_data_file: target_project_data = target_data_file['target'] # target tasks if 'tasks' in target_project_data: target_tasks_data = target_project_data['tasks'] for target_task_data in target_tasks_data: task = Task.from_dict(target_task_data) task.parse(process_data) target_data.tasks.append(task) # run all tasks Task.run_all_tasks( tasks=target_data.tasks, process_data=process_data, template_data={'target': target_data}, working_dir=target.repository.get_vendor_dir()) # execute command on target Logger.i('Executing command "{0}" on target "{1}"...'. format(target_command, target.get_name())) sys_path = list(sys.path) original_cwd = os.getcwd() try: sys.path.insert(0, target.repository.get_vendor_dir()) target_module = importlib.import_module( Constants.TARGET_MODULE_NAME) command = getattr(target_module, 'do_' + target_command) command( params={ 'project': project, 'target': target, 'target_data': target_data, 'process_data': process_data, }) del sys.modules[Constants.TARGET_MODULE_NAME] del target_module del command Logger.i('Command "{0}" finished for target "{1}"'. format(target_command, target.get_name())) except Exception as e: Logger.e( 'Error while call "{0}" on target "{1}": {2}'. format(target_command, target.get_name(), e.message)) raise sys.path = sys_path os.chdir(original_cwd) if not target_found: Logger.f('Target not found: {0}'.format(target_name)) else: Logger.i('Your project does not have targets')