Skip to main content
 首页 » 编程设计

python之Git + Python Virtualenvs : unique directory across repositories

2024年11月01日14kuangbin

我有 2 个存储库通过一个公共(public)服务器同步。

Repository 1 -- Server -- Repository 2 

在每个存储库中,都有一个名为“unique”的目录,可以保存有关在 virtualenv 中使用的数据库设置的数据。

我想要实现的是在不干扰 python 代码工作流的情况下,为每个 virtualenv 单独保留“唯一”目录的内容。

非常感谢您的帮助。

请您参考如下方法:

编辑:接下来是一种幼稚的“第一种方法”,它无法作为可行的解决方案进行验证。

这个问题要复杂得多,我会尝试改进我的方法,但这不是当前的优先事项。


我要解决这个问题的方法是将“唯一”文件或目录软链接(soft link)到 .local 文件夹中的文件或目录。

这个文件夹的结构必须第一次创建 - 在创建其他分支之前在 master 分支 - 使用以下命令:

cd ./$(git rev-parse --show-cdup) && mkdir -p .local/$(echo "$USER")/$(git symbolic-ref --short -q HEAD) 

此命令将在存储库的根目录中创建树:

.local/<username>/<git branch>. 

存储库的“唯一”文件必须移动到此树下的相关目录并且必须是软链接(soft link)。

例如:

唯一文件

./path/to/file/<my_file>  

将被手动移动到:

./.local/<username>/<git branch>/path/to/file/<my_file> 

而符号链接(symbolic link)将在链接到新位置的旧位置手动创建。

"./path/to/file/<my_file>" -> "./.local/<username>/<git branch>/path/to/file/<my_file>" 

符号链接(symbolic link)也可以添加到 .gitignore 文件中:

path/to/file/<my_file> 

如果其他分支已经存在,则必须 check out master 分支中的 .local 文件夹,并且必须为当前分支手动重复上述过程。

从现在开始,两个脚本称为 post-merge 和 post-checkout:

.git/hooks/post-merge 
.git/hooks/post-checkout 

如果权限设置为 755,将有望在每次 merge 后更新符号链接(symbolic link)。

在第一次 pull/merge 期间,它将创建一个本地环境,其中将复制原始存储库中的所有“唯一”文件并进行符号链接(symbolic link)。

用户必须相应地删除或安排 .local 子文件夹,然后链接将在需要时通过主要使用 sed 的 bash 编写的脚本自动更新。

由于时间限制,我没有对脚本进行全面测试。它似乎在我的 fedora 19 环境中工作,如果必须进行更改,我会更新帖子。

我仍然非常欢迎一个更面向 git 的解决方案。

merge 后:

#! /bin/sh 
 
# "origin": Refers to the existing merged files (soft links) information that will be overriden. 
# "local": Refers to the local system information that will replace origin information. 
 
#Get local user name to define the repository directory. 
local_user=$(echo "$USER") 
 
#Get local branch name to define the branch directory. 
local_branch=$(git symbolic-ref --short -q HEAD) 
 
#Go to the root of the repository 
cd ./$(git rev-parse --show-cdup) 
 
#Create local environment 
local_environment=$(echo './.local/'"$local_user"'/'"$local_branch"'/') 
mkdir -p $local_environment 
 
 
#Grub and manipulate all soft links. 
symlink_no=0 
for link in $(find -L ./ -xtype l);  
do 
    symlink_no=$[symlink_no+1] 
    # Grub information from each symlink 
    source_file=$(ls -la $link | sed -n 's/^.*[0-9]\+:[0-9]\+\s\(.*\)\s->.*$/\1/p') 
    origin_target_file=$(ls -la $link | sed -n 's/.*>\s\+\(.*\)$/\1/p') 
    relative_to_root_origin_target_file=$(ls -la $link | sed -n 's/.*>\s.*\(.\/.local.*\)$/\1/p') 
 
    find_origin_user='s/.*local\/\([a-zA-Z0-9._-]\+\).*/\1/p' 
    origin_user=$(echo $origin_target_file | sed -n "$find_origin_user") 
    find_origin_branch='s/.*local\/'"$origin_user"'\/\([a-zA-Z0-9._-]\+\).*/\1/p'  
    origin_branch=$(echo $origin_target_file | sed -n "$find_origin_branch") 
 
    local_target_file=$(echo $origin_target_file | sed -n 's/local\/'"$origin_user"'\/'"$origin_branch"'/local\/'"$local_user"'\/'"$local_branch"'/p') 
    relative_to_root_local_target_file=$(echo $relative_to_root_origin_target_file | sed -n 's/local\/'"$origin_user"'\/'"$origin_branch"'/local\/'"$local_user"'\/'"$local_branch"'/p') 
 
    remove_file_from_target_path='s/\(.*\)\/.*$/\1/p' 
    find_local_target_layout=$(echo $relative_to_root_origin_target_file | sed -n "$remove_file_from_target_path" | sed -n 's/.*'"$origin_branch"'\/\(.*$\)/\1/p') 
    local_environment_path=$(echo "$local_environment""$find_local_target_layout") 
 
    #Wording header. 
    echo '' 
    echo '' 
    echo 'Checking for symlink [' "$symlink_no"' ]:' "$source_file" 
    echo '------------------------------------------------------' 
    echo '' 
 
    # ------------------- Target File Manipulation --------------------------------- 
    #Check if target file does not exist in local environment. 
    if [ ! -f "$relative_to_root_local_target_file" ];  
    then 
        #If true, verify that the source file exists and copy it. 
        if [ -f "$relative_to_root_origin_target_file" ]; 
        then 
            mkdir -p "$local_environment_path" 
            rsync -va "$relative_to_root_origin_target_file" "$relative_to_root_local_target_file" 
            echo '' 
            origin_target_file_exists=1 
            local_target_file_exists=1 
            echo 'The source file has been created: ' "$relative_to_root_local_target_file" 
        else 
            echo 'The source file does not exist: ' "$relative_to_root_origin_target_file" 
            origin_target_file_exists=0 
            local_target_file_exists=0 
        fi 
    else 
        local_target_file_exists=1 
        echo 'The local file "'"$relative_to_root_local_target_file" '"  already exists.' 
    fi 
    source_path=$(echo $source_file | sed -n "$remove_file_from_target_path") 
    source_check_target=$(echo "$source_path"'/'"$local_target_file") 
    #-------------------- Source File Manipulation --------------------------------- 
    # If target file exists: 
    if [[ $local_target_file_exists -eq 1 ]]; 
    then 
        #Check the source file if it is already linked. 
        if [ "$local_target_file" == "$origin_target_file" ]; 
        then 
            #Is it a correct link? 
            if [ -f $source_check_target ]; 
            then 
                echo 'symlink "' "$source_file" '->' "$local_target_file" '" already exists and it is correct.' 
            else 
                echo '*********** This is a broken link: ' "$source_file" 
                echo '           **********************' 
            fi 
        else 
            #Delete existing symlink 
            rm "$source_file" 
 
            #Create new symlink according to local environment 
            ln -s "$local_target_file" "$source_file" 
            echo 'symlink "' "$source_file" '->' "$local_target_file" '" has been created.' 
        fi 
    else 
        echo '*********** This is a broken link: ' "$source_file" 
        echo '            **********************' 
    fi 
