示例#1
0
def install_python_version():
    if env.launched():
        return

    if not env.PYTHON_VERSION:
        env.set_python_version('.'.join(sys.version_info))
        print(ansi.warning() + ' %s does not exist. Creating with %s' %
              (env.VERSION_PATH, env.PYTHON_VERSION))
        with open(env.VERSION_PATH, 'w', encoding='utf-8') as f:
            f.write(env.PYTHON_VERSION + os.linesep)

    if platform.system() == 'Windows':
        print(ansi.warning() + ' Lore only uses the installed python version on Windows.')
    else:
        if not env.PYENV:
            sys.exit(
                ansi.error() + ' pyenv is not installed. Lore is broken. try:\n'
                               ' $ pip uninstall lore && pip install lore\n'
            )

        versions = subprocess.check_output(
            (env.BIN_PYENV, 'versions', '--bare')
        ).decode('utf-8').split(os.linesep)
        if env.PYTHON_VERSION not in versions:
            print(ansi.success('INSTALL') + ' python %s' % env.PYTHON_VERSION)
            if platform.system() == 'Darwin':
                install_xcode()
            subprocess.check_call(('git', '-C', env.PYENV, 'pull'))
            subprocess.check_call((env.BIN_PYENV, 'install', env.PYTHON_VERSION))
            subprocess.check_call((env.BIN_PYENV, 'rehash'))
示例#2
0
def check_requirements():
    if not os.path.exists(requirements):
        sys.exit(
            ansi.error() + ' %s is missing. Please check it in.' % ansi.underline(requirements)
        )
    
    with open(requirements, 'r', encoding='utf-8') as f:
        dependencies = f.readlines()
    
    vcs = [d for d in dependencies if re.match(r'^(-e )?(git|svn|hg|bzr).*', d)]
    
    dependencies = list(set(dependencies) - set(vcs))
    
    missing = []
    try:
        pkg_resources.require(dependencies)
    except (pkg_resources.ContextualVersionConflict, DistributionNotFound, VersionConflict) as error:
        missing.append(str(error))
    except pkg_resources.RequirementParseError:
        pass
    
    if missing:
        missing = ' missing requirement:\n  ' + os.linesep.join(missing)
        if '--env-checked' in sys.argv:
            sys.exit(ansi.error() + missing + '\nRequirement installation failure, please check for errors in:\n $ lore install\n')
        else:
            print(ansi.warning() + missing)
            import lore.__main__
            lore.__main__.install_requirements(None)
            reboot('--env-checked')
    else:
        return True
示例#3
0
def install_python_version():
    if env.launched():
        return

    if not env.python_version:
        env.set_python_version('.'.join(sys.version_info))
        print(ansi.warning() + ' %s does not exist. Creating with %s' %
              (env.version_path, env.python_version))
        with open(env.version_path, 'w', encoding='utf-8') as f:
            f.write(env.python_version + os.linesep)

    if not env.pyenv:
        sys.exit(ansi.error() +
                 ' pyenv is not installed. Lore is broken. try:\n'
                 ' $ pip uninstall lore && pip install lore\n')

    versions = subprocess.check_output(
        (env.bin_pyenv, 'versions',
         '--bare')).decode('utf-8').split(os.linesep)
    if env.python_version not in versions:
        print(ansi.success('INSTALL') + ' python %s' % env.python_version)
        if platform.system() == 'Darwin':
            install_xcode()
        subprocess.check_call(('git', '-C', env.pyenv, 'pull'))
        subprocess.check_call((env.bin_pyenv, 'install', env.python_version))
        subprocess.check_call((env.bin_pyenv, 'rehash'))
示例#4
0
文件: __main__.py 项目: tspannhw/lore
def install_pyenv():
    virtualenv = os.path.join(env.PYENV, 'plugins', 'pyenv-virtualenv')
    if os.path.exists(env.BIN_PYENV) and os.path.exists(virtualenv):
        return

    if os.path.exists(env.PYENV) and not os.path.isfile(env.BIN_PYENV):
        print(ansi.warning() +
              ' pyenv executable is not present at %s' % env.BIN_PYENV)
        while True:
            answer = input(
                'Would you like to blow away ~/.pyenv and rebuild from scratch? [Y/n] '
            )
            if answer in ['', 'y', 'Y']:
                shutil.rmtree(lore.env.PYENV)
                break
            elif answer in ['n', 'N']:
                sys.exit(ansi.error() + ' please fix pyenv before continuing')
            else:
                print('please enter Y or N')

    if not os.path.exists(env.PYENV):
        print(ansi.success('INSTALLING') + ' pyenv')
        subprocess.check_call(
            ('git', 'clone', 'https://github.com/pyenv/pyenv.git', env.PYENV))
    else:
        print(ansi.success('CHECK') + ' existing pyenv installation')
    env.set_python_version(env.PYTHON_VERSION)

    if not os.path.exists(virtualenv):
        print(ansi.success('INSTALLING') + ' pyenv virtualenv')
        subprocess.check_call(
            ('git', 'clone', 'https://github.com/pyenv/pyenv-virtualenv.git',
             virtualenv))
    else:
        print(ansi.success('CHECK') + ' existing virtualenv installation')
示例#5
0
def launch():
    if launched():
        check_version()
        os.chdir(root)
        return
    
    if not os.path.exists(bin_lore):
        missing = ' %s virtualenv is missing.' % project
        if '--launched' in sys.argv:
            sys.exit(ansi.error() + missing + ' Please check for errors during:\n $ lore install\n')
        else:
            print(ansi.warning() + missing)
            import lore.__main__
            lore.__main__.install(None, None)
        
    reboot('--env-launched')
