The Wayback Machine - https://web.archive.org/web/20200910210639/https://github.com/go-python/gpython/issues/114
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gpython: leverage runtime/debug.ReadBuildInfo to infer version #114

Open
sbinet opened this issue Oct 2, 2019 · 1 comment
Open

gpython: leverage runtime/debug.ReadBuildInfo to infer version #114

sbinet opened this issue Oct 2, 2019 · 1 comment

Comments

@sbinet
Copy link
Member

@sbinet sbinet commented Oct 2, 2019

starting with Go-1.12, there's a nifty function runtime/debug.ReadBuildInfo to extract build informations out of a Go binary.

we could leverage this to display:

$> gpython
Python 3.4.0 (none, unknown)
[Gpython (devel)]
- os/arch: linux/amd64
- go version: devel +4f13a9c5b1 Tue Oct 1 07:16:47 2019 +0000
>>> 

(notice the (devel) string that comes out of debug.ReadBuildInfo().Main.Version

this could be factored into a gpython/vm.Version() function:

// Copyright 2019 The go-python Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build go1.12

package vm

import (
	"fmt"
	"runtime/debug"
)

// Version returns the version of Gpython and its checksum. The returned
// values are only valid in binaries built with module support.
//
// If a replace directive exists in the Gpython go.mod, the replace will
// be reported in the version in the following format:
//  "version=>[replace-path] [replace-version]"
// and the replace sum will be returned in place of the original sum.
//
// The exact version format returned by Version may change in future.
func Version() (version, sum string) {
	b, ok := debug.ReadBuildInfo()
	if !ok {
		return "", ""
	}

	const root = "github.com/go-python/gpython"

	modules := append([]*debug.Module{&b.Main}, b.Deps...)

	for _, m := range modules {
		if m.Path == root {
			if m.Replace != nil {
				switch {
				case m.Replace.Version != "" && m.Replace.Path != "":
					return fmt.Sprintf("%s=>%s %s", m.Version, m.Replace.Path, m.Replace.Version), m.Replace.Sum
				case m.Replace.Version != "":
					return fmt.Sprintf("%s=>%s", m.Version, m.Replace.Version), m.Replace.Sum
				case m.Replace.Path != "":
					return fmt.Sprintf("%s=>%s", m.Version, m.Replace.Path), m.Replace.Sum
				default:
					return m.Version + "*", m.Sum + "*"
				}
			}
			return m.Version, m.Sum
		}
	}

	return "", ""
}
@ncw
Copy link
Collaborator

@ncw ncw commented Oct 8, 2019

That is a nice idea :-) I hadn't seen that function before.

Do you want to submit that as a PR?

Do we need the replace part of the above? How likely is that to happen?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.