Skip to content

Commit

Permalink
format_all
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanming-hu committed Dec 16, 2019
1 parent 71c60d1 commit 00e3253
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 deletions.
File renamed without changes.
73 changes: 52 additions & 21 deletions python/taichi/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,61 @@
import shutil
import sys
import ctypes
import glob

if sys.version_info[0] < 3 or sys.version_info[1] < 5:
print("\nPlease restart with python3. \n(Taichi supports Python 3.5+)\n")
print("Current version:", sys.version_info)
exit(-1)

tc_core = None


def in_docker():
if os.environ.get("TI_IN_DOCKER", "") == "":
return False
else:
return True


def import_tc_core():
global tc_core
if get_os_name() != 'win':
old_flags = sys.getdlopenflags()
sys.setdlopenflags(258) # 258 = RTLD_NOW | RTLD_GLOBAL
sys.setdlopenflags(258) # 258 = RTLD_NOW | RTLD_GLOBAL
else:
pyddir = os.path.join(package_root(), 'lib')
os.environ['PATH'] += ';' + pyddir
os.environ['PATH'] += ';' + pyddir
import taichi_core as core
tc_core = core
if get_os_name() != 'win':
sys.setdlopenflags(old_flags)
core.set_lib_dir(os.path.join(package_root(), 'lib'))



def is_ci():
return os.environ.get('TC_CI', '') == '1'


def package_root():
return os.path.join(os.path.dirname(os.path.realpath(__file__)), '../')


def is_release():
return os.environ.get('TAICHI_REPO_DIR', '') == ''


from colorama import Fore, Back, Style


def get_core_shared_object():
if is_release():
directory = os.path.join(package_root(), 'lib')
else:
directory = get_bin_directory()
return os.path.join(directory, 'libtaichi_core.so')


def get_repo():
from git import Repo
import taichi as tc
Expand All @@ -62,26 +71,36 @@ def print_red_bold(*args, **kwargs):
print(Style.RESET_ALL, end='')


def format():
def format(all=False):
import os
import taichi as tc
from yapf.yapflib.yapf_api import FormatFile
repo = get_repo()

print('* Formatting code', end='')
for item in repo.index.diff('HEAD'):
print('Code formatting ...')
if all:
directories = ['taichi', 'tests', 'examples', 'misc', 'python']
files = []
for d in directories:
files += glob.glob(os.path.join(tc.get_repo_directory(), d) + '/**/*')
else:
files = repo.index.diff('HEAD')

for item in files:
fn = os.path.join(tc.get_repo_directory(), item.a_path)
print(end='.')
if fn.endswith('.py'):
print(fn, '...')
FormatFile(
fn,
in_place=True,
style_config=os.path.join(tc.get_repo_directory(), '.style.yapf'))
if fn.endswith('.cpp'):
os.system('clang-format -i -style=file {}'.format(fn))
style_config=os.path.join(tc.get_repo_directory(), 'misc',
'.style.yapf'))
if fn.endswith('.cpp') or fn.endswith('.h'):
print(fn, '...')
os.system('clang-format-6.0 -i -style=file {}'.format(fn))
repo.git.add(item.a_path)

print('* Done!')
print('Formatting done!')


from taichi.misc.settings import get_output_directory, get_build_directory, get_bin_directory, get_repo_directory, get_runtime_directory
Expand All @@ -102,7 +121,7 @@ def build():
os.chdir(bin_dir)

flags = ' -DPYTHON_EXECUTABLE:FILEPATH="{}"'.format(sys.executable)

print('Running cmake...')
if is_ci():
print(' Note: building for CI.')
Expand All @@ -117,7 +136,8 @@ def build():
print('Building taichi...')
num_make_threads = min(20, multiprocessing.cpu_count())
if get_os_name() == 'win':
make_ret = os.system("msbuild /p:Configuration=Release /p:Platform=x64 /m taichi.sln")
make_ret = os.system(
"msbuild /p:Configuration=Release /p:Platform=x64 /m taichi.sln")
else:
make_ret = os.system('make -j {}'.format(num_make_threads))
if make_ret != 0:
Expand All @@ -126,6 +146,7 @@ def build():

os.chdir(tmp_cwd)


if is_release():
print("[Release mode]")
sys.path.append(os.path.join(package_root(), 'lib'))
Expand Down Expand Up @@ -217,7 +238,8 @@ def build():
raise e

os.chdir(old_wd)



def get_dll_name(name):
if get_os_name() == 'linux':
return 'libtaichi_%s.so' % name
Expand All @@ -228,6 +250,7 @@ def get_dll_name(name):
else:
assert False, "Unknown OS"


def load_module(name, verbose=True):
if verbose:
print('Loading module', name)
Expand All @@ -240,11 +263,13 @@ def load_module(name, verbose=True):
ctypes.PyDLL(name, mode=mode)
else:
ctypes.PyDLL(
os.path.join(get_repo_directory(), 'build',
get_dll_name(name)), mode=mode)
os.path.join(get_repo_directory(), 'build', get_dll_name(name)),
mode=mode)
except Exception as e:
print(Fore.YELLOW + "Warning: module [{}] loading failed: {}".format(
name, e) + Style.RESET_ALL)
print(Fore.YELLOW +
"Warning: module [{}] loading failed: {}".format(name, e) +
Style.RESET_ALL)


def at_startup():
if not is_release():
Expand All @@ -266,6 +291,7 @@ def start_memory_monitoring(output_fn, pid=-1, interval=1):
if pid == -1:
pid = os.getpid()
import multiprocessing

def task():
with open(output_fn, 'w') as f:
process = psutil.Process(pid)
Expand All @@ -277,6 +303,7 @@ def task():
time.sleep(interval)
print(time.time(), mem, file=f)
f.flush()

proc = multiprocessing.Process(target=task, daemon=True)
proc.start()

Expand All @@ -285,10 +312,14 @@ def task():
def clean_libs():
pass


at_startup()

device_string = 'cpu only' if not tc_core.with_cuda() else 'cuda {}'.format(tc_core.cuda_version())
print('[Taichi version {}, {}, commit {}]'.format(tc_core.get_version_string(), device_string, tc_core.get_commit_hash()[:8]))
device_string = 'cpu only' if not tc_core.with_cuda() else 'cuda {}'.format(
tc_core.cuda_version())
print('[Taichi version {}, {}, commit {}]'.format(
tc_core.get_version_string(), device_string,
tc_core.get_commit_hash()[:8]))

if not is_release():
tc_core.set_core_trigger_gdb_when_crash(True)
4 changes: 4 additions & 0 deletions python/taichi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def main(debug=False):
" ti test |-> Run all tests\n"
" ti test_python |-> Run python tests\n"
" ti test_cpp |-> Run cpp tests\n"
" ti format |-> Reformat modified source files\n"
" ti format_all |-> Reformat all source files\n"
" ti build |-> Build C++ files\n"
" ti video |-> Make a video using *.png files in the current folder\n"
" ti video_scale |-> Scale video resolution \n"
Expand Down Expand Up @@ -94,6 +96,8 @@ def main(debug=False):
ti.core.build()
elif mode == "format":
ti.core.format()
elif mode == "format_all":
ti.core.format(all=True)
elif mode == "statement":
exec(sys.argv[2])
elif mode == "update":
Expand Down

0 comments on commit 00e3253

Please sign in to comment.