bpo-27593: Get SCM build info from git instead of hg. (#446)

sys.version and the platform module python_build(),
python_branch(), and python_revision() functions now use
git information rather than hg when building from a repo.

Based on original patches by Brett Cannon and Steve Dower.
This commit is contained in:
Ned Deily 2017-03-04 00:19:55 -05:00 committed by GitHub
parent fc64c351c7
commit 5c4b0d063a
9 changed files with 84 additions and 78 deletions

View File

@ -70,8 +70,8 @@ PyAPI_FUNC(const char *) Py_GetCopyright(void);
PyAPI_FUNC(const char *) Py_GetCompiler(void); PyAPI_FUNC(const char *) Py_GetCompiler(void);
PyAPI_FUNC(const char *) Py_GetBuildInfo(void); PyAPI_FUNC(const char *) Py_GetBuildInfo(void);
#ifndef Py_LIMITED_API #ifndef Py_LIMITED_API
PyAPI_FUNC(const char *) _Py_hgidentifier(void); PyAPI_FUNC(const char *) _Py_gitidentifier(void);
PyAPI_FUNC(const char *) _Py_hgversion(void); PyAPI_FUNC(const char *) _Py_gitversion(void);
#endif #endif
/* Internal -- various one-time initializations */ /* Internal -- various one-time initializations */

View File

@ -1198,7 +1198,9 @@ def _sys_version(sys_version=None):
elif buildtime: elif buildtime:
builddate = builddate + ' ' + buildtime builddate = builddate + ' ' + buildtime
if hasattr(sys, '_mercurial'): if hasattr(sys, '_git'):
_, branch, revision = sys._git
elif hasattr(sys, '_mercurial'):
_, branch, revision = sys._mercurial _, branch, revision = sys._mercurial
elif hasattr(sys, 'subversion'): elif hasattr(sys, 'subversion'):
# sys.subversion was added in Python 2.5 # sys.subversion was added in Python 2.5

View File

@ -67,12 +67,12 @@ class PlatformTest(unittest.TestCase):
def setUp(self): def setUp(self):
self.save_version = sys.version self.save_version = sys.version
self.save_mercurial = sys._mercurial self.save_git = sys._git
self.save_platform = sys.platform self.save_platform = sys.platform
def tearDown(self): def tearDown(self):
sys.version = self.save_version sys.version = self.save_version
sys._mercurial = self.save_mercurial sys._git = self.save_git
sys.platform = self.save_platform sys.platform = self.save_platform
def test_sys_version(self): def test_sys_version(self):
@ -102,7 +102,7 @@ class PlatformTest(unittest.TestCase):
('CPython', '2.4.3', '', '', 'truncation', '', 'GCC')), ('CPython', '2.4.3', '', '', 'truncation', '', 'GCC')),
): ):
# branch and revision are not "parsed", but fetched # branch and revision are not "parsed", but fetched
# from sys._mercurial. Ignore them # from sys._git. Ignore them
(name, version, branch, revision, buildno, builddate, compiler) \ (name, version, branch, revision, buildno, builddate, compiler) \
= platform._sys_version(input) = platform._sys_version(input)
self.assertEqual( self.assertEqual(
@ -149,10 +149,10 @@ class PlatformTest(unittest.TestCase):
sys_versions.items(): sys_versions.items():
sys.version = version_tag sys.version = version_tag
if subversion is None: if subversion is None:
if hasattr(sys, "_mercurial"): if hasattr(sys, "_git"):
del sys._mercurial del sys._git
else: else:
sys._mercurial = subversion sys._git = subversion
if sys_platform is not None: if sys_platform is not None:
sys.platform = sys_platform sys.platform = sys_platform
self.assertEqual(platform.python_implementation(), info[0]) self.assertEqual(platform.python_implementation(), info[0])

View File

@ -41,9 +41,9 @@ RANLIB= @RANLIB@
READELF= @READELF@ READELF= @READELF@
SOABI= @SOABI@ SOABI= @SOABI@
LDVERSION= @LDVERSION@ LDVERSION= @LDVERSION@
HGVERSION= @HGVERSION@ GITVERSION= @GITVERSION@
HGTAG= @HGTAG@ GITTAG= @GITTAG@
HGBRANCH= @HGBRANCH@ GITBRANCH= @GITBRANCH@
PGO_PROF_GEN_FLAG=@PGO_PROF_GEN_FLAG@ PGO_PROF_GEN_FLAG=@PGO_PROF_GEN_FLAG@
PGO_PROF_USE_FLAG=@PGO_PROF_USE_FLAG@ PGO_PROF_USE_FLAG=@PGO_PROF_USE_FLAG@
LLVM_PROF_MERGER=@LLVM_PROF_MERGER@ LLVM_PROF_MERGER=@LLVM_PROF_MERGER@
@ -741,9 +741,9 @@ Modules/getbuildinfo.o: $(PARSER_OBJS) \
$(MODOBJS) \ $(MODOBJS) \
$(srcdir)/Modules/getbuildinfo.c $(srcdir)/Modules/getbuildinfo.c
$(CC) -c $(PY_CORE_CFLAGS) \ $(CC) -c $(PY_CORE_CFLAGS) \
-DHGVERSION="\"`LC_ALL=C $(HGVERSION)`\"" \ -DGITVERSION="\"`LC_ALL=C $(GITVERSION)`\"" \
-DHGTAG="\"`LC_ALL=C $(HGTAG)`\"" \ -DGITTAG="\"`LC_ALL=C $(GITTAG)`\"" \
-DHGBRANCH="\"`LC_ALL=C $(HGBRANCH)`\"" \ -DGITBRANCH="\"`LC_ALL=C $(GITBRANCH)`\"" \
-o $@ $(srcdir)/Modules/getbuildinfo.c -o $@ $(srcdir)/Modules/getbuildinfo.c
Modules/getpath.o: $(srcdir)/Modules/getpath.c Makefile Modules/getpath.o: $(srcdir)/Modules/getpath.c Makefile

View File

@ -806,6 +806,10 @@ Documentation
Build Build
----- -----
- bpo-27593: sys.version and the platform module python_build(),
python_branch(), and python_revision() functions now use
git information rather than hg when building from a repo.
- bpo-29572: Update Windows build and OS X installers to use OpenSSL 1.0.2k. - bpo-29572: Update Windows build and OS X installers to use OpenSSL 1.0.2k.
- Issue #27659: Prohibit implicit C function declarations: use - Issue #27659: Prohibit implicit C function declarations: use

View File

@ -21,47 +21,47 @@
#endif #endif
/* XXX Only unix build process has been tested */ /* XXX Only unix build process has been tested */
#ifndef HGVERSION #ifndef GITVERSION
#define HGVERSION "" #define GITVERSION ""
#endif #endif
#ifndef HGTAG #ifndef GITTAG
#define HGTAG "" #define GITTAG ""
#endif #endif
#ifndef HGBRANCH #ifndef GITBRANCH
#define HGBRANCH "" #define GITBRANCH ""
#endif #endif
const char * const char *
Py_GetBuildInfo(void) Py_GetBuildInfo(void)
{ {
static char buildinfo[50 + sizeof(HGVERSION) + static char buildinfo[50 + sizeof(GITVERSION) +
((sizeof(HGTAG) > sizeof(HGBRANCH)) ? ((sizeof(GITTAG) > sizeof(GITBRANCH)) ?
sizeof(HGTAG) : sizeof(HGBRANCH))]; sizeof(GITTAG) : sizeof(GITBRANCH))];
const char *revision = _Py_hgversion(); const char *revision = _Py_gitversion();
const char *sep = *revision ? ":" : ""; const char *sep = *revision ? ":" : "";
const char *hgid = _Py_hgidentifier(); const char *gitid = _Py_gitidentifier();
if (!(*hgid)) if (!(*gitid))
hgid = "default"; gitid = "default";
PyOS_snprintf(buildinfo, sizeof(buildinfo), PyOS_snprintf(buildinfo, sizeof(buildinfo),
"%s%s%s, %.20s, %.9s", hgid, sep, revision, "%s%s%s, %.20s, %.9s", gitid, sep, revision,
DATE, TIME); DATE, TIME);
return buildinfo; return buildinfo;
} }
const char * const char *
_Py_hgversion(void) _Py_gitversion(void)
{ {
return HGVERSION; return GITVERSION;
} }
const char * const char *
_Py_hgidentifier(void) _Py_gitidentifier(void)
{ {
const char *hgtag, *hgid; const char *gittag, *gitid;
hgtag = HGTAG; gittag = GITTAG;
if ((*hgtag) && strcmp(hgtag, "tip") != 0) if ((*gittag) && strcmp(gittag, "undefined") != 0)
hgid = hgtag; gitid = gittag;
else else
hgid = HGBRANCH; gitid = GITBRANCH;
return hgid; return gitid;
} }

View File

@ -1960,9 +1960,9 @@ _PySys_Init(void)
PyUnicode_FromString(Py_GetVersion())); PyUnicode_FromString(Py_GetVersion()));
SET_SYS_FROM_STRING("hexversion", SET_SYS_FROM_STRING("hexversion",
PyLong_FromLong(PY_VERSION_HEX)); PyLong_FromLong(PY_VERSION_HEX));
SET_SYS_FROM_STRING("_mercurial", SET_SYS_FROM_STRING("_git",
Py_BuildValue("(szz)", "CPython", _Py_hgidentifier(), Py_BuildValue("(szz)", "CPython", _Py_gitidentifier(),
_Py_hgversion())); _Py_gitversion()));
SET_SYS_FROM_STRING("dont_write_bytecode", SET_SYS_FROM_STRING("dont_write_bytecode",
PyBool_FromLong(Py_DontWriteBytecodeFlag)); PyBool_FromLong(Py_DontWriteBytecodeFlag));
SET_SYS_FROM_STRING("api_version", SET_SYS_FROM_STRING("api_version",

48
configure vendored
View File

@ -760,10 +760,10 @@ build_os
build_vendor build_vendor
build_cpu build_cpu
build build
HAS_HG HAS_GIT
HGBRANCH GITBRANCH
HGTAG GITTAG
HGVERSION GITVERSION
BASECPPFLAGS BASECPPFLAGS
target_alias target_alias
host_alias host_alias
@ -2698,17 +2698,17 @@ fi
if test -e $srcdir/.hg/dirstate if test -e $srcdir/.git/HEAD
then then
# Extract the first word of "hg", so it can be a program name with args. # Extract the first word of "git", so it can be a program name with args.
set dummy hg; ac_word=$2 set dummy git; ac_word=$2
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
$as_echo_n "checking for $ac_word... " >&6; } $as_echo_n "checking for $ac_word... " >&6; }
if ${ac_cv_prog_HAS_HG+:} false; then : if ${ac_cv_prog_HAS_GIT+:} false; then :
$as_echo_n "(cached) " >&6 $as_echo_n "(cached) " >&6
else else
if test -n "$HAS_HG"; then if test -n "$HAS_GIT"; then
ac_cv_prog_HAS_HG="$HAS_HG" # Let the user override the test. ac_cv_prog_HAS_GIT="$HAS_GIT" # Let the user override the test.
else else
as_save_IFS=$IFS; IFS=$PATH_SEPARATOR as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
for as_dir in $PATH for as_dir in $PATH
@ -2717,7 +2717,7 @@ do
test -z "$as_dir" && as_dir=. test -z "$as_dir" && as_dir=.
for ac_exec_ext in '' $ac_executable_extensions; do for ac_exec_ext in '' $ac_executable_extensions; do
if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
ac_cv_prog_HAS_HG="found" ac_cv_prog_HAS_GIT="found"
$as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
break 2 break 2
fi fi
@ -2725,13 +2725,13 @@ done
done done
IFS=$as_save_IFS IFS=$as_save_IFS
test -z "$ac_cv_prog_HAS_HG" && ac_cv_prog_HAS_HG="not-found" test -z "$ac_cv_prog_HAS_GIT" && ac_cv_prog_HAS_GIT="not-found"
fi fi
fi fi
HAS_HG=$ac_cv_prog_HAS_HG HAS_GIT=$ac_cv_prog_HAS_GIT
if test -n "$HAS_HG"; then if test -n "$HAS_GIT"; then
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $HAS_HG" >&5 { $as_echo "$as_me:${as_lineno-$LINENO}: result: $HAS_GIT" >&5
$as_echo "$HAS_HG" >&6; } $as_echo "$HAS_GIT" >&6; }
else else
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
$as_echo "no" >&6; } $as_echo "no" >&6; }
@ -2739,17 +2739,17 @@ fi
else else
HAS_HG=no-repository HAS_GIT=no-repository
fi fi
if test $HAS_HG = found if test $HAS_GIT = found
then then
HGVERSION="hg id -i \$(srcdir)" GITVERSION="git -C \$(srcdir) rev-parse HEAD"
HGTAG="hg id -t \$(srcdir)" GITTAG="git -C \$(srcdir) name-rev --tags --name-only HEAD"
HGBRANCH="hg id -b \$(srcdir)" GITBRANCH="git -C \$(srcdir) name-rev --name-only HEAD"
else else
HGVERSION="" GITVERSION=""
HGTAG="" GITTAG=""
HGBRANCH="" GITBRANCH=""
fi fi

