2018-09-06 10:09:40 +02:00
#!/usr/bin/env python
2018-09-05 22:03:16 +02:00
import os
import sys
import subprocess
import configparser
import socket
import datetime
import shutil
from pathlib import Path
import stat
import pwd
import grp
2018-09-06 10:09:40 +02:00
2018-09-05 22:03:16 +02:00
CONFIG_SYNC_VERSION = " 1.0.0 "
2018-09-06 10:09:40 +02:00
CONFIG_SYNC_FILE = " /etc/cfgs.conf "
# -----------------------------------------
# color defintions
# -----------------------------------------
2018-09-05 22:03:16 +02:00
2018-09-05 22:25:54 +02:00
class bcolors :
HEADER = ' \033 [95m '
OKBLUE = ' \033 [94m '
OKGREEN = ' \033 [92m '
WARNING = ' \033 [93m '
FAIL = ' \033 [91m '
ENDC = ' \033 [0m '
BOLD = ' \033 [1m '
UNDERLINE = ' \033 [4m '
2018-09-06 10:09:40 +02:00
# -----------------------------------------
# UI helper functions
# -----------------------------------------
2018-09-05 22:03:16 +02:00
def check_dependencies ( ) :
status = True
try :
subprocess . call ( [ " git " , " version " ] , stdout = subprocess . DEVNULL )
except OSError :
status = False
2018-09-05 22:25:54 +02:00
print ( bcolors . FAIL + " git cannot be found on your system! " + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
print ( " Install git: pacman -S git " )
return status
2018-09-06 10:09:40 +02:00
2018-09-05 22:03:16 +02:00
def print_splash ( ) :
2018-09-06 10:09:40 +02:00
print ( bcolors . HEADER + " ---=== cfgs ===--- " + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
print ( " Version: " + CONFIG_SYNC_VERSION )
print ( )
2018-09-05 22:25:54 +02:00
print ( bcolors . HEADER + " ---=== USAGE ===--- " + bcolors . ENDC )
2018-09-06 10:09:40 +02:00
print ( " cfgs init " )
print ( " cfgs add <file path> " )
print ( " cfgs remove <file path> " )
print ( " cfgs store " )
print ( " cfgs restore " )
2018-09-05 22:03:16 +02:00
sys . exit ( 1 )
def init_dialog ( ) :
config = get_config ( )
2018-09-06 10:09:40 +02:00
print ( bcolors . HEADER + " ---=== Welcome to cfgs! ===--- " + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
print ( )
if config [ ' DEFAULT ' ] [ ' INITIALIZED ' ] :
2018-09-06 10:09:40 +02:00
reinitialize = input ( bcolors . WARNING + " WARNING: cfgs has already been initialized! Do you really want to change the configuration? [y/N]: " + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
if not reinitialize or reinitialize != " y " :
print ( " Skipping new initialization! " )
sys . exit ( 0 )
2018-09-05 22:25:54 +02:00
local_path = input ( bcolors . OKBLUE + " Enter the local storage directory for configuration files [ " + config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] + " ]: " + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
if not local_path :
local_path = config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ]
config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] = local_path
2018-09-05 22:25:54 +02:00
remote_repo = input ( bcolors . OKBLUE + " Enter the remote repository url [ " + config [ ' GIT ' ] [ ' REMOTE_REPOSITORY ' ] + " ]: " + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
if not remote_repo :
remote_repo = config [ ' GIT ' ] [ ' REMOTE_REPOSITORY ' ]
config [ ' GIT ' ] [ ' REMOTE_REPOSITORY ' ] = remote_repo
2018-09-05 22:25:54 +02:00
git_ssh_key = input ( bcolors . OKBLUE + " Enter the path to the ssh key to use [ " + config [ ' GIT ' ] [ ' SSHKEY ' ] + " ]: " + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
if not git_ssh_key :
git_ssh_key = config [ ' GIT ' ] [ ' SSHKEY ' ]
config [ ' GIT ' ] [ ' SSHKEY ' ] = git_ssh_key
2018-09-05 22:25:54 +02:00
git_user = input ( bcolors . OKBLUE + " Enter the git user name (displayname) [ " + config [ ' GIT ' ] [ ' USER ' ] + " ]: " + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
if not git_user :
git_user = config [ ' GIT ' ] [ ' USER ' ]
config [ ' GIT ' ] [ ' USER ' ] = git_user
2018-09-05 22:25:54 +02:00
git_email = input ( bcolors . OKBLUE + " Enter the git user email address [ " + config [ ' GIT ' ] [ ' EMAIL ' ] + " ]: " + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
if not git_email :
git_email = config [ ' GIT ' ] [ ' EMAIL ' ]
config [ ' GIT ' ] [ ' EMAIL ' ] = git_email
config [ ' DEFAULT ' ] [ ' INITIALIZED ' ] = " True "
update_config ( config )
2018-09-06 10:09:40 +02:00
# -----------------------------------------
# local config helper functions
# -----------------------------------------
def validate_or_create_config ( config ) :
if not os . path . exists ( CONFIG_SYNC_FILE ) :
print ( bcolors . OKBLUE + " Creating default configuration file: " + CONFIG_SYNC_FILE + bcolors . ENDC )
config [ ' DEFAULT ' ] = { }
config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] = " /opt/cfgs/storage "
config [ ' DEFAULT ' ] [ ' INITIALIZED ' ] = " "
config [ ' GIT ' ] = { }
config [ ' GIT ' ] [ ' REMOTE_REPOSITORY ' ] = " "
config [ ' GIT ' ] [ ' USER ' ] = " cfgs "
config [ ' GIT ' ] [ ' EMAIL ' ] = " cfgs@ " + socket . gethostname ( )
config [ ' GIT ' ] [ ' SSHKEY ' ] = str ( Path . home ( ) ) + " /.ssh/id_rsa "
update_config ( config )
else :
config . read ( CONFIG_SYNC_FILE )
return config
def get_config ( ) :
config = configparser . ConfigParser ( )
config = validate_or_create_config ( config )
return config
def update_config ( config ) :
try :
config . write ( open ( CONFIG_SYNC_FILE , ' w ' ) )
except PermissionError as e :
print ( bcolors . FAIL + " Unable to write config file! Error: " + e . strerror + bcolors . ENDC )
sys . exit ( 1 )
except Exception as e :
print ( bcolors . FAIL + " Unable to write config file! Error: " + str ( e ) + bcolors . ENDC )
sys . exit ( 1 )
2018-09-05 22:03:16 +02:00
def create_initialization_file ( path ) :
try :
2018-09-06 10:09:40 +02:00
with open ( path + " /cfgs.info " , mode = ' w ' ) as file :
file . write ( ' cfgs repo initialized: %s . host: %s \n ' % ( datetime . datetime . now ( ) , socket . gethostname ( ) ) )
2018-09-05 22:03:16 +02:00
except PermissionError as e :
2018-09-05 22:25:54 +02:00
print ( bcolors . FAIL + " Unable to write initialization file! Error: " + e . strerror + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
sys . exit ( 1 )
except Exception as e :
2018-09-05 22:25:54 +02:00
print ( bcolors . FAIL + " Unable to write initialization file! Error: " + str ( e ) + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
sys . exit ( 1 )
try :
2018-09-06 10:09:40 +02:00
open ( path + " /cfgs.db " , mode = ' a ' ) . close ( )
2018-09-05 22:03:16 +02:00
except PermissionError as e :
2018-09-05 22:25:54 +02:00
print ( bcolors . FAIL + " Unable to write database file! Error: " + e . strerror + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
sys . exit ( 1 )
except Exception as e :
2018-09-05 22:25:54 +02:00
print ( bcolors . FAIL + " Unable to write database file! Error: " + str ( e ) + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
sys . exit ( 1 )
2018-09-06 10:09:40 +02:00
# -----------------------------------------
# local database helper functions
# -----------------------------------------
2018-09-05 22:03:16 +02:00
def add_file_to_db ( file_path ) :
config = get_config ( )
try :
insert = True
2018-09-06 10:09:40 +02:00
with open ( config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] + " /cfgs.db " , " r+ " ) as search :
2018-09-05 22:03:16 +02:00
for line in search :
line = line . rstrip ( ) # remove '\n' at end of line
if file_path == line :
insert = False
if insert :
2018-09-06 10:09:40 +02:00
with open ( config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] + " /cfgs.db " , mode = ' a ' ) as file :
2018-09-05 22:03:16 +02:00
file . write ( ' %s \n ' % ( file_path ) )
except PermissionError as e :
2018-09-05 22:25:54 +02:00
print ( bcolors . FAIL + " Unable to write db file! Error: " + e . strerror + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
sys . exit ( 1 )
except Exception as e :
2018-09-05 22:25:54 +02:00
print ( bcolors . FAIL + " Unable to write db file! Error: " + str ( e ) + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
sys . exit ( 1 )
2018-09-06 10:09:40 +02:00
2018-09-05 22:03:16 +02:00
def delete_file_from_db ( file_path ) :
config = get_config ( )
try :
2018-09-06 10:09:40 +02:00
with open ( config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] + " /cfgs.db " , " r+ " ) as search :
2018-09-05 22:03:16 +02:00
lines = search . readlines ( )
search . seek ( 0 )
for line in lines :
if line . rstrip ( ) != file_path :
search . write ( line . rstrip ( ) + " \n " )
search . truncate ( )
except PermissionError as e :
2018-09-05 22:25:54 +02:00
print ( bcolors . FAIL + " Unable to write db file! Error: " + e . strerror + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
sys . exit ( 1 )
except Exception as e :
2018-09-05 22:25:54 +02:00
print ( bcolors . FAIL + " Unable to write db file! Error: " + str ( e ) + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
sys . exit ( 1 )
2018-09-06 10:09:40 +02:00
# -----------------------------------------
# git helper functions
# -----------------------------------------
def git_setup_ssh_environment ( ) :
2018-09-05 22:03:16 +02:00
config = get_config ( )
os . environ [ ' GIT_SSH_COMMAND ' ] = " ssh -i " + config [ ' GIT ' ] [ ' SSHKEY ' ]
2018-09-06 10:09:40 +02:00
2018-09-05 22:03:16 +02:00
def git_is_repo ( path ) :
return subprocess . call ( [ " git " , " branch " ] , stderr = subprocess . DEVNULL , stdout = subprocess . DEVNULL , cwd = path ) == 0
2018-09-06 10:09:40 +02:00
2018-09-05 22:03:16 +02:00
def git_set_author ( path , user , email ) :
status = subprocess . call ( [ " git " , " config " , " user.name " , user ] , stderr = subprocess . STDOUT , stdout = subprocess . DEVNULL , cwd = path ) == 0
status | = subprocess . call ( [ " git " , " config " , " user.email " , email ] , stderr = subprocess . STDOUT , stdout = subprocess . DEVNULL , cwd = path ) == 0
return status
2018-09-06 10:09:40 +02:00
2018-09-05 22:03:16 +02:00
def git_set_upstream ( path , remote_url ) :
# make sure that remote origin does not exist
status = subprocess . call ( [ " git " , " remote " , " rm " , " origin " ] , stderr = subprocess . DEVNULL , stdout = subprocess . DEVNULL , cwd = path ) == 0
# now add new origin
status | = subprocess . call ( [ " git " , " remote " , " add " , " origin " , remote_url ] , cwd = path ) == 0
return status
2018-09-06 10:09:40 +02:00
2018-09-05 22:03:16 +02:00
def git_init_repo ( path ) :
status = subprocess . call ( [ " git " , " init " ] , cwd = path ) == 0
return status
2018-09-06 10:09:40 +02:00
2018-09-05 22:03:16 +02:00
def git_push ( path , message = " configuration update " ) :
status = subprocess . call ( [ " git " , " add " , " -A " ] , cwd = path ) == 0
status | = subprocess . call ( [ " git " , " commit " , " -a " , " -m " , message ] , cwd = path ) == 0
status | = subprocess . call ( [ " git " , " push " , " -u " , " origin " , " master " ] , cwd = path ) == 0
return status
2018-09-06 10:09:40 +02:00
2018-09-05 22:03:16 +02:00
def git_pull ( path ) :
status = subprocess . call ( [ " git " , " pull " , " origin " , " master " ] , cwd = path ) == 0
return status
2018-09-06 10:09:40 +02:00
2018-09-05 22:03:16 +02:00
def git_check_status ( status ) :
config = get_config ( )
if not status :
2018-09-05 22:25:54 +02:00
print ( bcolors . FAIL + bcolors . BOLD + " FATAL: Git operation failed! Repository has to be recovered manually! " + bcolors . ENDC )
print ( bcolors . FAIL + " FATAL: Repository path: " + config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
sys . exit ( 1 )
2018-09-06 10:09:40 +02:00
# -----------------------------------------
# main business logic
# -----------------------------------------
2018-09-05 22:03:16 +02:00
def init_local_repo ( ) :
config = get_config ( )
print ( " Setting up local repository. " )
if not os . path . exists ( config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] ) :
2018-09-05 22:25:54 +02:00
print ( bcolors . OKBLUE + " Creating new directory: " + config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
try :
os . makedirs ( config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] )
except OSError as e :
2018-09-05 22:25:54 +02:00
print ( bcolors . FAIL + " Failed to create local repository directory: " + e . strerror + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
sys . exit ( 1 )
# init git repo
if not git_is_repo ( config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] ) :
git_init_repo ( config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] )
git_check_status ( git_set_author ( config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] , config [ ' GIT ' ] [ ' USER ' ] , config [ ' GIT ' ] [ ' EMAIL ' ] ) )
git_check_status ( git_set_upstream ( config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] , config [ ' GIT ' ] [ ' REMOTE_REPOSITORY ' ] ) )
2018-09-06 10:09:40 +02:00
git_setup_ssh_environment ( )
2018-09-05 22:03:16 +02:00
git_check_status ( git_pull ( config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] ) )
create_initialization_file ( config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] )
2018-09-06 10:09:40 +02:00
git_check_status ( git_push ( config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] , " cfgs initialized " ) )
2018-09-05 22:03:16 +02:00
git_check_status ( git_pull ( config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] ) )
2018-09-06 10:09:40 +02:00
print ( bcolors . OKGREEN + " cfgs initialization successfull! " + bcolors . ENDC )
print ( bcolors . OKBLUE + " You can now use cfgs to keep your config files up to date. " + bcolors . ENDC )
2018-09-05 22:25:54 +02:00
2018-09-05 22:03:16 +02:00
def add_file ( filepath ) :
config = get_config ( )
abs_path = os . path . abspath ( filepath )
if not os . path . exists ( abs_path ) :
2018-09-05 22:25:54 +02:00
print ( bcolors . WARNING + " Invalid file, skipping! " + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
sys . exit ( 1 )
else :
2018-09-06 10:09:40 +02:00
print ( bcolors . OKBLUE + " Adding ' " + abs_path + " ' to cfgs... " + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
local_path = config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] + abs_path
if os . path . exists ( local_path ) :
2018-09-06 10:09:40 +02:00
print ( bcolors . WARNING + " File is already registered to cfgs, skipping! " + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
sys . exit ( 1 )
else :
target_directory = os . path . abspath ( os . path . join ( local_path , os . pardir ) )
if not os . path . exists ( target_directory ) :
os . makedirs ( target_directory )
file_stat = os . stat ( abs_path )
file_permissions = file_stat [ stat . ST_MODE ]
file_owner = file_stat [ stat . ST_UID ]
file_group = file_stat [ stat . ST_GID ]
file_owner_name = pwd . getpwuid ( file_owner ) . pw_name
file_group_name = grp . getgrgid ( file_group ) . gr_name
try :
2018-09-06 10:09:40 +02:00
with open ( local_path + " .cfgs " , mode = ' w ' ) as file :
file . write ( ' %s ; %s ; %s ; %s ; %s ; %s ' % ( file_permissions , file_owner , file_group , file_owner_name , file_group_name , socket . gethostname ( ) ) )
2018-09-05 22:03:16 +02:00
except PermissionError as e :
2018-09-05 22:25:54 +02:00
print ( bcolors . FAIL + " Unable to write stat file! Error: " + e . strerror + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
sys . exit ( 1 )
except Exception as e :
2018-09-05 22:25:54 +02:00
print ( bcolors . FAIL + " Unable to write stat file! Error: " + str ( e ) + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
sys . exit ( 1 )
shutil . copy2 ( abs_path , local_path )
os . chown ( local_path , file_owner , file_group )
os . remove ( abs_path )
os . symlink ( local_path , abs_path )
os . chown ( abs_path , file_owner , file_group , follow_symlinks = False )
add_file_to_db ( abs_path )
2018-09-06 10:09:40 +02:00
print ( bcolors . OKGREEN + " File added to cfgs! " + bcolors . ENDC )
print ( bcolors . OKBLUE + " Use cfgs store to push the file to the remote repository! " + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
def remove_file ( filepath ) :
config = get_config ( )
abs_path = os . path . abspath ( filepath )
local_path = config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] + abs_path
if not os . path . exists ( local_path ) :
2018-09-06 10:09:40 +02:00
print ( bcolors . WARNING + " File not registered to cfgs, skipping! " + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
sys . exit ( 1 )
else :
2018-09-06 10:09:40 +02:00
print ( bcolors . OKBLUE + " Removing ' " + abs_path + " ' from cfgs... " + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
target_directory = os . path . abspath ( os . path . join ( abs_path , os . pardir ) )
if not os . path . exists ( target_directory ) :
os . makedirs ( target_directory )
if os . path . exists ( abs_path ) :
2018-09-05 22:25:54 +02:00
confirmed = input ( bcolors . OKBLUE + " Do you really want to override the local file ' " + abs_path + " ' ? [y/N]: " + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
if not confirmed or confirmed != " y " :
2018-09-05 22:25:54 +02:00
print ( bcolors . WARNING + " Skipping removal process! " + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
sys . exit ( 0 )
else :
os . remove ( abs_path )
shutil . copy2 ( local_path , abs_path )
file_stat = os . stat ( local_path )
file_permissions = file_stat [ stat . ST_MODE ]
file_owner = file_stat [ stat . ST_UID ]
file_group = file_stat [ stat . ST_GID ]
os . chown ( abs_path , file_owner , file_group )
os . chmod ( abs_path , file_permissions )
os . remove ( local_path )
2018-09-06 10:09:40 +02:00
os . remove ( local_path + " .cfgs " )
2018-09-05 22:03:16 +02:00
delete_file_from_db ( abs_path )
2018-09-06 10:09:40 +02:00
print ( bcolors . OKGREEN + " File removed from cfgs! " + bcolors . ENDC )
print ( bcolors . OKBLUE + " Use cfgs store to push the file to the remote repository! " + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
def store ( ) :
config = get_config ( )
2018-09-06 10:09:40 +02:00
git_setup_ssh_environment ( )
2018-09-05 22:03:16 +02:00
git_push ( config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] )
2018-09-06 10:09:40 +02:00
print ( bcolors . OKGREEN + " cfgs store successfull! " + bcolors . ENDC )
2018-09-05 22:25:54 +02:00
print ( bcolors . OKBLUE + " You can now restore the configuration on your other systems. " + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
def update_local_metadata ( ) :
config = get_config ( )
2018-09-06 10:09:40 +02:00
with open ( config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] + " /cfgs.db " , " r " ) as search :
2018-09-05 22:03:16 +02:00
for line in search :
abs_path = line . rstrip ( ) # remove '\n' at end of line
local_path = config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] + abs_path
if not os . path . exists ( local_path ) :
2018-09-05 22:25:54 +02:00
print ( bcolors . WARNING + " Invalid file in database, skipping! ( " + abs_path + " ) " + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
else :
2018-09-06 10:09:40 +02:00
with open ( local_path + " .cfgs " , " r " ) as metadata_file :
2018-09-05 22:03:16 +02:00
metadata_string = metadata_file . readline ( ) . rstrip ( )
metadata = metadata_string . split ( " ; " )
file_permissions = int ( metadata [ 0 ] )
file_owner = int ( metadata [ 1 ] )
file_group = int ( metadata [ 2 ] )
os . chmod ( local_path , file_permissions )
os . chown ( local_path , file_owner , file_group )
2018-09-06 10:09:40 +02:00
2018-09-05 22:03:16 +02:00
def restore ( ) :
config = get_config ( )
2018-09-06 10:09:40 +02:00
git_setup_ssh_environment ( )
2018-09-05 22:03:16 +02:00
git_pull ( config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] )
update_local_metadata ( )
2018-09-05 22:25:54 +02:00
confirmed = input ( bcolors . WARNING + " Do you really want to restore files from the repository? Local files will be overwritten! [y/N]: " + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
if not confirmed or confirmed != " y " :
2018-09-05 22:25:54 +02:00
print ( bcolors . WARNING + " Skipping restore process! " + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
sys . exit ( 0 )
else :
2018-09-06 10:09:40 +02:00
with open ( config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] + " /cfgs.db " , " r " ) as search :
2018-09-05 22:03:16 +02:00
for line in search :
abs_path = line . rstrip ( ) # remove '\n' at end of line
local_path = config [ ' DEFAULT ' ] [ ' LOCAL_STORAGE_DIRECTORY ' ] + abs_path
if not os . path . exists ( local_path ) :
2018-09-05 22:25:54 +02:00
print ( bcolors . WARNING + " Invalid file, skipping! " + bcolors . ENDC )
2018-09-05 22:03:16 +02:00
else :
target_directory = os . path . abspath ( os . path . join ( abs_path , os . pardir ) )
if not os . path . exists ( target_directory ) :
os . makedirs ( target_directory )
if os . path . exists ( abs_path ) :
os . remove ( abs_path )
os . symlink ( local_path , abs_path )
file_stat = os . stat ( local_path )
file_owner = file_stat [ stat . ST_UID ]
file_group = file_stat [ stat . ST_GID ]
os . chown ( abs_path , file_owner , file_group , follow_symlinks = False )
2018-09-06 10:09:40 +02:00
print ( bcolors . OKGREEN + " cfgs restore successfull! " + bcolors . ENDC )
2018-09-05 22:25:54 +02:00
print ( bcolors . OKBLUE + " Configuration files have been updated. A reboot might be required. " + bcolors . ENDC )
2018-09-06 10:09:40 +02:00
# -----------------------------------------
# main entry point
# -----------------------------------------
2018-09-05 22:03:16 +02:00
def main ( argv ) :
if not check_dependencies ( ) :
sys . exit ( 1 )
if len ( argv ) == 2 :
arg = argv [ 1 ]
if arg == " init " :
init_dialog ( )
init_local_repo ( )
sys . exit ( 0 )
elif arg == " store " :
store ( )
elif arg == " restore " :
restore ( )
else :
print_splash ( )
elif len ( argv ) == 3 :
arg = argv [ 1 ]
config_file = argv [ 2 ]
if arg == " add " :
add_file ( config_file )
elif arg == " remove " :
remove_file ( config_file )
else :
print_splash ( )
else :
print_splash ( )
2018-09-06 10:09:40 +02:00
2018-09-05 22:03:16 +02:00
if __name__ == " __main__ " :
main ( sys . argv )