Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def ensure_pipfile(validate=True, skip_requirements=False, system=False):
"""Creates a Pipfile for the project, if it doesn't exist."""
# Assert Pipfile exists.
python = which('python') if not (USING_DEFAULT_PYTHON or system) else None
if project.pipfile_is_empty:
# Show an error message and exit if system is passed and no pipfile exists
if system and not PIPENV_VIRTUALENV:
click.echo(
'{0}: --system is intended to be used for pre-existing Pipfile '
'installation, not installation of specific packages. Aborting.'.format(
crayons.red('Warning', bold=True)
),
err=True,
)
sys.exit(1)
# If there's a requirements file, but no Pipfile...
if project.requirements_exists and not skip_requirements:
click.echo(
crayons.normal(
u'requirements.txt found, instead of Pipfile! Converting...',
bold=True,
)
)
# Create a Pipfile...
def abort():
click.echo(
'You can specify specific versions of Python with:\n {0}'.format(
crayons.red(
'$ pipenv --python {0}'.format(
os.sep.join(('path', 'to', 'python'))
)
)
),
err=True,
)
sys.exit(1)
'--pre' if pre else '',
'--verbose' if verbose else '',
'--clear' if clear else '',
'--system' if allow_global else '',
)
with temp_environ():
os.environ['PIPENV_PACKAGES'] = '\n'.join(deps)
if pypi_mirror:
os.environ['PIPENV_PYPI_MIRROR'] = str(pypi_mirror)
c = delegator.run(cmd, block=True)
try:
assert c.return_code == 0
except AssertionError:
if verbose:
click.echo(c.out, err=True)
click.echo(c.err, err=True)
else:
click.echo(c.err[int(len(c.err) / 2) - 1:], err=True)
sys.exit(c.return_code)
if verbose:
click.echo(c.out.split('RESULTS:')[0], err=True)
try:
return json.loads(c.out.split('RESULTS:')[1].strip())
except IndexError:
raise RuntimeError('There was a problem with locking.')
three=None,
python=None,
bare=False,
dont_upgrade=False,
user=False,
verbose=False,
clear=False,
unused=False,
sequential=False,
pypi_mirror=None,
system=False,
deploy=False,
):
# The lock file needs to exist because sync won't write to it.
if not project.lockfile_exists:
click.echo(
'{0}: Pipfile.lock is missing! You need to run {1} first.'.format(
crayons.red('Error', bold=True),
crayons.red('$ pipenv lock', bold=True),
),
err=True,
)
sys.exit(1)
# Ensure that virtualenv is available if not system.
ensure_project(three=three, python=python, validate=False, deploy=deploy)
# Install everything.
requirements_dir = TemporaryDirectory(
suffix='-requirements', prefix='pipenv-'
)
do_init(
with temp_environ():
os.environ['PIPENV_PACKAGES'] = '\n'.join(deps)
if pypi_mirror:
os.environ['PIPENV_PYPI_MIRROR'] = str(pypi_mirror)
c = delegator.run(cmd, block=True)
try:
assert c.return_code == 0
except AssertionError:
if verbose:
click.echo(c.out, err=True)
click.echo(c.err, err=True)
else:
click.echo(c.err[int(len(c.err) / 2) - 1:], err=True)
sys.exit(c.return_code)
if verbose:
click.echo(c.out.split('RESULTS:')[0], err=True)
try:
return json.loads(c.out.split('RESULTS:')[1].strip())
except IndexError:
raise RuntimeError('There was a problem with locking.')
def _ensure_environment():
# Skip this on Windows...
if os.name != 'nt':
if 'LANG' not in os.environ:
click.echo(
'{0}: the environment variable {1} is not set!'
'\nWe recommend setting this in {2} (or equivalent) for '
'proper expected behavior.'.format(
crayons.red('Warning', bold=True),
crayons.normal('LANG', bold=True),
crayons.green('~/.profile'),
),
err=True,
)
def cleanup_virtualenv(bare=True):
"""Removes the virtualenv directory from the system."""
if not bare:
click.echo(crayons.red('Environment creation aborted.'))
try:
# Delete the virtualenv.
shutil.rmtree(project.virtualenv_location)
except OSError as e:
click.echo(
'{0} An error occurred while removing {1}!'.format(
crayons.red('Error: ', bold=True),
crayons.green(project.virtualenv_location),
),
err=True,
)
click.echo(crayons.blue(e), err=True)
def do_update(
package, more_packages, three, python, pypi_mirror, verbose, clear,
keep_outdated, pre, dev, bare, sequential, dry_run, outdated,
):
ensure_project(three=three, python=python, warn=True)
if not outdated:
outdated = bool(dry_run)
if outdated:
_do_outdated(pypi_mirror=pypi_mirror)
if not package:
click.echo(
'{0} {1} {2} {3}{4}'.format(
crayons.white('Running', bold=True),
crayons.red('$ pipenv lock', bold=True),
crayons.white('then', bold=True),
crayons.red('$ pipenv sync', bold=True),
crayons.white('.', bold=True),
)
)
else:
for package in ([package] + list(more_packages)):
if package not in project.all_packages:
click.echo(
'{0}: {1} was not found in your Pipfile! Aborting.'
''.format(
crayons.red('Warning', bold=True),
crayons.green(package, bold=True),
def _do_run_posix(script, command):
command_path = system_which(script.command)
if not command_path:
if project.has_script(command):
click.echo(
'{0}: the command {1} (from {2}) could not be found within {3}.'
''.format(
crayons.red('Error', bold=True),
crayons.red(script.command),
crayons.normal(command, bold=True),
crayons.normal('PATH', bold=True),
),
err=True,
)
else:
click.echo(
'{0}: the command {1} could not be found within {2} or Pipfile\'s {3}.'
''.format(
crayons.red('Error', bold=True),
crayons.red(command),
crayons.normal('PATH', bold=True),
actually_installed.append(dep)
if not bare:
click.echo(
u'Found {0} installed package(s), purging...'.format(
len(actually_installed)
)
)
command = '{0} uninstall {1} -y'.format(
escape_grouped_arguments(which_pip(allow_global=allow_global)),
' '.join(actually_installed),
)
if verbose:
click.echo('$ {0}'.format(command))
c = delegator.run(command)
if not bare:
click.echo(crayons.blue(c.out))
click.echo(crayons.green('Environment now purged and fresh!'))