示例#1
0
def main():
    lines = []
    print()
    lines.append(u'{:^43}'.format(u' '.join([u'\u262f'] * 8)))
    lines.append(u' *******************************************')
    lines.append(u' **                Taichi                 **')
    lines.append(u' **                ~~~~~~                 **')
    lines.append(u' ** Open Source Computer Graphics Library **')
    lines.append(u' *******************************************')
    lines.append(u'{:^43}'.format(u"\u2630 \u2631 \u2632 \u2633 "
                                  "\u2634 \u2635 \u2636 \u2637"))
    print(u'\n'.join(lines))
    print()
    import taichi as tc

    argc = len(sys.argv)
    if argc == 1 or sys.argv[1] == 'help':
        print(
            "    Usage: ti run [task name]        |-> Run a specific task\n"
            "           ti test                   |-> Run tests\n"
            "           ti daemon                 |-> Start daemon process\n"
            "           ti install                |-> Install package\n"
            "           ti proj                   |-> List all projects\n"
            "           ti proj activate [name]   |-> Activate project\n"
            "           ti proj deactivate [name] |-> Deactivate project\n"
            "           ti build                  |-> Build C++ files\n"
            "           ti amal                   |-> Generate amalgamated taichi.h\n"
            "           ti clean asm [*.s]        |-> Clean up gcc ASM\n"
            "           ti plot [*.txt]           |-> Plot a memory usage curve\n"
            "           ti update                 |-> Update taichi and projects\n"
            "           ti video                  |-> Make a video using *.png files in the current folder\n"
            "           ti convert                |-> Delete color controllers in a log file\n"
            "           ti exec                   |-> Invoke a executable in the 'build' folder\n"
            "           ti format                 |-> Format taichi and projects\n"
            "                                         (C++ source and python scripts)\n"
            "           ti statement [statement]  |-> Execute a single statement (with taichi imported as tc\n"
            "           ti [script.py]            |-> Run script\n"
            "           ti doc                    |-> Build documentation\n"
            "           ti merge                  |-> Merge images in folders horizontally\n"
            "           ti debug [script.py]      |-> Debug script\n")
        exit(-1)
    mode = sys.argv[1]

    if mode.endswith('.py'):
        with open(mode) as script:
            script = script.read()
        exec(script, {'__name__': '__main__'})
        exit()

    if mode.endswith('.cpp'):
        command = 'g++ {} -o {} -g -std=c++14 -O3 -lX11 -lpthread'.format(
            mode, mode[:-4])
        print(command)
        ret = os.system(command)
        if ret == 0:
            os.system('./{}'.format(mode[:-4]))
        exit()

    if mode == "run":
        if argc <= 2:
            print("Please specify [task name], e.g. test_math")
            exit(-1)
        name = sys.argv[2]
        task = tc.Task(name)
        task.run(*sys.argv[3:])
    elif mode == "debug":
        tc.core.set_core_trigger_gdb_when_crash(True)
        if argc <= 2:
            print("Please specify [file name], e.g. render.py")
            exit(-1)
        name = sys.argv[2]
        with open(name) as script:
            script = script.read()
        exec(script, {'__name__': '__main__'})
        exit()
    elif mode == "daemon":
        from taichi.system.daemon import start
        if len(sys.argv) > 2:
            # Master daemon
            start(True)
        else:
            # Slave daemon
            start(False)
    elif mode == "proj":
        if len(sys.argv) == 2:
            print_all_projects()
        elif sys.argv[2] == 'activate':
            proj = sys.argv[3]
            activate_package(proj)
        elif sys.argv[2] == 'deactivate':
            proj = sys.argv[3]
            deactivate_package(proj)
        else:
            assert False
    elif mode == "test":
        task = tc.Task('test')
        task.run(*sys.argv[2:])
    elif mode == "build":
        tc.core.build()
    elif mode == "format":
        tc.core.format()
    elif mode == "statement":
        exec(sys.argv[2])
    elif mode == "plot":
        plot(sys.argv[2])
    elif mode == "update":
        tc.core.update(True)
        tc.core.build()
    elif mode == "install":
        os.chdir(tc.get_directory('projects'))
        pkg = sys.argv[2]
        if pkg not in packages:
            tc.error('package {} not found.'.format(pkg))
        else:
            tc.info('Installing package {}...'.format(pkg))
            url = packages[pkg]
        os.system('git clone {} {} --depth=1'.format(url, pkg))
        tc.core.build()
    elif mode == "asm":
        fn = sys.argv[2]
        os.system(
            r"sed '/^\s*\.\(L[A-Z]\|[a-z]\)/ d' {0} > clean_{0}".format(fn))
    elif mode == "exec":
        import subprocess
        exec_name = sys.argv[2]
        folder = tc.get_bin_directory()
        assert exec_name in os.listdir(folder)
        subprocess.call([os.path.join(folder, exec_name)] + sys.argv[3:])
    elif mode == "interpolate":
        interpolate_frames('.')
    elif mode == "amal":
        cwd = os.getcwd()
        os.chdir(tc.get_repo_directory())
        with open('misc/amalgamate.py') as script:
            script = script.read()
        exec(script, {'__name__': '__main__'})
        os.chdir(cwd)
        shutil.copy(os.path.join(tc.get_repo_directory(), 'build/taichi.h'),
                    './taichi.h')
        exit()
    elif mode == "doc":
        os.system('cd docs && sphinx-build -b html . build')
    elif mode == "video":
        files = sorted(os.listdir('.'))
        files = list(filter(lambda x: x.endswith('.png'), files))
        if len(sys.argv) >= 3:
            frame_rate = int(sys.argv[2])
        else:
            frame_rate = 24
        if len(sys.argv) >= 4:
            trunc = int(sys.argv[3])
            files = files[:trunc]
        tc.info('Making video using {} png files...', len(files))
        tc.info("frame_rate={}", frame_rate)
        output_fn = 'video.mp4'
        make_video(files, output_path=output_fn, frame_rate=frame_rate)
        tc.info('Done! Output video file = {}', output_fn)
    elif mode == "convert":
        # http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed
        # TODO: Windows support
        for fn in sys.argv[2:]:
            print("Converting logging file: {}".format(fn))
            tmp_fn = '/tmp/{}.{:05d}.backup'.format(fn,
                                                    random.randint(0, 10000))
            shutil.move(fn, tmp_fn)
            command = r'sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"'
            os.system('{} {} > {}'.format(command, tmp_fn, fn))
    elif mode == "merge":
        import cv2  # TODO: remove this dependency
        import numpy as np
        folders = sys.argv[2:]
        os.makedirs('merged', exist_ok=True)
        for fn in sorted(os.listdir(folders[0])):
            imgs = []
            for fld in folders:
                img = cv2.imread(os.path.join(fld, fn))
                imgs.append(img)
            img = np.hstack(imgs)
            cv2.imwrite(os.path.join('merged', fn), img)
    else:
        name = sys.argv[1]
        print('Running task [{}]...'.format(name))
        task = tc.Task(name)
        task.run(*sys.argv[2:])
