def _copy_single_file(src_file, tgt_file): if should_overwrite_file(src_file, tgt_file): try: # In case the file is readonly, we'll remove the existing file first if os.path.exists(tgt_file): os.chmod(target_file, stat.S_IWRITE) fast_copy2(copy_external_file, target_file) except: Logs.warn( '[WARN] Unable to copy {} to destination {}. ' 'Check the file permissions or any process that may be locking it.' .format(copy_external_file, target_file))
def copy_local_python_to_target(task, source_python_dll_path): """ Perform a quick simple copy of python.dll from a local python folder :param task: The current running task :param source_python_dll_path: The source python path """ current_platform = task.env['PLATFORM'].lower() current_configuration = task.env['CONFIGURATION'].lower() # Determine the target folder(s) output_sub_folder = getattr(task, 'output_sub_folder', None) # If we have a custom output folder, then make a list of nodes from it target_folders = getattr(task, 'output_folder', []) if len(target_folders) > 0: target_folders = [ task.bld.path.make_node(node) if isinstance(node, str) else node for node in target_folders ] else: target_folders = task.bld.get_output_folders(current_platform, current_configuration) python_name = os.path.basename(source_python_dll_path) # Copy to each output folder target node for target_node in target_folders: python_dll_target_path = os.path.join(target_node.abspath(), python_name) if output_sub_folder: python_dll_target_path = os.path.join( target_node.make_node(output_sub_folder).abspath(), python_name) if should_overwrite_file(source_python_dll_path, python_dll_target_path): try: # In case the file is readonly, we'll remove the existing file first if os.path.exists(python_dll_target_path): os.chmod(python_dll_target_path, stat.S_IWRITE) fast_copy2(source_python_dll_path, python_dll_target_path) except Exception as e: Logs.warn( '[WARN] Unable to copy {} to destination {} ({}). ' 'Check the file permissions or any process that may be locking it.' .format(python_dll_target_path, python_dll_target_path, e.message))
def add_copy_3rd_party_artifacts(self): if self.bld.env['PLATFORM'] == 'project_generator': return third_party_artifacts = self.env['COPY_3RD_PARTY_ARTIFACTS'] current_platform = self.bld.env['PLATFORM'] current_configuration = self.bld.env['CONFIGURATION'] if third_party_artifacts: copied_files = 0 # Iterate through all target output folders for target_node in self.bld.get_output_folders(current_platform, current_configuration, self): # Determine the final output directory output_sub_folder = getattr(self, 'output_sub_folder', None) if output_sub_folder: # If the output subfolder is blacklisted, do not copy the dependency if output_sub_folder in DEPENDENT_TARGET_BLACKLISTED_SUBFOLDERS: return output_path_node = target_node.make_node(output_sub_folder) else: output_path_node = target_node target_folder = output_path_node.abspath() for source_node in third_party_artifacts: source_full_path = source_node.abspath() source_filename = os.path.basename(source_full_path) target_full_path = os.path.join(target_folder, source_filename) if should_overwrite_file(source_full_path, target_full_path): try: # In case the file is readonly, we'll remove the existing file first if os.path.exists(target_full_path): os.chmod(target_full_path, stat.S_IWRITE) fast_copy2(source_full_path, target_full_path) copied_files += 1 except: Logs.warn( '[WARN] Unable to copy {} to destination {}. ' 'Check the file permissions or any process that may be locking it.' .format(source_full_path, target_full_path)) if copied_files > 0 and Logs.verbose > 0: Logs.info('[INFO] {} External files copied.'.format(copied_files))
def _copy_single_file(src_file, tgt_folder): src_filename = os.path.split(src_file)[1] tgt_file = os.path.join(tgt_folder, src_filename) if should_overwrite_file(src_file, tgt_file): try: # In case the file is readonly, we'll remove the existing file first if os.path.exists(tgt_file): os.chmod(tgt_file, stat.S_IWRITE) fast_copy2(src_file, tgt_file) return True except: Logs.warn( '[WARN] Unable to copy {} to destination {}. ' 'Check the file permissions or any process that may be locking it.' .format(copy_external_file, tgt_file)) return False
def copy_external_files(self): """ Feature to process copying external (files outside of the WAF root) folder as part of the build """ if self.bld.env['PLATFORM'] == 'project_generator': return if 'COPY_EXTERNAL_FILES' not in self.env: return external_files = self.env['COPY_EXTERNAL_FILES'] current_platform = self.bld.env['PLATFORM'] current_configuration = self.bld.env['CONFIGURATION'] copied_files = 0 def _copy_single_file(src_file, tgt_file): if should_overwrite_file(src_file, tgt_file): try: # In case the file is readonly, we'll remove the existing file first if os.path.exists(tgt_file): os.chmod(target_file, stat.S_IWRITE) fast_copy2(copy_external_file, target_file) except: Logs.warn( '[WARN] Unable to copy {} to destination {}. ' 'Check the file permissions or any process that may be locking it.' .format(copy_external_file, target_file)) # Iterate through all target output folders for target_node in self.bld.get_output_folders(current_platform, current_configuration, self): if hasattr(self, 'output_sub_folder'): output_path = os.path.join(target_node.abspath(), self.output_sub_folder) else: output_path = target_node.abspath() for copy_external_file in external_files: if not os.path.exists(copy_external_file): continue filename = os.path.split(copy_external_file)[1] target_file = os.path.join(output_path, filename) if should_overwrite_file(copy_external_file, target_file): try: # In case the file is readonly, we'll remove the existing file first if os.path.exists(target_file): os.chmod(target_file, stat.S_IWRITE) fast_copy2(copy_external_file, target_file) except: Logs.warn( '[WARN] Unable to copy {} to destination {}. ' 'Check the file permissions or any process that may be locking it.' .format(copy_external_file, target_file)) copied_files += 1 if copied_files > 0: Logs.info('[INFO] {} External files copied.'.format(copied_files))
def copy_tree2(src, dst, overwrite_existing_file=False, pattern_paths=None, is_pattern_required=False, fail_on_error=True): """ Copy a tree from a source folder to a destination folder. If the destination does not exist, then create a copy automatically. If a destination does exist, then either overwrite based on the owerwrite_existing_file parameter or based on if the file is different (currently only file size is checked) :param src: The source tree to copy from :param dst: The target tree to copy to :param overwrite_existing_file: Flag to always overwrite (otherwise follow the copy rule) :param is_pattern_required: Tells the function how to interpret the pattern_paths, pattern_paths are required paths if is_pattern_required is True, it will be the ignore paths if it is False :param pattern_paths: Any particular file/pattern to ignore :return: The number of files actually copied """ if os.path.isdir(src) is False: Logs.warn( '[WARN] Unable to copy {} to destination {} using copy_tree2. {} is not a directory.' .format(src, dst, src)) return 0 supports_symlinks = not Utils.unversioned_sys_platform().startswith('win') src = os.path.normpath(src) dst = os.path.normpath(dst) # get all non ignored paths/files in dir based on ignore path input def _get_non_ignored_paths_in_dir_recursively(src, ignore_paths): non_ignored_paths = [] paths = os.listdir(src) for item in paths: ignore = False src_path = os.path.join(src, item) if any(path in src_path for path in ignore_paths): continue if supports_symlinks and os.path.islink(src_path): non_ignored_paths.append(src_path) elif os.path.isdir(src_path): non_ignored_paths.extend( _get_non_ignored_paths_in_dir_recursively( src_path, ignore_paths)) else: non_ignored_paths.append(src_path) return non_ignored_paths # copy everything if pattern_path is none paths_to_copy = os.listdir(src) copied_files = 0 if pattern_paths is not None: filtered_paths = [] if is_pattern_required is True: for path in pattern_paths: filtered_paths.extend(glob.glob(os.path.join(src, path))) else: filtered_paths = _get_non_ignored_paths_in_dir_recursively( src, pattern_paths) # sanitize the paths in filtered_paths for further consumption (we only want relative path from src) for idx, item in enumerate(filtered_paths): item = os.path.normpath(item) sep = src + os.path.sep filtered_paths[idx] = item.replace(sep, "") paths_to_copy = filtered_paths symlinks = [] # now we copy all files specified from the paths in paths_to_copy for path in paths_to_copy: srcname = os.path.join(src, path) dstname = os.path.join(dst, path) if supports_symlinks and os.path.islink(srcname): linkto = os.readlink(srcname) symlinks.append([linkto, dstname]) elif os.path.isdir(srcname): # if we encounter a srcname that is a folder, we assume that we want the entire folder # pattern_paths to None tells this function that we want to copy the entire folder copied_files += copy_tree2(srcname, dstname, overwrite_existing_file, None, fail_on_error=fail_on_error) else: # check to see if we should copy the file copy = overwrite_existing_file or should_overwrite_file( srcname, dstname) if copy is False: continue file_copied = 0 Logs.debug('lumberyard: Copying file {} to {}'.format( srcname, dstname)) try: # In case the file is readonly, we'll remove the existing file first if os.path.exists(dstname): os.chmod(dstname, stat.S_IWRITE) # In the case where the path doesn't exist elif os.path.exists(os.path.dirname(dstname)) is False: try: os.makedirs(os.path.dirname(dstname)) except: pass if not os.path.exists(dst): raise shutil.Error( "Unable to create target folder `{}`".format(dst)) try: file_copied = fast_copy2(srcname, dstname) except: # Second try with detail hash check file_copied = fast_copy2(srcname, dstname, True) except Exception as err: if fail_on_error: raise err else: Logs.warn( '[WARN] Unable to copy {} to destination {}. Check the file permissions or any process that may be locking it.' .format(srcname, dstname)) copied_files += file_copied # symlinks will be empty if a platform does not support them so no need to # protect against running this code for symlink in symlinks: if os.path.exists(symlink[1]): if os.path.islink(symlink[1]): os.unlink(symlink[1]) elif os.path.isdir(symlink[1]): shutil.rmtree(symlink[1]) else: os.remove(symlink[1]) try: os.symlink(symlink[0], symlink[1]) except OSError as oops: Logs.warn("Unable to create symbolic link {} because {}".format( symlink[1], oops)) copied_files = False return copied_files
def copy_tree2(src, dst, overwrite_existing_file=False, ignore_paths=None, fail_on_error=True): """ Copy a tree from a source folder to a destination folder. If the destination does not exist, then create a copy automatically. If a destination does exist, then either overwrite based on the owerwrite_existing_file parameter or based on if the file is different (currently only file size is checked) :param src: The source tree to copy from :param dst: The target tree to copy to :param overwrite_existing_file: Flag to always overwrite (otherwise follow the copy rule) :param ignore_paths: Any particular file/pattern to ignore :return: The number of files actually copied """ copied_files = 0 try: os.makedirs(dst) except: pass if not os.path.exists(dst): raise shutil.Error("Unable to create target folder `{}`".format(dst)) items = os.listdir(src) for item in items: srcname = os.path.join(src, item) dstname = os.path.join(dst, item) ignore = False if ignore_paths is not None: for ignore_path in ignore_paths: if ignore_path in srcname: ignore = True break if ignore: continue if os.path.isdir(srcname): copied_files += copy_tree2(srcname, dstname, overwrite_existing_file, ignore_paths, fail_on_error) else: copy = overwrite_existing_file or should_overwrite_file( srcname, dstname) if copy: Logs.debug('lumberyard: Copying file {} to {}'.format( srcname, dstname)) try: # In case the file is readonly, we'll remove the existing file first if os.path.exists(dstname): os.chmod(dstname, stat.S_IWRITE) fast_copy2(srcname, dstname) except Exception as err: if fail_on_error: raise err else: Logs.warn( '[WARN] Unable to copy {} to destination {}. Check the file permissions or any process that may be locking it.' .format(srcname, dstname)) copied_files += 1 return copied_files