done 

checkout 后:

#! /bin/sh 
 
# Start from the repository root. 
cd ./$(git rev-parse --show-cdup) 
 
# Delete .pyc files and empty directories. 
find . -name "*.pyc" -delete 
#find . -type d -empty -delete 
 
#----------------Symlink manipulation--------------------------------------- 
#Get local user name to define the repository directory. 
local_user=$(echo "$USER") 
 
#Get local branch name to define the branch directory. 
local_branch=$(git symbolic-ref --short -q HEAD) 
 
#Go to the root of the repository 
cd ./$(git rev-parse --show-cdup) 
 
#Grub and manipulate all soft links. 
symlink_no=0 
for link in $(find -L ./ -xtype l);  
do 
    symlink_no=$[symlink_no+1] 
    # Grub information from each symlink 
    source_file=$(ls -la $link | sed -n 's/^.*[0-9]\+:[0-9]\+\s\(.*\)\s->.*$/\1/p') 
    origin_target_file=$(ls -la $link | sed -n 's/.*>\s\+\(.*\)$/\1/p') 
    relative_to_root_origin_target_file=$(ls -la $link | sed -n 's/.*>\s.*\(.\/.local.*\)$/\1/p') 
 
    find_origin_user='s/.*local\/\([a-zA-Z0-9._-]\+\).*/\1/p' 
    origin_user=$(echo $origin_target_file | sed -n "$find_origin_user") 
    find_origin_branch='s/.*local\/'"$origin_user"'\/\([a-zA-Z0-9._-]\+\).*/\1/p'  
    origin_branch=$(echo $origin_target_file | sed -n "$find_origin_branch") 
 
    local_target_file=$(echo $origin_target_file | sed -n 's/local\/'"$origin_user"'\/'"$origin_branch"'/local\/'"$local_user"'\/'"$local_branch"'/p') 
    relative_to_root_local_target_file=$(echo $relative_to_root_origin_target_file | sed -n 's/local\/'"$origin_user"'\/'"$origin_branch"'/local\/'"$local_user"'\/'"$local_branch"'/p') 
 
    remove_file_from_target_path='s/\(.*\)\/.*$/\1/p' 
    find_local_target_layout=$(echo $relative_to_root_origin_target_file | sed -n "$remove_file_from_target_path" | sed -n 's/.*'"$origin_branch"'\/\(.*$\)/\1/p') 
    local_environment_path=$(echo "$local_environment""$find_local_target_layout") 
 
    source_path=$(echo $source_file | sed -n "$remove_file_from_target_path") 
    source_check_target=$(echo "$source_path"'/'"$local_target_file") 
 
    #Wording header. 
    echo '' 
    echo '' 
    echo 'Checking for symlink [' "$symlink_no"' ]:' "$source_file" 
    echo '------------------------------------------------------' 
    echo '' 
 
    # ------------------- Target File Manipulation --------------------------------- 
    #Check if target file does not exist in local environment. 
    if [ -f "$relative_to_root_local_target_file" ];  
    then 
        #Check the source file if it is already linked. 
        if [ "$local_target_file" == "$origin_target_file" ]; 
        then 
            #Is it a correct link? 
            if [ -f $source_check_target ]; 
            then 
                echo 'symlink "' "$source_file" '->' "$local_target_file" '" already exists and it is correct.' 
            else 
                echo '*********** This is a broken link: ' "$source_file" 
                echo '           **********************' 
            fi 
        else 
            #Delete existing symlink 
            rm "$source_file" 
 
            #Create new symlink according to local environment 
            ln -s "$local_target_file" "$source_file" 
            echo 'symlink "' "$source_file" '->' "$local_target_file" '" has been created.' 
        fi 
    else 
        echo 'The source file does not exist: ' "$relative_to_root_local_target_file" 
    fi 
done