def test_more_than_one_package(): """ Search for more than one package. """ result = list(search_packages_info(['Pip', 'pytest', 'Virtualenv'])) assert len(result) == 3
def find_extra_reqs(options): # 1. find files used by imports in the code (as best we can without # executing) used_modules = common.find_imported_modules(options) # 2. find which packages provide which files installed_files = {} all_pkgs = (pkg.project_name for pkg in get_installed_distributions()) for package in search_packages_info(all_pkgs): log.debug("installed package: %s (at %s)", package["name"], package["location"]) for f in package.get("files", []): path = os.path.realpath(os.path.join(package["location"], f)) installed_files[path] = package["name"] package_path = common.is_package_file(path) if package_path: # we've seen a package file so add the bare package directory # to the installed list as well as we might want to look up # a package by its directory path later installed_files[package_path] = package["name"] # 3. match imported modules against those packages used = collections.defaultdict(list) for modname, info in used_modules.items(): # probably standard library if it's not in the files list if info.filename in installed_files: used_name = canonicalize_name(installed_files[info.filename]) log.debug("used module: %s (from package %s)", modname, installed_files[info.filename]) used[used_name].append(info) else: log.debug("used module: %s (from file %s, assuming stdlib or local)", modname, info.filename) # 4. compare with requirements.txt explicit = common.find_required_modules(options) return [name for name in explicit if name not in used]
def get_package_info(name=None, start=0, end=-1): """ calls ``pip show`` to retrieve information about packages @param name name of he packages or None to get all of them in a list @param start start at package n (in list return by @see fn get_packages_list) @param end end at package n, -1 for all @return dictionary or list of dictionaries """ if name is None: res = [] packs = get_packages_list() if end == -1: end = len(packs) subp = packs[start:end] if len(subp) == 0: raise PQPipError( "no package, start={0}, end={1}, len(subp)={2}, len(packs)={3}".format(start, end, len(subp), len(packs))) for i, cp in enumerate(subp): pack = cp.project_name info = get_package_info(pack) res.append(info) if len(res) == 0 and len(subp) > 0: raise PQPipError( "empty list, unexpected, start={0}, end={1}, len(subp)={3}".format(start, end, len(subp))) return res else: res = [_ for _ in search_packages_info([name])] if len(res) != 1: raise PQPipError( "unexpected number of results {0} for {1}".format(len(res), name)) return res[0]
def main(pb_file, src_fname, idx_dir, embed_data_dir, debug_cmt, output_nodes, method, version, model_dir): if version: pkg_version = next(search_packages_info(['utensor_cgen']))["version"] print("\033[33mutensor_cgen version: {}\033[0m".format(pkg_version)) return 0 if pb_file is None: raise ValueError("No pb file given") if not os.path.exists(model_dir): os.makedirs(model_dir) # MODEL should default to pb_file if idx_dir is None: idx_dir = os.path.join("constants", _get_pb_model_name(pb_file)) if src_fname is None: src_fname = _get_pb_model_name(pb_file) + ".cpp" model_path = os.path.join(model_dir, src_fname) from .code_generator import CodeGenerator if embed_data_dir is None: embed_data_dir = os.path.join("/fs", idx_dir) generator = CodeGenerator(pb_file, idx_dir, embed_data_dir, method, debug_cmt, output_nodes) generator.generate(model_path)
def test_find_package_not_found(): """ Test trying to get info about a nonexistent package. """ result = search_packages_info(['abcd3']) assert len(list(result)) == 0
def test_search_any_case(): """ Search for a package in any case. """ result = list(search_packages_info(['PIP'])) assert len(result) == 1 assert 'pip' == result[0]['name']
def run(self, options, args): if not args: logger.warning('ERROR: Please provide a package name or names.') return ERROR query = args from pip.commands.show import search_packages_info results = search_packages_info(query) if not self.save_results(results, options.files): return ERROR return SUCCESS
def get_package_version(mixed, logger=None): def normalize_dependency_package(mixed): if isinstance(mixed, RequirementsFile): return None if isinstance(mixed, Dependency): if mixed.url: return None return mixed.name else: return mixed package_query = [normalized_package for normalized_package in (normalize_dependency_package(p) for p in as_list(mixed)) if normalized_package] pip_working_set_init() search_packages_results = search_packages_info(package_query) return dict(((result['name'].lower(), result['version']) for result in search_packages_results))
def get_installed_packages(self): final = [] get_list = ListCommand() options,args = get_list.parse_args(["--outdated"]) for package in get_installed_distributions(): name = str(package).split(" ")[0] if name == "team": continue for pkg in get_list.iter_packages_latest_infos([package], options): latest = str(pkg.latest_version) for attributes in search_packages_info([name]): result = {"name": attributes["name"], "version": attributes["version"], "latest": latest, "summary": attributes["summary"], "home-page": attributes["home-page"]} final.append(result) return final
def run(self): # Anything that can cause a failure must happen before install.run(). # If install.run() completes successfully, pip will always return that # the installation succeeded. if "win32" != sys.platform: raise Exception("Windows is the only supported platform") rootdir = os.path.dirname(os.path.realpath(__file__)) # install_requires/dependency_links doesn't work because the wheel # wouldn't be installed until install.run(self). This is a bad plan # because pywin32_postinstall can't run until the wheel is installed, # if this is after install.run(self) then we can't return failures # properly. pkgs = list(search_packages_info(["pywin32"])) if len(pkgs) > 1: raise Exception("error: multiple pywin32 detected") elif len(pkgs) == 1: pywin32 = pkgs[0] assert pywin32['name'] == 'pywin32' if pywin32['version'] != '220': raise Exception("error: incompatible version of pywin32 installed: {}".format(pywin32['version'])) else: assert len(pkgs) == 0 # wheel lifted from here http://www.lfd.uci.edu/~gohlke/pythonlibs/ wheel = os.path.join(rootdir, "pyWin32Wrapper", "pywin32-220-cp27-none-win32.whl") pip.main(['install', wheel]) postinstall = os.path.join(rootdir, "pyWin32Wrapper", "pywin32_postinstall.py") subprocess.check_call([sys.executable, postinstall, "-install"]) # python27.dll must be available to Lib/site-packages/win32/PythonService.exe # This piece of code is only going to work if python27.dll is available # in the same directory as the python binary. In the case of # virtualenv this condition is met if the python binary used to create # the venv has python27.dll in the same directory (virtualenv will copy # this python27.dll to the venv). The python used to bootstrap # preveil daemon and updater meets this criteria. src = os.path.join(os.path.dirname(os.path.realpath(sys.executable)), "python27.dll") dest = os.path.join(get_python_lib(), "win32", "python27.dll") shutil.copyfile(src, dest) install.run(self)
def find_missing_reqs(options): # 1. find files used by imports in the code (as best we can without # executing) used_modules = common.find_imported_modules(options) # 2. find which packages provide which files installed_files = {} all_pkgs = (pkg.project_name for pkg in get_installed_distributions()) for package in search_packages_info(all_pkgs): log.debug('installed package: %s (at %s)', package['name'], package['location']) for file in package['files'] or []: path = os.path.realpath(os.path.join(package['location'], file)) installed_files[path] = package['name'] package_path = common.is_package_file(path) if package_path: # we've seen a package file so add the bare package directory # to the installed list as well as we might want to look up # a package by its directory path later installed_files[package_path] = package['name'] # 3. match imported modules against those packages used = collections.defaultdict(list) for modname, info in used_modules.items(): # probably standard library if it's not in the files list if info.filename in installed_files: used_name = normalize_name(installed_files[info.filename]) log.debug('used module: %s (from package %s)', modname, installed_files[info.filename]) used[used_name].append(info) else: log.debug( 'used module: %s (from file %s, assuming stdlib or local)', modname, info.filename) # 4. compare with requirements.txt explicit = set() for requirement in parse_requirements('requirements.txt', session=PipSession()): log.debug('found requirement: %s', requirement.name) explicit.add(normalize_name(requirement.name)) return [(name, used[name]) for name in used if name not in explicit]
def translate_req_to_module_names(requirement_name): provides = set() def is_module_folder(filepath): return bool(filepath) and \ '/' not in filepath and \ '.egg-info' not in filepath and \ '.dist-info' not in filepath and \ '__pycache__' not in filepath def is_top_level_file(filepath): return bool(filepath) and \ '/' not in filepath and \ filepath.endswith('.py') for result in search_packages_info([requirement_name]): if 'files' not in result or not result['files']: # Assume that only one module is installed in this case continue # Handle modules that are installed as folders in site-packages folders = [os.path.dirname(filepath) for filepath in result['files']] folders = filter(is_module_folder, folders) provides |= set(folders) # Handle modules that are installed as .py files in site-packages top_level_files = filter(is_top_level_file, result['files']) provides |= { os.path.splitext(filename)[0] for filename in top_level_files } if provides: return provides module_name = requirement_name.split('.')[0] if module_name not in ALL_MODULES: LOGGER.warning( "Cannot find install location of '%s'; please \ install this package for more accurate name resolution", requirement_name) return provides if provides else set([requirement_name])
def get_installed_packages(self): # type: () -> List[Dict[str, str]] try: get_list = ListCommand() except TypeError: get_list = ListCommand("Pippel", "Backend server for the Pippel service.") options, args = get_list.parse_args(["--outdated"]) packages = [ package for package in get_installed_distributions() if package.key != "team" ] final = [ { "name": attributes.get("name"), "version": attributes.get("version"), "latest": str(getattr(package, "latest_version")), "summary": attributes.get("summary"), "home-page": attributes.get("home-page") } for package in get_list.iter_packages_latest_infos( packages, options) for attributes in search_packages_info([package.key]) # noqa ] # TODO: To profile performance speed of snippet above and below. # final = [ # {"name": attributes.get("name"), # "version": attributes.get("version"), # "latest": str(getattr(latest_info, "latest_version")), # "summary": attributes.get("summary"), # "home-page": attributes.get("home-page") # } # for latest_info, attributes in zip( # [get_list.iter_packages_latest_infos(packages, options), # search_packages_info([getattr(package, "key") # for package in packages])] # ) # ] return final
def run(self, options, args): if not args: sys.stdout.write('ERROR: Please provide a package name or names.\n') return ERROR if not os.getenv('EDITOR'): sys.stdout.write( 'ERROR: Please set $EDITOR to open the package.\n') return ERROR query = args shell_command = os.getenv('EDITOR').split() results = list(search_packages_info(query)) installed = dict( [(p.project_name.lower(), p) for p in pkg_resources.working_set]) if len(results) is 0: sys.stdout.write("ERROR: Could not find package(s).\n") return ERROR for dist in results: pkg = installed[dist['name'].lower()] names = list(pkg.get_metadata_lines('top_level.txt')) for i in range(len(names)): fullpath = os.path.join(dist['location'], names[i]) if os.path.isdir(fullpath): names[i] = fullpath elif os.path.isfile(fullpath + '.py'): names[i] = fullpath + '.py' elif os.path.isfile(fullpath + '.so'): names[i] = fullpath + '.so' elif os.path.isfile(fullpath + '.dll'): names[i] = fullpath + '.dll' elif os.path.isfile(fullpath + '.pyd'): names[i] = fullpath + '.pyd' else: return ERROR status_code = subprocess.call(shell_command + names) if status_code is not SUCCESS: return ERROR return SUCCESS