def find_from_path(path): names = set() try: dirlist = os.listdir(path) except PermissionError as err: raise PermissionMissing(path) from err for item in dirlist: item_path = os.path.abspath(os.path.join(path, item)) if os.path.isdir(item_path): if is_virtualenv(item_path): continue names |= find_from_path(item_path) elif not os.path.islink(item_path) and item_path.endswith(".py"): try: contents = encoding.read_py_file(item_path) names |= find_from_imports(contents) except encoding.CouldNotHandleEncoding as err: # TODO: this output will break output formats such as JSON warnings.warn("{0}: {1}".format(err.path, err.cause), ImportWarning) if len(names) == len(POSSIBLE_LIBRARIES): # don't continue on recursing, there's no point! break return names
def _find_paths(ignore, curpath, rootpath): files, modules, packages, directories = [], [], [], [] for filename in os.listdir(curpath): fullpath = os.path.join(curpath, filename) try: relpath = os.path.relpath(fullpath, rootpath) except ValueError: # relpath breaks on long paths in Windows continue if filename.startswith('.') and os.path.isdir(fullpath): continue if os.path.islink(fullpath): continue ignored = any([m.search(relpath) for m in ignore]) if os.path.isdir(fullpath): split_fullpath = fullpath.split(os.path.sep) # node_modules should be ignored if 'node_modules' in split_fullpath: continue # __pycache__ should also be skipped if '__pycache__' in split_fullpath: continue # is it probably a virtualenvironment? if is_virtualenv(fullpath) or '.tox' in fullpath: continue # this is a directory directories.append((relpath, ignored)) # is it also a package? initpy = os.path.join(fullpath, '__init__.py') if os.path.exists(initpy) and os.path.isfile(initpy): packages.append((relpath, ignored)) # do the same for this directory recurse = _find_paths(ignore, os.path.join(curpath, filename), rootpath) files += recurse[0] modules += recurse[1] packages += recurse[2] directories += recurse[3] else: # this is a file, is it a python module? if fullpath.endswith('.py'): modules.append((relpath, ignored)) files.append((relpath, ignored)) return files, modules, packages, directories
def test_long_path_not_a_venv(self): """ Windows doesn't allow extremely long paths. This unit test has to be run in Windows to be meaningful, though it shouldn't fail in other operating systems. """ path = [os.path.dirname(__file__), 'testdata', 'venvs'] path.extend(['long_path_not_a_venv'] * 14) path.append('long_path_not_a_venv_long_path_not_a_v') path = os.path.join(*path) self.assertFalse(is_virtualenv(path))
def _find_paths(ignore, curpath, rootpath): files, modules, packages, directories = [], [], [], [] for filename in os.listdir(curpath): fullpath = os.path.join(curpath, filename) try: relpath = os.path.relpath(fullpath, rootpath) except ValueError: # relpath breaks on long paths in Windows continue if filename.startswith('.') and os.path.isdir(fullpath): continue if os.path.islink(fullpath): continue ignored = any([m.search(relpath) for m in ignore]) if os.path.isdir(fullpath): # is it probably a virtualenvironment? if is_virtualenv(fullpath) or '.tox' in fullpath: continue # this is a directory directories.append((relpath, ignored)) # is it also a package? initpy = os.path.join(fullpath, '__init__.py') if os.path.exists(initpy) and os.path.isfile(initpy): packages.append((relpath, ignored)) # do the same for this directory recurse = _find_paths(ignore, os.path.join(curpath, filename), rootpath) files += recurse[0] modules += recurse[1] packages += recurse[2] directories += recurse[3] else: # this is a file, is it a python module? if fullpath.endswith('.py'): modules.append((relpath, ignored)) files.append((relpath, ignored)) return files, modules, packages, directories
def _find_paths(ignore, curpath, rootpath): files, modules, packages, directories = [], [], [], [] for filename in os.listdir(curpath): fullpath = os.path.join(curpath, filename) relpath = os.path.relpath(fullpath, rootpath) if filename.startswith('.') and os.path.isdir(fullpath): continue if os.path.islink(fullpath): continue ignored = any([m.search(relpath) for m in ignore]) if os.path.isdir(fullpath): # is it probably a virtualenvironment? if is_virtualenv(fullpath): continue # this is a directory directories.append((relpath, ignored)) # is it also a package? initpy = os.path.join(fullpath, '__init__.py') if os.path.exists(initpy) and os.path.isfile(initpy): packages.append((relpath, ignored)) # do the same for this directory recurse = _find_paths(ignore, os.path.join(curpath, filename), rootpath) files += recurse[0] modules += recurse[1] packages += recurse[2] directories += recurse[3] else: # this is a file, is it a python module? if fullpath.endswith('.py'): modules.append((relpath, ignored)) files.append((relpath, ignored)) return files, modules, packages, directories
def find_from_path(path): names = set() max_possible = len(POSSIBLE_LIBRARIES) for item in os.listdir(path): item_path = os.path.abspath(os.path.join(path, item)) if os.path.isdir(item_path): if is_virtualenv(item_path): continue names |= find_from_path(item_path) elif not os.path.islink(item_path) and item_path.endswith('.py'): try: contents = encoding.read_py_file(item_path) names |= find_from_imports(contents) except encoding.CouldNotHandleEncoding as err: # TODO: this output will break output formats such as JSON warnings.warn('{0}: {1}'.format(err.path, err.cause), ImportWarning) if len(names) == max_possible: # don't continue on recursing, there's no point! break return names
def test_not_a_venv(self): path = os.path.join(os.path.dirname(__file__), 'testdata', 'venvs', 'not_a_venv') self.assertFalse(is_virtualenv(path))
def test_not_a_venv(self): path = os.path.join(os.path.dirname(__file__), "testdata", "venvs", "not_a_venv") self.assertFalse(is_virtualenv(path))
def test_not_a_venv(self): path = os.path.join(os.path.dirname(__file__), 'testdata', 'venvs', 'not_a_venv') self.assertFalse(is_virtualenv(path))