Find resource examples.
()
| 26 | |
| 27 | |
| 28 | def find_example_resource(): |
| 29 | """Find resource examples.""" |
| 30 | curr_path = os.path.dirname(os.path.realpath(os.path.expanduser(__file__))) |
| 31 | base_path = os.path.abspath(os.path.join(curr_path, "..", "..", "..")) |
| 32 | index_page = os.path.join(base_path, "web", "apps", "browser", "rpc_server.html") |
| 33 | default_plugin_page = os.path.join(base_path, "web", "apps", "browser", "rpc_plugin.html") |
| 34 | |
| 35 | resource_files = [ |
| 36 | ("/", os.path.join(base_path, "web", "dist", "tvmjs.bundle.js")), |
| 37 | ("/", os.path.join(base_path, "web", "dist", "wasm", "tvmjs_runtime.wasi.js")), |
| 38 | ("/", index_page), |
| 39 | ] |
| 40 | allow_format = ("json", "bin", "js", "wasm", "html", "css", "model") |
| 41 | |
| 42 | # recursively apend things in www, up to two levels |
| 43 | resource_bases = [ |
| 44 | os.path.join(base_path, "web", "dist", "www"), |
| 45 | os.path.join(base_path, "web", ".tensor_cache"), |
| 46 | ] |
| 47 | for base in resource_bases: |
| 48 | if not os.path.isdir(base): |
| 49 | continue |
| 50 | for full_name in glob.glob(f"{base}/**", recursive=True): |
| 51 | fname = os.path.relpath(full_name, base) |
| 52 | dirname = os.path.dirname(fname) |
| 53 | fmt = fname.rsplit(".", 1)[-1] |
| 54 | if os.path.isfile(full_name) and fmt in allow_format: |
| 55 | resource_files.append((dirname, full_name)) |
| 56 | |
| 57 | for item in resource_files: |
| 58 | fname = item[-1] |
| 59 | if not os.path.exists(fname): |
| 60 | raise RuntimeError(f"Cannot find {fname}") |
| 61 | |
| 62 | if not any(item[-1].endswith("rpc_plugin.html") for item in resource_files): |
| 63 | resource_files.append(("/", default_plugin_page)) |
| 64 | |
| 65 | return index_page, resource_files |
| 66 | |
| 67 | |
| 68 | def main(args): |