def perform_init(self): default_url = 'http://example.com' try: default_url = subprocess.check_output(['git', 'config', 'remote.origin.url']).decode().strip() if 'git@' in default_url: default_url = default_url.replace(':', '/') default_url = default_url.replace('git@', 'https://') if default_url.endswith('.git'): default_url = default_url[:-4] except (subprocess.CalledProcessError, FileNotFoundError) as e: pass default_author = 'ACME Inc.' default_email = '*****@*****.**' try: default_author = subprocess.check_output(['git', 'config', 'user.name']).decode().strip() default_email = subprocess.check_output(['git', 'config', 'user.email']).decode().strip() except (subprocess.CalledProcessError, FileNotFoundError) as e: pass vars = {} for key, prompt, default in ( ('name', 'PyPI project name', os.path.basename(os.getcwd())), ('version', 'Version', '1.0.0'), ('description', 'Short description', 'FooBar Enhanced Edition'), ('url', 'Project URL', default_url), ('author', 'Author', default_author), ('author_email', 'E-mail', default_email), ('package', 'Root Python package name', os.path.basename(os.getcwd()).replace('-', '_')), ): vars[key] = ui.prompt(prompt, default=default) if not os.path.exists(vars['package']): os.mkdir(vars['package']) for name, template in ( ('setup.py', templates.setup_py), ('setup.cfg', templates.setup_cfg), ('.gitignore', templates.gitignore), ('requirements.txt', ''), (os.path.join(vars['package'], '__init__.py'), templates.package_init), ): if os.path.exists(name): ui.warn(ui.bold(name), 'already exists') else: ui.info('writing', ui.bold(name)) with open(name, 'w') as f: f.write(template.format(**vars))
def remove(self, packages): for name in packages: pkg = self.graph.find(name) if pkg: yield RemoveAction(pkg) elif not self.quiet: ui.error(ui.bold(name), 'is not installed')
def create_virtualenv(self, path, interpreter): ui.info('Setting up a virtualenv in', ui.bold(path)) create_environment( path, site_packages=False, download=True, symlink=True, )
def perform_list(self): pkgs = self.load_dependency_graph() ui.info(ui.bold(str(len(pkgs))), 'packages installed') for pkg in pkgs: print(' -', ui.pkg(pkg)) for index, dep in enumerate(pkg.deps): print(' ', '├──' if (index < len(pkg.deps) - 1) else '└──', end='') print(dep.name + ui.cyan(str(dep.req.specifier)), end=' ') if dep.resolved_to: print(ui.green('→'), ui.pkg(dep.resolved_to)) else: print(ui.red('→ none'))
def perform_download(self, deps, source=False): for dep in deps: candidates = self.index.candidates_for(dep, source=source) best_candidate = self.index.best_candidate_of(dep, candidates) if not best_candidate: ui.error('No packages available for', ui.dep(dep)) sys.exit(1) url = best_candidate.location.url name = os.path.basename(urlparse(url).path) ui.info('Downloading', ui.bold(name)) urlretrieve(url, name)
def perform_check(self, silent=False): pkgs = self.load_dependency_graph() problem_counter = 0 extraneous_counter = 0 for pkg in pkgs: if pkg.name in PackageGraph.SYSTEM_PKGS: continue if len(pkg.incoming_mismatched) > 0: if not silent: print(' -', ui.pkg(pkg), '(installed)') for index, dep in enumerate(pkg.incoming + pkg.incoming_mismatched): print(' ', '├──' if (index < len(pkg.incoming + pkg.incoming_mismatched) - 1) else '└──', end='') print( ui.bold(ui.green('[ok]') if dep in pkg.incoming else ui.yellow('[mismatch]')), ui.dep(dep, name=False), 'required by', ui.pkg(dep.parent, version=False) ) print() problem_counter += 1 if len(pkg.incoming + pkg.incoming_mismatched) == 0: extraneous_counter += 1 if not silent: print(' -', ui.pkg(pkg), '(installed)') print(' └──', ui.bold(ui.yellow('extraneous'))) print() if problem_counter: ui.warn(problem_counter, 'dependency problems found') if silent: ui.warn('Run', ui.bold('grip check'), 'for more information') elif not silent and not extraneous_counter: ui.info('No problems found')
def perform_outdated(self): pkgs = self.load_dependency_graph() if self.requirements: deps = pkgs.requirements.deps else: deps = [Dependency(Requirement(x.name)) for x in pkgs] def process_single(dep): # TODO pip.utils.logging._log_state.indentation = 0 installed = pkgs.find(dep.name) if not installed: return candidates = self.index.candidates_for(dep) best_candidate = self.index.best_candidate_of(dep, candidates) best_release = self.index.best_candidate_of(None, candidates) if best_release and (not installed or best_release.version > installed.version): return ( installed, best_candidate.version if best_candidate else None, best_release.version if best_release else None, ) import click with multiprocessing.pool.ThreadPool(processes=16) as pool: with click.progressbar(pool.imap_unordered(process_single, deps), length=len(deps), label='Checking latest versions') as bar: results = [x for x in bar if x] rows = [] for installed, best_candidate, best_release in sorted(results, key=lambda x: x[0]): rows.append(( ui.pkg(installed), ui.cyan(best_candidate) if best_candidate and best_candidate > installed.version else ui.darkwhite(best_candidate), ui.green(best_release) if best_release and best_release > installed.version else ui.darkwhite(best_release), )) if len(rows): ui.table(['Installed', 'Available', 'Latest'], rows) ui.info(ui.bold(str(len(results))), 'outdated packages')
def perform_freeze(self): pkgs = self.load_dependency_graph() for pkg in pkgs: print(ui.bold(pkg.name) + ui.cyan('==' + str(pkg.version)))