Skip to content

Commit

Permalink
Change printversion to be generic readini
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Hawkins <sjjhsjjhsjjh@gmail.com>
  • Loading branch information
sjjhsjjh committed Apr 16, 2024
1 parent 4f7ee71 commit 384f2cd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 33 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/windows-build-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
matrix:
python-version: ["3.10"]
outputs:
version_number: ${{ steps.get_version_number.outputs.version_number }}
VersionNumber: ${{ steps.get_version_number.outputs.VersionNumber }}
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
Expand All @@ -25,7 +25,8 @@ jobs:
- name: Get version number
id: get_version_number
run: |
python src/utils/printversion.py version_number >> "$GITHUB_OUTPUT"
python src/utils/readini.py\
assets/Version.ini Release VersionNumber >> "$GITHUB_OUTPUT"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down Expand Up @@ -59,7 +60,7 @@ jobs:
# pyinstaller --distpath dist-portable build-portable.spec
- name: Demo version number
run: |
New-Item delme-${{needs.check.outputs.version_number}}.txt -type file
New-Item delme-${{needs.check.outputs.VersionNumber}}.txt -type file
# - name: Zip Portable
Expand Down
30 changes: 0 additions & 30 deletions src/utils/printversion.py

This file was deleted.

46 changes: 46 additions & 0 deletions src/utils/readini.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Standard library imports in alphabetical order.
#
# INI file parser, used to get the version number.
# https://docs.python.org/3/library/configparser.html
from configparser import ConfigParser
#
# Object oriented path handling.
# https://docs.python.org/3/library/pathlib.html
from pathlib import Path

def get_ini_value(iniPath, section, key):
ini = ConfigParser()
# Use .read_file() not .read() so that FileNotFoundError is raised if the
# file doesn't exist.
ini.read_file(Path(iniPath).open())
return ini[section][key]

if __name__ == "__main__":
# Argument parsing module.
# https://docs.python.org/3/library/argparse.html
from argparse import ArgumentParser

argumentParser = ArgumentParser(
description=
'Print a value from a .ini file in a key=value format for a GitHub'
' workflow output.'
)
argumentParser.add_argument(
"path", metavar="File.ini", type=Path, help='Path of the .ini file.')
argumentParser.add_argument(
"section", type=str, help=
'Within the file, the section name that contains the key.'
' Section names are enclosed in square brackets in .ini files, like'
' [Release].')
argumentParser.add_argument(
"key", type=str, help=
'Within the section, the key whose value is to be printed.')
argumentParser.add_argument(
"alias", type=str, nargs='?', help=
'Optional key to appear in the output.'
' Default is to use the same key that appears in the .ini file.')
arguments = argumentParser.parse_args()
print('='.join((
arguments.key if arguments.alias is None else arguments.alias,
get_ini_value(arguments.path, arguments.section, arguments.key)
)))

0 comments on commit 384f2cd

Please sign in to comment.