From 5445c2a19415e80f04282b1ed452f5f0a30142f5 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Tue, 13 Dec 2016 22:04:30 +0100 Subject: [PATCH] refactore the code I've created multiple functions to give the code a better structure --- borg_interface.py | 116 ++++++++++++++++++++++++++-------------------- 1 file changed, 67 insertions(+), 49 deletions(-) diff --git a/borg_interface.py b/borg_interface.py index e4b838c..d92b3ce 100755 --- a/borg_interface.py +++ b/borg_interface.py @@ -1,37 +1,80 @@ #!/usr/bin/env python3 import os -import sys import configparser import subprocess chosen_activity = None mount_point = None -# setup the config parser -config = configparser.ConfigParser() +def list_archives(): + os.system('borg list | less') -# read config file -config.read('borg_interface.cfg') +def show_info(): + archive_name = input("Please enter the archive name: ") + os.system('clear') + os.system('borg info ::' + archive_name) + print() + input("Press a key to continue.") -# assign the repository variable -if 'server' in config: - repository = (config['DEFAULT']['user'] - + "@" - + config['DEFAULT']['server'] - + ":" - + config['DEFAULT']['repository_path']) -else: - repository = config['DEFAULT']['repository_path'] +def mount_archive(): + global mount_point + archive_name = input("Please enter the archive name: ") + mount_point = "/tmp/" + archive_name + if not os.path.exists(mount_point): + os.makedirs(mount_point) + os.system('borg mount ::' + archive_name + " " + mount_point) + print() + print("Archive mounted at " + mount_point + "/") + print() -# assign the password variable -password = config['DEFAULT']['password'] +def restore_archive(): + archive_name = input("Please enter the archive name: ") + restore_path = input("Please enter the path where you want to " + "restore to: ") + if not os.path.exists(restore_path): + os.makedirs(restore_path) + p = subprocess.Popen(['borg', 'extract', '::' + archive_name] + ,cwd=restore_path) + p.wait() + print() + print("Archive extracted to " + restore_path) + print() -# set the environment variables -os.environ['BORG_REPO'] = repository -os.environ['BORG_PASSPHRASE'] = password +def configuration(): + # setup the config parser + config = configparser.ConfigParser() + # read config file + config.read('borg_interface.cfg') + # assign the repository variable + if 'server' in config: + repository = (config['DEFAULT']['user'] + + "@" + + config['DEFAULT']['server'] + + ":" + + config['DEFAULT']['repository_path']) + else: + repository = config['DEFAULT']['repository_path'] + # assign the password variable + password = config['DEFAULT']['password'] + # set the environment variables + os.environ['BORG_REPO'] = repository + os.environ['BORG_PASSPHRASE'] = password + +def exit(): + global mount_point + if (not mount_point): + print() + else: + print() + print("Unmount Archive and remove folder.") + print() + os.system('fusermount -u' + " " + mount_point) + os.rmdir(mount_point) # The main menu starts there +configuration() while chosen_activity != 0: + os.system('clear') print("What would you like to do?") # Start the chosen activity and go back to the activity selector. print("1: List Backups, 2: Show archive details, 3: Mount Archive, " @@ -41,42 +84,17 @@ while chosen_activity != 0: if chosen_activity == 1: # prints all the archives in the repository and lists them with # less - os.system('borg list | less') + list_archives() if chosen_activity == 2: # Displays all the information related to the archive name the user # enters - archive_name = input("Please enter the archive name: ") - os.system('borg info ::' + archive_name) + show_info() if chosen_activity == 3: # mounts a chosen archive to /tmp/archive name - archive_name = input("Please enter the archive name: ") - mount_point = "/tmp/" + archive_name - if not os.path.exists(mount_point): - os.makedirs(mount_point) - os.system('borg mount ::' + archive_name + " " + mount_point) - print() - print("Archive mounted at " + mount_point + "/") - print() + mount_archive() if chosen_activity == 4: - archive_name = input("Please enter the archive name: ") - restore_path = input("Please enter the path where you want to " - "restore to: ") - if not os.path.exists(restore_path): - os.makedirs(restore_path) - p = subprocess.Popen(['borg', 'extract', '::' + archive_name] - ,cwd=restore_path) - p.wait() - print() - print("Archive extracted to " + restore_path) - print() + restore_archive() elif chosen_activity == 0: - if (not mount_point): - print() - else: - print() - print("Unmount Archive and remove folder.") - print() - os.system('fusermount -u' + " " + mount_point) - os.rmdir(mount_point) + exit() except ValueError: print("Please enter a full number.")