Python os.path.exists() Examples
The following are 30
code examples of os.path.exists().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
os.path
, or try the search function
.

Example #1
Source File: command_line.py From securityheaders with Apache License 2.0 | 6 votes |
def create_urls(args): urlcolumn = args.urlcolumn result = list() data = [] for f in args.url: if(exists(f)): data.extend(open(f)) else: data.extend(f.split(',')) i = 1 for line in data: if i > args.startrow: line = line.strip() k = line.split(',') fid = k[0] if(len(k) == 1): fid = str(i) urlcolumn = 0 result.append((fid,k[urlcolumn])) i = i + 1 return result
Example #2
Source File: test_toolchain.py From calmjs with GNU General Public License v2.0 | 6 votes |
def test_transpiler(self): # a kind of silly test but shows concept tmpdir = mkdtemp(self) js_code = 'var dummy = function() {\n};\n' source = join(tmpdir, 'source.js') target = 'target.js' with open(source, 'w') as fd: fd.write(js_code) spec = Spec(build_dir=tmpdir) modname = 'dummy' self.toolchain.transpile_modname_source_target( spec, modname, source, target) with open(join(tmpdir, target)) as fd: result = fd.read() self.assertEqual(js_code, result) self.assertFalse(exists(join(tmpdir, target + '.map')))
Example #3
Source File: test_npm.py From calmjs with GNU General Public License v2.0 | 6 votes |
def test_view(self): stub_mod_call(self, cli) tmpdir = mkdtemp(self) os.chdir(tmpdir) dist = Distribution(dict( script_name='setup.py', script_args=['npm', '--view'], name='foo', )) dist.parse_command_line() dist.run_commands() self.assertFalse(exists(join(tmpdir, 'package.json'))) # also log handlers removed. self.assertEqual(len(getLogger('calmjs.cli').handlers), 0) # written to stdout with the correct indentation level. self.assertIn('\n "jquery": "~1.11.0"', sys.stdout.getvalue())
Example #4
Source File: test_npm.py From calmjs with GNU General Public License v2.0 | 6 votes |
def test_install_dryrun(self): stub_mod_call(self, cli) tmpdir = mkdtemp(self) os.chdir(tmpdir) dist = Distribution(dict( script_name='setup.py', script_args=['npm', '--install', '--dry-run'], name='foo', )) dist.parse_command_line() dist.run_commands() self.assertFalse(exists(join(tmpdir, 'package.json'))) # Ensure that install is NOT called. self.assertIsNone(self.call_args) # also log handlers removed. self.assertEqual(len(getLogger('calmjs.cli').handlers), 0) # However, default action is view, the package.json should be # written to stdout with the correct indentation level. self.assertIn('\n "jquery": "~1.11.0"', sys.stdout.getvalue())
Example #5
Source File: mock_data.py From grlc with MIT License | 6 votes |
def mock_requestsGithub(uri, headers={}, params={}): if uri.endswith('contents'): return_value = Mock(ok=True) return_value.json.return_value = mock_files return return_value else: targetFile = uri.replace('https://raw.githubusercontent.com/fakeuser/fakerepo/master/', path.join(base_url, '')) if path.exists(targetFile): f = open(targetFile, 'r') lines = f.readlines() text = ''.join(lines) return_value = Mock(status_code=200) return_value.text = text return return_value else: return_value = Mock(status_code=404) return return_value
Example #6
Source File: test_toolchain.py From calmjs with GNU General Public License v2.0 | 6 votes |
def test_toolchain_standard_build_dir_set(self): spec = Spec() spec['build_dir'] = mkdtemp(self) with self.assertRaises(NotImplementedError): self.toolchain(spec) # Manually specified build_dirs do not get deleted from the # filesystem self.assertTrue(exists(spec['build_dir'])) not_exist = join(spec['build_dir'], 'not_exist') spec['build_dir'] = not_exist with pretty_logging(stream=StringIO()) as s: with self.assertRaises(OSError): # well, dir does not exist self.toolchain(spec) # Manually specified build_dirs do not get modified if they just # simply don't exist. self.assertEqual(spec['build_dir'], not_exist) self.assertIn(not_exist, s.getvalue()) self.assertIn("is not a directory", s.getvalue())
Example #7
Source File: configuration.py From spleeter with MIT License | 6 votes |
def load_configuration(descriptor): """ Load configuration from the given descriptor. Could be either a `spleeter:` prefixed embedded configuration name or a file system path to read configuration from. :param descriptor: Configuration descriptor to use for lookup. :returns: Loaded description as dict. :raise ValueError: If required embedded configuration does not exists. :raise SpleeterError: If required configuration file does not exists. """ # Embedded configuration reading. if descriptor.startswith(_EMBEDDED_CONFIGURATION_PREFIX): name = descriptor[len(_EMBEDDED_CONFIGURATION_PREFIX):] if not loader.is_resource(resources, f'{name}.json'): raise SpleeterError(f'No embedded configuration {name} found') with loader.open_text(resources, f'{name}.json') as stream: return json.load(stream) # Standard file reading. if not exists(descriptor): raise SpleeterError(f'Configuration file {descriptor} not found') with open(descriptor, 'r') as stream: return json.load(stream)
Example #8
Source File: dataset.py From spleeter with MIT License | 6 votes |
def cache(self, dataset, cache, wait): """ Cache the given dataset if cache is enabled. Eventually waits for cache to be available (useful if another process is already computing cache) if provided wait flag is True. :param dataset: Dataset to be cached if cache is required. :param cache: Path of cache directory to be used, None if no cache. :param wait: If caching is enabled, True is cache should be waited. :returns: Cached dataset if needed, original dataset otherwise. """ if cache is not None: if wait: while not exists(f'{cache}.index'): get_logger().info( 'Cache not available, wait %s', self.WAIT_PERIOD) time.sleep(self.WAIT_PERIOD) cache_path = os.path.split(cache)[0] os.makedirs(cache_path, exist_ok=True) return dataset.cache(cache) return dataset
Example #9
Source File: test_toolchain.py From calmjs with GNU General Public License v2.0 | 6 votes |
def test_toolchain_standard_not_implemented(self): spec = Spec() with self.assertRaises(NotImplementedError): self.toolchain(spec) with self.assertRaises(NotImplementedError): self.toolchain.assemble(spec) with self.assertRaises(NotImplementedError): self.toolchain.link(spec) # Check that the build_dir is set on the spec based on tempfile self.assertTrue(spec['build_dir'].startswith( realpath(tempfile.gettempdir()))) # Also that it got deleted properly. self.assertFalse(exists(spec['build_dir']))
Example #10
Source File: alarmdata.py From SecPi with GNU General Public License v3.0 | 6 votes |
def extract(self): if(hasattr(cherrypy.request, 'json')): if('dir' in cherrypy.request.json and cherrypy.request.json['dir']!='' and 'name' in cherrypy.request.json and cherrypy.request.json['name']!=''): dir = cherrypy.request.json['dir'] name = cherrypy.request.json['name'] fdir = path.join(self.datapath, dir) fp = path.join(fdir, name) if(path.exists(fp)): with zipfile.ZipFile(fp, "r") as z: z.extractall(fdir) return {'status': 'success', 'message': "File %s/%s extracted!"%(dir, name)} else: return {'status': 'error', 'message': "File doesn't exist!"} else: return {'status': 'error', 'message': "Invalid filename!"} else: return {'status': 'error', 'message': "No filename given!"}
Example #11
Source File: tools.py From delocate with BSD 2-Clause "Simplified" License | 6 votes |
def ensure_permissions(mode_flags=stat.S_IWUSR): """decorator to ensure a filename has given permissions. If changed, original permissions are restored after the decorated modification. """ def decorator(f): def modify(filename, *args, **kwargs): m = chmod_perms(filename) if exists(filename) else mode_flags if not m & mode_flags: os.chmod(filename, m | mode_flags) try: return f(filename, *args, **kwargs) finally: # restore original permissions if not m & mode_flags: os.chmod(filename, m) return modify return decorator # Open filename, checking for read permission
Example #12
Source File: test_cli.py From calmjs with GNU General Public License v2.0 | 6 votes |
def test_pkg_manager_init_working_dir(self): self.setup_requirements_json() original = mkdtemp(self) os.chdir(original) cwd = mkdtemp(self) target = join(cwd, 'requirements.json') driver = cli.PackageManagerDriver( pkg_manager_bin='mgr', pkgdef_filename='requirements.json', dep_keys=('require',), working_dir=cwd, ) driver.pkg_manager_init('calmpy.pip') self.assertFalse(exists(join(original, 'requirements.json'))) self.assertTrue(exists(target)) with open(target) as fd: result = json.load(fd) self.assertEqual(result, { "require": {"setuptools": "25.1.6"}, "name": "calmpy.pip", })
Example #13
Source File: tools.py From delocate with BSD 2-Clause "Simplified" License | 6 votes |
def find_package_dirs(root_path): """ Find python package directories in directory `root_path` Parameters ---------- root_path : str Directory to search for package subdirectories Returns ------- package_sdirs : set Set of strings where each is a subdirectory of `root_path`, containing an ``__init__.py`` file. Paths prefixed by `root_path` """ package_sdirs = set() for entry in os.listdir(root_path): fname = entry if root_path == '.' else pjoin(root_path, entry) if isdir(fname) and exists(pjoin(fname, '__init__.py')): package_sdirs.add(fname) return package_sdirs
Example #14
Source File: test_artifact.py From calmjs with GNU General Public License v2.0 | 6 votes |
def test_existing_removed(self): # force an existing file target = self.registry.records[('app', 'nothing.js')] os.mkdir(dirname(target)) with open(target, 'w'): pass with pretty_logging(stream=mocks.StringIO()) as stream: self.registry.process_package('app') log = stream.getvalue() self.assertIn( "package 'app' has declared 3 entry points for the " "'calmjs.artifacts' registry for artifact construction", log ) log = stream.getvalue() self.assertIn("removing existing export target at ", log) self.assertFalse(exists(target))
Example #15
Source File: toolchain.py From calmjs with GNU General Public License v2.0 | 6 votes |
def compile_bundle_entry(self, spec, entry): """ Handler for each entry for the bundle method of the compile process. This copies the source file or directory into the build directory. """ modname, source, target, modpath = entry bundled_modpath = {modname: modpath} bundled_target = {modname: target} export_module_name = [] if isfile(source): export_module_name.append(modname) copy_target = join(spec[BUILD_DIR], target) if not exists(dirname(copy_target)): makedirs(dirname(copy_target)) shutil.copy(source, copy_target) elif isdir(source): copy_target = join(spec[BUILD_DIR], modname) shutil.copytree(source, copy_target) return bundled_modpath, bundled_target, export_module_name
Example #16
Source File: coco.py From Collaborative-Learning-for-Weakly-Supervised-Object-Detection with MIT License | 6 votes |
def gt_roidb(self): """ Return the database of ground-truth regions of interest. This function loads/saves from/to a cache file to speed up future calls. """ cache_file = osp.join(self.cache_path, self.name + '_gt_roidb.pkl') if osp.exists(cache_file): with open(cache_file, 'rb') as fid: roidb = pickle.load(fid) print('{} gt roidb loaded from {}'.format(self.name, cache_file)) return roidb gt_roidb = [self._load_coco_annotation(index) for index in self._image_index] with open(cache_file, 'wb') as fid: pickle.dump(gt_roidb, fid, pickle.HIGHEST_PROTOCOL) print('wrote gt roidb to {}'.format(cache_file)) return gt_roidb
Example #17
Source File: coco.py From cascade-rcnn_Pytorch with MIT License | 6 votes |
def gt_roidb(self): """ Return the database of ground-truth regions of interest. This function loads/saves from/to a cache file to speed up future calls. """ cache_file = osp.join(self.cache_path, self.name + '_gt_roidb.pkl') if osp.exists(cache_file): with open(cache_file, 'rb') as fid: roidb = pickle.load(fid) print('{} gt roidb loaded from {}'.format(self.name, cache_file)) return roidb gt_roidb = [self._load_coco_annotation(index) for index in self._image_index] with open(cache_file, 'wb') as fid: pickle.dump(gt_roidb, fid, pickle.HIGHEST_PROTOCOL) print('wrote gt roidb to {}'.format(cache_file)) return gt_roidb
Example #18
Source File: venv_update.py From mealpy with MIT License | 6 votes |
def has_system_site_packages(interpreter): # TODO: unit-test system_site_packages = check_output(( interpreter, '-c', # stolen directly from virtualenv's site.py """\ import site, os.path print( 0 if os.path.exists( os.path.join(os.path.dirname(site.__file__), 'no-global-site-packages.txt') ) else 1 )""" )) system_site_packages = int(system_site_packages) assert system_site_packages in (0, 1) return bool(system_site_packages)
Example #19
Source File: test_runtime.py From calmjs with GNU General Public License v2.0 | 5 votes |
def test_prompted_execution_exists_cancel(self): stub_check_interactive(self, True) stub_stdouts(self) stub_stdin(self, u'n\n') target_dir = mkdtemp(self) export_target = join(target_dir, 'export_file') open(export_target, 'w').close() # write an empty file tc = toolchain.NullToolchain() rt = runtime.ToolchainRuntime(tc) result = rt(['--export-target', export_target]) self.assertIn( "export target '%s' already exists, overwrite? " % export_target, sys.stdout.getvalue() ) self.assertNotIn('CRITICAL', sys.stderr.getvalue()) self.assertTrue(isinstance(result, toolchain.Spec)) # prove that the cancel really happened. self.assertIn('build_dir', result) self.assertNotIn('link', result) # Should not have unexpected warnings logged. stderr = sys.stderr.getvalue() self.assertNotIn('WARNING', stderr) self.assertNotIn("spec missing key 'export_target';", stderr)
Example #20
Source File: test_artifact.py From calmjs with GNU General Public License v2.0 | 5 votes |
def test_prepare_existed_file_removed(self): basedir = utils.mkdtemp(self) export_target = join(basedir, 'export.js') with open(export_target, 'w'): pass with pretty_logging(stream=mocks.StringIO()) as s: self.assertTrue(prepare_export_location(export_target)) self.assertIn( "removing existing export target at '%s'" % export_target, s.getvalue()) self.assertFalse(exists(export_target))
Example #21
Source File: test_artifact.py From calmjs with GNU General Public License v2.0 | 5 votes |
def test_prepare_base(self): basedir = utils.mkdtemp(self) export_target = join(basedir, 'artifacts', 'export.js') with pretty_logging(stream=mocks.StringIO()) as s: self.assertTrue(prepare_export_location(export_target)) self.assertTrue(exists(join(basedir, 'artifacts'))) self.assertIn("artifacts", s.getvalue())
Example #22
Source File: toolchain.py From calmjs with GNU General Public License v2.0 | 5 votes |
def _generate_transpile_target(self, spec, target): # ensure that the target is fully normalized. bd_target = join(spec[BUILD_DIR], normpath(target)) self._validate_build_target(spec, bd_target) if not exists(dirname(bd_target)): logger.debug("creating dir '%s'", dirname(bd_target)) makedirs(dirname(bd_target)) return bd_target
Example #23
Source File: toolchain.py From calmjs with GNU General Public License v2.0 | 5 votes |
def _check_key_exists(spec, keys): for key in keys: if key not in spec: continue logger.error( "attempted to write '%s' to spec but key already exists; " "not overwriting, skipping", key ) return True return False
Example #24
Source File: indexer.py From calmjs with GNU General Public License v2.0 | 5 votes |
def resource_filename_mod_entry_point(module_name, entry_point): """ If a given package declares a namespace and also provide submodules nested at that namespace level, and for whatever reason that module is needed, Python's import mechanism will not have a path associated with that module. However, if given an entry_point, this path can be resolved through its distribution. That said, the default resource_filename function does not accept an entry_point, and so we have to chain that back together manually. """ if entry_point.dist is None: # distribution missing is typically caused by mocked entry # points from tests; silently falling back to basic lookup result = pkg_resources.resource_filename(module_name, '') else: result = resource_filename_mod_dist(module_name, entry_point.dist) if not result: logger.warning( "fail to resolve the resource path for module '%s' and " "entry_point '%s'", module_name, entry_point ) return None if not exists(result): logger.warning( "resource path resolved to be '%s' for module '%s' and " "entry_point '%s', but it does not exist", result, module_name, entry_point, ) return None return result
Example #25
Source File: utilities.py From alibuild with GNU General Public License v3.0 | 5 votes |
def readDefaults(configDir, defaults, error): defaultsFilename = "%s/defaults-%s.sh" % (configDir, defaults) if not exists(defaultsFilename): viableDefaults = ["- " + basename(x).replace("defaults-","").replace(".sh", "") for x in glob("%s/defaults-*.sh" % configDir)] error(format("Default `%(d)s' does not exists. Viable options:\n%(v)s", d=defaults or "<no defaults specified>", v="\n".join(viableDefaults))) err, defaultsMeta, defaultsBody = parseRecipe(getRecipeReader(defaultsFilename)) if err: error(err) sys.exit(1) return (defaultsMeta, defaultsBody) # Get the appropriate recipe reader depending on th filename
Example #26
Source File: coco.py From Collaborative-Learning-for-Weakly-Supervised-Object-Detection with MIT License | 5 votes |
def image_path_from_index(self, index): """ Construct an image path from the image's "index" identifier. """ # Example image path for index=119993: # images/train2014/COCO_train2014_000000119993.jpg file_name = ('COCO_' + self._data_name + '_' + str(index).zfill(12) + '.jpg') image_path = osp.join(self._data_path, 'images', self._data_name, file_name) assert osp.exists(image_path), \ 'Path does not exist: {}'.format(image_path) return image_path
Example #27
Source File: build.py From alibuild with GNU General Public License v3.0 | 5 votes |
def getDirectoryHash(d): if exists(join(d, ".git")): err, out = getstatusoutput("GIT_DIR=%s/.git git rev-parse HEAD" % d) dieOnError(err, "Impossible to find reference for %s " % d) else: err, out = getstatusoutput("pip --disable-pip-version-check show alibuild | grep -e \"^Version:\" | sed -e 's/.* //'") dieOnError(err, "Impossible to find reference for %s " % d) return out # Helper class which does not do anything to sync
Example #28
Source File: clean.py From alibuild with GNU General Public License v3.0 | 5 votes |
def decideClean(workDir, architecture, aggressiveCleanup): """ Decides what to delete, without actually doing it: - Find all the symlinks in "BUILD" - Find all the directories in "BUILD" - Schedule a directory for deletion if it does not have a symlink """ symlinksBuild = [os.readlink(x) for x in glob.glob("%s/BUILD/*-latest*" % workDir)] # $WORK_DIR/TMP should always be cleaned up. This does not happen only # in the case we run out of space while unpacking. # $WORK_DIR/<architecture>/store can be cleaned up as well, because # we do not need the actual tarballs after they have been built. toDelete = ["%s/TMP" % workDir] if aggressiveCleanup: toDelete += ["%s/TARS/%s/store" % (workDir, architecture), "%s/SOURCES" % (workDir)] allBuildStuff = glob.glob("%s/BUILD/*" % workDir) toDelete += [x for x in allBuildStuff if not path.islink(x) and not basename(x) in symlinksBuild] installGlob ="%s/%s/*/" % (workDir, architecture) installedPackages = set([dirname(x) for x in glob.glob(installGlob)]) symlinksInstall = [] for x in installedPackages: symlinksInstall += [path.realpath(y) for y in glob.glob(x + "/latest*")] toDelete += [x for x in glob.glob(installGlob+ "*") if not path.islink(x) and not path.realpath(x) in symlinksInstall] toDelete = [x for x in toDelete if path.exists(x)] return toDelete
Example #29
Source File: test_testing.py From calmjs with GNU General Public License v2.0 | 5 votes |
def cleanup(self): for p in self.dirs: if exists(p): rmtree(p)
Example #30
Source File: test_artifact.py From calmjs with GNU General Public License v2.0 | 5 votes |
def test_update_artifact_metadata(self): # inject dummy module and add cleanup mod = ModuleType('calmjs_testing_dummy') mod.complete = generic_builder self.addCleanup(sys.modules.pop, 'calmjs_testing_dummy') sys.modules['calmjs_testing_dummy'] = mod working_dir = utils.mkdtemp(self) utils.make_dummy_dist(self, ( ('requires.txt', '\n'.join([ 'calmjs', ])), ('entry_points.txt', '\n'.join([ '[calmjs.artifacts]', 'artifact.js = calmjs_testing_dummy:complete', ])), ), 'app', '1.0', working_dir=working_dir) # mock a version of calmjs within that environment too utils.make_dummy_dist(self, ( ('entry_points.txt', ''), ), 'calmjs', '1.0', working_dir=working_dir) mock_ws = WorkingSet([working_dir]) registry = ArtifactRegistry('calmjs.artifacts', _working_set=mock_ws) registry.update_artifact_metadata('app', {}) self.assertTrue(exists(registry.metadata.get('app'))) with pretty_logging(stream=mocks.StringIO()) as s: registry.update_artifact_metadata('calmjs', {}) self.assertIn( "package 'calmjs' has not declare any artifacts", s.getvalue())