示例#2
0
def main():
  lines = []
  print()
  lines.append(u'{:^43}'.format(u' '.join([u'\u262f'] * 8)))
  lines.append(u' ******************************************')
  lines.append(u' ** Taichi - A Computer Graphics Library **')
  lines.append(u' ******************************************')
  lines.append(u'{:^43}'.format(u"\u2630 \u2631 \u2632 \u2633 "
                                "\u2634 \u2635 \u2636 \u2637"))
  print(u'\n'.join(lines))
  print()

  argc = len(sys.argv)
  if argc == 1 or sys.argv[1] == 'help':
    print(
        "    Usage: ti run [task name]        |-> Run a specific task\n"
        "           ti test                   |-> Run tests\n"
        "           ti build                  |-> Build C++ files\n"
        "           ti update                 |-> Update taichi and projects\n"
        "           ti video                  |-> Make a video using *.png files in the current folder\n"
        "           ti convert                |-> Delete color controllers in a log file\n"
        "           ti exec                   |-> Invoke a executable in the 'build' folder\n"
        "           ti format                 |-> Format taichi and projects\n"
        "                                         (C++ source and python scripts)\n"
        "           ti *.py [arguments]       |-> Run scripts\n")
    exit(-1)
  mode = sys.argv[1]

  if mode.endswith('.py'):
    with open(mode) as script:
      exec(script.read())
    exit()

  if mode == "run":
    if argc <= 2:
      print("Please specify [task name], e.g. test_math")
      exit(-1)
    name = sys.argv[2]
    task = tc.Task(name)
    task.run(sys.argv[3:])
  elif mode == "test":
    # tc.core.set_core_trigger_gdb_when_crash(True)
    task = tc.Task('test')
    task.run(sys.argv[2:])
  elif mode == "build":
    tc.core.build()
  elif mode == "format":
    tc.core.format()
  elif mode == "update":
    tc.core.update(True)
    tc.core.build()
  elif mode == "exec":
    import subprocess
    exec_name = sys.argv[2]
    folder = tc.get_bin_directory()
    assert exec_name in os.listdir(folder)
    subprocess.call([os.path.join(folder, exec_name)] + sys.argv[3:])
  elif mode == "video":
    files = sorted(os.listdir('.'))
    files = list(filter(lambda x: x.endswith('.png'), files))
    tc.info('Making video using {} png files...', len(files))
    output_fn = 'video.mp4'
    make_video(files, output_path=output_fn)
    tc.info('Done! Output video file = {}', output_fn)
  elif mode == "convert":
    # http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed
    # TODO: Windows support
    for fn in sys.argv[2:]:
      print("Converting logging file: {}".format(fn))
      tmp_fn = '/tmp/{}.{:05d}.backup'.format(fn, random.randint(0, 10000))
      shutil.move(fn, tmp_fn)
      command = r'sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"'
      os.system('{} {} > {}'.format(command, tmp_fn, fn))
  else:
    print("Unknown command '{}'".format(mode))
    exit(-1)
