Backuppy/install.py

177 lines
5.7 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""
project: Backuppy
version: 0.7
file: install.py
summary: main entry python file
"""
# Standard library imports
import sys
import os
import time
# local imports
from languages import english
from languages import german
# local globals
# ----------------------
VERSION: str = "0.7"
# ----------------------
MYDIR = os.getcwd()
EXCLUDE_FILE = "exclude.txt"
BACKUPPY_SCRIPT = "Backuppy.sh"
SHELL = os.environ.get("SHELL")
HOME = os.environ.get("HOME")
LANG_EN: str = "English"
LANG_DE: str = "German"
LANGUAGE: str = None
RSYNC_CMD: str = None
EMAIL: str = "fotocoder@joschu.ch"
def set_language(language):
global LANGUAGE
LANGUAGE = language
def trace(message_txt):
""" Print a formatted message to std out. """
print("[ OK ] " + message_txt)
def get_lang_text(search_str: str):
global LANGUAGE
""" Returns a string from the appropriate language file. """
return_str: str = eval("english." + search_str)
if LANGUAGE == LANG_DE:
return_str = eval("german." + search_str)
return return_str
def install_cli_main():
language = input("Hello, first of all, which language do you prefer: German [DE] or English [EN]?\n> ")
if language.upper() == "DE":
set_language(LANG_DE)
print("Perfekt, nun ist das deutsche Sprachpaket aktiviert. Willkommen!\n")
else:
print("Perfect, the English language package is now activated. Welcome!.\n")
time.sleep(1)
print("\n" + get_lang_text("intromsg1") + "\n")
time.sleep(1)
print("\n" + get_lang_text("intromsg2") + "\n")
time.sleep(1)
# which Rsync options are available and which one you want to use
print(get_lang_text("rsyncopt") + "\n")
time.sleep(1)
# asks if you want to exclude files/directories from backup and creates an exclude file in case of Yes
exclude = input(get_lang_text("excludefile1") + "\n> ")
if exclude.upper() in ("J", "Y"):
print(get_lang_text("excludefile2") + "\n")
os.environ["BUPY_CREATE_EXCLUDE"] = "True"
else:
print(get_lang_text("excludefile3") + "\n")
time.sleep(1)
# Asks for the source directory which should be saved
print(get_lang_text("srcdir1"))
time.sleep(1)
sourcedir = input(get_lang_text("srcdir2") + "\n> ")
print(f"{get_lang_text('srcdir3_1')} {sourcedir} {get_lang_text('srcdir3_2')}")
time.sleep(1)
# asks for the destination directory in which the backup should be saved
targetdir = input(get_lang_text("targetdir1") + "\n> ")
print(f"{get_lang_text('targetdir2_1')} {targetdir} {get_lang_text('targetdir2_2')}")
time.sleep(1)
# collects all the information needed to execute the rsync command and creates it.
print(get_lang_text("collect") + "\n")
time.sleep(1)
exclude_file = os.path.join(MYDIR, EXCLUDE_FILE)
RSYNC_CMD = f"rsync -aqp --exclude-from={exclude_file} {sourcedir} {targetdir}"
os.environ["BUPY_RSYNC_CMD"] = RSYNC_CMD
print(f"{RSYNC_CMD}")
time.sleep(1)
# Outro
print(get_lang_text("outro1"))
time.sleep(2)
print(get_lang_text("outro2") + " " + EMAIL)
def create_exclude_file(directory, exclude_file):
exclude_file = os.path.join(directory, exclude_file)
with open(exclude_file, "w") as fExclude:
trace(f"creating exclude-file '{exclude_file}'.")
fExclude.write("\n")
return exclude_file
def create_alias(directory, backuppy_script):
# alias entry in .bashrc or .zshrc
backuppy_script = os.path.join(directory, backuppy_script)
alias_str = f"alias backuppy='sudo {backuppy_script}'"
# Check for installed ZSH
if SHELL.upper().find("ZSH") > 0:
rc_filepath = os.path.join(HOME, ".zshrc")
# Check for installed BASH
if SHELL.upper().find("BASH") > 0:
rc_filepath = os.path.join(HOME, ".bashrc")
# Append our alias if not already existing
if os.path.isfile(rc_filepath):
fileRc = open(rc_filepath, "r") # open file in read mode
backuppy_entry_exists = False
for line in fileRc:
if "alias backuppy=" in line:
backuppy_entry_exists = True
break
if not backuppy_entry_exists:
trace(f"Writing {alias_str} to config file '{rc_filepath}'.")
fileRc = open(rc_filepath, "a") # open file in append mode
fileRc.write("\n# Following line was created by Backuppy\n" + alias_str + "\n")
fileRc.close()
def create_backuppy_script(directory, backuppy_script, rsync_cmd):
# creates the file 'Backuppy.sh'
backuppy_file = os.path.join(directory, backuppy_script)
with open(backuppy_file, "w") as fBackuppy:
trace(f"creating backuppy-file '{backuppy_file}'.")
fBackuppy.write("#!/bin/bash\n" + rsync_cmd + "\n")
os.chmod(backuppy_file, 0o777) # make file executable
def do_the_install():
""" Does the things with our environment variables. """
is_exclude = os.environ.get("BUPY_CREATE_EXCLUDE")
rsync_cmd = os.environ.get("BUPY_RSYNC_CMD")
if is_exclude == "True":
exclude_file = create_exclude_file(MYDIR, EXCLUDE_FILE)
create_alias(MYDIR, BACKUPPY_SCRIPT)
create_backuppy_script(MYDIR, BACKUPPY_SCRIPT, rsync_cmd)
def main(argv):
trace(f"Starting Backuppy install.py v{VERSION}")
if argv and argv[0] == "--gui":
import install_gui
trace("Starting GUI-version.\n")
install_gui.main() # collect user input via GUI and store in env. variables
else:
trace("Starting CLI-version.\n")
install_cli_main() # collect user input via CLI and store in env. variables
do_the_install()
trace("Ending Backuppy install.py")
if __name__ == '__main__':
# sys.argv.append("--gui") # TODO: disable for production
sys.exit(main(sys.argv[1:]))