View File

@ -25,25 +25,25 @@ else
BASECPPFLAGS="" BASECPPFLAGS=""
fi fi
AC_SUBST(HGVERSION) AC_SUBST(GITVERSION)
AC_SUBST(HGTAG) AC_SUBST(GITTAG)
AC_SUBST(HGBRANCH) AC_SUBST(GITBRANCH)
if test -e $srcdir/.hg/dirstate if test -e $srcdir/.git/HEAD
then then
AC_CHECK_PROG(HAS_HG, hg, found, not-found) AC_CHECK_PROG(HAS_GIT, git, found, not-found)
else else
HAS_HG=no-repository HAS_GIT=no-repository
fi fi
if test $HAS_HG = found if test $HAS_GIT = found
then then
HGVERSION="hg id -i \$(srcdir)" GITVERSION="git -C \$(srcdir) rev-parse HEAD"
HGTAG="hg id -t \$(srcdir)" GITTAG="git -C \$(srcdir) name-rev --tags --name-only HEAD"
HGBRANCH="hg id -b \$(srcdir)" GITBRANCH="git -C \$(srcdir) name-rev --name-only HEAD"
else else
HGVERSION="" GITVERSION=""
HGTAG="" GITTAG=""
HGBRANCH="" GITBRANCH=""
fi fi
AC_CONFIG_SRCDIR([Include/object.h]) AC_CONFIG_SRCDIR([Include/object.h])