build_info_getter/run.py

68 lines
2.3 KiB
Python
Executable File

#!/usr/bin/env python3
#
# Copyright 2025 James Pace
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
#
import argparse
import os
from pathlib import Path
from datetime import datetime, UTC
import subprocess
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--project-dir", type=str, default=None)
parser.add_argument("--install-path", type=str)
args = parser.parse_args()
prep_install_directory(args.install_path)
export_vcs(args.project_dir, args.install_path)
generate_version_number(args.install_path)
def prep_install_directory(install_path):
# Make the install directory if it doesn't exist.
mkdir_command = "mkdir -p {}".format(install_path)
subprocess.run(mkdir_command, shell=True)
def generate_version_number(install_path):
# Try to get the version info from the environment.
version_number = os.environ.get("J7S_VERSION_NUMBER")
# If we can't assign a date/time.
if not version_number:
now = datetime.now(UTC)
version_number = now.strftime("%Y.%m.%d.%H.%M")
# Save in install space.
output_file = install_path + "/version_number"
with open(output_file, 'w') as output:
print(version_number, file=output)
def export_vcs(project_directory, install_path):
if not project_directory:
# Assume we're being called from colcon and need to figure this out ourselves.
# When run by colcon cwd is something like <path i want>/build/package_name
cwd = Path(os.getcwd())
project_directory = cwd.parent.parent
# Find src directory from project directory.
src_directory = project_directory / "src"
# Where we're going to save the output.
output_file = install_path + "/project_state.repos"
# Now call vcs.
vcs_command = "vcs export --exact-with-tags {} > {}".format(src_directory, output_file)
result = subprocess.run(vcs_command, shell=True, capture_output=True)
if result.returncode != 0:
print("Failed to export software version. Return code: {}".format(result.returncode))
if __name__ == "__main__":
main()