diff --git a/.github/workflows/windows-build-version.yml b/.github/workflows/windows-build-version.yml index 363ebc0..dca4a58 100644 --- a/.github/workflows/windows-build-version.yml +++ b/.github/workflows/windows-build-version.yml @@ -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 }} @@ -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 @@ -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 diff --git a/src/utils/printversion.py b/src/utils/printversion.py deleted file mode 100644 index d8e635e..0000000 --- a/src/utils/printversion.py +++ /dev/null @@ -1,30 +0,0 @@ -# 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 ini_version(): - versionINIPath = Path(__file__).parents[2] / "assets" / "Version.ini" - versionINI = ConfigParser() - versionINI.read(versionINIPath) - return versionINI['Release']['VersionNumber'] - -if __name__ == "__main__": - # Argument parsing module. - # https://docs.python.org/3/library/argparse.html - from argparse import ArgumentParser - argumentParser = ArgumentParser( - description=''' -Print the version number from the assets/Version.ini file in a key=value format -for a GitHub workflow output.''' - ) - argumentParser.add_argument( - "key", type=str, default="version_number", help= - 'Key name. Default: version_number') - arguments = argumentParser.parse_args() - print(f'{arguments.key}={ini_version()}') diff --git a/src/utils/readini.py b/src/utils/readini.py new file mode 100644 index 0000000..d81cc52 --- /dev/null +++ b/src/utils/readini.py @@ -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) + )))