def _matches_recursive_blob(path, pattern): split = pattern.split('**', maxsplit=1) try: remaining_path = path.relative_to(split[0]) except ValueError: return False if len(split) == 1: return False remaining_pattern = split[1] if remaining_pattern.startswith(os_utils.path_sep()): remaining_pattern = remaining_pattern[len(os_utils.path_sep()):] if remaining_pattern == '': # this can happen if pattern ends with ** return True if remaining_path.match(remaining_pattern): return True elif '**' not in remaining_pattern: return False (remaining_head, remaining_tail ) = SingleFileMatcher._split_first_parent(remaining_pattern) for parent in remaining_path.parents: if parent.name == '': continue if fnmatch(parent.name, remaining_head): if SingleFileMatcher._matches_recursive_blob( remaining_path.relative_to(parent), remaining_tail): return True return False
def test_mixed_images_when_multiple_output(self): path1 = test_utils.create_file('test123.png') path2 = test_utils.create_file('images/test.png') path3 = test_utils.create_file('a.b.c.png') path4 = test_utils.create_file('test456.png') path5 = test_utils.create_file('some/long/path/me.jpg') config = create_config_model( 'my_script', output_files=[ inline_image(test_utils.temp_folder + os_utils.path_sep() + '#test\d+.png#'), inline_image(path2), inline_image(path3), inline_image('##any_path/path/\w+#.jpg') ]) execution_id = self.start_execution(config) paths = [ normalize_path(p) for p in (path1, path2, path3, path4, path5) ] for index, path in enumerate(paths): self.write_output(execution_id, '__ ' + path + ' __\n') self.wait_output_chunks(execution_id, chunks_count=index + 1) self.write_output(execution_id, '__ ' + path2 + ' __\n') self.wait_output_chunks(execution_id, chunks_count=len(paths) + 1) self.assert_images(*paths)
def _pre_3_5_recursive_glob(path_pattern, parent_path=None): if path_pattern.startswith('~'): path_pattern = os.path.expanduser(path_pattern) file_name_regex = '([\w.-]|(\\\ ))*' pattern_chunks = path_pattern.split(os_utils.path_sep()) current_paths = [] if parent_path is not None: current_paths.append(parent_path) elif os.path.isabs(path_pattern): root_path = os.path.abspath(os.sep) current_paths.append(root_path) else: current_paths.append('') for i, pattern_chunk in enumerate(pattern_chunks): new_paths = [] for current_path in current_paths: if '*' not in pattern_chunk: new_path = os.path.join(current_path, pattern_chunk) if os.path.exists(new_path): new_paths.append(new_path) elif '**' not in pattern_chunk: if os.path.exists(current_path) and os.path.isdir(current_path): pattern_chunk = pattern_chunk.replace('*', file_name_regex) for file in os.listdir(current_path): if re.match(pattern_chunk, file): new_path = os.path.join(current_path, file) new_paths.append(new_path) else: all_paths = [] next_path_pattern = os.path.sep.join(pattern_chunks[i + 1:]) if next_path_pattern == '': next_path_pattern = '*' all_paths.append(current_path + os.path.sep) all_paths.extend(_pre_3_5_recursive_glob(next_path_pattern, current_path)) remaining_pattern = os.path.sep.join(pattern_chunks[i:]) if os.path.exists(current_path) and os.path.isdir(current_path): for file in os.listdir(current_path): file_path = os.path.join(current_path, file) if os.path.isdir(file_path): all_paths.extend(_pre_3_5_recursive_glob(remaining_pattern, file_path)) for child_path in all_paths: if child_path.endswith('/') and child_path[:-1] in all_paths: continue new_paths.append(child_path) current_paths = new_paths if '**' in pattern_chunk: break return current_paths
def find_matching_files(file_pattern, script_output): files = [] separator = re.escape(os_utils.path_sep()) output_patterns = [file_pattern] while len(output_patterns) > 0: output_pattern = output_patterns.pop(0) if '#' in output_pattern: regex_start = output_pattern.find('#') group_number_matches = re.findall('^#\d+#', output_pattern[regex_start:]) if group_number_matches: first_match = group_number_matches[0] group_number = int(first_match[1:-1]) pattern_start = regex_start + len(first_match) - 1 else: group_number = 0 pattern_start = regex_start regex_end = output_pattern.find('#', pattern_start + 1) while (regex_end >= 0) and output_pattern[regex_end:].startswith('#any_path'): regex_end = output_pattern.find('#', regex_end + 1) if regex_end >= 0: regex_pattern = output_pattern[pattern_start + 1:regex_end] if regex_pattern.startswith('#any_path') and (regex_start == 0): if os_utils.is_linux() or os_utils.is_mac(): regex_pattern = '~?' + regex_pattern elif os_utils.is_win(): regex_pattern = '(([^\W\d_]:)|~)' + regex_pattern regex_pattern = regex_pattern.replace( '#any_path', '(' + separator + '([\w.\-]|(\\\ ))+)+') found_matches = re.finditer(regex_pattern, script_output) for match in found_matches: matched_group = match.group(group_number) new_output_pattern = string_utils.replace( output_pattern, matched_group, regex_start, regex_end) output_patterns.append(new_output_pattern) continue if '*' not in output_pattern: files.append(output_pattern) else: recursive = '**' in output_pattern matching_files = file_utils.search_glob(output_pattern, recursive=recursive) files.extend(matching_files) return files
def find_matching_files(file_pattern, script_output): files = [] separator = re.escape(os_utils.path_sep()) output_patterns = [file_pattern] while len(output_patterns) > 0: output_pattern = output_patterns.pop(0) if '#' in output_pattern: regex_start = output_pattern.find('#') group_number_matches = re.findall('^#\d+#', output_pattern[regex_start:]) if group_number_matches: first_match = group_number_matches[0] group_number = int(first_match[1:-1]) pattern_start = regex_start + len(first_match) - 1 else: group_number = 0 pattern_start = regex_start regex_end = output_pattern.find('#', pattern_start + 1) while (regex_end >= 0) and output_pattern[regex_end:].startswith('#any_path'): regex_end = output_pattern.find('#', regex_end + 1) if regex_end >= 0: regex_pattern = output_pattern[pattern_start + 1:regex_end] if regex_pattern.startswith('#any_path') and (regex_start == 0): if os_utils.is_linux() or os_utils.is_mac(): regex_pattern = '~?' + regex_pattern elif os_utils.is_win(): regex_pattern = '(([^\W\d_]:)|~)' + regex_pattern regex_pattern = regex_pattern.replace('#any_path', '(' + separator + '([\w.\-]|(\\\ ))+)+') found_matches = re.finditer(regex_pattern, script_output) for match in found_matches: matched_group = match.group(group_number) new_output_pattern = string_utils.replace(output_pattern, matched_group, regex_start, regex_end) output_patterns.append(new_output_pattern) continue if '*' not in output_pattern: files.append(output_pattern) else: recursive = '**' in output_pattern matching_files = file_utils.search_glob(output_pattern, recursive=recursive) files.extend(matching_files) return files