Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ tasks.named<Jar>("jar") { enabled = false }

val copyScripts by tasks.registering(Copy::class) {
from("scripts") {
include("start.sh", "start.bat", "download.bat", "upload.bat")
include("start.sh", "start.bat", "configure-backup.py", "templates/**")
}
into(layout.buildDirectory.dir("libs"))
doLast {
Expand Down
104 changes: 104 additions & 0 deletions scripts/configure-backup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/usr/bin/env python3

import platform
import re
import subprocess
from pathlib import Path
import os
import stat
import logging

logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)

year = str(input("Enter financial year in the format like (2026-27): "))


def check_valid_financial_year(year: str):
if not re.fullmatch(r"\d{4}-\d{2}", year):
return False
start_year = year.split("-")[0][2:]
end_year = year.split("-")[1]
return int(start_year) + 1 == int(end_year)


def replace_in_template(input_file, output_file, placeholder, replacement):
logger.info(f"generating {output_file}")
with open(f"templates/{input_file}", "r") as f:
lines = f.readlines()
new_lines = []
for line in lines:
new_line = line.replace(placeholder, replacement)
new_lines.append(new_line)
with open(output_file, "w") as f:
f.writelines(new_lines)


def make_executable(filename):
st = os.stat(filename)
os.chmod(filename, st.st_mode | stat.S_IXUSR)


def generate_bash_scripts():
logger.info("generating bash scripts")
replace_in_template(
"download.template.sh", "download.sh", "{{PLACEHOLDER_YEAR}}", year
)
replace_in_template("upload.template.sh", "upload.sh", "{{PLACEHOLDER_YEAR}}", year)
make_executable("download.sh")
make_executable("upload.sh")
make_executable("start.sh")


def generate_batch_scripts():
logger.info("generating batch scripts")
replace_in_template(
"download.template.bat", "download.bat", "{{PLACEHOLDER_YEAR}}", year
)
replace_in_template(
"upload.template.bat", "upload.bat", "{{PLACEHOLDER_YEAR}}", year
)


def init_github_repository():
repo_name = f"Satyam-Database-{year}"
if (Path(repo_name) / ".git").is_dir():
logger.info(f"{repo_name} already exists, so skipping clone")
return
github_username = "drcbabu81"
repo_url = f"git@github.com:{github_username}/{repo_name}.git"

try:
logger.info(f"cloning {repo_name}")
subprocess.run(
["git", "clone", repo_url], check=True, capture_output=True, text=True
)

if (Path(repo_name) / ".git").is_dir():
logger.info("clone successful")
else:
logger.error("git returned success but .git directory is missing")

except subprocess.CalledProcessError as e:
logger.error("clone failed")
logger.error(e.stderr)


if not check_valid_financial_year(year):
logger.error("Please enter a valid financial year in the format like (2026-27)")
exit(0)

os_name = platform.system()
init_github_repository()

if os_name == "Windows":
generate_batch_scripts()
elif os_name == "Darwin":
generate_bash_scripts()
elif os_name == "Linux":
generate_bash_scripts()
else:
logger.error(f"Unknown OS: {os_name}")
exit(1)
12 changes: 6 additions & 6 deletions scripts/download.bat → scripts/templates/download.template.bat
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
@echo off
@echo off

echo "Fetching Database from remote"

cd Satyam-Database-2026-27
cd Satyam-Database-{{PLACEHOLDER_YEAR}}
git pull origin main

git reset --hard
copy database.db ..\database.db

pause
pause
16 changes: 16 additions & 0 deletions scripts/templates/download.template.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

DIR=Satyam-Database-{{PLACEHOLDER_YEAR}}

if [ ! -d "$DIR" ]; then
echo "$DIR Directory does not exist"
exit 1
fi

cd $DIR || exit 1

echo "Fetching Database from remote"
git pull origin main

git reset --hard
cp database.db ../database.db
7 changes: 7 additions & 0 deletions scripts/templates/upload.template.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@echo off
copy database.db Satyam-Database-{{PLACEHOLDER_YEAR}}\database.db
cd Satyam-Database-{{PLACEHOLDER_YEAR}}
git add .
git commit -m "Updated database"
git push origin main
pause
16 changes: 16 additions & 0 deletions scripts/templates/upload.template.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

DIR=Satyam-Database-{{PLACEHOLDER_YEAR}}

if [ ! -d "$DIR" ]; then
echo "$DIR Directory does not exist"
exit 1
fi

cp database.db $DIR\database.db

cd $DIR

git add .
git commit -m "Updated database"
git push origin main
7 changes: 0 additions & 7 deletions scripts/upload.bat

This file was deleted.

Loading