示例#6
0
def install_pyenv():
    home = os.environ.get('HOME')
    if not home:
        return
    
    pyenv = os.path.join(home, '.pyenv')
    bin_pyenv = os.path.join(pyenv, 'bin', 'pyenv')
    virtualenv = os.path.join(pyenv, 'plugins', 'pyenv-virtualenv')
    if os.path.exists(bin_pyenv) and os.path.exists(virtualenv):
        return
    
    if os.path.exists(pyenv) and not os.path.isfile(bin_pyenv):
        print(ansi.warning() + ' pyenv executable is not present at %s' % bin_pyenv)
        while True:
            answer = input('Would you like to blow away ~/.pyenv and rebuild from scratch? [Y/n] ')
            if answer in ['', 'y', 'Y']:
                shutil.rmtree(pyenv)
                break
            elif answer in ['n', 'N']:
                sys.exit(ansi.error() + ' please fix pyenv before continuing')
            else:
                print('please enter Y or N')
    
    if not os.path.exists(pyenv):
        print(ansi.success('INSTALLING') + ' pyenv')
        subprocess.check_call((
            'git',
            'clone',
            'https://github.com/pyenv/pyenv.git',
            pyenv
        ))
    else:
        print(ansi.success('CHECK') + ' existing pyenv installation')
    env.pyenv = pyenv
    env.bin_pyenv = bin_pyenv
    env.set_python_version(env.python_version)
    
    if not os.path.exists(virtualenv):
        print(ansi.success('INSTALLING') + ' pyenv virtualenv')
        subprocess.check_call((
            'git',
            'clone',
            'https://github.com/pyenv/pyenv-virtualenv.git',
            virtualenv
        ))
    else:
        print(ansi.success('CHECK') + ' existing virtualenv installation')
示例#7
0
def launch():
    """Ensure that python is running from the Lore virtualenv past this point.
    """
    if launched():
        check_version()
        os.chdir(ROOT)
        return

    if not os.path.exists(BIN_LORE):
        missing = ' %s virtualenv is missing.' % APP
        if '--launched' in sys.argv:
            sys.exit(ansi.error() + missing + ' Please check for errors during:\n $ lore install\n')
        else:
            print(ansi.warning() + missing)
            import lore.__main__
            lore.__main__.install(None, None)

    reboot('--env-launched')
示例#8
0
def install(parsed, unknown):
    env.validate()
    if platform.system() == 'Darwin':
        install_darwin()
    elif platform.system() == 'Linux':
        install_linux()
    elif platform.system() == 'Windows':
        print(ansi.warning() + ' pyenv does not '
              'support Windows. Creating virtual env with installed python.')
    else:
        raise KeyError('unknown system: ' % platform.system())

    install_python_version()
    create_virtual_env()
    install_requirements(parsed)

    if hasattr(parsed, 'native') and parsed.native:
        install_tensorflow()
示例#9
0
def install(parsed, unknown):
    env.validate()
    if platform.system() == 'Darwin':
        install_darwin()
    elif platform.system() == 'Linux':
        install_linux()
    elif platform.system() == 'Windows':
        print(ansi.warning() + ' pyenv does not '
              'support Windows. You\'ll have the task of managing'
              ' python versions yourself 😰')
        return
    else:
        raise KeyError('unknown system: ' % platform.system())

    install_python_version()
    create_virtual_env()
    install_requirements(parsed)

    if hasattr(parsed, 'native') and parsed.native:
        install_tensorflow()
示例#10
0
def freeze_requirements():
    source = env.requirements

    print(
        ansi.success('EXECUTE') + ' ' + env.bin_python + ' -m pip freeze -r ' +
        source)
    vcs = split_vcs_lines()
    pip = subprocess.Popen(
        (env.bin_python, '-m', 'pip', 'freeze', '-r', source),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE)
    (stdout, stderr) = pip.communicate()
    pip.wait()

    restore_vcs_lines(vcs)

    present = stdout.decode('utf-8').split(os.linesep)
    errors = stderr.decode('utf-8').split(os.linesep)
    missing = [line for line in errors if 'package is not installed' in line]
    regex = re.compile(r'contains ([\w\-\_]+)')
    needed = [
        m.group(1).lower() for l in missing for m in [regex.search(l)] if m
    ]

    added_index = present.index(
        '## The following requirements were added by pip freeze:')
    unsafe = None
    if added_index:
        added = present[added_index + 1:-1]
        present = set(present[0:added_index])
        safe = set()
        unsafe = set()
        for package in added:
            name = package.split('==')[0]

            for bad in vcs:
                if name in bad:
                    unsafe.add(package)
                    continue

            if name.lower() in needed:
                needed.remove(name.lower())

            safe.add(package)
        present |= safe
        present -= unsafe

    if needed:
        args = [env.bin_python, '-m', 'pip', 'install'] + needed
        print(ansi.success('EXECUTE ') + ' '.join(args))
        subprocess.check_call(args)
        return freeze_requirements()

    if unsafe:
        if vcs:
            print(ansi.warning() +
                  ' Non pypi packages were detected in your ' +
                  ansi.underline('requirements.txt') + ' that can not be '
                  'completely frozen by pip. ' + os.linesep + os.linesep +
                  os.linesep.join(vcs))

        print(ansi.info() + ' You should check the following packages in to ' +
              ansi.underline('requirements.txt') +
              ' or `lore pip uninstall` them')
        if vcs:
            print(
                ansi.warning('unless') +
                ' they are covered by the previously listed packages'
                ' that pip can not freeze.')
        print(os.linesep + os.linesep.join(unsafe) + os.linesep)

    with open(source, 'w', encoding='utf-8') as f:
        f.write(
            os.linesep.join(sorted(present, key=lambda s: s.lower())).strip() +
            os.linesep)
        if vcs:
            f.write(os.linesep.join(vcs) + os.linesep)