Detect Orca source from anywhere and make scripts work from anywhere

This commit is contained in:
Ben Visness 2023-08-16 19:58:52 -05:00 committed by Martin Fouilleul
parent 4ae8bc3bfa
commit 18069fbc58
2 changed files with 74 additions and 22 deletions

45
orca
View File

@ -7,24 +7,45 @@ import os
import sys import sys
root = True if __name__ != "__main__":
try: print("why are you importing the orca command-line tool as a Python module, you absolute goofball")
os.stat(".orcaroot") exit(1)
except FileNotFoundError:
root = False
if root:
# Running from Orca source checkout; use local source's scripts.
scriptdir = os.path.dirname(os.path.abspath(__file__)) # If you modify this, be sure to modify the version in scripts/dev.py as well.
if scriptdir != os.getcwd(): def check_if_source():
# Only print this warning if running the system-installed Orca. def path_is_in_orca_source(path):
# It's annoying to see this if you run ./orca from the source. dir = path
while True:
try:
os.stat(os.path.join(dir, ".orcaroot"))
return (True, dir)
except FileNotFoundError:
pass
newdir = os.path.dirname(dir)
if newdir == dir:
return (False, None)
dir = newdir
in_source, current_source_dir = path_is_in_orca_source(os.getcwd())
script_is_source, script_source_dir = path_is_in_orca_source(os.path.dirname(os.path.abspath(__file__)))
use_source = in_source or script_is_source
source_dir = current_source_dir or script_source_dir
return (use_source, source_dir, script_is_source)
use_source, source_dir, is_source = check_if_source()
if use_source:
# Use the source checkout's scripts instead of the system-installed scripts.
if not is_source:
print("The Orca tool is running from a local source checkout and will") print("The Orca tool is running from a local source checkout and will")
print("use that instead of the system Orca installation.") print("use that instead of the system Orca installation.")
print() print()
sys.path.append(os.getcwd()) sys.path.append(source_dir)
import scripts.orca import scripts.orca
else: else:
# Running from outside Orca source checkout; use system Orca install. # Running from outside Orca source checkout; use system Orca install.

View File

@ -19,9 +19,9 @@ ANGLE_VERSION = "2023-07-05"
def attach_dev_commands(subparsers): def attach_dev_commands(subparsers):
dev_cmd = subparsers.add_parser("dev", help="Commands for building Orca itself. Must be run from the root of an Orca source checkout.") dev_cmd = subparsers.add_parser("dev", help="Commands for building Orca itself. Must be run from the root of an Orca source checkout.")
dev_cmd.set_defaults(func=orca_root_only) dev_cmd.set_defaults(func=orca_source_only)
dev_sub = dev_cmd.add_subparsers(required=is_orca_root(), title='commands') dev_sub = dev_cmd.add_subparsers(required=is_orca_source(), title='commands')
build_cmd = dev_sub.add_parser("build-runtime", help="Build the Orca runtime from source.") build_cmd = dev_sub.add_parser("build-runtime", help="Build the Orca runtime from source.")
build_cmd.add_argument("--release", action="store_true", help="compile Orca in release mode (default is debug)") build_cmd.add_argument("--release", action="store_true", help="compile Orca in release mode (default is debug)")
@ -35,15 +35,39 @@ def attach_dev_commands(subparsers):
install_cmd.set_defaults(func=dev_shellish(install)) install_cmd.set_defaults(func=dev_shellish(install))
def is_orca_root(): # Checks if the Orca tool should use a source checkout of Orca instead of a system install.
try: # This is copy-pasted to the command-line tool so it can work before loading anything.
os.stat(".orcaroot") #
return True # Returns: (use source, source directory, is actually the source's tool)
except FileNotFoundError: def check_if_source():
return False def path_is_in_orca_source(path):
dir = path
while True:
try:
os.stat(os.path.join(dir, ".orcaroot"))
return (True, dir)
except FileNotFoundError:
pass
newdir = os.path.dirname(dir)
if newdir == dir: # TODO: Verify on Windows (it will probably not work)
return (False, None)
dir = newdir
in_source, current_source_dir = path_is_in_orca_source(os.getcwd())
script_is_source, script_source_dir = path_is_in_orca_source(os.path.dirname(os.path.abspath(__file__)))
use_source = in_source or script_is_source
source_dir = current_source_dir or script_source_dir
return (use_source, source_dir, script_is_source)
def orca_root_only(args): def is_orca_source():
use_source, _, _ = check_if_source()
return use_source
def orca_source_only(args):
print("The Orca dev commands can only be run from an Orca source checkout.") print("The Orca dev commands can only be run from an Orca source checkout.")
print() print()
print("If you want to build Orca yourself, download the source here:") print("If you want to build Orca yourself, download the source here:")
@ -52,7 +76,14 @@ def orca_root_only(args):
def dev_shellish(func): def dev_shellish(func):
return shellish(func) if is_orca_root() else orca_root_only use_source, source_dir, _ = check_if_source()
if not use_source:
return orca_source_only
def func_from_source(args):
os.chdir(source_dir)
func(args)
return shellish(func_from_source)
def build_runtime(args): def build_runtime(args):