1

I am trying to have 2 simultaneous versions of a single package on a server. 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.

Is it possible to dynamically change the package name in setup.py, depending on the git branch? Or is it possible to deploy them with different names using 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.

  • Why change the name dynamically? Isn't it possible to keep the argument for the `name` parameter of `setuptools.setup()` different in your two _git_ branches. – sinoroc Oct 26 '19 at 12:54
  • Would it be possible to merge the test branch to master omitting the setup.py file? – Milan Urminský Oct 26 '19 at 16:48

2 Answers2

2

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.

Inspired from: https://stackoverflow.com/a/56245722/11138259.

sinoroc
  • 18,409
  • 2
  • 39
  • 70
0

Using .gitattributes file with content "setup.py merge=ours" and also setting up the git config --global merge.ours.driver true. Makes the merge "omit" the setup.py file (it keeps our file instead). This works only if both master and child branch have changed the file since they firstly parted ways. (it seems)