Use rust _platform to version

This commit is contained in:
Aviv Palivoda
2020-02-06 18:14:28 +02:00
parent 6660b64507
commit bbb04c3ded
3 changed files with 20 additions and 92 deletions

87
Lib/platform.py vendored
View File

@@ -971,14 +971,6 @@ _pypy_sys_version_parser = re.compile(
r'\(#?([^,]+),\s*([\w ]+),\s*([\w :]+)\)\s*'
r'\[PyPy [^\]]+\]?')
_rustpython_sys_version_parser = re.compile(
r'RustPython '
r'([\w.+]+)\s*' # "version<space>"
r'\(#?([^,]+)' # "(#buildno"
r'(?:,\s*([\w ]*)' # ", builddate"
r'(?:,\s*([\w :]*))?)?\)\s*' # ", buildtime)<space>"
r'\[([^\]]+)\]?', re.ASCII) # "[compiler]"
_sys_version_cache = {}
def _sys_version(sys_version=None):
@@ -1051,16 +1043,6 @@ def _sys_version(sys_version=None):
version, buildno, builddate, buildtime = match.groups()
compiler = ""
elif "RustPython" in sys_version:
# RustPython
name = "RustPython"
match = _rustpython_sys_version_parser.match(sys_version)
if match is None:
raise ValueError("failed to parse RustPython sys.version: %s" %
repr(sys_version))
version, buildno, builddate, buildtime, compiler = \
match.groups()
else:
# CPython
match = _sys_version_parser.match(sys_version)
@@ -1095,28 +1077,8 @@ def _sys_version(sys_version=None):
_sys_version_cache[sys_version] = result
return result
def python_implementation():
""" Returns a string identifying the Python implementation.
Currently, the following implementations are identified:
'CPython' (C implementation of Python),
'IronPython' (.NET implementation of Python),
'Jython' (Java implementation of Python),
'PyPy' (Python implementation of Python).
"""
return _sys_version()[0]
def python_version():
""" Returns the Python version as string 'major.minor.patchlevel'
Note that unlike the Python sys.version, the returned value
will always include the patchlevel (it defaults to 0).
"""
return _sys_version()[1]
# RustPython specific
from _platform import *
def python_version_tuple():
@@ -1127,50 +1089,7 @@ def python_version_tuple():
will always include the patchlevel (it defaults to 0).
"""
return tuple(_sys_version()[1].split('.'))
def python_branch():
""" Returns a string identifying the Python implementation
branch.
For CPython this is the SCM branch from which the
Python binary was built.
If not available, an empty string is returned.
"""
return _sys_version()[2]
def python_revision():
""" Returns a string identifying the Python implementation
revision.
For CPython this is the SCM revision from which the
Python binary was built.
If not available, an empty string is returned.
"""
return _sys_version()[3]
def python_build():
""" Returns a tuple (buildno, builddate) stating the Python
build number and date as strings.
"""
return _sys_version()[4:6]
def python_compiler():
""" Returns a string identifying the compiler used for compiling
Python.
"""
return _sys_version()[6]
return tuple(python_version().split('.'))
### The Opus Magnum of platform strings :-)

View File

@@ -17,6 +17,7 @@ mod keyword;
mod marshal;
mod math;
mod operator;
mod platform;
mod pystruct;
mod random;
mod re;
@@ -75,6 +76,7 @@ pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
"marshal".to_owned() => Box::new(marshal::make_module),
"math".to_owned() => Box::new(math::make_module),
"_operator".to_owned() => Box::new(operator::make_module),
"_platform".to_owned() => Box::new(platform::make_module),
"regex_crate".to_owned() => Box::new(re::make_module),
"_random".to_owned() => Box::new(random::make_module),
"_string".to_owned() => Box::new(string::make_module),

View File

@@ -23,7 +23,7 @@ use std::time::{Duration, UNIX_EPOCH};
pub fn get_version() -> String {
format!(
"RustPython {:.80} ({:.80}) {:.80}",
"{:.80} ({:.80}) \n[{:.80}]",
get_version_number(),
get_build_info(),
get_compiler()
@@ -40,16 +40,16 @@ pub fn get_version_info() -> VersionInfo {
}
}
fn get_version_number() -> String {
pub fn get_version_number() -> String {
format!("{}.{}.{}{}", MAJOR, MINOR, MICRO, RELEASELEVEL)
}
fn get_compiler() -> String {
pub fn get_compiler() -> String {
let rustc_version = rustc_version_runtime::version_meta();
format!("\n[rustc {}]", rustc_version.semver)
format!("rustc {}", rustc_version.semver)
}
fn get_build_info() -> String {
pub fn get_build_info() -> String {
// See: https://reproducible-builds.org/docs/timestamps/
let git_revision = get_git_revision();
let separator = if git_revision.is_empty() { "" } else { ":" };
@@ -78,7 +78,7 @@ pub fn get_git_tag() -> String {
option_env!("RUSTPYTHON_GIT_TAG").unwrap_or("").to_owned()
}
fn get_git_branch() -> String {
pub fn get_git_branch() -> String {
option_env!("RUSTPYTHON_GIT_BRANCH")
.unwrap_or("")
.to_owned()
@@ -107,14 +107,21 @@ fn get_git_timestamp_datetime() -> DateTime<Local> {
datetime
}
fn get_git_date() -> String {
pub fn get_git_date() -> String {
let datetime = get_git_timestamp_datetime();
datetime.format("%b %e %Y").to_string()
}
fn get_git_time() -> String {
pub fn get_git_time() -> String {
let datetime = get_git_timestamp_datetime();
datetime.format("%H:%M:%S").to_string()
}
pub fn get_git_datetime() -> String {
let date = get_git_date();
let time = get_git_time();
format!("{} {}", date, time)
}