Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

--version support in a Python program built with Pants

How can I get Pants to store the output of git describe somewhere in my .pex file so that I can access it from the Python code I'm writing?

Basically I want to be able to clone my project and do this:

  1. ./pants binary px
  2. Distribute the resulting dist/px.pex to somebody
  3. That somebody should be able to do px.pex --version and get a printout of whatever git describe said when I built the .pex in step one.

Help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Turns out pex already does git describe on build. The result it stores in a PEX-INFO file in the root of the .pex file. So to read it, I did this:

def get_version():
    """Extract version string from PEX-INFO file"""
    my_pex_name = os.path.dirname(__file__)
    zip = zipfile.ZipFile(my_pex_name)
    with zip.open("PEX-INFO") as pex_info:
        return json.load(pex_info)['build_properties']['tag']

This is good enough IMO, but there are also drawbacks. If somebody has an improved answer I'm prepared to switch to that one as the accepted one.

Outages with this one:

  • Relies on relative paths to locate PEX-INFO, would be better if there was some kind of API call for this.
  • No way to customize how the version number is computed; I'd like to do git describe --dirty for example.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...