示例#3
0
文件: main.py 项目: lineCode/taichi
def main():
    lines = []
    print()
    lines.append(u'{:^43}'.format(u' '.join([u'\u262f'] * 8)))
    lines.append(u' *******************************************')
    lines.append(u' **                Taichi                 **')
    lines.append(u' **                ~~~~~~                 **')
    lines.append(u' ** Open Source Computer Graphics Library **')
    lines.append(u' *******************************************')
    lines.append(u'{:^43}'.format(u"\u2630 \u2631 \u2632 \u2633 "
                                  "\u2634 \u2635 \u2636 \u2637"))
    print(u'\n'.join(lines))
    print()
    import taichi as tc

    argc = len(sys.argv)
    if argc == 1 or sys.argv[1] == 'help':
        print(
            "    Usage: ti run [task name]        |-> Run a specific task\n"
            "           ti test                   |-> Run tests\n"
            "           ti daemon                 |-> Start daemon process\n"
            "           ti proj                   |-> List all projects\n"
            "           ti proj activate [name]   |-> Activate project\n"
            "           ti proj deactivate [name] |-> Deactivate project\n"
            "           ti build                  |-> Build C++ files\n"
            "           ti clean asm [*.s]        |-> Clean up gcc ASM\n"
            "           ti plot [*.txt]           |-> Plot a memory usage curve\n"
            "           ti update                 |-> Update taichi and projects\n"
            "           ti video                  |-> Make a video using *.png files in the current folder\n"
            "           ti convert                |-> Delete color controllers in a log file\n"
            "           ti exec                   |-> Invoke a executable in the 'build' folder\n"
            "           ti format                 |-> Format taichi and projects\n"
            "                                         (C++ source and python scripts)\n"
            "           ti statement [statement]  |-> Execute a single statement (with taichi imported as tc\n"
            "           ti [script.py]            |-> Run script\n"
            "           ti debug [script.py]      |-> Debug script\n")
        exit(-1)
    mode = sys.argv[1]

    if mode.endswith('.py'):
        with open(mode) as script:
            script = script.read()
        exec(script, {'__name__': '__main__'})
        exit()

    if mode == "run":
        if argc <= 2:
            print("Please specify [task name], e.g. test_math")
            exit(-1)
        name = sys.argv[2]
        task = tc.Task(name)
        task.run(*sys.argv[3:])
    elif mode == "debug":
        tc.core.set_core_trigger_gdb_when_crash(True)
        if argc <= 2:
            print("Please specify [file name], e.g. render.py")
            exit(-1)
        name = sys.argv[2]
        with open(name) as script:
            script = script.read()
        exec(script, {'__name__': '__main__'})
        exit()
    elif mode == "daemon":
        from taichi.system.daemon import start
        if len(sys.argv) > 2:
            # Master daemon
            start(True)
        else:
            # Slave daemon
            start(False)
    elif mode == "proj":
        if len(sys.argv) == 2:
            print_all_projects()
        elif sys.argv[2] == 'activate':
            proj = sys.argv[3]
            activate_package(proj)
        elif sys.argv[2] == 'deactivate':
            proj = sys.argv[3]
            deactivate_package(proj)
        else:
            assert False
    elif mode == "test":
        task = tc.Task('test')
        task.run(*sys.argv[2:])
    elif mode == "build":
        tc.core.build()
    elif mode == "format":
        tc.core.format()
    elif mode == "statement":
        exec(sys.argv[2])
    elif mode == "plot":
        plot(sys.argv[2])
    elif mode == "update":
        tc.core.update(True)
        tc.core.build()
    elif mode == "asm":
        fn = sys.argv[2]
        os.system(
            r"sed '/^\s*\.\(L[A-Z]\|[a-z]\)/ d' {0} > clean_{0}".format(fn))
    elif mode == "exec":
        import subprocess
        exec_name = sys.argv[2]
        folder = tc.get_bin_directory()
        assert exec_name in os.listdir(folder)
        subprocess.call([os.path.join(folder, exec_name)] + sys.argv[3:])
    elif mode == "interpolate":
        interpolate_frames('.')
    elif mode == "video":
        files = sorted(os.listdir('.'))
        files = list(filter(lambda x: x.endswith('.png'), files))
        if len(sys.argv) >= 3:
            frame_rate = int(sys.argv[2])
        else:
            frame_rate = 24
        if len(sys.argv) >= 4:
            trunc = int(sys.argv[3])
            files = files[:trunc]
        tc.info('Making video using {} png files...', len(files))
        tc.info("frame_rate={}", frame_rate)
        output_fn = 'video.mp4'
        make_video(files, output_path=output_fn, frame_rate=frame_rate)
        tc.info('Done! Output video file = {}', output_fn)
    elif mode == "convert":
        # http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed
        # TODO: Windows support
        for fn in sys.argv[2:]:
            print("Converting logging file: {}".format(fn))
            tmp_fn = '/tmp/{}.{:05d}.backup'.format(fn,
                                                    random.randint(0, 10000))
            shutil.move(fn, tmp_fn)
            command = r'sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"'
            os.system('{} {} > {}'.format(command, tmp_fn, fn))
    else:
        print("Unknown command '{}'".format(mode))
        exit(-1)
