• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env bash
2# Copyright (c) 2021-2025 Huawei Device Co., Ltd.
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15set -e
16
17function get_my_home()
18{
19    local user_name=$1
20    local my_home
21    my_home=$(grep "^${user_name}:" /etc/passwd | cut -d: -f6)
22    if [[ ! -e "${my_home}" ]]; then
23        my_home=/home/${user_name}
24    fi
25    echo "${my_home}"
26}
27
28DOT_VENV_PANDA=.venv-panda
29MY_USERNAME=${SUDO_USER}
30if [[ -z "${VENV_DIR}" && -n "${MY_USERNAME}" ]]; then
31    my_home=$( get_my_home "${MY_USERNAME}" )
32    VENV_DIR=${my_home}/${DOT_VENV_PANDA}
33elif [[ -z "${VENV_DIR}" && -z "${MY_USERNAME}" ]]; then
34    if [[ -n "${USER}" ]]; then
35        my_home=$( get_my_home "${USER}" )
36        VENV_DIR=${my_home}/${DOT_VENV_PANDA}
37    else
38        VENV_DIR=/root/${DOT_VENV_PANDA}
39    fi
40fi
41
42function activate_venv()
43{
44    if [[ -d "${VENV_DIR}" ]]; then
45        source "${VENV_DIR}/bin/activate"
46        echo "${VENV_DIR} activated"
47    fi
48}
49
50function deactivate_venv()
51{
52    if [[ -d "${VENV_DIR}" && -n "${VIRTUAL_ENV}" ]]; then
53        deactivate
54    fi
55}
56