Device monitor causes extra_scripts to run

I have two scripts i run as extra_scripts. One creates a version file, the other copies the firmware from the build directory to another. The scripts work fine. However, when launching the device monitor, this causes the scripts to run again, thus incrementing the build.


[env]
platform = espressif32@5.3.0
board = esp32dev
framework = espidf
monitor_speed = 115200
monitor_filters = esp32_exception_decoder
board_build.partitions = factory_app_two_ota.csv
custom_ota_port = 3232

[env:debug_home_serial]
upload_port = /dev/cu.usbserial-8344401
build_type = debug
upload_speed = 1500000
monitor_port = /dev/cu.usbserial-8344401
extra_scripts = pre:tools/buildscript_versioning.py
                pre:tools/copy_firmware.py


builscript_versioning.py


Import ("env")
import datetime
import subprocess
import shutil

tm = datetime.datetime.today()
FILENAME_BUILDNO = 'versioning'
FILENAME_VERSION_H = './lib/version/version.h'
version = 'v0.1.' + str(tm.year)[-2:]+('0'+str(tm.month))[-2:]+('0'+str(tm.day))[-2:] + '_'
build_dir = env.subst("${BUILD_DIR}/")
project_firmware_dir = env.subst("${BUILD_DIR}/firmware/")

def run(*args):
    return subprocess.run(['git'] + list(args))

build_no = 0
try:
    with open(FILENAME_BUILDNO) as f:
        build_no = int(f.readline()) + 1
        firmware = f.readline()
except:
    #print('Starting build number from 1..')
    build_no = 1
with open(FILENAME_BUILDNO, 'w+') as f:
    f.write(str(build_no)+"\n")
    f.write(str(version) + str(build_no))
    
    #print('Build number: {}'.format(build_no))

firmware_name = "firmware_%s" % version + str(build_no)
env.Replace(PROGNAME=firmware_name) 

hf = """
#pragma once
#ifndef BUILD_NUMBER
  #define BUILD_NUMBER "{}"
#endif
#ifndef VERSION
  #define VERSION "{} - {}"
#endif
#ifndef VERSION_SHORT
  #define VERSION_SHORT "{}"
#endif
""".format(build_no, version+str(build_no), datetime.datetime.now(), version+str(build_no))
with open(FILENAME_VERSION_H, 'w+') as f:
    f.write(hf)

tag = version + str(build_no)
commit_message = "Updates"

build_type = env.GetProjectOption("build_type")

if build_type == "release":
  subprocess.run(['git', 'add', '.'] ,stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False)
  subprocess.run(['git', 'tag', tag] ,stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False)
  subprocess.run(['git', 'commit', '-am', commit_message, "--quiet"])

  subprocess.run(['git', 'push', 'origin', "--tags", "--quiet"])
  subprocess.run(['git', 'push', "--quiet"])

copy_firmware.py


Import ("env")
import datetime
import subprocess
import shutil

FILENAME_BUILDNO = 'versioning'
FILENAME_VERSION_H = './lib/version/version.h'
build_dir = env.subst("${BUILD_DIR}/")
project_firmware_dir = env.subst("${PROJECT_DIR}/firmware/")

build_type = env.GetProjectOption("build_type")


def after_build(source, target, env):
    if build_type == "release":
        try:
            with open(FILENAME_BUILDNO) as f:
                build_no = f.readline()
                firmware = f.readline()
        except:
            pass
            #("ERROR OPENING FILE")

        firmware_name = "firmware_%s" % str(firmware) + ".bin"

        src = build_dir + firmware_name
        dst = project_firmware_dir + firmware_name
        shutil.copyfile(src, dst)

env.AddPostAction("buildprog", after_build)

This is similar…

In buildscript_versioning.py, don’t unconditionally execute code, guard it by checking if the current target is “build”.

Thank you Max that worked great !

Is there a way to see the pre and post scripts execute ? I tried a verbose build but did not see the execution. Is there a build log somewhere?