示例#4
0
def main(debug=False):
  lines = []
  print()
  lines.append(u' *******************************************')
  lines.append(u' **                Taichi                 **')
  lines.append(u' **                                       **')
  lines.append(u' ** High-Performance Programming Language **')
  lines.append(u' *******************************************')
  print(u'\n'.join(lines))
  print()
  import taichi as ti

  ti.tc_core.set_core_debug(debug)

  argc = len(sys.argv)
  if argc == 1 or sys.argv[1] == 'help':
    print(
      "    Usage: ti run [task name]        |-> Run a specific task\n"
      "           ti test                   |-> Run all tests\n"
      "           ti test_python            |-> Run python tests\n"
      "           ti test_cpp               |-> Run cpp tests\n"
      "           ti build                  |-> Build C++ files\n"
      "           ti video                  |-> Make a video using *.png files in the current folder\n"
      "           ti doc                    |-> Build documentation\n"
      "           ti debug [script.py]      |-> Debug script\n")
    exit(-1)
  mode = sys.argv[1]

  t = time.time()
  if mode.endswith('.py'):
    with open(mode) as script:
      script = script.read()
    exec(script, {'__name__': '__main__'})
  elif mode.endswith('.cpp'):
    command = 'g++ {} -o {} -g -std=c++14 -O3 -lX11 -lpthread'.format(mode, mode[:-4])
    print(command)
    ret = os.system(command)
    if ret == 0:
      os.system('./{}'.format(mode[:-4]))
  elif mode == "run":
    if argc <= 2:
      print("Please specify [task name], e.g. test_math")
      exit(-1)
    name = sys.argv[2]
    task = ti.Task(name)
    task.run(*sys.argv[3:])
  elif mode == "debug":
    ti.core.set_core_trigger_gdb_when_crash(True)
    if argc <= 2:
      print("Please specify [file name], e.g. render.py")
      exit(-1)
    name = sys.argv[2]
    with open(name) as script:
      script = script.read()
    exec(script, {'__name__': '__main__'})
  elif mode == "test_python":
    return test_python()
  elif mode == "test_cpp":
    return test_cpp()
  elif mode == "test":
    if test_python() != 0:
      return -1
    return test_cpp()
  elif mode == "build":
    ti.core.build()
  elif mode == "format":
    ti.core.format()
  elif mode == "statement":
    exec(sys.argv[2])
  elif mode == "update":
    ti.core.update(True)
    ti.core.build()
  elif mode == "asm":
    fn = sys.argv[2]
    os.system(r"sed '/^\s*\.\(L[A-Z]\|[a-z]\)/ d' {0} > clean_{0}".format(fn))
  elif mode == "exec":
    import subprocess
    exec_name = sys.argv[2]
    folder = ti.get_bin_directory()
    assert exec_name in os.listdir(folder)
    subprocess.call([os.path.join(folder, exec_name)] + sys.argv[3:])
  elif mode == "interpolate":
    interpolate_frames('.')
  elif mode == "amal":
    cwd = os.getcwd()
    os.chdir(ti.get_repo_directory())
    with open('misc/amalgamate.py') as script:
      script = script.read()
    exec(script, {'__name__': '__main__'})
    os.chdir(cwd)
    shutil.copy(os.path.join(ti.get_repo_directory(), 'build/taichi.h'), './taichi.h')
  elif mode == "doc":
    os.system('cd docs && sphinx-build -b html . build')
  elif mode == "video":
    files = sorted(os.listdir('.'))
    files = list(filter(lambda x: x.endswith('.png'), files))
    if len(sys.argv) >= 3:
      frame_rate = int(sys.argv[2])
    else:
      frame_rate = 24
    if len(sys.argv) >= 4:
      trunc = int(sys.argv[3])
      files = files[:trunc]
    ti.info('Making video using {} png files...', len(files))
    ti.info("frame_rate={}", frame_rate)
    output_fn = 'video.mp4'
    make_video(files, output_path=output_fn, frame_rate=frame_rate)
    ti.info('Done! Output video file = {}', output_fn)
  elif mode == "convert":
    # http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed
    # TODO: Windows support
    for fn in sys.argv[2:]:
      print("Converting logging file: {}".format(fn))
      tmp_fn = '/tmp/{}.{:05d}.backup'.format(fn, random.randint(0, 10000))
      shutil.move(fn, tmp_fn)
      command = r'sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"'
      os.system('{} {} > {}'.format(command, tmp_fn, fn))
  elif mode == "merge":
    import cv2 # TODO: remove this dependency
    import numpy as np
    folders = sys.argv[2:]
    os.makedirs('merged', exist_ok=True)
    for fn in sorted(os.listdir(folders[0])):
      imgs = []
      for fld in folders:
        img = cv2.imread(os.path.join(fld, fn))
        imgs.append(img)
      img = np.hstack(imgs)
      cv2.imwrite(os.path.join('merged', fn), img)
  else:
    name = sys.argv[1]
    print('Running task [{}]...'.format(name))
    task = ti.Task(name)
    task.run(*sys.argv[2:])
  print()
  print(">>> Running time: {:.2f}s".format(time.time() - t))