Skip to content

Commit

Permalink
[Lang] Add check version function to taichi main (taichi-dev#3526)
Browse files Browse the repository at this point in the history
* [Lang] Add check version function to taichi main
  • Loading branch information
Leonz5288 authored Nov 17, 2021
1 parent 0a451ce commit c21fa24
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
69 changes: 69 additions & 0 deletions python/taichi/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import argparse
import math
import os
import platform
import runpy
import shutil
import subprocess
Expand All @@ -11,6 +12,7 @@
from pathlib import Path

import numpy as np
import requests
import taichi.cc_compose
import taichi.diagnose
from colorama import Fore
Expand Down Expand Up @@ -67,6 +69,8 @@ def __init__(self, test_mode: bool = False):

self.main_parser = parser

self._check_version()

@timer
def __call__(self):
# Print help if no command provided
Expand All @@ -88,6 +92,71 @@ def __call__(self):

return getattr(self, args.command)(sys.argv[2:])

@staticmethod
def _check_version():
# Check Taichi version for the user.
print('Checking your Taichi version...')
major = _ti_core.get_version_major()
minor = _ti_core.get_version_minor()
patch = _ti_core.get_version_patch()
version = f'{major}.{minor}.{patch}'
payload = {'version': version, 'platform': '', 'python': ''}

system = platform.system()
if system == 'Linux':
payload['platform'] = 'manylinux1_x86_64'
elif system == 'Windows':
payload['platform'] = 'win_amd64'
elif system == 'Darwin':
if platform.release() < '19.0.0':
payload['platform'] = 'macosx_10_14_x86_64'
elif platform.machine() == 'x86_64':
payload['platform'] = 'macosx_10_15_x86_64'
else:
payload['platform'] = 'macosx_11_0_arm64'

python_version = platform.python_version()
if python_version.startswith('3.6'):
payload['python'] = 'cp36'
elif python_version.startswith('3.7'):
payload['python'] = 'cp37'
elif python_version.startswith('3.8'):
payload['python'] = 'cp38'
elif python_version.startswith('3.9'):
payload['python'] = 'cp39'

# We do not want request exceptions break users' usage of Taichi.
try:
response = requests.post(
'http://ec2-54-90-48-192.compute-1.amazonaws.com/check_version',
json=payload,
timeout=3)
response.raise_for_status()
except requests.exceptions.ConnectionError as err:
print('Checking latest version failed: No internet,', err)
return
except requests.exceptions.HTTPError as err:
print('Checking latest version failed: Server error,', err)
return
except requests.exceptions.Timeout as err:
print(
'Checking latest version failed: Time out when connecting server,',
err)
return
except requests.exceptions.RequestException as err:
print('Checking latest version failed:', err)
return

response = response.json()
if response['status'] == 1:
print(
f'Your Taichi version {version} is outdated. The latest version is {response["latest_version"]}, you can use\n'
+ f'pip install taichi=={response["latest_version"]}\n' +
'to upgrade to the latest Taichi!')
elif response['status'] == 0:
# Status 0 means that user already have the latest Taichi. The message here prompts this infomation to users.
print(response['message'])

@staticmethod
def _get_friend_links():
return '\n' \
Expand Down
1 change: 1 addition & 0 deletions requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pytest-rerunfailures
pytest-cov
numpy
autograd
requests==2.26

0 comments on commit c21fa24

Please sign in to comment.