From 85bb38686e57b55303f907548fe990b0b7f98096 Mon Sep 17 00:00:00 2001 From: Rex W Date: Sat, 16 May 2020 20:52:17 -0400 Subject: [PATCH 01/20] Bugfix: shutil not imported for command:convert. --- python/taichi/main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/taichi/main.py b/python/taichi/main.py index 3e341d115b93a..70aac82f72980 100644 --- a/python/taichi/main.py +++ b/python/taichi/main.py @@ -377,6 +377,7 @@ def main(debug=False): framerate = 24 mp4_to_gif(input_fn, output_fn, framerate) elif mode == "convert": + import shutil # http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed # TODO: Windows support for fn in sys.argv[2:]: From 2038b1eb682af71dd1e6fbc767e94fa3b8cd2445 Mon Sep 17 00:00:00 2001 From: Rex W Date: Sun, 17 May 2020 11:50:50 -0400 Subject: [PATCH 02/20] Minor improvement to dev_install docs. --- docs/dev_install.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/dev_install.rst b/docs/dev_install.rst index c2f0fed04ef47..d603670321afa 100644 --- a/docs/dev_install.rst +++ b/docs/dev_install.rst @@ -15,6 +15,7 @@ Installing Depedencies ---------------------- - Make sure you are using Python 3.6/3.7/3.8 +- Make sure you have `CMake `_ installed and configured. - Execute .. code-block:: bash From 9edb5f0fa12b0759f5a8a881cd9b90d3243c6c7c Mon Sep 17 00:00:00 2001 From: Rex W Date: Sun, 17 May 2020 12:32:37 -0400 Subject: [PATCH 03/20] Document some hacks for dev_install under Ubuntu. --- docs/dev_install.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/docs/dev_install.rst b/docs/dev_install.rst index d603670321afa..229a8007e5bb4 100644 --- a/docs/dev_install.rst +++ b/docs/dev_install.rst @@ -80,6 +80,20 @@ Setting up Taichi for development # cmake .. -DCUDA_VERSION=10.0 -DTI_WITH_CUDA:BOOL=True make -j 8 + * You may run into errors with cmake if you have gcc/g++ as your default compiler + instead of Clang/LLVM, you could let CMake respect your clang by setting environment + variables like the following: + + .. code-block:: bash + + # remember to use the correct path based on your clang installation + export CC=/usr/bin/clang-8 + export CXX=/usr/bin/clang++-8 + + * You may also have to install some extra packages on Linux, based on the specific errors + you get, you may need to run something like: ``sudo apt install libxcursor-dev libxinerama-dev libxrandr-dev``. + + - Add the following script to your ``~/.bashrc``: .. code-block:: bash From 84b96478e42efa41fc729ac85ed99f9c4b9ccca9 Mon Sep 17 00:00:00 2001 From: Rex Date: Sun, 17 May 2020 20:13:28 -0400 Subject: [PATCH 04/20] The naive way to implement this. --- python/taichi/main.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/python/taichi/main.py b/python/taichi/main.py index 70aac82f72980..983ae0fb81345 100644 --- a/python/taichi/main.py +++ b/python/taichi/main.py @@ -72,6 +72,22 @@ def test_cpp(args): return int(task.run(*test_files)) +def run_example(name: str): + """Run an example based on example NAME.""" + import taichi as ti + from pathlib import Path + + root_dir = ti.package_root() if ti.is_release() else ti.get_repo_directory() + examples_dir = Path(root_dir) / 'examples' + all_examples = examples_dir.rglob('*.py') + all_example_names = {str(f.resolve()).split("/")[-1].split(".")[0] for f in all_examples} + + if not name in all_example_names: + print(f"Sorry, {name} is not an available example name!") + print(f"Available examples names: {all_example_names}") + os.system(f"python3 {examples_dir / f'{name}.py'}") + + def get_benchmark_baseline_dir(): import taichi as ti return os.path.join(ti.core.get_repo_dir(), 'benchmarks', 'baseline') @@ -183,6 +199,10 @@ def make_argument_parser(): action='store_true', help='Exclude arch(s) instead of include, e.g. -na opengl,metal') parser.add_argument('files', nargs='*', help='Files to be tested') + + # commands = parser.add_subparsers(help='Available Taichi commands') + # examples = commands.add_parser('examples', help='Interact with Taichi examples') + return parser @@ -237,7 +257,8 @@ def main(debug=False): " ti gif |-> Convert mp4 file to gif\n" " ti doc |-> Build documentation\n" " ti release |-> Make source code release\n" - " ti debug [script.py] |-> Debug script\n") + " ti debug [script.py] |-> Debug script\n" + " ti example [name] |-> Run an example by name\n") return 0 t = time.time() @@ -409,6 +430,10 @@ def main(debug=False): fn = f'taichi-src-v{ver[0]}-{ver[1]}-{ver[2]}-{commit}-{md5}.zip' import shutil shutil.move('release.zip', fn) + elif mode == "example": + example = sys.argv[2] + print(f"Running example {example} ...") + run_example(name=example) else: name = sys.argv[1] print('Running task [{}]...'.format(name)) From fc7afe8bc030b118ae186c5fbb1799d76dcf67a2 Mon Sep 17 00:00:00 2001 From: Rex Date: Sun, 17 May 2020 20:14:02 -0400 Subject: [PATCH 05/20] Include the examples folder in Python build. --- python/build.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/python/build.py b/python/build.py index b5942dbfcf665..2eb4062859368 100644 --- a/python/build.py +++ b/python/build.py @@ -64,6 +64,7 @@ def get_python_executable(): shutil.rmtree('taichi/lib', ignore_errors=True) shutil.rmtree('taichi/tests', ignore_errors=True) +shutil.rmtree('taichi/examples', ignore_errors=True) os.makedirs('taichi/lib', exist_ok=True) shutil.rmtree('build', ignore_errors=True) shutil.rmtree('dist', ignore_errors=True) @@ -80,6 +81,7 @@ def get_python_executable(): 'taichi/lib/taichi_core.pyd') shutil.copytree('../tests/python', './taichi/tests') +shutil.copytree('../examples', './taichi/examples') if get_os_name() != 'osx': libdevice_path = ti.core.libdevice_path() @@ -106,6 +108,7 @@ def get_python_executable(): shutil.rmtree('taichi/lib') shutil.rmtree('taichi/tests') +shutil.rmtree('taichi/examples') shutil.rmtree('./build') if mode == 'upload': From ed8bc9177253e04a8264816e0faf19c1d31f5ef0 Mon Sep 17 00:00:00 2001 From: Rex W Date: Sun, 17 May 2020 22:06:30 -0400 Subject: [PATCH 06/20] Nice, macOS could use Homebrew. --- docs/dev_install.rst | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/docs/dev_install.rst b/docs/dev_install.rst index 229a8007e5bb4..b3b700b84071d 100644 --- a/docs/dev_install.rst +++ b/docs/dev_install.rst @@ -23,7 +23,24 @@ Installing Depedencies python3 -m pip install --user setuptools astpretty astor pytest opencv-python pybind11 python3 -m pip install --user Pillow numpy scipy GitPython yapf colorama psutil autograd -* (If on Ubuntu) Execute ``sudo apt install libtinfo-dev clang-8``. (``clang-7`` should work as well). +* (**If on Ubuntu**) Execute ``sudo apt install libtinfo-dev clang-8``. (``clang-7`` should work as well). + +* (**If on macOS**) You could rely on `Homebrew `_ to install LLVM with clang for you. + ``brew install llvm@8`` will only install llvm and clang 8 for you, but since macOS usually has pre-installed + clang, Homebrew won't link it automatically. You could run ``brew --prefix llvm@8`` to check the location of + installation and usually you will need to add lines similar to the following to your shell profile (~/.bashrc or + ~/.zshrc): + + .. code-block:: bash + + export PATH="/usr/local/opt/llvm@8/bin:$PATH" + export LDFLAGS="-L/usr/local/opt/llvm@8/lib" + export CPPFLAGS="-I/usr/local/opt/llvm@8/include" + export CC=clang + export CXX=clang++ + + After restarting your shell, you could then use ``clang --version`` to verify you are using the + right version of clang (8.0.1). * (If on other Linux distributions) Please build clang 8.0.1 from scratch: @@ -81,8 +98,8 @@ Setting up Taichi for development make -j 8 * You may run into errors with cmake if you have gcc/g++ as your default compiler - instead of Clang/LLVM, you could let CMake respect your clang by setting environment - variables like the following: + instead of Clang/LLVM or you have multiple versions of clang. You could let CMake + respect your clang by setting environment variables like the following: .. code-block:: bash From 1787d605e6c224164d0ecab39b1fda7ecda0b1d6 Mon Sep 17 00:00:00 2001 From: Rex W Date: Sun, 17 May 2020 22:12:04 -0400 Subject: [PATCH 07/20] Error out when invalid example name is given. --- python/taichi/main.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/taichi/main.py b/python/taichi/main.py index 983ae0fb81345..58d08e770055d 100644 --- a/python/taichi/main.py +++ b/python/taichi/main.py @@ -83,8 +83,7 @@ def run_example(name: str): all_example_names = {str(f.resolve()).split("/")[-1].split(".")[0] for f in all_examples} if not name in all_example_names: - print(f"Sorry, {name} is not an available example name!") - print(f"Available examples names: {all_example_names}") + sys.exit(f"Sorry, {name} is not an available example name!\nAvailable examples names: {all_example_names}") os.system(f"python3 {examples_dir / f'{name}.py'}") From 05752295765dc6d5caaae983b840ac571b0a4b98 Mon Sep 17 00:00:00 2001 From: Taichi Gardener Date: Mon, 18 May 2020 08:23:32 -0400 Subject: [PATCH 08/20] [skip ci] enforce code format --- docs/dev_install.rst | 6 +++--- python/taichi/main.py | 28 ++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/docs/dev_install.rst b/docs/dev_install.rst index b3b700b84071d..5070fab4350b3 100644 --- a/docs/dev_install.rst +++ b/docs/dev_install.rst @@ -28,7 +28,7 @@ Installing Depedencies * (**If on macOS**) You could rely on `Homebrew `_ to install LLVM with clang for you. ``brew install llvm@8`` will only install llvm and clang 8 for you, but since macOS usually has pre-installed clang, Homebrew won't link it automatically. You could run ``brew --prefix llvm@8`` to check the location of - installation and usually you will need to add lines similar to the following to your shell profile (~/.bashrc or + installation and usually you will need to add lines similar to the following to your shell profile (~/.bashrc or ~/.zshrc): .. code-block:: bash @@ -39,7 +39,7 @@ Installing Depedencies export CC=clang export CXX=clang++ - After restarting your shell, you could then use ``clang --version`` to verify you are using the + After restarting your shell, you could then use ``clang --version`` to verify you are using the right version of clang (8.0.1). * (If on other Linux distributions) Please build clang 8.0.1 from scratch: @@ -106,7 +106,7 @@ Setting up Taichi for development # remember to use the correct path based on your clang installation export CC=/usr/bin/clang-8 export CXX=/usr/bin/clang++-8 - + * You may also have to install some extra packages on Linux, based on the specific errors you get, you may need to run something like: ``sudo apt install libxcursor-dev libxinerama-dev libxrandr-dev``. diff --git a/python/taichi/main.py b/python/taichi/main.py index 58d08e770055d..039b2244fd55d 100644 --- a/python/taichi/main.py +++ b/python/taichi/main.py @@ -77,13 +77,19 @@ def run_example(name: str): import taichi as ti from pathlib import Path - root_dir = ti.package_root() if ti.is_release() else ti.get_repo_directory() + root_dir = ti.package_root() if ti.is_release() else ti.get_repo_directory( + ) examples_dir = Path(root_dir) / 'examples' all_examples = examples_dir.rglob('*.py') - all_example_names = {str(f.resolve()).split("/")[-1].split(".")[0] for f in all_examples} + all_example_names = { + str(f.resolve()).split("/")[-1].split(".")[0] + for f in all_examples + } if not name in all_example_names: - sys.exit(f"Sorry, {name} is not an available example name!\nAvailable examples names: {all_example_names}") + sys.exit( + f"Sorry, {name} is not an available example name!\nAvailable examples names: {all_example_names}" + ) os.system(f"python3 {examples_dir / f'{name}.py'}") @@ -101,8 +107,10 @@ def display_benchmark_regression(xd, yd, args): def parse_dat(file): dict = {} for line in open(file).readlines(): - try: a, b = line.strip().split(':') - except: continue + try: + a, b = line.strip().split(':') + except: + continue dict[a.strip()] = int(float(b)) return dict @@ -127,7 +135,8 @@ def plot_in_gui(scatter): gui = ti.GUI('Regression Test', (640, 480), 0x001122) print('[Hint] press SPACE to go for next display') for key, data in scatter.items(): - data = np.array([((i + 0.5)/len(data), x/2) for i, x in enumerate(data)]) + data = np.array([((i + 0.5) / len(data), x / 2) + for i, x in enumerate(data)]) while not gui.get_event((ti.GUI.PRESS, ti.GUI.SPACE)): gui.core.title = key gui.line((0, 0.5), (1, 0.5), 1.8, 0x66ccff) @@ -176,7 +185,9 @@ def make_argument_parser(): '--verbose', action='store_true', help='Run with verbose outputs') - parser.add_argument('-r', '--rerun', help='Rerun failed tests for given times') + parser.add_argument('-r', + '--rerun', + help='Rerun failed tests for given times') parser.add_argument('-t', '--threads', help='Number of threads for parallel testing') @@ -257,7 +268,8 @@ def main(debug=False): " ti doc |-> Build documentation\n" " ti release |-> Make source code release\n" " ti debug [script.py] |-> Debug script\n" - " ti example [name] |-> Run an example by name\n") + " ti example [name] |-> Run an example by name\n" + ) return 0 t = time.time() From 7f207c04b99978d5adddf68650ec9c8160005f8c Mon Sep 17 00:00:00 2001 From: Rex W Date: Mon, 18 May 2020 17:08:17 -0400 Subject: [PATCH 09/20] Revert changes to docs for correctness and simplicity. --- docs/dev_install.rst | 34 +--------------------------------- 1 file changed, 1 insertion(+), 33 deletions(-) diff --git a/docs/dev_install.rst b/docs/dev_install.rst index b3b700b84071d..c2f0fed04ef47 100644 --- a/docs/dev_install.rst +++ b/docs/dev_install.rst @@ -15,7 +15,6 @@ Installing Depedencies ---------------------- - Make sure you are using Python 3.6/3.7/3.8 -- Make sure you have `CMake `_ installed and configured. - Execute .. code-block:: bash @@ -23,24 +22,7 @@ Installing Depedencies python3 -m pip install --user setuptools astpretty astor pytest opencv-python pybind11 python3 -m pip install --user Pillow numpy scipy GitPython yapf colorama psutil autograd -* (**If on Ubuntu**) Execute ``sudo apt install libtinfo-dev clang-8``. (``clang-7`` should work as well). - -* (**If on macOS**) You could rely on `Homebrew `_ to install LLVM with clang for you. - ``brew install llvm@8`` will only install llvm and clang 8 for you, but since macOS usually has pre-installed - clang, Homebrew won't link it automatically. You could run ``brew --prefix llvm@8`` to check the location of - installation and usually you will need to add lines similar to the following to your shell profile (~/.bashrc or - ~/.zshrc): - - .. code-block:: bash - - export PATH="/usr/local/opt/llvm@8/bin:$PATH" - export LDFLAGS="-L/usr/local/opt/llvm@8/lib" - export CPPFLAGS="-I/usr/local/opt/llvm@8/include" - export CC=clang - export CXX=clang++ - - After restarting your shell, you could then use ``clang --version`` to verify you are using the - right version of clang (8.0.1). +* (If on Ubuntu) Execute ``sudo apt install libtinfo-dev clang-8``. (``clang-7`` should work as well). * (If on other Linux distributions) Please build clang 8.0.1 from scratch: @@ -97,20 +79,6 @@ Setting up Taichi for development # cmake .. -DCUDA_VERSION=10.0 -DTI_WITH_CUDA:BOOL=True make -j 8 - * You may run into errors with cmake if you have gcc/g++ as your default compiler - instead of Clang/LLVM or you have multiple versions of clang. You could let CMake - respect your clang by setting environment variables like the following: - - .. code-block:: bash - - # remember to use the correct path based on your clang installation - export CC=/usr/bin/clang-8 - export CXX=/usr/bin/clang++-8 - - * You may also have to install some extra packages on Linux, based on the specific errors - you get, you may need to run something like: ``sudo apt install libxcursor-dev libxinerama-dev libxrandr-dev``. - - - Add the following script to your ``~/.bashrc``: .. code-block:: bash From 94a4da8ef2733e5ed6b5e87c2cef9a27eeff63d2 Mon Sep 17 00:00:00 2001 From: Rex W Date: Mon, 18 May 2020 18:19:10 -0400 Subject: [PATCH 10/20] Switch off os.system and use importlib. --- python/taichi/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/taichi/main.py b/python/taichi/main.py index 039b2244fd55d..2946036f23b8e 100644 --- a/python/taichi/main.py +++ b/python/taichi/main.py @@ -8,6 +8,7 @@ from collections import defaultdict from colorama import Fore, Back, Style from taichi.tools.video import make_video, interpolate_frames, mp4_to_gif, scale_video, crop_video, accelerate_video +import importlib def test_python(args): @@ -90,7 +91,7 @@ def run_example(name: str): sys.exit( f"Sorry, {name} is not an available example name!\nAvailable examples names: {all_example_names}" ) - os.system(f"python3 {examples_dir / f'{name}.py'}") + importlib.import_module(f"examples.{name}") def get_benchmark_baseline_dir(): From daa787645e19f75cff948935614a9d613d0e2b90 Mon Sep 17 00:00:00 2001 From: Rex W Date: Mon, 18 May 2020 18:27:18 -0400 Subject: [PATCH 11/20] Remove redundant comments. --- python/taichi/main.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/python/taichi/main.py b/python/taichi/main.py index 2946036f23b8e..62b9212c40885 100644 --- a/python/taichi/main.py +++ b/python/taichi/main.py @@ -210,10 +210,6 @@ def make_argument_parser(): action='store_true', help='Exclude arch(s) instead of include, e.g. -na opengl,metal') parser.add_argument('files', nargs='*', help='Files to be tested') - - # commands = parser.add_subparsers(help='Available Taichi commands') - # examples = commands.add_parser('examples', help='Interact with Taichi examples') - return parser From b7a5e0b47d4af4f6244246c8a7f699ee49b1ec9d Mon Sep 17 00:00:00 2001 From: Rex W Date: Mon, 18 May 2020 22:06:00 -0400 Subject: [PATCH 12/20] Travis CI failed. Please retry. --- python/taichi/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/taichi/main.py b/python/taichi/main.py index 62b9212c40885..ab2aeaebd8dbf 100644 --- a/python/taichi/main.py +++ b/python/taichi/main.py @@ -74,7 +74,7 @@ def test_cpp(args): def run_example(name: str): - """Run an example based on example NAME.""" + """Run an example based on the example NAME.""" import taichi as ti from pathlib import Path From 61d0720a52946ebf59ccd525148b0c0e697d39a7 Mon Sep 17 00:00:00 2001 From: Taichi Gardener Date: Mon, 18 May 2020 22:16:54 -0400 Subject: [PATCH 13/20] [skip ci] enforce code format --- docs/utilities.rst | 2 +- python/taichi/main.py | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/utilities.rst b/docs/utilities.rst index 777abc64e5d6a..7043b75e09db4 100644 --- a/docs/utilities.rst +++ b/docs/utilities.rst @@ -62,7 +62,7 @@ The suggested workflow for the performance related PR author to run the regressi * Run ``git checkout -b your-branch-name``. * Do works on the issue, stage 1. - + * Run ``ti benchmark && ti regression`` to obtain the result. * (If result BAD) Do further improvements, until the result is satisfying. diff --git a/python/taichi/main.py b/python/taichi/main.py index 3844295cfc523..a3c3f0577e6cf 100644 --- a/python/taichi/main.py +++ b/python/taichi/main.py @@ -108,8 +108,10 @@ def display_benchmark_regression(xd, yd, args): def parse_dat(file): dict = {} for line in open(file).readlines(): - try: a, b = line.strip().split(':') - except: continue + try: + a, b = line.strip().split(':') + except: + continue b = float(b) if abs(b % 1.0) < 1e-5: # codegen_* b = int(b) From 08eae82b7db443eea2c45f772daf5c31b389a67c Mon Sep 17 00:00:00 2001 From: Rex W Date: Mon, 18 May 2020 22:41:13 -0400 Subject: [PATCH 14/20] Travis CI failed. Please retry. --- python/taichi/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/taichi/main.py b/python/taichi/main.py index 3844295cfc523..f119087fd2c33 100644 --- a/python/taichi/main.py +++ b/python/taichi/main.py @@ -83,7 +83,7 @@ def run_example(name: str): examples_dir = Path(root_dir) / 'examples' all_examples = examples_dir.rglob('*.py') all_example_names = { - str(f.resolve()).split("/")[-1].split(".")[0] + str(f.resolve()).split('/')[-1].split('.')[0] for f in all_examples } From 99b3b9b32aed2328ed13a0ea4acf6e987da03242 Mon Sep 17 00:00:00 2001 From: Rex Date: Tue, 19 May 2020 22:10:50 -0400 Subject: [PATCH 15/20] In case the user forgets to specify anything. we SHOULD really switch to argparse. --- python/taichi/main.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/python/taichi/main.py b/python/taichi/main.py index 162c0c4b9d9bf..632adbccf98cb 100644 --- a/python/taichi/main.py +++ b/python/taichi/main.py @@ -73,7 +73,7 @@ def test_cpp(args): return int(task.run(*test_files)) -def run_example(name: str): +def run_example(name: list): """Run an example based on the example NAME.""" import taichi as ti from pathlib import Path @@ -86,12 +86,16 @@ def run_example(name: str): str(f.resolve()).split('/')[-1].split('.')[0] for f in all_examples } - - if not name in all_example_names: + if not len(name) == 1: + sys.exit( + f"Sorry, please specify a name of example!\nAvailable examples names are: {all_example_names}" + ) + elif name[0] not in all_example_names: sys.exit( - f"Sorry, {name} is not an available example name!\nAvailable examples names: {all_example_names}" + f"Sorry, {name} is not an available example name!\nAvailable examples names are: {all_example_names}" ) - importlib.import_module(f"examples.{name}") + print(f"Running example {name[0]} ...") + importlib.import_module(f"examples.{name[0]}") def get_benchmark_baseline_dir(): @@ -469,8 +473,7 @@ def main(debug=False): import shutil shutil.move('release.zip', fn) elif mode == "example": - example = sys.argv[2] - print(f"Running example {example} ...") + example = sys.argv[2:] run_example(name=example) else: name = sys.argv[1] From d566618bbd244e4776c44901de0450ef6f648ece Mon Sep 17 00:00:00 2001 From: Rex Date: Tue, 19 May 2020 22:14:22 -0400 Subject: [PATCH 16/20] [skip ci] i must be sleepy. --- python/taichi/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/taichi/main.py b/python/taichi/main.py index 632adbccf98cb..264246feed3af 100644 --- a/python/taichi/main.py +++ b/python/taichi/main.py @@ -86,7 +86,7 @@ def run_example(name: list): str(f.resolve()).split('/')[-1].split('.')[0] for f in all_examples } - if not len(name) == 1: + if len(name) != 1: sys.exit( f"Sorry, please specify a name of example!\nAvailable examples names are: {all_example_names}" ) From 417a10593b0ee56e0205567283efd06bd9595fb5 Mon Sep 17 00:00:00 2001 From: Rex W Date: Thu, 21 May 2020 18:29:57 -0400 Subject: [PATCH 17/20] How about factor them out. --- python/taichi/main.py | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/python/taichi/main.py b/python/taichi/main.py index 264246feed3af..b78f4b78e80f4 100644 --- a/python/taichi/main.py +++ b/python/taichi/main.py @@ -8,7 +8,8 @@ from collections import defaultdict from colorama import Fore, Back, Style from taichi.tools.video import make_video, interpolate_frames, mp4_to_gif, scale_video, crop_video, accelerate_video -import importlib +from pathlib import Path +import runpy def test_python(args): @@ -73,29 +74,38 @@ def test_cpp(args): return int(task.run(*test_files)) -def run_example(name: list): - """Run an example based on the example NAME.""" +def get_examples_dir() -> Path: + """Get the path to the examples directory.""" import taichi as ti - from pathlib import Path root_dir = ti.package_root() if ti.is_release() else ti.get_repo_directory( ) examples_dir = Path(root_dir) / 'examples' + return examples_dir + + +def get_available_examples() -> set: + """Get a set of all available example names.""" + examples_dir = get_examples_dir() all_examples = examples_dir.rglob('*.py') all_example_names = { str(f.resolve()).split('/')[-1].split('.')[0] for f in all_examples } - if len(name) != 1: - sys.exit( - f"Sorry, please specify a name of example!\nAvailable examples names are: {all_example_names}" - ) - elif name[0] not in all_example_names: + return all_example_names + + +def run_example(name: str): + """Run an example based on the example NAME.""" + all_example_names = get_available_examples() + if name not in all_example_names: sys.exit( - f"Sorry, {name} is not an available example name!\nAvailable examples names are: {all_example_names}" + f"Sorry, {name} is not an available example name!\nAvailable example names are: {sorted(all_example_names)}" ) - print(f"Running example {name[0]} ...") - importlib.import_module(f"examples.{name[0]}") + examples_dir = get_examples_dir() + target = str((examples_dir / f"{name}.py").resolve()) + print(f"Running example {name} ...") + runpy.run_path(target, run_name='__main__') def get_benchmark_baseline_dir(): @@ -473,7 +483,11 @@ def main(debug=False): import shutil shutil.move('release.zip', fn) elif mode == "example": - example = sys.argv[2:] + if len(sys.argv) != 3: + sys.exit( + f"Invalid arguments! Usage: ti example [name]\nAvailable example names are: {sorted(get_available_examples())}" + ) + example = sys.argv[2] run_example(name=example) else: name = sys.argv[1] From 28c441794a84784e9647262f966900307ce80bcd Mon Sep 17 00:00:00 2001 From: Rex Date: Thu, 21 May 2020 19:43:11 -0400 Subject: [PATCH 18/20] One more thing to make it runnable from anywhere. --- python/taichi/main.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/python/taichi/main.py b/python/taichi/main.py index b78f4b78e80f4..f0844576258b4 100644 --- a/python/taichi/main.py +++ b/python/taichi/main.py @@ -104,6 +104,9 @@ def run_example(name: str): ) examples_dir = get_examples_dir() target = str((examples_dir / f"{name}.py").resolve()) + # we need to modify path for examples that use + # implicit relative imports + sys.path.append(str(examples_dir.resolve())) print(f"Running example {name} ...") runpy.run_path(target, run_name='__main__') From 6cba5b811d22eef10a9fb7e9f77be042fb9133e1 Mon Sep 17 00:00:00 2001 From: Rex Date: Thu, 21 May 2020 19:45:33 -0400 Subject: [PATCH 19/20] Alright lint. --- python/taichi/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/taichi/main.py b/python/taichi/main.py index f0844576258b4..0c752c84d36c1 100644 --- a/python/taichi/main.py +++ b/python/taichi/main.py @@ -104,7 +104,7 @@ def run_example(name: str): ) examples_dir = get_examples_dir() target = str((examples_dir / f"{name}.py").resolve()) - # we need to modify path for examples that use + # we need to modify path for examples that use # implicit relative imports sys.path.append(str(examples_dir.resolve())) print(f"Running example {name} ...") From e4131f2814c50da1d2a5f4d730a7486a9d8b7ef9 Mon Sep 17 00:00:00 2001 From: Yuanming Hu Date: Thu, 21 May 2020 21:28:20 -0400 Subject: [PATCH 20/20] add taichi.examples to package --- python/MANIFEST.in | 1 - setup.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/python/MANIFEST.in b/python/MANIFEST.in index 95330015e4dbf..cad89c137928c 100644 --- a/python/MANIFEST.in +++ b/python/MANIFEST.in @@ -1,4 +1,3 @@ -include README include MANIFEST.in include *.txt include *.py diff --git a/setup.py b/setup.py index cddffc1df6d1b..a33e30e2a115b 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ data_files = glob.glob('python/lib/*') print(data_files) -packages = setuptools.find_packages() +packages = setuptools.find_packages() + ['taichi.examples'] print(packages) setuptools.setup(