Extra_scripts seems to be executed when I change GitHub branches in VSCode

In my platformio.ini I set up some environments, one common part and some release environments for different ESP32 boards.

Here’s the file:

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env]
platform = espressif32
board = az-delivery-devkit-v4
framework = arduino
monitor_speed = 115200
monitor_port = COM7
;build_type = debug
;check_skip_packages = yes
build_flags = 
	-DUNITY_INCLUDE_DOUBLE
	;!powershell -NonInteractive -NoLogo -NoProfile -File .\GetVersion.ps1 -ProjectDirectory . -OutputFile .\include\Version.h

[env:az-delivery-devkit-v4-OTA-018]
upload_port = 192.168.178.117
upload_protocol = espota
extra_scripts = my_extra_script.py
build_flags = 
	${env.build_flags}
; lib_deps = chuckbell/MySQL Connector Arduino@^1.2.0

[env:az-delivery-devkit-v4-OTA-6F0]
upload_port = 192.168.178.118
upload_protocol = espota
build_flags = 
	${env.build_flags}

[env:az-delivery-devkit-v4-Serial]
upload_port = COM7
upload_protocol = esptool
extra_scripts = my_extra_script.py
build_flags = 
	${env.build_flags}
; lib_deps = chuckbell/MySQL Connector Arduino@^1.2.0

In the my_extra_script.py a Version.h file will be generated and some data will be written into a MySQL database. This works. My problem is, that with every change of a branch or with the startup of VSCode the extra_scripts seems to be executed. But what I try to achieve is, that this script will be executed during the build process only. What I’m doing wrong ? Need help.
Thx in advance

Yes. Your extra_script could be adding compile / linker settings, so PlatformIO of course executes it when the project is loaded or the platformio.ini itself is changed.

You can determine whether you are in an active build or not as shown in e.g.

Thx for the advise. I’ll give it a try. First of all I have to understand what’s going on.

So, I tested different approaches. But with no success.
The extra_script file is as follows:

Import("env")

import os
import git
import time
import subprocess
import shutil
import json

try:
    import mysql.connector
except ImportError:
    env.Execute("$PYTHONEXE -m pip install mysql-connector-python")
    import mysql.connector
"""
try:
    import git
except ImportError:
    env.Execute("$PYTHONEXE -m pip install GitPython")
    import git

BUILDNR = int(round(time.time()))
VERSION_FILE = "version.h"
CONFIG_FILE = "config.h"
BUILD_DIR = "build/"
BOARD = env['BOARD']
API_URL = ""
FILENAME = ""
cwd = os.path.abspath(os.getcwd())
print(cwd)
r = git.repo.Repo(cwd)
print(r)
# GITVERSION = r.git.describe("--tag")
#if is_pio_build:
#    GITVERSION = os.system("powershell -NonInteractive -NoLogo -NoProfile -File .\GetVersion.ps1 -ProjectDirectory . -OutputFile .\include\Version.h")
#print(GITVERSION)
"""
def is_pio_build():
    from SCons.Script import DefaultEnvironment
    env = DefaultEnvironment()
    if "IsCleanTarget" in dir(env) and env.IsCleanTarget(): return False
    return not env.IsIntegrationDump()

#def prepareVersion(source, target, env):
if is_pio_build:
    os.system("powershell -NonInteractive -NoLogo -NoProfile -File .\GetVersion.ps1 -ProjectDirectory . -OutputFile .\include\Version.h")
    mydb = mysql.connector.connect(
    host="192.168.178.49",
    user="user",
    password="passwd",
    database="esp32"
    )

    mycursor = mydb.cursor()

    sql = "INSERT INTO boards (IP_Address) VALUES (%s)"
    val = ("192.168.178.200",)
    mycursor.execute(sql, val)

    mydb.commit()

    print(mycursor.rowcount, "record inserted.")

#env.AddPreAction("$PROGPATH", before_build)
#env.AddPreAction("$BUILD_DIR/src/main.o", prepareVersion)
#env.AddPreAction("buildprog", before_build)

The behaviour is still the same. The script will be executed during build AND when I modify the platformio.ini. I couldn’t find anything about env.IsCleanTarget() and env.IsIntegrationDump(). Therefore the question for more help. Any further ideas ?

No. You’re not executing your function. You have to write if is_pio_build():

1 Like

You’re right. This was the mistake. That’s the difference between expert and Newbie. :grinning:
Thx a lot for your support.