def _get_m_file_from_repos(mesh): m_file = None for repo in os.listdir(ROS_MESH_DATA_DIR): repo_path = join_paths(ROS_MESH_DATA_DIR, repo) mesh_path = join_paths(repo_path, mesh) if path_exists(mesh_path): m_file = mesh_path return m_file
def _get_file_path(file_name): """ Selects package subdir based on file extension. """ path = session['pkg'] (_, ext) = split_extension(file_name) if ext == ".pl": path = join_paths(path, "prolog") elif ext == ".owl": path = join_paths(path, "owl") return join_paths(path, file_name)
def _get_episode_files(cat, exp): episode_files = [] exp_path = get_experiment_path(cat, exp) for episode in list_directories(exp_path): episode_dir = join_paths(exp_path, episode) if not is_directory(episode_dir): continue for f in list_directories(episode_dir): if f.endswith('.json') or f.endswith('.bson'): episode_files.append((f[:-5], join_paths(episode_dir, f))) return episode_files
def _copy_pkg_to_user_container_and_replace_keywords(pkg_name, pkg_path, template_path): for root, dirs, files in walk_directories(template_path): for f in files: abs_path = join_paths(root, f) rel_path = relative_path(abs_path, template_path) user_path = join_paths(pkg_path, rel_path) copy_template_file_and_replace_keywords( abs_path, user_path, { "pkgName": pkg_name, "userName": session['user_container_name'] })
def _create_pkg_zip_and_unzip_in_filetransfer_folder(exercise, large_file_transferer, pkg_name): zip_path = join_paths(large_file_transferer.get_filetransfer_folder(), pkg_name + '.zip') _create_pkg_zip_in_filetransfer_folder(exercise, zip_path) _unzip_file_to_filetransfer_folder(large_file_transferer, zip_path)
def _get_episode_count(cat, exp): exp_path = get_experiment_path(cat, exp) count = 0 for episode in list_directories(exp_path): episode_dir = join_paths(exp_path, episode) if not is_directory(episode_dir): continue count += 1 return count
def test_list_directories(): dir_list = [] os.makedirs(TEST_DIR) _check_directory_list(dir_list, TEST_DIR) for i in range(0, random.randint(5, 11)): dir_list.append(str(i)) os.makedirs(join_paths(TEST_DIR, str(i))) _check_directory_list(dir_list, TEST_DIR) remove_directory_if_exists(TEST_DIR)
def join_paths_and_assert_correctness(*args): template_string = build_template_string(len(args)) expected_result_path = template_string.format(*args) builder_result_path = join_paths(*args) # replace is needed so tests run on uniformly on all OS assert builder_result_path.replace(os.sep, '/') == expected_result_path
import os from webrob.test.utility.testbase_file_io import TEMP_DIR, EMPTY_TEMP_FILE, TEMP_FILE_WITH_CONTENT, \ create_empty_temp_file, create_temp_file_with_content, remove_file from webrob.utility.directory_handler import mk_dir, rm_nonempty_dir from webrob.utility.path_handler import join_paths, path_exists, absolute_path, get_parent_dir_name, \ get_path_basename, get_unix_style_path_basename, is_directory, get_path_size, relative_path, split_path, \ split_extension EXISTING_PATH = TEMP_DIR NOT_EXISTING_PATH = join_paths(TEMP_DIR, 'nothing') BASENAME = 'base' BASENAME_TEST_DIR = join_paths(EXISTING_PATH, BASENAME) BASENAME_TEST_DIR_UNIX_STLYE = join_paths(BASENAME_TEST_DIR, '') EMPTY_STRING = '' ONLY_EXTENSION = get_path_basename(EMPTY_TEMP_FILE) PATH, FILE_NAME = os.path.split(EMPTY_TEMP_FILE) PATH_JOINED_WITH_ONLY_EXTENSION = join_paths(PATH, ONLY_EXTENSION) # cannot use testbase_file_io.create_temp() as it uses functionality of this module # instead use directory_handler and create and delete temp_dir manually def setup_module(): if os.path.exists( EXISTING_PATH ): # for the case that due to debugging errors teardown wasn't executed rm_nonempty_dir(EXISTING_PATH) mk_dir(EXISTING_PATH) return
import os import shutil import random import pytest from webrob.test.utility.testbase_file_io import TEMP_DIR from webrob.utility.directory_handler import rm_nonempty_dir, make_dirs, rm_empty_dir, mk_dir, \ get_current_working_directory, ch_dir, list_directories, walk_directories from webrob.utility.path_handler import join_paths, path_exists TEST_DIR = join_paths(TEMP_DIR, 'test') TEST_DIR_NESTED = join_paths(TEST_DIR, 'test') # both setup_function() and teardown_function() have to use the os-module # instead of the directory_handler as it's being tested in this module def setup_function(): remove_directory_if_exists(TEMP_DIR) os.mkdir(TEMP_DIR) def remove_directory_if_exists(path): if path_exists(path): shutil.rmtree(path) def teardown_function(): remove_directory_if_exists(TEMP_DIR)
def _build_container_path(large_file_transferer, name): return join_paths(large_file_transferer.get_filetransfer_folder(), name)
def _zip_dir(src_path, path_prefix, zip_file_writer): for root, dirs, files in walk_directories(src_path): for f in files: abs_p = join_paths(root, f) rel_p = relative_path(abs_p, path_prefix) zip_file_writer.write(abs_p, rel_p)
import pytest from webrob.test.utility.testbase_file_io import delete_temp_dir, EMPTY_TEMP_FILE, TEMP_DIR, \ create_temp_dir, create_empty_temp_file from webrob.utility.file_handler import read_file, write_to_file from webrob.utility.path_handler import join_paths, path_exists, get_parent_dir_name from webrob.utility.template_file_copyer import _copy_file_and_replace_keywords, _create_parent_dir, \ copy_template_file_and_replace_keywords, _get_number_of_template_fillers BUILD_DESTINATION = join_paths(TEMP_DIR, 'copy.txt') PARENT_DIR = get_parent_dir_name(BUILD_DESTINATION) TEMPLATE_FILE = EMPTY_TEMP_FILE TEMPLATE_CONTENT = '{0} and {1}' KEYWORDS = ['apples', 'oranges'] TOO_FEW_KEYWORDS = list(KEYWORDS).pop(1) TOO_MANY_KEYWORDS = list(KEYWORDS) TOO_MANY_KEYWORDS.append( 'pears' ) # for some reason cannot do list(...).append(object), returns None EXPECTED_RESULT = '{0} and {1}'.format(KEYWORDS[0], KEYWORDS[1]) def setup_function(): create_temp_dir() create_empty_temp_file() write_to_file(TEMPLATE_FILE, TEMPLATE_CONTENT) def teardown_function(): delete_temp_dir()