|
38 | 38 |
|
39 | 39 |
|
40 | 40 | def get_examples_dir_or_die():
|
41 |
| - # Examples are provided for Jupyter and Commandline flows. This returns the |
42 |
| - # examples directory in both flows. |
43 |
| - |
44 |
| - # First priority - locally installed examples with the default directory name |
45 |
| - # Case for most users that installed examples. |
46 |
| - examples_dir = os.path.join(os.getcwd(), "chipscopy-examples") |
47 |
| - if os.path.isdir(examples_dir): |
48 |
| - return examples_dir |
49 |
| - |
50 |
| - # If we get here, try to find examples in the site-libraries area or wherever |
51 |
| - # we are running chipscopy from. This makes life easier for developers working |
52 |
| - # who want to use examples directly from the chipscopy/examples directory. |
53 |
| - if os.path.isfile(inspect.getfile(inspect.currentframe())): |
54 |
| - # __file__ does not work in jupyter flows |
55 |
| - examples_dir = os.path.realpath( |
56 |
| - os.path.dirname(inspect.getfile(inspect.currentframe())) + "/examples" |
57 |
| - ) |
| 41 | + # Check if the environment variable is set |
| 42 | + env_examples_dir = os.getenv("CHIPSCOPY_EXAMPLES") |
| 43 | + if env_examples_dir and os.path.isdir(env_examples_dir): |
| 44 | + return env_examples_dir |
| 45 | + |
| 46 | + # Start from the current working directory |
| 47 | + current_dir = os.getcwd() |
| 48 | + |
| 49 | + while True: |
| 50 | + # Check for 'chipscopy-examples' directory |
| 51 | + examples_dir = os.path.join(current_dir, "chipscopy-examples") |
| 52 | + if os.path.isdir(examples_dir): |
| 53 | + return examples_dir |
| 54 | + |
| 55 | + # Check for 'examples' directory |
| 56 | + examples_dir = os.path.join(current_dir, "examples") |
58 | 57 | if os.path.isdir(examples_dir):
|
59 | 58 | return examples_dir
|
60 | 59 |
|
61 |
| - # Jupyter doesn't work with currentframe above, just try examples as a last resort |
62 |
| - examples_dir = os.path.join(os.getcwd(), "examples") |
63 |
| - if os.path.isdir(examples_dir): |
64 |
| - return examples_dir |
| 60 | + # Move up one directory level |
| 61 | + parent_dir = os.path.dirname(current_dir) |
| 62 | + |
| 63 | + # If the parent directory is the same as the current directory, we've reached the root |
| 64 | + if parent_dir == current_dir: |
| 65 | + break |
| 66 | + |
| 67 | + current_dir = parent_dir |
65 | 68 |
|
66 |
| - raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), examples_dir) |
| 69 | + # If we reach here, neither directory was found |
| 70 | + error_message = ( |
| 71 | + "Could not find the 'examples' or 'chipscopy-examples' directory.\n" |
| 72 | + "Please ensure that one of these directories exists in the current or parent directories.\n" |
| 73 | + "Alternatively, you can set the 'CHIPSCOPY_EXAMPLES' environment variable to the path of the examples directory.\n" |
| 74 | + "Example: export CHIPSCOPY_EXAMPLES=/path/to/your/examples" |
| 75 | + ) |
| 76 | + raise FileNotFoundError(errno.ENOENT, error_message) |
67 | 77 |
|
68 | 78 |
|
69 | 79 | def _get_design_files_in_dir(p: Path) -> namedtuple:
|
|
0 commit comments