How can I get the construction environment from Python postinstall scripts defined in library.json?

For example, consider a library somelib with the following directory structure:

somelib/
├── examples/
│   └── platformio/
│       ├── etc/
│       ├── include/
│       ├── lib/
│       ├── src/
│       ├── test/
│       └── platformio.ini
├── include/
│   └── somelib.h
├── scripts/
│   └── platformio/
│       └── postinstall.py
└── library.json

And the following relevant content in the PlatformIO files:

  • examples/platformio/platformio.ini
    [env]
    lib_deps =
      somelib=symlink://../..
    
  • library.json
    {
      "scripts": {
        "postinstall": "scripts/platformio/postinstall.py"
      }
    }
    
  • scripts/platformio/postinstall.py
    import traceback
    
    try: 
        Import("env")
    except:
        traceback.print_exc()
        try: 
            env = DefaultEnvironment()
        except:
            traceback.print_exc()
    
    print(env)
    

Now when trying to run the example project, I encounter the following errors:

$ cd examples/platformio
$ pio run
Processing [...]
---------------------------------------------------------------------------------------
Library Manager: Installing symlink://../../
Traceback (most recent call last):
File "/opt/somelib/src/scripts/platformio/postinstall.py", line 4, in <module>
Import("env")
^^^^^^
NameError: name 'Import' is not defined
Traceback (most recent call last):
File "/opt/somelib/src/scripts/platformio/postinstall.py", line 4, in <module>
Import("env")
^^^^^^
NameError: name 'Import' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/opt/somelib/src/scripts/platformio/postinstall.py", line 8, in <module>
env = DefaultEnvironment()
      ^^^^^^^^^^^^^^^^^^
NameError: name 'DefaultEnvironment' is not defined
Traceback (most recent call last):
File "/opt/somelib/src/scripts/platformio/postinstall.py", line 12, in <module>
print(env)
      ^^^
NameError: name 'env' is not defined

Note I don’t see these errors when using, e.g., extra_scripts = post:[...] from a project’s platformio.ini. These errors appear only when using scripts defined in a library.json.

How should I be retrieving the construction environment in this case?


EDIT

If you are recreating this example, be aware that PlatformIO considers the library installed after producing these errors — i.e., running the project with pio run again will not execute the postinstall scripts or generate any errors. You will need to remove the installed library before running again:

$ pio pkg uninstall --no-save -l somelib # || rm -rf .pio

To the developers: consider ways to handle dependencies installed after their preinstall/postinstall scripts exit with non-zero status.

In my case above, I would prefer that the installation fail. Whether this means it is removed or is somehow marked incomplete, it should effectually cause PlatformIO to re-install it when I run the project again.

However, that solution may be extreme in the general case. Another option would be to highlight the command that failed in the error message/diagnostics so the user can re-run it manually without having to intervene with dependency management.