2022-07-11 14:44:23 +00:00
#!/usr/bin/env python3
#
# Copyright 2022 by Moshe Kaplan
# Based on make-version.pl by Jörg Mayer
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <gerald@wireshark.org>
# Copyright 1998 Gerald Combs
#
# SPDX-License-Identifier: GPL-2.0-or-later
# See below for usage.
#
# If run with the "-r" or "--set-release" argument the VERSION macro in
# CMakeLists.txt will have the version_extra template appended to the
# version number. vcs_version.h will _not_ be generated if either argument is
# present.
#
# make-version.py is called during the build to update vcs_version.h in the build
# directory. To set a fixed version, use something like:
#
# cmake -DVCSVERSION_OVERRIDE="Git v3.1.0 packaged as 3.1.0-1"
#
# XXX - We're pretty dumb about the "{vcsinfo}" substitution, and about having
# spaces in the package format.
import argparse
import os
import os . path
import re
import shlex
import shutil
import sys
import subprocess
2024-12-31 13:24:32 -08:00
from enum import Enum
Flavor = Enum ( ' Flavor ' , [ ' Wireshark ' , ' Stratoshark ' ] )
2022-07-11 14:44:23 +00:00
GIT_ABBREV_LENGTH = 12
# `git archive` will use an 'export-subst' entry in .gitattributes to replace
# the $Format strings with `git log --pretty=format:` placeholders.
# The output will look something like the following:
# GIT_EXPORT_SUBST_H = '51315cf37cdf6c0add1b1c99cb7941aac4489a6f'
# GIT_EXPORT_SUBST_D = 'HEAD -> master, upstream/master, upstream/HEAD'
# If the text "$Format" is still present, it means that
# git archive did not replace the $Format string, which
# means that this not a git archive.
GIT_EXPORT_SUBST_H = ' $Format: % H$ '
GIT_EXPORT_SUBST_D = ' $Format: % D$ '
IS_GIT_ARCHIVE = not GIT_EXPORT_SUBST_H . startswith ( ' $Format ' )
def update_cmakelists_txt ( src_dir , set_version , repo_data ) :
2024-12-31 13:24:32 -08:00
if not set_version and repo_data [ ' ws_package_string ' ] == " " and repo_data [ ' ss_package_string ' ] == " " :
2022-07-11 14:44:23 +00:00
return
cmake_filepath = os . path . join ( src_dir , " CMakeLists.txt " )
with open ( cmake_filepath , encoding = ' utf-8 ' ) as fh :
cmake_contents = fh . read ( )
new_cmake_contents = cmake_contents
2024-12-31 13:24:32 -08:00
new_cmake_contents = re . sub ( r " ^set * \ ( *PROJECT_MAJOR_VERSION * \ d+ * \ )$ " ,
f " set(PROJECT_MAJOR_VERSION { repo_data [ ' ws_version_major ' ] } ) " ,
new_cmake_contents ,
flags = re . MULTILINE )
new_cmake_contents = re . sub ( r " ^set * \ ( *PROJECT_MINOR_VERSION * \ d+ * \ )$ " ,
f " set(PROJECT_MINOR_VERSION { repo_data [ ' ws_version_minor ' ] } ) " ,
new_cmake_contents ,
flags = re . MULTILINE )
new_cmake_contents = re . sub ( r " ^set * \ ( *PROJECT_PATCH_VERSION * \ d+ * \ )$ " ,
f " set(PROJECT_PATCH_VERSION { repo_data [ ' ws_version_patch ' ] } ) " ,
new_cmake_contents ,
flags = re . MULTILINE )
new_cmake_contents = re . sub ( r " ^set * \ ( *PROJECT_VERSION_EXTENSION .*?$ " ,
f " set(PROJECT_VERSION_EXTENSION \" { repo_data [ ' ws_package_string ' ] } \" ) " ,
new_cmake_contents ,
flags = re . MULTILINE )
new_cmake_contents = re . sub ( r " ^set * \ ( *STRATOSHARK_MAJOR_VERSION * \ d+ * \ )$ " ,
f " set(STRATOSHARK_MAJOR_VERSION { repo_data [ ' ss_version_major ' ] } ) " ,
2022-07-11 14:44:23 +00:00
new_cmake_contents ,
flags = re . MULTILINE )
2024-12-31 13:24:32 -08:00
new_cmake_contents = re . sub ( r " ^set * \ ( *STRATOSHARK_MINOR_VERSION * \ d+ * \ )$ " ,
f " set(STRATOSHARK_MINOR_VERSION { repo_data [ ' ss_version_minor ' ] } ) " ,
2022-07-11 14:44:23 +00:00
new_cmake_contents ,
flags = re . MULTILINE )
2024-12-31 13:24:32 -08:00
new_cmake_contents = re . sub ( r " ^set * \ ( *STRATOSHARK_PATCH_VERSION * \ d+ * \ )$ " ,
f " set(STRATOSHARK_PATCH_VERSION { repo_data [ ' ss_version_patch ' ] } ) " ,
2022-07-11 14:44:23 +00:00
new_cmake_contents ,
flags = re . MULTILINE )
2024-12-31 13:24:32 -08:00
new_cmake_contents = re . sub ( r " ^set * \ ( *STRATOSHARK_VERSION_EXTENSION .*?$ " ,
f " set(STRATOSHARK_VERSION_EXTENSION \" { repo_data [ ' ss_package_string ' ] } \" ) " ,
2022-07-11 14:44:23 +00:00
new_cmake_contents ,
flags = re . MULTILINE )
with open ( cmake_filepath , mode = ' w ' , encoding = ' utf-8 ' ) as fh :
fh . write ( new_cmake_contents )
2023-10-26 13:43:27 -07:00
print ( cmake_filepath + " has been updated. " )
2022-07-11 14:44:23 +00:00
def update_debian_changelog ( src_dir , repo_data ) :
# Read packaging/debian/changelog, then write back out an updated version.
deb_changelog_filepath = os . path . join ( src_dir , " packaging " , " debian " , " changelog " )
with open ( deb_changelog_filepath , encoding = ' utf-8 ' ) as fh :
changelog_contents = fh . read ( )
CHANGELOG_PATTERN = r " ^.* "
2024-12-31 13:24:32 -08:00
text_replacement = f " wireshark ( { repo_data [ ' ws_version_major ' ] } . { repo_data [ ' ws_version_minor ' ] } . { repo_data [ ' ws_version_patch ' ] } { repo_data [ ' ws_package_string ' ] } ) UNRELEASED; urgency=low "
2022-07-11 14:44:23 +00:00
# Note: Only need to replace the first line, so we don't use re.MULTILINE or re.DOTALL
new_changelog_contents = re . sub ( CHANGELOG_PATTERN , text_replacement , changelog_contents )
with open ( deb_changelog_filepath , mode = ' w ' , encoding = ' utf-8 ' ) as fh :
fh . write ( new_changelog_contents )
2023-10-26 13:43:27 -07:00
print ( deb_changelog_filepath + " has been updated. " )
2022-07-11 14:44:23 +00:00
2024-12-31 13:24:32 -08:00
def create_version_file ( version_f , repo_data , flavor ) :
2023-10-28 14:13:10 +02:00
' Write the version to the specified file handle '
2024-12-31 13:24:32 -08:00
fpfx = ' ss ' if flavor == Flavor . Stratoshark else ' ws '
2023-10-26 13:43:27 -07:00
2024-12-31 13:24:32 -08:00
version_f . write ( f " { repo_data [ f ' { fpfx } _version_major ' ] } . { repo_data [ f ' { fpfx } _version_minor ' ] } . { repo_data [ f ' { fpfx } _version_patch ' ] } { repo_data [ f ' { fpfx } _package_string ' ] } \n " )
2023-10-28 14:13:10 +02:00
print ( version_f . name + " has been created. " )
2023-10-26 13:43:27 -07:00
2022-07-11 14:44:23 +00:00
def update_attributes_asciidoc ( src_dir , repo_data ) :
2023-11-13 16:01:25 -08:00
# Read doc/attributes.adoc, then write it back out with an updated
2022-07-11 14:44:23 +00:00
# wireshark-version replacement line.
2023-11-13 16:01:25 -08:00
asiidoc_filepath = os . path . join ( src_dir , " doc " , " attributes.adoc " )
2022-07-11 14:44:23 +00:00
with open ( asiidoc_filepath , encoding = ' utf-8 ' ) as fh :
2024-12-31 13:24:32 -08:00
new_asciidoc_contents = fh . read ( )
2022-07-11 14:44:23 +00:00
2024-12-31 13:24:32 -08:00
# Sample line (without quotes): ":wireshark-version: 4.3.1"
ws_replacement = f " :wireshark-version: { repo_data [ ' ws_version_major ' ] } . { repo_data [ ' ws_version_minor ' ] } . { repo_data [ ' ws_version_patch ' ] } "
ss_replacement = f " :stratoshark-version: { repo_data [ ' ss_version_major ' ] } . { repo_data [ ' ss_version_minor ' ] } . { repo_data [ ' ss_version_patch ' ] } "
2022-07-11 14:44:23 +00:00
2024-12-31 13:24:32 -08:00
new_asciidoc_contents = re . sub ( r " ^:wireshark-version:.*$ " , ws_replacement , new_asciidoc_contents , flags = re . MULTILINE )
new_asciidoc_contents = re . sub ( r " ^:stratoshark-version:.*$ " , ss_replacement , new_asciidoc_contents , flags = re . MULTILINE )
2022-07-11 14:44:23 +00:00
with open ( asiidoc_filepath , mode = ' w ' , encoding = ' utf-8 ' ) as fh :
fh . write ( new_asciidoc_contents )
2023-10-26 13:43:27 -07:00
print ( asiidoc_filepath + " has been updated. " )
2022-07-11 14:44:23 +00:00
def update_docinfo_asciidoc ( src_dir , repo_data ) :
doc_paths = [ ]
2024-07-29 09:02:17 -07:00
doc_paths + = [ os . path . join ( src_dir , ' doc ' , ' wsdg_src ' , ' developer-guide-docinfo.xml ' ) ]
doc_paths + = [ os . path . join ( src_dir , ' doc ' , ' wsug_src ' , ' user-guide-docinfo.xml ' ) ]
2022-07-11 14:44:23 +00:00
for doc_path in doc_paths :
with open ( doc_path , encoding = ' utf-8 ' ) as fh :
doc_contents = fh . read ( )
# Sample line (without quotes): "<subtitle>For Wireshark 1.2</subtitle>"
DOC_PATTERN = r " ^<subtitle>For Wireshark \ d+. \ d+< \ /subtitle>$ "
2024-12-31 13:24:32 -08:00
text_replacement = f " <subtitle>For Wireshark { repo_data [ ' ws_version_major ' ] } . { repo_data [ ' ws_version_minor ' ] } </subtitle> "
2022-07-11 14:44:23 +00:00
new_doc_contents = re . sub ( DOC_PATTERN , text_replacement , doc_contents , flags = re . MULTILINE )
with open ( doc_path , mode = ' w ' , encoding = ' utf-8 ' ) as fh :
fh . write ( new_doc_contents )
2023-10-26 13:43:27 -07:00
print ( doc_path + " has been updated. " )
2022-07-11 14:44:23 +00:00
def update_cmake_lib_releases ( src_dir , repo_data ) :
# Read CMakeLists.txt for each library, then write back out an updated version.
dir_paths = [ ]
dir_paths + = [ os . path . join ( src_dir , ' epan ' ) ]
dir_paths + = [ os . path . join ( src_dir , ' wiretap ' ) ]
for dir_path in dir_paths :
cmakelists_filepath = os . path . join ( dir_path , " CMakeLists.txt " )
with open ( cmakelists_filepath , encoding = ' utf-8 ' ) as fh :
cmakelists_contents = fh . read ( )
# Sample line (without quotes; note leading tab: " VERSION "0.0.0" SOVERSION 0")
VERSION_PATTERN = r ' ^( \ s*VERSION \ s+ " \ d+ \ . \ d+ \ .) \ d+ '
2024-12-31 13:24:32 -08:00
replacement_text = f " \\ g<1> { repo_data [ ' ws_version_patch ' ] } "
2022-07-11 14:44:23 +00:00
new_cmakelists_contents = re . sub ( VERSION_PATTERN ,
replacement_text ,
cmakelists_contents ,
flags = re . MULTILINE )
with open ( cmakelists_filepath , mode = ' w ' , encoding = ' utf-8 ' ) as fh :
fh . write ( new_cmakelists_contents )
2023-10-26 13:43:27 -07:00
print ( cmakelists_filepath + " has been updated. " )
2022-07-11 14:44:23 +00:00
# Update distributed files that contain any version information
def update_versioned_files ( src_dir , set_version , repo_data ) :
update_cmakelists_txt ( src_dir , set_version , repo_data )
update_debian_changelog ( src_dir , repo_data )
if set_version :
update_attributes_asciidoc ( src_dir , repo_data )
update_docinfo_asciidoc ( src_dir , repo_data )
update_cmake_lib_releases ( src_dir , repo_data )
def generate_version_h ( repo_data ) :
# Generate new contents of version.h from repository data
2024-12-24 15:09:29 -06:00
# XXX Is there any way we can simplify this?
2022-07-11 14:44:23 +00:00
2024-12-24 15:09:29 -06:00
ws_vcs_line = ' #define WIRESHARK_VCS_VERSION " Git Rev Unknown from unknown " '
ws_num_commits_line = ' #define WIRESHARK_VCS_NUM_COMMITS " 0 " '
ws_commit_id_line = ' /* #undef WIRESHARK_VCS_COMMIT_ID */ '
2024-01-31 18:59:22 -08:00
2024-12-24 15:09:29 -06:00
ss_vcs_line = ' #define STRATOHARK_VCS_VERSION " Git Rev Unknown from unknown " '
2024-12-31 13:24:32 -08:00
ss_num_commits_line = ' #define STRATOSHARK_VCS_NUM_COMMITS " 0 " '
2024-12-24 15:09:29 -06:00
ss_commit_id_line = ' /* #undef STRATOSHARK_VCS_COMMIT_ID */ '
2024-01-31 18:59:22 -08:00
2024-12-31 13:24:32 -08:00
if repo_data . get ( ' ws_num_commits ' ) :
ws_num_commits_line = f ' #define WIRESHARK_VCS_NUM_COMMITS " { int ( repo_data [ " ws_num_commits " ] ) } " '
if repo_data . get ( ' ss_num_commits ' ) :
ss_num_commits_line = f ' #define STRATOSHARK_VCS_NUM_COMMITS " { int ( repo_data [ " ss_num_commits " ] ) } " '
2022-07-11 14:44:23 +00:00
if repo_data . get ( ' commit_id ' ) :
2024-12-24 15:09:29 -06:00
ws_commit_id_line = f ' #define WIRESHARK_VCS_COMMIT_ID " { repo_data [ " commit_id " ] } " '
2025-04-14 16:11:42 +00:00
ss_commit_id_line = ' #define STRATOSHARK_VCS_COMMIT_ID WIRESHARK_VCS_COMMIT_ID '
2024-12-24 15:09:29 -06:00
if repo_data . get ( ' enable_vcsversion ' ) :
2024-12-31 13:24:32 -08:00
if repo_data . get ( ' ws_git_description ' ) :
2024-12-24 15:09:29 -06:00
# Do not bother adding the git branch, the git describe output
# normally contains the base tag and commit ID which is more
# than sufficient to determine the actual source tree.
2024-12-31 13:24:32 -08:00
ws_vcs_line = f ' #define WIRESHARK_VCS_VERSION " { repo_data [ " ws_git_description " ] } " '
elif repo_data . get ( ' last_change ' ) and repo_data . get ( ' ws_num_commits ' ) :
version_string = f " v { repo_data [ ' ws_version_major ' ] } . { repo_data [ ' ws_version_minor ' ] } . { repo_data [ ' ws_version_patch ' ] } "
ws_vcs_line = f ' #define WIRESHARK_VCS_VERSION " { version_string } -Git- { repo_data [ " ws_num_commits " ] } " '
2024-12-24 15:09:29 -06:00
elif repo_data . get ( ' commit_id ' ) :
ws_vcs_line = f ' #define WIRESHARK_VCS_VERSION " Git commit { repo_data [ " commit_id " ] } " '
2024-12-31 13:24:32 -08:00
if repo_data . get ( ' ss_git_description ' ) :
# Do not bother adding the git branch, same as above.
ss_vcs_line = f ' #define STRATOSHARK_VCS_VERSION " { repo_data [ " ss_git_description " ] } " '
elif repo_data . get ( ' last_change ' ) and repo_data . get ( ' ss_num_commits ' ) :
version_string = f " v { repo_data [ ' ss_version_major ' ] } . { repo_data [ ' ss_version_minor ' ] } . { repo_data [ ' ss_version_patch ' ] } "
ss_vcs_line = f ' #define STRATOSHARK_VCS_VERSION " { version_string } -Git- { repo_data [ " ss_num_commits " ] } " '
elif repo_data . get ( ' commit_id ' ) :
ss_vcs_line = f ' #define STRATOSHARK_VCS_VERSION " Git commit { repo_data [ " commit_id " ] } " '
2024-12-24 15:09:29 -06:00
return f ''' \
/ / Generated by tools / make - version . py
#pragma once
{ ws_vcs_line }
{ ws_num_commits_line }
{ ws_commit_id_line }
{ ss_vcs_line }
{ ss_num_commits_line }
{ ss_commit_id_line }
'''
2022-07-11 14:44:23 +00:00
def print_VCS_REVISION ( version_file , repo_data , set_vcs ) :
# Write the version control system's version to $version_file.
# Don't change the file if it is not needed.
#
2024-12-24 15:09:29 -06:00
# XXX - We might want to add WIRESHARK_VCS_VERSION to CMakeLists.txt so that it can
2022-07-11 14:44:23 +00:00
# generate vcs_version.h independently.
new_version_h = generate_version_h ( repo_data )
needs_update = True
if os . path . exists ( version_file ) :
with open ( version_file , encoding = ' utf-8 ' ) as fh :
current_version_h = fh . read ( )
if current_version_h == new_version_h :
needs_update = False
if not set_vcs :
return
if needs_update :
with open ( version_file , mode = ' w ' , encoding = ' utf-8 ' ) as fh :
fh . write ( new_version_h )
2023-10-26 13:43:27 -07:00
print ( version_file + " has been updated. " )
2022-07-11 14:44:23 +00:00
elif not repo_data [ ' enable_vcsversion ' ] :
print ( version_file + " disabled. " )
else :
print ( version_file + " unchanged. " )
return
2024-12-31 13:24:32 -08:00
def get_version ( cmakelists_file_data , flavor ) :
2022-07-11 14:44:23 +00:00
# Reads major, minor, and patch
# Sample data:
# set(PROJECT_MAJOR_VERSION 3)
# set(PROJECT_MINOR_VERSION 7)
# set(PROJECT_PATCH_VERSION 2)
2024-12-31 13:24:32 -08:00
fpfx = ' STRATOSHARK ' if flavor == Flavor . Stratoshark else ' PROJECT '
MAJOR_PATTERN = rf " ^set * \ ( * { fpfx } _MAJOR_VERSION *( \ d+) * \ )$ "
MINOR_PATTERN = rf " ^set * \ ( * { fpfx } _MINOR_VERSION *( \ d+) * \ )$ "
PATCH_PATTERN = rf " ^set * \ ( * { fpfx } _PATCH_VERSION *( \ d+) * \ )$ "
2022-07-11 14:44:23 +00:00
major_match = re . search ( MAJOR_PATTERN , cmakelists_file_data , re . MULTILINE )
minor_match = re . search ( MINOR_PATTERN , cmakelists_file_data , re . MULTILINE )
patch_match = re . search ( PATCH_PATTERN , cmakelists_file_data , re . MULTILINE )
if not major_match :
2024-12-31 13:24:32 -08:00
raise Exception ( f " Couldn ' t get { flavor . name } major version " )
2022-07-11 14:44:23 +00:00
if not minor_match :
2024-12-31 13:24:32 -08:00
raise Exception ( f " Couldn ' t get { flavor . name } minor version " )
2022-07-11 14:44:23 +00:00
if not patch_match :
2024-12-31 13:24:32 -08:00
raise Exception ( f " Couldn ' t get { flavor . name } patch version " )
2022-07-11 14:44:23 +00:00
major_version = major_match . groups ( ) [ 0 ]
minor_version = minor_match . groups ( ) [ 0 ]
patch_version = patch_match . groups ( ) [ 0 ]
return major_version , minor_version , patch_version
def read_git_archive ( tagged_version_extra , untagged_version_extra ) :
# Reads key data from the git repo.
# For git archives, this does not need to access the source directory because
# `git archive` will use an 'export-subst' entry in .gitattributes to replace
# the value for GIT_EXPORT_SUBST_H in the script.
# Returns a dictionary with key values from the repository
is_tagged = False
for git_ref in GIT_EXPORT_SUBST_D . split ( r ' , ' ) :
match = re . match ( r ' ^tag: (v[1-9].+) ' , git_ref )
if match :
is_tagged = True
2022-07-28 21:02:54 +00:00
vcs_tag = match . groups ( ) [ 0 ]
2022-07-11 14:44:23 +00:00
if is_tagged :
print ( f " We are on tag { vcs_tag } . " )
2024-12-31 13:24:32 -08:00
ws_package_string = tagged_version_extra
2022-07-11 14:44:23 +00:00
else :
print ( " We are not tagged. " )
2024-12-31 13:24:32 -08:00
ws_package_string = untagged_version_extra
2022-07-11 14:44:23 +00:00
# Always 0 commits for a git archive
2024-12-31 13:24:32 -08:00
ws_num_commits = 0
2022-07-11 14:44:23 +00:00
# Assume a full commit hash, abbreviate it.
commit_id = GIT_EXPORT_SUBST_H [ : GIT_ABBREV_LENGTH ]
2024-12-31 13:24:32 -08:00
ws_package_string = ws_package_string . replace ( " {vcsinfo} " , str ( ws_num_commits ) + " - " + commit_id )
2022-07-11 14:44:23 +00:00
repo_data = { }
repo_data [ ' commit_id ' ] = commit_id
repo_data [ ' enable_vcsversion ' ] = True
repo_data [ ' info_source ' ] = " git archive "
2024-12-31 13:24:32 -08:00
repo_data [ ' ws_num_commits ' ] = ws_num_commits
repo_data [ ' ws_package_string ' ] = ws_package_string
# XXX Do we need a separate Stratoshark package string?
repo_data [ ' ss_package_string ' ] = ws_package_string
2022-07-11 14:44:23 +00:00
return repo_data
def read_git_repo ( src_dir , tagged_version_extra , untagged_version_extra ) :
# Reads metadata from the git repo for generating the version string
# Returns the data in a dict
IS_GIT_INSTALLED = shutil . which ( ' git ' ) != ' '
if not IS_GIT_INSTALLED :
print ( " Git unavailable. Git revision will be missing from version string. " , file = sys . stderr )
return { }
GIT_DIR = os . path . join ( src_dir , ' .git ' )
# Check whether to include VCS version information in vcs_version.h
enable_vcsversion = True
git_get_commondir_cmd = shlex . split ( f ' git --git-dir= " { GIT_DIR } " rev-parse --git-common-dir ' )
git_commondir = subprocess . check_output ( git_get_commondir_cmd , universal_newlines = True ) . strip ( )
if git_commondir and os . path . exists ( f " { git_commondir } { os . sep } wireshark-disable-versioning " ) :
print ( " Header versioning disabled using git override. " )
enable_vcsversion = False
git_last_changetime_cmd = shlex . split ( f ' git --git-dir= " { GIT_DIR } " log -1 --pretty=format:%at ' )
git_last_changetime = subprocess . check_output ( git_last_changetime_cmd , universal_newlines = True ) . strip ( )
# Commits since last annotated tag.
# Output could be something like: v3.7.2rc0-64-g84d83a8292cb
# Or g84d83a8292cb
2024-12-31 13:24:32 -08:00
git_describe_cmd = shlex . split ( f ' git --git-dir= " { GIT_DIR } " describe --abbrev= { GIT_ABBREV_LENGTH } --long --always --match " v[1-9]* " ' )
ws_git_description = subprocess . check_output ( git_describe_cmd , universal_newlines = True ) . strip ( )
parts = ws_git_description . split ( ' - ' )
2022-07-11 14:44:23 +00:00
if len ( parts ) > 1 :
2024-12-31 13:24:32 -08:00
ws_num_commits = int ( parts [ 1 ] )
2022-07-11 14:44:23 +00:00
else :
2024-12-31 13:24:32 -08:00
ws_num_commits = 0
2022-07-11 14:44:23 +00:00
commit_id = parts [ - 1 ]
2025-01-08 14:01:55 -08:00
ws_release_candidate = ' '
match = re . match ( r ' ^v \ d+ \ . \ d+ \ . \ d+(rc \ d+)$ ' , parts [ 0 ] )
if match :
ws_release_candidate = match . groups ( ) [ 0 ]
2024-12-31 13:24:32 -08:00
git_describe_cmd = shlex . split ( f ' git --git-dir= " { GIT_DIR } " describe --abbrev= { GIT_ABBREV_LENGTH } --long --always --match " ssv[0-9]* " ' )
ss_git_description = subprocess . check_output ( git_describe_cmd , universal_newlines = True ) . strip ( )
parts = ss_git_description . split ( ' - ' )
if len ( parts ) > 1 :
ss_num_commits = int ( parts [ 1 ] )
else :
ss_num_commits = 0
2025-01-08 14:01:55 -08:00
ss_release_candidate = ' '
match = re . match ( r ' ^ssv \ d+ \ . \ d+ \ . \ d+(rc \ d+)$ ' , parts [ 0 ] )
2022-07-11 14:44:23 +00:00
if match :
2025-01-08 14:01:55 -08:00
ss_release_candidate = match . groups ( ) [ 0 ]
2022-07-11 14:44:23 +00:00
try :
2024-12-31 13:24:32 -08:00
# This command is expected to fail if the version is not tagged
2022-07-11 14:44:23 +00:00
git_vcs_tag_cmd = shlex . split ( f ' git --git-dir= " { GIT_DIR } " describe --exact-match --match " v[1-9]* " ' )
git_vcs_tag = subprocess . check_output ( git_vcs_tag_cmd , stderr = subprocess . DEVNULL , universal_newlines = True ) . strip ( )
2024-12-31 13:24:32 -08:00
print ( f " We are on Wireshark tag { git_vcs_tag } . " )
ws_package_string = tagged_version_extra
2022-07-11 14:44:23 +00:00
except subprocess . CalledProcessError :
2024-12-31 13:24:32 -08:00
print ( " We are not on a Wireshark tag. " )
ws_package_string = untagged_version_extra
2022-07-11 14:44:23 +00:00
2025-01-08 14:01:55 -08:00
ws_package_string = ws_release_candidate + ws_package_string . replace ( " {vcsinfo} " , str ( ws_num_commits ) + " - " + commit_id )
2022-07-11 14:44:23 +00:00
2024-12-31 13:24:32 -08:00
try :
# This command is expected to fail if the version is not tagged
git_vcs_tag_cmd = shlex . split ( f ' git --git-dir= " { GIT_DIR } " describe --exact-match --match " ssv[0-9]* " ' )
git_vcs_tag = subprocess . check_output ( git_vcs_tag_cmd , stderr = subprocess . DEVNULL , universal_newlines = True ) . strip ( )
print ( f " We are on Stratoshark tag { git_vcs_tag } . " )
ss_package_string = tagged_version_extra
except subprocess . CalledProcessError :
print ( " We are not on a Stratoshark tag. " )
ss_package_string = untagged_version_extra
2022-07-11 14:44:23 +00:00
2025-01-08 14:01:55 -08:00
ss_package_string = ss_release_candidate + ss_package_string . replace ( " {vcsinfo} " , str ( ss_num_commits ) + " - " + commit_id )
2022-07-11 14:44:23 +00:00
repo_data = { }
repo_data [ ' commit_id ' ] = commit_id
repo_data [ ' enable_vcsversion ' ] = enable_vcsversion
2024-12-31 13:24:32 -08:00
repo_data [ ' ws_git_description ' ] = ws_git_description
repo_data [ ' ss_git_description ' ] = ss_git_description
2022-07-11 14:44:23 +00:00
repo_data [ ' info_source ' ] = " Command line (git) "
repo_data [ ' last_change ' ] = git_last_changetime
2024-12-31 13:24:32 -08:00
repo_data [ ' ws_num_commits ' ] = ws_num_commits
repo_data [ ' ss_num_commits ' ] = ss_num_commits
repo_data [ ' ws_package_string ' ] = ws_package_string
repo_data [ ' ss_package_string ' ] = ss_package_string
2022-07-11 14:44:23 +00:00
return repo_data
def parse_versionstring ( version_arg ) :
version_parts = version_arg . split ( ' . ' )
if len ( version_parts ) != 3 :
msg = " Version must have three numbers of the form x.y.z. You entered: " + version_arg
raise argparse . ArgumentTypeError ( msg )
for i , version_type in enumerate ( ( ' Major ' , ' Minor ' , ' Patch ' ) ) :
try :
int ( version_parts [ i ] )
except ValueError :
msg = f " { version_type } version must be a number! { version_type } version was ' { version_parts [ i ] } ' "
raise argparse . ArgumentTypeError ( msg )
return version_parts
def read_repo_info ( src_dir , tagged_version_extra , untagged_version_extra ) :
if IS_GIT_ARCHIVE :
repo_data = read_git_archive ( tagged_version_extra , untagged_version_extra )
elif os . path . exists ( src_dir + os . sep + ' .git ' ) and not os . path . exists ( os . path . join ( src_dir , ' .git ' , ' svn ' ) ) :
repo_data = read_git_repo ( src_dir , tagged_version_extra , untagged_version_extra )
else :
raise Exception ( src_dir + " does not appear to be a git repo or git archive! " )
return repo_data
# CMakeLists.txt calls this with no arguments to create vcs_version.h
# AppVeyor calls this with --set-release --untagged-version-extra=-{vcsinfo}-AppVeyor --tagged-version-extra=-AppVeyor
# .gitlab-ci calls this with --set-release
# Release checklist requires --set-version
def main ( ) :
parser = argparse . ArgumentParser ( description = ' Wireshark file and package versions ' )
action_group = parser . add_mutually_exclusive_group ( )
2024-12-31 13:24:32 -08:00
action_group . add_argument ( ' --set-wireshark-version ' , ' -v ' , metavar = ' <x.y.z> ' , type = parse_versionstring , help = ' Set the Wireshark major, minor, and patch versions in the top-level CMakeLists.txt, doc/attributes.adoc, packaging/debian/changelog, and the CMakeLists.txt for all libraries to the provided version number ' )
action_group . add_argument ( ' --set-stratoshark-version ' , ' -S ' , metavar = ' <x.y.z> ' , type = parse_versionstring , help = ' Set the Stratoshark major, minor, and patch versions in the top-level CMakeLists.txt and doc/attributes.adoc ' )
2023-06-13 21:47:22 +02:00
action_group . add_argument ( ' --set-release ' , ' -r ' , action = ' store_true ' , help = ' Set the extra release information in the top-level CMakeLists.txt based on either default or command-line specified options. ' )
2022-07-11 14:44:23 +00:00
setrel_group = parser . add_argument_group ( )
setrel_group . add_argument ( ' --tagged-version-extra ' , ' -t ' , default = " " , help = " Extra version information format to use when a tag is found. No format \
( an empty string ) is used by default . " )
setrel_group . add_argument ( ' --untagged-version-extra ' , ' -u ' , default = ' - {vcsinfo} ' , help = ' Extra version information format to use when no tag is found. The format " - {vcsinfo} " (the number of commits and commit ID) is used by default. ' )
2024-12-31 13:24:32 -08:00
parser . add_argument ( ' --wireshark-version-file ' , ' -f ' , metavar = ' <file> ' , type = argparse . FileType ( ' w ' ) , help = ' path to file containing a bare Wireshark version string ' )
parser . add_argument ( ' --stratoshark-version-file ' , metavar = ' <file> ' , type = argparse . FileType ( ' w ' ) , help = ' path to file containing a bare Stratoshark version string ' )
2022-07-11 14:44:23 +00:00
parser . add_argument ( " src_dir " , metavar = ' src_dir ' , nargs = 1 , help = " path to source code " )
args = parser . parse_args ( )
2024-12-31 13:24:32 -08:00
if args . wireshark_version_file and not args . set_release :
sys . stderr . write ( ' Error: --wireshark-version-file must be used with --set-release. \n ' )
sys . exit ( 1 )
if args . stratoshark_version_file and not args . set_release :
sys . stderr . write ( ' Error: --stratoshark-version-file must be used with --set-release. \n ' )
2023-10-28 14:13:10 +02:00
sys . exit ( 1 )
2022-07-11 14:44:23 +00:00
src_dir = args . src_dir [ 0 ]
2024-12-31 13:24:32 -08:00
set_version = args . set_wireshark_version or args . set_stratoshark_version
2022-07-11 14:44:23 +00:00
2024-12-31 13:24:32 -08:00
# Always get our version info from CMakeLists.txt
repo_data = { ' ws_package_string ' : ' ' , ' ss_package_string ' : ' ' }
cmake_path = os . path . join ( src_dir , " CMakeLists.txt " )
with open ( cmake_path , encoding = ' utf-8 ' ) as fh :
cmakelists_file_data = fh . read ( )
version_major , version_minor , version_patch = get_version ( cmakelists_file_data , Flavor . Wireshark )
repo_data [ ' ws_version_major ' ] = version_major
repo_data [ ' ws_version_minor ' ] = version_minor
repo_data [ ' ws_version_patch ' ] = version_patch
version_major , version_minor , version_patch = get_version ( cmakelists_file_data , Flavor . Stratoshark )
repo_data [ ' ss_version_major ' ] = version_major
repo_data [ ' ss_version_minor ' ] = version_minor
repo_data [ ' ss_version_patch ' ] = version_patch
if args . set_wireshark_version :
repo_data [ ' ws_version_major ' ] = args . set_wireshark_version [ 0 ]
repo_data [ ' ws_version_minor ' ] = args . set_wireshark_version [ 1 ]
repo_data [ ' ws_version_patch ' ] = args . set_wireshark_version [ 2 ]
elif args . set_stratoshark_version :
repo_data [ ' ss_version_major ' ] = args . set_stratoshark_version [ 0 ]
repo_data [ ' ss_version_minor ' ] = args . set_stratoshark_version [ 1 ]
repo_data [ ' ss_version_patch ' ] = args . set_stratoshark_version [ 2 ]
2022-07-11 14:44:23 +00:00
else :
2024-12-31 13:24:32 -08:00
repo_data . update ( read_repo_info ( src_dir , args . tagged_version_extra , args . untagged_version_extra ) )
2022-07-11 14:44:23 +00:00
2024-12-31 13:24:32 -08:00
set_vcs = not ( args . set_release or set_version )
2022-07-11 14:44:23 +00:00
VERSION_FILE = ' vcs_version.h '
print_VCS_REVISION ( VERSION_FILE , repo_data , set_vcs )
2024-12-31 13:24:32 -08:00
if args . set_release or set_version :
update_versioned_files ( src_dir , set_version , repo_data )
if args . wireshark_version_file :
create_version_file ( args . wireshark_version_file , repo_data , Flavor . Wireshark )
2022-07-11 14:44:23 +00:00
2024-12-31 13:24:32 -08:00
if args . stratoshark_version_file :
create_version_file ( args . stratoshark_version_file , repo_data , Flavor . Stratoshark )
2023-10-28 14:13:10 +02:00
2022-07-11 14:44:23 +00:00
if __name__ == " __main__ " :
main ( )