def create_package(package_parent_dir, package_name): """Create a new Opus package named package_name in the directory given by package_parent_dir. """ from opus_core.opus_package_info import package as OpusPackageInfo package_template = path(OpusPackageInfo().get_path_for_package('opus_core')) / 'package_template' new_package_dir=path(package_parent_dir) / package_name if not os.path.exists(new_package_dir.parent): os.makedirs(new_package_dir.parent) copytree(package_template, new_package_dir, skip_subdirectories=['CVS', '.svn']) # Replace each instance of opus_core.package_template in any of the # template files with the actual name of this Opus package. replace_string_in_files(new_package_dir, 'opus_core.package_template', package_name)
def create_package(package_parent_dir, package_name): """Create a new Opus package named package_name in the directory given by package_parent_dir. """ from opus_core.opus_package_info import package as OpusPackageInfo package_template = path(OpusPackageInfo().get_path_for_package( 'opus_core')) / 'package_template' new_package_dir = path(package_parent_dir) / package_name if not os.path.exists(new_package_dir.parent): os.makedirs(new_package_dir.parent) copytree(package_template, new_package_dir, skip_subdirectories=['CVS', '.svn']) # Replace each instance of opus_core.package_template in any of the # template files with the actual name of this Opus package. replace_string_in_files(new_package_dir, 'opus_core.package_template', package_name)
def test_change_package_order(self): """Does it find the correct dataset when there are multiple packages?""" opus_path = package().get_package_parent_path() old_sys_path = sys.path try: temp_dir = tempfile.mkdtemp(prefix='opus_tmp') # Create a temporary Opus package with a variant of the alldata dataset. temp_package_name = '__test_change_package_order__' create_package(package_parent_dir=temp_dir, package_name=temp_package_name) # Make sure Python can find this temporary package. sys.path = [temp_dir] + sys.path # Add the datasets directory. temp_package_path = os.path.join(temp_dir, temp_package_name) os.mkdir(os.path.join(temp_package_path, 'datasets')) opus_core_dataset_path = os.path.join(opus_path, 'opus_core', 'datasets') temp_package_dataset_path = os.path.join(temp_package_path, 'datasets') # Create a 'newdata' dataset. copyfile(os.path.join(opus_core_dataset_path, '__init__.py'), os.path.join(temp_package_dataset_path, '__init__.py')) copyfile( os.path.join(opus_core_dataset_path, 'alldata_dataset.py'), os.path.join(temp_package_dataset_path, 'newdata_dataset.py')) replace_string_in_files(temp_package_dataset_path, 'alldata', 'newdata') replace_string_in_files(temp_package_dataset_path, 'Alldata', 'Newdata') package_order = [temp_package_name, 'opus_core'] dataset_pool = self.get_dataset_pool(package_order) temp_dir = tempfile.mkdtemp(prefix='opus_tmp') # Create a temporary Opus package with a variant of the alldata dataset. temp_package_name = '__test_change_package_order__' create_package(package_parent_dir=temp_dir, package_name=temp_package_name) # Make sure Python can find this temporary package. sys.path = [temp_dir] + sys.path # Add the datasets directory. temp_package_path = os.path.join(temp_dir, temp_package_name) os.mkdir(os.path.join(temp_package_path, 'datasets')) opus_core_dataset_path = os.path.join(opus_path, 'opus_core', 'datasets') temp_package_dataset_path = os.path.join(temp_package_path, 'datasets') # Create a 'newdata' dataset. copyfile(os.path.join(opus_core_dataset_path, '__init__.py'), os.path.join(temp_package_dataset_path, '__init__.py')) copyfile( os.path.join(opus_core_dataset_path, 'alldata_dataset.py'), os.path.join(temp_package_dataset_path, 'newdata_dataset.py')) replace_string_in_files(temp_package_dataset_path, 'alldata', 'newdata') replace_string_in_files(temp_package_dataset_path, 'Alldata', 'Newdata') package_order = [temp_package_name, 'opus_core'] dataset_pool = self.get_dataset_pool(package_order) # Make sure it works as expected for 'alldata'. self.do_test_get_dataset(dataset_pool) # Try it for 'newdata'. newdata = dataset_pool.get_dataset('newdata') self.assert_(newdata is not None, "Could not get 'newdata' dataset") finally: sys.path = old_sys_path rmtree(temp_dir)
def test_change_package_order(self): """Does it find the correct dataset when there are multiple packages?""" opus_path = package().get_package_parent_path() old_sys_path = sys.path try: temp_dir = tempfile.mkdtemp(prefix='opus_tmp') # Create a temporary Opus package with a variant of the alldata dataset. temp_package_name = '__test_change_package_order__' create_package(package_parent_dir=temp_dir, package_name=temp_package_name) # Make sure Python can find this temporary package. sys.path = [temp_dir] + sys.path # Add the datasets directory. temp_package_path = os.path.join(temp_dir, temp_package_name) os.mkdir(os.path.join(temp_package_path, 'datasets')) opus_core_dataset_path = os.path.join(opus_path, 'opus_core', 'datasets') temp_package_dataset_path = os.path.join(temp_package_path, 'datasets') # Create a 'newdata' dataset. copyfile(os.path.join(opus_core_dataset_path, '__init__.py'), os.path.join(temp_package_dataset_path, '__init__.py')) copyfile(os.path.join(opus_core_dataset_path, 'alldata_dataset.py'), os.path.join(temp_package_dataset_path, 'newdata_dataset.py')) replace_string_in_files(temp_package_dataset_path, 'alldata', 'newdata') replace_string_in_files(temp_package_dataset_path, 'Alldata', 'Newdata') package_order = [temp_package_name, 'opus_core'] dataset_pool = self.get_dataset_pool(package_order) temp_dir = tempfile.mkdtemp(prefix='opus_tmp') # Create a temporary Opus package with a variant of the alldata dataset. temp_package_name = '__test_change_package_order__' create_package(package_parent_dir=temp_dir, package_name=temp_package_name) # Make sure Python can find this temporary package. sys.path = [temp_dir] + sys.path # Add the datasets directory. temp_package_path = os.path.join(temp_dir, temp_package_name) os.mkdir(os.path.join(temp_package_path, 'datasets')) opus_core_dataset_path = os.path.join(opus_path, 'opus_core', 'datasets') temp_package_dataset_path = os.path.join(temp_package_path, 'datasets') # Create a 'newdata' dataset. copyfile(os.path.join(opus_core_dataset_path, '__init__.py'), os.path.join(temp_package_dataset_path, '__init__.py')) copyfile(os.path.join(opus_core_dataset_path, 'alldata_dataset.py'), os.path.join(temp_package_dataset_path, 'newdata_dataset.py')) replace_string_in_files(temp_package_dataset_path, 'alldata', 'newdata') replace_string_in_files(temp_package_dataset_path, 'Alldata', 'Newdata') package_order = [temp_package_name, 'opus_core'] dataset_pool = self.get_dataset_pool(package_order) # Make sure it works as expected for 'alldata'. self.do_test_get_dataset(dataset_pool) # Try it for 'newdata'. newdata = dataset_pool.get_dataset('newdata') self.assert_(newdata is not None, "Could not get 'newdata' dataset") finally: sys.path = old_sys_path rmtree(temp_dir)