Last active
June 10, 2026 00:37
-
-
Save AntumDeluge/09f8d2abb81ed572851e819e8f58c51e to your computer and use it in GitHub Desktop.
Script to set Luanti mod version to current date in format YYYY-MM-DD.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| ''' | |
| MIT License | |
| Copyright © 2026 Jordan Irwin (AntumDeluge) | |
| Permission is hereby granted, free of charge, to any person obtaining a copy of | |
| this software and associated documentation files (the "Software"), to deal in | |
| the Software without restriction, including without limitation the rights to | |
| use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | |
| of the Software, and to permit persons to whom the Software is furnished to do | |
| so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in | |
| all copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| SOFTWARE. | |
| ''' | |
| ''' | |
| Script to set Luanti mod version to current date in format YYYY-MM-DD. | |
| Must be run from mod directory. Updates the following files if they exist: | |
| - mod.conf | |
| - changelog.txt | |
| - .ldoc/config.ld | |
| ''' | |
| import errno | |
| import os | |
| import sys | |
| from datetime import datetime | |
| root = os.getcwd() | |
| version = datetime.now().strftime("%Y-%m-%d") | |
| mod_conf = [] | |
| ## Prints a warning message to stdout. | |
| # | |
| # @param msg | |
| # Message text. | |
| def logWarn(msg): | |
| sys.stdout.write(f"\nWARNING: {msg}\n") | |
| ## Prints an error message to stderr. | |
| # | |
| # @param msg | |
| # Message text. | |
| def logError(msg): | |
| sys.stderr.write(f"\nERROR: {msg}\n") | |
| ## Prints an error message to stderr & exits. | |
| # | |
| # @param e | |
| # Error code. | |
| # @param msg | |
| # Message text. | |
| def exitWithError(e, msg): | |
| logError(msg) | |
| sys.exit(e) | |
| ## Parses mod configuration file `mod.conf` to memory. | |
| def parseModConf(): | |
| conf = "mod.conf" | |
| conf_path = os.path.join(root, "mod.conf") | |
| if not os.path.exists(conf_path): | |
| exitWithError(errno.ENOENT, f"mod configuration not found: {conf}") | |
| elif os.path.isdir(conf_path): | |
| exitWithError(errno.EISDIR, f"mod configuration path is directory: {conf}") | |
| fin = open(conf_path, "r", newline="\n") | |
| lines = fin.read().split("\n") | |
| fin.close() | |
| l_idx = 0 | |
| for l in lines: | |
| l_idx = l_idx + 1 | |
| l = l.strip() | |
| if len(l) == 0 or l.startswith("#"): | |
| # ignore empty & commented-out lines | |
| continue | |
| elif l.startswith(" ") or l.startswith("\t") or "=" not in l: | |
| logWarn(f"malformed configuration at line {l_idx}: {conf}") | |
| continue | |
| temp = l.split("=", 1) | |
| mod_conf.append((temp[0].strip(), temp[1].strip())) | |
| ## Retrieves a value parsed from `mod.conf` file. | |
| # | |
| # @param key | |
| # String key index. | |
| # @return | |
| # String value or `None`. | |
| def getModConf(key): | |
| for c in mod_conf: | |
| if c[0] == key: | |
| return c[1] | |
| ## Updates contents of a text file. | |
| # | |
| # @param r_path | |
| # Relative path to file to be updated. | |
| # @param search | |
| # The string to match. | |
| # @param replace | |
| # The new string where "{v}" denotes version. | |
| def freplace(r_path, search, replace): | |
| f_path = os.path.join(root, r_path) | |
| if not os.path.isfile(f_path): | |
| print(f"`{r_path}` not updated, file not found") | |
| return | |
| replace = replace.replace("{v}", version) | |
| fin = open(f_path, "r", newline="\n") | |
| lines = fin.readlines() | |
| fin.close() | |
| # TODO: use regex on entire file | |
| text_orig = "".join(lines) | |
| found = False | |
| for idx in range(len(lines)): | |
| l = lines[idx] | |
| if l.startswith(search): | |
| lines[idx] = f"{replace}\n" | |
| found = True | |
| break | |
| if not found: | |
| print(f"`{r_path}` not updated, does not contain text \"{search.strip("\n")}\"") | |
| return | |
| text_new = "".join(lines) | |
| if text_new == text_orig: | |
| print(f"`{r_path}` not updated, contents unchanged") | |
| return | |
| fout = open(f_path, "w", newline="\n") | |
| fout.write(text_new) | |
| fout.close() | |
| print(f"`{r_path}` contents updated") | |
| if __name__ == "__main__": | |
| to_update = ( | |
| ("mod.conf", "version =", "version = {v}"), | |
| ("changelog.txt", "next\n", "{v}"), | |
| (os.path.normpath(".ldoc/config.ld"), "local version =", "local version = {v}") | |
| ) | |
| parseModConf() | |
| mod_name = getModConf("name") | |
| mod_ver_old = getModConf("version") | |
| mod_info = "\nMod info:" | |
| if mod_name: | |
| mod_info = mod_info + f"\n name: {mod_name}" | |
| if mod_ver_old: | |
| mod_info = mod_info + f"\n old version: {mod_ver_old}" | |
| mod_info = mod_info + f"\n new version: {version}" | |
| print(mod_info) | |
| print() | |
| for x in to_update: | |
| freplace(os.path.normpath(x[0]), x[1], x[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment