Hope this is the correct category for my question. I’m trying to import platformio
and use it in my Python project to build firmware.
I am doing something like:
from platformio.package.manager.platform import PlatformPackageManager
from platformio.platform.factory import PlatformFactory
from platformio.project.config import ProjectConfig
package_manager = PlatformPackageManager()
project_config = ProjectConfig(os.path.join("myproject", "platformio.ini")
project_config.items(env=self.env, as_dict=True)
package_manager.install(spec=platform)
platform = self.project_config.items(env="my_env", as_dict=True)["platform"]
factory = PlatformFactory.new(platform)
factory.run({"pioenv": "my_env", "project_config": "myproject"}, [], True, False, 1)
But I run into Error: BoardConfig: Board is not defined
. If I run the same code in the project folder (making sure it is the current directory) it works.
I’m wondering if I am missing something or if there is a much better way of importing the project to use it as a library? I noticed PlatformFactory.run()
only gives 0 or 1 for result but would be nice to capture errors as well.
Thank you for your time in advance!
I would direct questions regarding the PlatformIO core library against https://github.com/platformio/platformio-core/issues .
I came here as opening an issue there explicitly stated to:
This issue tracker is not the place for questions. If you want to ask how to do something, or to understand why something isn’t working the way you expect it to, use https://community.platformio.org
Hmmm in this case I would argue that questions regarding explicitly the PlatformIO core python code and not a “why does my PlatformIO project not build when using it normally” would be fair game. But you may be right.
@ivankravets can you have a look at why above python code that directly imports PlatformIO python libs throws that error?
1 Like
I figured this out so I thought I’d share in case a future someone encounters this.
What is happening is that at SCons build time the ProjectConfig
gets initialized again and the one from PlatformFactory
is ignored in platformio-core/pioproject.py at 59e1c8872680bd2c8af01471f3351707228f2c2c · platformio/platformio-core · GitHub . This may be considered a bug…
Sorry, the lowest API for PlatformIO Core is the Command-Line interface. This is the level which we have been maintaining since 2014. We don’t recommend to use internal things from PlatformIO Core. We changed the code structure very often and it can affect you.
Do you have any issues with CLI?
Related issue
opened 06:00PM - 15 Dec 20 UTC
closed 05:25PM - 22 Dec 20 UTC
wontfix
What kind of issue is this?
- [ ] **Question**.
This issue tracker is … not the place for questions. If you want to ask how to do something,
or to understand why something isn't working the way you expect it to,
use [Community Forums](https://community.platformio.org) or [Premium Support](https://platformio.org/support)
- [ ] **PlatformIO IDE**.
All issues related to PlatformIO IDE should be reported to appropriate repository:
[PlatformIO IDE for Atom](https://github.com/platformio/platformio-atom-ide/issues) or
[PlatformIO IDE for VSCode](https://github.com/platformio/platformio-vscode-ide/issues)
- [ ] **Development Platform or Board**.
All issues (building, uploading, adding new boards, etc.) related to PlatformIO development platforms
should be reported to appropriate repository related to your hardware
https://github.com/topics/platformio-platform
- [ ] **Feature Request**.
Start by telling us what problem you’re trying to solve. Often a solution
already exists! Don’t send pull requests to implement new features without first getting our
support. Sometimes we leave features out on purpose to keep the project small.
- [x] **PlatformIO Core**.
If you’ve found a bug, please provide an information below.
*You can erase any parts of this template not applicable to your Issue.*
------------------------------------------------------------------
### Configuration
**Operating system**: macOS 10.15.7
**PlatformIO Version** (`platformio --version`): PlatformIO, version 5.0.3
### Description of problem
First, I understand this may be out of scope and unsupported. If that is the case and it is not an issue worth pursuing then I understand if you want to close the issue.
I am trying to use `platformio` as a library to build programmatically in Python. I am building a `PlatformConfig` and `PlatformFactory`. I then update the default `PlatformFactory.config` with my built config (no file). But when SCons comes time to build the project it ends up ignoring this config because it appears to initialize a new one at https://github.com/platformio/platformio-core/blob/3e7e9e2b3d1b75cd76fc623b6adf9927f35f4225/platformio/builder/tools/pioproject.py#L20-L21
It feels like it should be reusing the configuration from the `PlatformFactory`. But maybe I am doing something wrong?
#### Steps to Reproduce
Example code:
```
import os
import sys
import configparser
from platformio.platform.factory import PlatformFactory
from platformio.project.config import ProjectConfig
platform = "espressif8266@2.6.2"
dir = "/Users/andy/Documents/GitHub/andyshinn/wled"
os.chdir(dir)
s = '''[env:quinled_dig_test]
extends = env:d1_mini
build_flags = ${common.build_flags_esp8266}
-D USERMOD_DALLASTEMPERATURE
-D USERMOD_DALLASTEMPERATURE_CELSIUS
lib_deps = ${env.lib_deps}
milesburton/DallasTemperature@^3.9.0
OneWire@~2.3.5'''
cc = configparser.ConfigParser()
cc.read_string(s)
pf = PlatformFactory.new("espressif8266@2.6.2")
pf.config.update([(cc.sections()[0], list(cc[cc.sections()[0]].config.items()))])
variables = variables = {"pioenv": "quinled_dig_test", "project_config": pf.config.path}
pr.run()
```
This works with WLED config from project config folder to extend:
```
[env:d1_mini]
board = d1_mini
platform = ${common.platform_wled_default}
platform_packages = ${common.platform_packages}
upload_speed = 921600
board_build.ldscript = ${common.ldscript_4m1m}
build_unflags = ${common.build_unflags}
build_flags = ${common.build_flags_esp8266}
monitor_filters = esp8266_exception_decoder
```
### Actual Results
The resulting build will fail because `env.get("BOARD")` is empty (the `env:quinled_dig_test` is gone at this point).
### Expected Results
The `config` object loaded by `ProjectFactory` should be preserved even when updated to add configuration on the fly and passed to SCons instead of initializing a new one.
### Additional info
Related to https://community.platformio.org/t/building-programmatically-in-another-python-project/17568 (original question).
I understand. It would be cool if future development gave some thought into separating the CLI from core functionality so it could be used more as a library. For now, I’ll use as-is and make note that the classes I use may change out from under me.