我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用os.path.pardir()。
def test_toolchain_standard_build_dir_remapped(self): """ This can either be caused by relative paths or symlinks. Will result in the manually specified build_dir being remapped to its real location """ fake = mkdtemp(self) real = mkdtemp(self) real_base = basename(real) spec = Spec() spec['build_dir'] = join(fake, pardir, real_base) with pretty_logging(stream=StringIO()) as s: with self.assertRaises(NotImplementedError): self.toolchain(spec) self.assertIn('realpath of build_dir resolved to', s.getvalue()) self.assertEqual(spec['build_dir'], real)
def test_module2_recursive_es6(self): from calmjs.testing import module2 calmjs_base_dir = abspath(join( indexer.modpath_pkg_resources(indexer)[0], pardir)) results = { k: relpath(v, calmjs_base_dir) for k, v in indexer.mapper(module2, globber='recursive').items() } self.assertEqual(results, { 'calmjs/testing/module2/index': to_os_sep_path('calmjs/testing/module2/index.js'), 'calmjs/testing/module2/helper': to_os_sep_path('calmjs/testing/module2/helper.js'), 'calmjs/testing/module2/mod/helper': to_os_sep_path('calmjs/testing/module2/mod/helper.js'), })
def test_module2_callables(self): from calmjs.testing import module2 calmjs_base_dir = abspath(join( indexer.modpath_pkg_resources(indexer)[0], pardir)) results = { k: relpath(v, calmjs_base_dir) for k, v in indexer.mapper( module2, globber=indexer.globber_recursive, modname=indexer.modname_python, modpath=indexer.modpath_pkg_resources, ).items() } self.assertEqual(results, { 'calmjs.testing.module2.index': to_os_sep_path('calmjs/testing/module2/index.js'), 'calmjs.testing.module2.helper': to_os_sep_path('calmjs/testing/module2/helper.js'), 'calmjs.testing.module2.mod.helper': to_os_sep_path('calmjs/testing/module2/mod/helper.js'), })
def save_cache(src_path, des_path, get_feature_func): des_path = osp.splitext(des_path)[0] + '.npy' try: X, sr = librosa.load(src_path) src = int(sr) feature = get_feature_func(X, sr) print('[INFO] Saving Cache in {} ...'.format(des_path)) des_par = osp.abspath(osp.join(des_path, osp.pardir)) if not osp.exists(des_par): os.makedirs(des_par) except Exception, e: print("[ERROR] Unkown error happend when dealing with{}".format(src_path)) #print(e) return -1 np.save(des_path, feature) return 0
def test_find_files(): tests_dir = dirname(__file__) proj_dir = normpath(opj(dirname(__file__), pardir)) ff = find_files('.*', proj_dir) ok_generator(ff) files = list(ff) assert(len(files) > 10) # we have more than 10 test files here assert_in(opj(tests_dir, 'test_utils.py'), files) # and no directories should be mentioned assert_not_in(tests_dir, files) ff2 = find_files('.*', proj_dir, dirs=True) files2 = list(ff2) assert_in(opj(tests_dir, 'test_utils.py'), files2) assert_in(tests_dir, files2) # now actually matching the path ff3 = find_files('.*/test_.*\.py$', proj_dir, dirs=True) files3 = list(ff3) assert_in(opj(tests_dir, 'test_utils.py'), files3) assert_not_in(tests_dir, files3) for f in files3: ok_startswith(basename(f), 'test_')
def skip_if_no_module(module): try: imp = __import__(module) except Exception as exc: raise SkipTest("Module %s fails to load: %s" % (module, exc_str(exc))) # def create_tree_archive(path, name, load, overwrite=False, archives_leading_dir=True): # """Given an archive `name`, create under `path` with specified `load` tree # """ # from ..support.archives import compress_files # dirname = file_basename(name) # full_dirname = opj(path, dirname) # os.makedirs(full_dirname) # create_tree(full_dirname, load, archives_leading_dir=archives_leading_dir) # # create archive # if archives_leading_dir: # compress_files([dirname], name, path=path, overwrite=overwrite) # else: # compress_files(list(map(basename, glob.glob(opj(full_dirname, '*')))), # opj(pardir, name), # path=opj(path, dirname), # overwrite=overwrite) # # remove original tree # shutil.rmtree(full_dirname)
def find_package_name_dir_up(parent_path): for file_name in listdir(parent_path): if isdir(file_name): continue if file_name == 'AndroidManifest.xml': for line in open(join(parent_path, file_name), 'r'): package_name_re_result = PACKAGE_NAME_RE.search(line) if package_name_re_result is not None: return package_name_re_result.groups()[0] if file_name == 'build.gradle': for line in open(join(parent_path, file_name), 'r'): application_id_re_result = APPLICATION_ID_RE.search(line) if application_id_re_result is not None: return application_id_re_result.groups()[0] return find_package_name_dir_up(abspath(join(parent_path, pardir)))
def split_template_path(template): """Split a path into segments and perform a sanity check. If it detects '..' in the path it will raise a `TemplateNotFound` error. """ pieces = [] for piece in template.split('/'): if path.sep in piece \ or (path.altsep and path.altsep in piece) or \ piece == path.pardir: raise TemplateNotFound(template) elif piece and piece != '.': pieces.append(piece) return pieces
def get_includes(): """Return a list of directories to include for linking against pyzmq with cython.""" from os.path import join, dirname, abspath, pardir base = dirname(__file__) parent = abspath(join(base, pardir)) return [ parent ] + [ join(parent, base, subdir) for subdir in ('utils',) ]
def cmk_root(): return normpath(realpath(join(__file__, pardir, pardir)))
def test_module1_loader_es6(self): from calmjs.testing import module1 calmjs_base_dir = abspath(join( indexer.modpath_pkg_resources(indexer)[0], pardir)) results = { k: relpath(v, calmjs_base_dir) for k, v in indexer.mapper_es6(module1).items() } self.assertEqual(results, { 'calmjs/testing/module1/hello': to_os_sep_path('calmjs/testing/module1/hello.js'), })
def test_module1_loader_python(self): from calmjs.testing import module1 calmjs_base_dir = abspath(join( indexer.modpath_pkg_resources(indexer)[0], pardir)) results = { k: relpath(v, calmjs_base_dir) for k, v in indexer.mapper_python(module1).items() } self.assertEqual(results, { 'calmjs.testing.module1.hello': to_os_sep_path('calmjs/testing/module1/hello.js'), })
def test_module2_recursive_es6_legacy(self): # ensure legacy behavior is maintained, where a single argument # is accepted by the modpath function. def modpath_last(module): return indexer.modpath_last(module) from calmjs.testing import module2 calmjs_base_dir = abspath(join( indexer.modpath_pkg_resources(indexer)[0], pardir)) with pretty_logging(stream=StringIO()) as fd: with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') results = { k: relpath(v, calmjs_base_dir) for k, v in indexer.mapper( module2, modpath=modpath_last, globber='recursive', ).items() } self.assertIn( "method will need to accept entry_point argument by calmjs-", str(w[-1].message) ) self.assertIn( "method will need to accept entry_point argument by calmjs-", fd.getvalue() ) self.assertEqual(results, { 'calmjs/testing/module2/index': to_os_sep_path('calmjs/testing/module2/index.js'), 'calmjs/testing/module2/helper': to_os_sep_path('calmjs/testing/module2/helper.js'), 'calmjs/testing/module2/mod/helper': to_os_sep_path('calmjs/testing/module2/mod/helper.js'), })
def _conf_paths(self): paths = [] current = abspath(".") while current != "/": paths.append(join(current, DEFAULT_CONF_NAME)) current = abspath(join(current, pardir)) return list(reversed(paths))
def check_dir(path): """ make sure the dir specified by path got created """ d = osp.abspath(osp.join(path, osp.pardir)) if not osp.exists(d): os.makedirs(d)
def check_dir(path): d = osp.abspath(osp.join(path, osp.pardir)) if not osp.exists(d): os.makedirs(d)
def save_dataset(data_path, X, y): print('Data Saving in {} (X.shape={},y.shape={})'.format( data_path, X.shape, y.shape)) data_dir = osp.abspath(osp.join(data_path, osp.pardir)) if not osp.exists(data_dir): os.makedirs(data_dir) data = {'X': X, 'y': y} with open(data_path, 'wb') as f: pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
def get_dataset_base(): return osp.abspath(osp.join(__file__, osp.pardir, osp.pardir, osp.pardir, osp.pardir, "datasets"))
def get_dataset_cache_base(): return osp.abspath(osp.join(__file__, osp.pardir, osp.pardir, osp.pardir, osp.pardir, "datasets-cache"))
def test_find_common_same_base_parents_common(self): base = tempfile.mktemp() source = join(base, 'src', 'file.js') source_min = join(base, 'build', 'file.min.js') source_map = join(base, 'build', 'file.min.js.map') # mapping from source_map to source self.assertEqual([pardir, 'src', 'file.js'], utils.normrelpath( source_map, source).split(sep)) # for pointing from source_map.source to the source_min self.assertEqual('file.min.js', utils.normrelpath( source_map, source_min))
def test_find_double_parent(self): base = tempfile.mktemp() root = join(base, 'file.js') nested = join(base, 'src', 'dir', 'blahfile.js') self.assertEqual([pardir, pardir, 'file.js'], utils.normrelpath( nested, root).split(sep)) self.assertEqual(['src', 'dir', 'blahfile.js'], utils.normrelpath( root, nested).split(sep))
def test_find_same_prefix(self): base = tempfile.mktemp() src = join(base, 'basesrc', 'source.js') tgt = join(base, 'basetgt', 'target.js') self.assertEqual([pardir, 'basetgt', 'target.js'], utils.normrelpath( src, tgt).split(sep))
def setUp(self): super(CrcTestCase, self).setUp() self.test_files = abspath( join(__file__, pardir, 'files') )