2025-10-13 23:08:34 +02:00
|
|
|
import json
|
|
|
|
|
import os
|
|
|
|
|
import argparse
|
2025-10-14 21:31:05 +02:00
|
|
|
import sys
|
2025-10-13 23:08:34 +02:00
|
|
|
|
2025-10-14 21:31:05 +02:00
|
|
|
# ======================================================================
|
|
|
|
|
# Constantes
|
2025-10-13 23:08:34 +02:00
|
|
|
FILE_NAME = "version.json"
|
2025-10-14 21:31:05 +02:00
|
|
|
# ======================================================================
|
2025-10-13 23:08:34 +02:00
|
|
|
|
2025-10-14 21:31:05 +02:00
|
|
|
# ======================================================================
|
|
|
|
|
# Parser
|
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
|
prog="pyvers",
|
|
|
|
|
description="xD",
|
|
|
|
|
)
|
2025-10-13 23:08:34 +02:00
|
|
|
|
2025-10-14 21:31:05 +02:00
|
|
|
parser.add_argument(
|
|
|
|
|
"config",
|
|
|
|
|
help="The location of the config file to edit.",
|
|
|
|
|
type=str,
|
|
|
|
|
)
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--patch",
|
|
|
|
|
help="Bumps the patch version by one.",
|
|
|
|
|
action="store_true",
|
|
|
|
|
)
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--minor",
|
|
|
|
|
help="Bumps the minor version by one.",
|
|
|
|
|
action="store_true",
|
|
|
|
|
)
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--major",
|
|
|
|
|
help="Bumps the major version by one.",
|
|
|
|
|
action="store_true",
|
|
|
|
|
)
|
2025-10-13 23:08:34 +02:00
|
|
|
|
2025-10-14 21:31:05 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
# ======================================================================
|
2025-10-13 23:08:34 +02:00
|
|
|
|
|
|
|
|
|
2025-10-14 21:31:05 +02:00
|
|
|
# ======================================================================
|
|
|
|
|
# Functions
|
2025-10-13 23:08:34 +02:00
|
|
|
def create_version_file(file_path) -> None:
|
2025-10-14 21:31:05 +02:00
|
|
|
"""Creates a version.json file with all versions set to 0 at the
|
|
|
|
|
input location.
|
2025-10-13 23:08:34 +02:00
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
file_path: (str): Path with file name.
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
>>>create_version_file(/path/to/file)
|
|
|
|
|
file:
|
|
|
|
|
{
|
|
|
|
|
"minor": 0,
|
|
|
|
|
"major": 0,
|
|
|
|
|
"patch": 0
|
|
|
|
|
}"""
|
|
|
|
|
|
|
|
|
|
# Defining the dictionary with the initial version numbers.
|
|
|
|
|
initial_version = {"minor": 0, "major": 0, "patch": 0}
|
|
|
|
|
|
|
|
|
|
# Opening/creating the file to write the dictionary as json to it.
|
|
|
|
|
file = open(file_path, "x")
|
|
|
|
|
file.write(json.dumps(initial_version, indent=4))
|
|
|
|
|
print("File written successfully.")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_version(input_path) -> dict[str, int]:
|
|
|
|
|
"""Opens the given file and returns its contents."""
|
2025-10-14 21:31:05 +02:00
|
|
|
# Open given path and loading the files contents into a variable
|
2025-10-13 23:08:34 +02:00
|
|
|
file_path = open(input_path)
|
|
|
|
|
prog_version = json.load(file_path)
|
|
|
|
|
return prog_version
|
|
|
|
|
|
|
|
|
|
|
2025-10-14 21:31:05 +02:00
|
|
|
def bump_patch(version_file) -> None:
|
|
|
|
|
"""Bumps the patch number of the given file by one."""
|
|
|
|
|
# Get the version as dictionary
|
|
|
|
|
version = load_version(version_file)
|
2025-10-13 23:08:34 +02:00
|
|
|
|
2025-10-14 21:31:05 +02:00
|
|
|
# Adding one to the version from the file and updating
|
|
|
|
|
# the dictionary
|
|
|
|
|
new_version = version["patch"] + 1
|
|
|
|
|
version.update({"patch": new_version})
|
2025-10-13 23:08:34 +02:00
|
|
|
|
2025-10-14 21:31:05 +02:00
|
|
|
# Opening the file and overwriting its contents.
|
|
|
|
|
with open(version_file, "w") as f:
|
|
|
|
|
f.write(json.dumps(version, indent=4))
|
2025-10-13 23:08:34 +02:00
|
|
|
|
|
|
|
|
|
2025-10-14 21:31:05 +02:00
|
|
|
def bump_minor(version_file) -> None:
|
|
|
|
|
"""Bumps the minor version number of the given file by one."""
|
|
|
|
|
# Get the version as dictionary
|
|
|
|
|
version = load_version(version_file)
|
2025-10-13 23:08:34 +02:00
|
|
|
|
2025-10-14 21:31:05 +02:00
|
|
|
# Adding one to the version from the file and updating
|
|
|
|
|
# the dictionary
|
|
|
|
|
new_version = version["minor"] + 1
|
|
|
|
|
version.update({"minor": new_version})
|
2025-10-13 23:08:34 +02:00
|
|
|
|
2025-10-14 21:31:05 +02:00
|
|
|
# Opening the file and overwriting its contents.
|
|
|
|
|
with open(version_file, "w") as f:
|
|
|
|
|
f.write(json.dumps(version, indent=4))
|
2025-10-13 23:08:34 +02:00
|
|
|
|
|
|
|
|
|
2025-10-14 21:31:05 +02:00
|
|
|
def bump_major(version_file) -> None:
|
|
|
|
|
"""Bumps the major version number of the given file by one."""
|
|
|
|
|
# Get the version as dictionary
|
|
|
|
|
version = load_version(version_file)
|
|
|
|
|
|
|
|
|
|
# Adding one to the version from the file and updating
|
|
|
|
|
# the dictionary
|
|
|
|
|
new_version = version["major"] + 1
|
|
|
|
|
version.update({"major": new_version})
|
|
|
|
|
|
|
|
|
|
# Opening the file and overwriting its contents.
|
|
|
|
|
with open(version_file, "w") as f:
|
|
|
|
|
f.write(json.dumps(version, indent=4))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def version_bumper(version_file, major_version, minor_version, patch) -> None:
|
|
|
|
|
"""Decides what version to bump, based on the users passed flags"""
|
|
|
|
|
if patch:
|
|
|
|
|
bump_patch(version_file)
|
|
|
|
|
if minor_version:
|
|
|
|
|
bump_minor(version_file)
|
|
|
|
|
if major_version:
|
|
|
|
|
bump_major(version_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_for_file(input_path: str) -> bool:
|
|
|
|
|
"""Checks if the file 'version.json' exists at the given path.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
input_path ( str ): Path to check for the file.
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
>>>check_for_file(".")
|
|
|
|
|
'False'
|
|
|
|
|
>>>check_for_file("version.json")
|
|
|
|
|
'True'
|
|
|
|
|
>>>check_for_file("./version.json")
|
|
|
|
|
'True'
|
|
|
|
|
"""
|
|
|
|
|
if FILE_NAME in input_path:
|
|
|
|
|
file_path = os.path.expanduser(input_path)
|
|
|
|
|
if os.path.exists(file_path):
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
return False
|
|
|
|
|
elif os.path.exists(os.path.join(input_path, FILE_NAME)):
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def request_to_create():
|
|
|
|
|
"""Prompts the user if he wants to create the file and exits if the
|
|
|
|
|
user declines or presses any other character instead of 'y/Y'"""
|
2025-10-13 23:08:34 +02:00
|
|
|
|
|
|
|
|
PROMPT = "The file does not exist, do you want to create it? [y/n]"
|
|
|
|
|
PROMPT += "\n>>>"
|
2025-10-14 21:31:05 +02:00
|
|
|
|
2025-10-13 23:08:34 +02:00
|
|
|
response = input(PROMPT)
|
|
|
|
|
if "y" in response or "Y" in response:
|
2025-10-14 21:31:05 +02:00
|
|
|
file_path = os.path.join(
|
|
|
|
|
os.path.expanduser(args.config),
|
|
|
|
|
FILE_NAME,
|
|
|
|
|
)
|
2025-10-13 23:08:34 +02:00
|
|
|
create_version_file(file_path)
|
2025-10-14 21:31:05 +02:00
|
|
|
return
|
2025-10-13 23:08:34 +02:00
|
|
|
else:
|
|
|
|
|
parser.print_help()
|
2025-10-14 21:31:05 +02:00
|
|
|
sys.exit(1)
|
2025-10-13 23:08:34 +02:00
|
|
|
|
2025-10-14 21:31:05 +02:00
|
|
|
|
|
|
|
|
# ======================================================================
|
|
|
|
|
# Dispatching
|
|
|
|
|
if check_for_file(args.config):
|
|
|
|
|
file_path = os.path.join(os.path.expanduser(args.config), FILE_NAME)
|
|
|
|
|
if args.patch or args.minor or args.major:
|
|
|
|
|
version_bumper(
|
|
|
|
|
version_file=file_path,
|
|
|
|
|
major_version=args.major,
|
|
|
|
|
minor_version=args.minor,
|
|
|
|
|
patch=args.patch,
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
print(load_version(file_path))
|
|
|
|
|
|
|
|
|
|
if check_for_file(args.config) is False:
|
|
|
|
|
request_to_create()
|
|
|
|
|
file_path = os.path.join(os.path.expanduser(args.config), FILE_NAME)
|
|
|
|
|
version_bumper(
|
|
|
|
|
version_file=file_path,
|
|
|
|
|
major_version=args.major,
|
|
|
|
|
minor_version=args.minor,
|
|
|
|
|
patch=args.patch,
|
|
|
|
|
)
|