Skip to content

Commit c3734ad

Browse files
committed
Add exception handling and split VCS script.
This way the script is more modular.
1 parent 91aa125 commit c3734ad

4 files changed

Lines changed: 49 additions & 19 deletions

File tree

define_vsc_id_macro.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
generate_vcs_identifier.py
3+
4+
This script adds a CPP definition of a version identifier using version control
5+
system information. It retrieves the VCS identifier from the 'vcs_utils' module
6+
and appends it to the PlatformIO build environment as a preprocessor definition
7+
if available.
8+
9+
This script should be loaded by PlatformIO during the project build and is not
10+
designed to be run independently.
11+
"""
12+
13+
from vcs_utils import get_vcs_id
14+
Import("env")
15+
16+
# Get the VCS ID and conditionally append it to the environment definitions
17+
vcs_id = get_vcs_id()
18+
if vcs_id: # Only append if a valid VCS ID is retrieved
19+
env.Append(CPPDEFINES=[
20+
("VCS_ID", env.StringifyMacro(vcs_id)),
21+
])

generate_vcs_identifier.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

get_vcs_identifier.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import subprocess
2+
import sys
3+
4+
def get_vcs_id():
5+
"""
6+
Attempts to retrieve the VCS identifier using the 'git describe' command.
7+
8+
Uses git to retrieve the version control system (VCS) identifier.
9+
It is designed to handle cases where Git is not installed or the current
10+
directory is not a Git repository, without raising exceptions.
11+
12+
Returns:
13+
str: A string containing the VCS identifier if successful, or an empty
14+
string if Git is not available or the directory is not a Git repository.
15+
16+
All warnings are printed to stderr.
17+
"""
18+
try:
19+
vcs_output = subprocess.run(
20+
["git", "describe", "--always", "--dirty", "--all", "--long"],
21+
stdout=subprocess.PIPE, text=True, check=True
22+
)
23+
vcs_string = vcs_output.stdout.strip()
24+
return vcs_string
25+
except (subprocess.CalledProcessError, FileNotFoundError) as e:
26+
print("Warning: Unable to retrieve VCS description. Error:", str(e), file=sys.stderr)
27+
return ""

platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ build_flags =
2222
-DBAUD_RATE=${this.monitor_speed}
2323
monitor_speed = 115200
2424
extra_scripts =
25-
pre:generate_vcs_identifier.py
25+
pre:get_vcs_identifier.py
2626

2727
[env:native]
2828
platform = native

0 commit comments

Comments
 (0)