\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/cloudlinux/venv/lib64/python3.11/site-packages/clselect/clselectruby/

Viewing File: rubyvenv.py

# -*- coding: utf-8 -*-

# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2019 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT

from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from distutils import spawn
import getopt
import os
import re
import sys

from clselect.utils import mkdir_p


ACTIVATE_PATTERN = r'''deactivate () {
    if [ -n "$_OLD_VIRTUAL_PATH" ] ; then
        PATH="$_OLD_VIRTUAL_PATH"
        export PATH
        unset _OLD_VIRTUAL_PATH
    fi
    if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then
        hash -r 2>/dev/null
    fi
    if [ -n "$_OLD_VIRTUAL_PS1" ] ; then
        PS1="$_OLD_VIRTUAL_PS1"
        export PS1
        unset _OLD_VIRTUAL_PS1
    fi
    unset VIRTUAL_ENV
    unset RUBYHOME
    unset GEM_HOME
    unset GEM_PATH
    unset GEM_SPEC_CACHE
    if [ ! "$1" = "nondestructive" ] ; then
        unset -f deactivate
    fi
}
deactivate nondestructive
VIRTUAL_ENV=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
RUBYHOME="%s"
# set home directory if it is not for some reason
# (e.g. cagefs_enter in selectorctl)
# othewise recent ruby2.7 version fails to install gem
# with no valid reason in output (like "nil argument, exit")
if [[ "${HOME}" == "" ]]; then
    HOME="$(echo ~)"
    export HOME
fi
ORIG_GEM_HOME=$("$RUBYHOME/bin/gem" env --config-file "" GEM_HOME)
GEM_HOME="$VIRTUAL_ENV"
GEM_PATH="$VIRTUAL_ENV:$ORIG_GEM_HOME"
GEM_SPEC_CACHE="$GEM_HOME/specs"
export VIRTUAL_ENV
export RUBYHOME
export GEM_HOME
export GEM_PATH
export GEM_SPEC_CACHE
_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/bin:$RUBYHOME/bin:$PATH"
export PATH
if [ -z "$VIRTUAL_ENV_DISABLE_PROMPT" ] ; then
    _OLD_VIRTUAL_PS1="$PS1"
    if [ "x%s" != x ] ; then
        PS1="%s$PS1"
    else
    if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then
        PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1"
    else
        PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1"
    fi
    fi
    export PS1
fi
if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then
    hash -r 2>/dev/null
fi
'''

WRAP_LIST = [
    ['ruby', []],
    ['gem', ['--config-file', '"$VIRTUAL_ENV/gemrc"']],
    ]

WRAPPER_PATTERN = r'''#!/bin/bash
PWD=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
source "$PWD/activate"
exec "$RUBYHOME/bin/%s" "$@"%s
'''


def create(destination, prompt='', ruby=None):
    destination = os.path.expanduser(destination)

    if not ruby:
        ruby = spawn.find_executable('ruby')

    if not os.path.isabs(ruby):
        ruby = os.path.realpath(ruby)

    ruby_home = os.path.dirname(os.path.dirname(ruby))
    basename = os.path.basename(ruby)
    version = re.match('ruby(?P<version>.*)', basename).groupdict()['version']
    venv_bin = os.path.join(destination, 'bin')

    mkdir_p(venv_bin)

    for binary, args in WRAP_LIST:
        bin_name = binary + version
        bin_abs = os.path.join(ruby_home, 'bin', bin_name)
        if not os.path.isfile(bin_abs):
            raise OSError(
                "%s binary does not exist '%s'" % (bin_name, ruby))
        wrapper_abs = os.path.join(venv_bin, binary)
        if args:
            args_str = str.join(' ', [''] + args)
        else:
            args_str = ''
        wrapper_file = open(wrapper_abs, 'w')
        wrapper_file.write(WRAPPER_PATTERN % (bin_name, args_str))
        wrapper_file.close()
        os.chmod(wrapper_abs, 0o755)

    activate_abs = os.path.join(venv_bin, 'activate')
    activate_file = open(activate_abs, 'w')
    activate_file.write(ACTIVATE_PATTERN % (ruby_home, prompt, prompt))
    activate_file.close()


def main():
    # DiD (F-04, CLOS-5518): every shipped caller invokes rubyvenv.py through
    # commons/lib/clselect/clselectruby/environments.py:97's
    # check_call(..., preexec_fn=self._demote()), which does os.setgid()+
    # os.setuid() — a permanent drop — so create() and its open()/os.chmod
    # sinks run under the target user's DAC identity. Refuse root here so any
    # future refactor that reaches this entry point without a caller-side
    # demote fails loud instead of writing venv files as root.
    if os.geteuid() == 0:
        # Exception text is intentionally generic; the message can surface
        # to clients. Rationale for the refusal is in the DiD comment
        # above.
        print('Operation not permitted.', file=sys.stderr)
        sys.exit(2)

    try:
        opts, args = getopt.getopt(sys.argv[1:], 'p:r:', ['prompt=', 'ruby='])
    except getopt.GetoptError as err:
        print(str(err))
        usage()
        sys.exit(1)

    prompt = ''
    ruby = None

    if not args:
        print('no destination specified')
        usage()
        sys.exit(1)

    destination = args[0]

    for o, a in opts:
        if o in ('-p', '--prompt'):
            prompt = a
        elif o in ('-r', '--ruby'):
            ruby = a
        else:
            print('unhandled option')
            sys.exit(1)

    create(destination, prompt, ruby)


def usage():
    print('Usage: ' + sys.argv[0] + ' [OPTIONS] VENV')
    print()
    print('Options:')
    print(' -p, --prompt PROMPT   Set alternative prompt prefix for venv')
    print(' -r, --ruby PATH       Ruby interpreter to use to create new venv')
    print(' -h, --help            Show this message')


if __name__ == '__main__':
    main()