简体   繁体   English

以 2 个不同的名称部署 1 个 python package

[英]Deploying 1 python package under 2 different names

I am trying to have 2 simultaneous versions of a single package on a server.我正在尝试在服务器上同时拥有单个 package 的 2 个版本。 A production and a testing one.一个生产和一个测试。 I want these 2 to be on the same git repository on 2 different branches (The testing would merge into production) however i would love to keep them in the same directory so its not needed to change any imports or paths.我希望这 2 个在 2 个不同分支上的同一个 git 存储库中(测试将合并到生产中)但是我希望将它们保留在同一个目录中,因此不需要更改任何导入或路径。

Is it possible to dynamically change the package name in setup.py, depending on the git branch?是否可以根据 git 分支动态更改 setup.py 中的 package 名称? Or is it possible to deploy them with different names using pip?或者是否可以使用 pip 以不同的名称部署它们?

EDIT: i may have found a proper solution for my problem here: Git: ignore some files during a merge (keep some files restricted to one branch) Gitattributes can be setup to ignore merging of my setup.py, ill close this question after i test it.编辑:我可能在这里找到了适合我的问题的解决方案: Git:在合并期间忽略一些文件(将一些文件限制在一个分支中) Gitattributes 可以设置为忽略我的 setup.py 的合并,我在我之后关闭这个问题测试它。

This could be done with a setup script that looks like this:这可以通过如下所示的设置脚本来完成:

#!/usr/bin/env python3

import pathlib
import setuptools

def _get_git_ref():
    ref = None
    git_head_path = pathlib.Path(__file__).parent.joinpath('.git', 'HEAD')
    with git_head_path.open('r') as git_head:
        ref = git_head.readline().split()[-1]
    return ref

def _get_project_name():
    name_map = {
        'refs/heads/master': 'ThingProd',
        'refs/heads/develop': 'ThingTest',
    }
    git_ref = _get_git_ref()
    name = name_map.get(git_ref, 'ThingUnknown')
    return name

setuptools.setup(
    # see 'setup.cfg'
    name=_get_project_name(),
)

It reads the current git ref directly from the .git/HEAD file and looks up the corresponding name in a table.它直接从.git/HEAD文件中读取当前的git ref并在表中查找相应的名称。

Inspired from: https://stackoverflow.com/a/56245722/11138259 .灵感来自: https://stackoverflow.com/a/56245722/11138259

Using.gitattributes file with content "setup.py merge=ours" and also setting up the git config --global merge.ours.driver true.使用内容为“setup.py merge=ours”的 .gitattributes 文件,并设置 git 配置 --global merge.ours.driver true。 Makes the merge "omit" the setup.py file (it keeps our file instead).使合并“省略” setup.py 文件(它保留我们的文件)。 This works only if both master and child branch have changed the file since they firstly parted ways.仅当主分支和子分支自从他们第一次分道扬镳后都更改了文件时,这才有效。 (it seems) (它似乎)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM