BUGFIX: ensure_dir function

This commit is contained in:
koumakpet 2020-03-29 17:59:24 +02:00
parent 7e9cc80a0e
commit 08c0933b27
2 changed files with 87 additions and 48 deletions

View file

@ -2,17 +2,21 @@ from lib import Input, Print, Install, Path
import os
from datetime import datetime
def make_backup(files_dir):
if Input.yes_no('Do you wish to create a backup of current state of your dotfiles?'):
if Input.yes_no(
'Do you wish to create a backup of current state of your dotfiles?'
):
Print.action('Creating dotfiles backup')
backup_dir = str(datetime.now()).replace(' ', '--')
current_backup_dir = os.path.join(Path.get_parent(__file__), 'Backups', backup_dir)
current_backup_dir = os.path.join(Path.get_parent(__file__), 'Backups',
backup_dir)
Path.ensure_dirs(current_backup_dir)
for file in Path.get_all_files(files_dir):
from_pos = os.path.join(
Path.get_home(), file.replace(f'{files_dir}/', ''))
to_pos = os.path.join(
current_backup_dir, file.replace(f'{files_dir}/', ''))
from_pos = os.path.join(Path.get_home(),
file.replace(f'{files_dir}/', ''))
to_pos = os.path.join(current_backup_dir,
file.replace(f'{files_dir}/', ''))
if Path.check_file_exists(from_pos):
Path.copy(from_pos, to_pos)
@ -25,14 +29,17 @@ def make_backup(files_dir):
def check_installation():
if Install.check_not_installed('zsh'):
if not Install.package('zsh', 'default + (This is required shell for dotfiles)'):
if not Install.package(
'zsh', 'default + (This is required shell for dotfiles)'):
Print.err('Dotfiles installation cancelled - zsh not installed')
return False
global oh_my_zsh_path, zsh_highlight_path
zsh_highlight_path = None
if Path.check_file_exists('/usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh'):
if Path.check_file_exists(
'/usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh'
):
zsh_highlight_path = '/usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh'
oh_my_zsh_path = None
@ -55,7 +62,6 @@ def check_installation():
return False
def personalized_changes(file):
if '.zshrc' in file:
global oh_my_zsh_path, zsh_highlight_path
@ -65,17 +71,23 @@ def personalized_changes(file):
filedata_old = filedata
# Change path to oh-my-zsh
filedata = filedata.replace('"$HOME/.config/oh-my-zsh"', f'"{oh_my_zsh_path}"')
filedata = filedata.replace('"$HOME/.config/oh-my-zsh"',
f'"{oh_my_zsh_path}"')
# Change path to zsh-color-highlight
if zsh_highlight_path is not None:
original_path='/usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh'
filedata = filedata.replace(f'source {original_path}', f'source {zsh_highlight_path}')
original_path = '/usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh'
filedata = filedata.replace(f'source {original_path}',
f'source {zsh_highlight_path}')
else:
if Install.package('zsh-syntax-highlighting', 'default'):
zsh_highlight_path = Input.question('Please specify path to your zsh-syntax-highlighting plugin (blank=do not include)')
zsh_highlight_path = Input.question(
'Please specify path to your zsh-syntax-highlighting plugin (blank=do not include)'
)
if zsh_highlight_path != '':
filedata = filedata.replace(f'source {original_path}', f'source {zsh_highlight_path}')
filedata = filedata.replace(
f'source {original_path}',
f'source {zsh_highlight_path}')
else:
filedata = filedata.replace(f'source {original_path}', f'')
else:
@ -87,7 +99,9 @@ def personalized_changes(file):
f.write(filedata)
if 'vimrc' in file:
if not Input.yes_no('Do you wish to use .vimrc (If you choose yes, please install Vundle or you will get errors)'):
if not Input.yes_no(
'Do you wish to use .vimrc (If you choose yes, please install Vundle or you will get errors)'
):
# TODO: Install vundle
return False
return True
@ -95,25 +109,25 @@ def personalized_changes(file):
def init(symlink):
# Get path to files/ floder (contains all dotfiles)
files_dir = os.path.join(
Path.get_parent(__file__), 'files')
files_dir = os.path.join(Path.get_parent(__file__), 'files')
# Create optional backup
make_backup(files_dir)
files_list, dirs_list = Path.get_all_dirs_and_files(files_dir)
# Create all dirs
for directory in dirs_list:
position = os.path.join(
Path.get_home(), directory.replace(f'{files_dir}/', ''))
Path.ensure_dirs(position)
if Path.check_dir_exists(directory):
position = os.path.join(Path.get_home(),
directory.replace(f'{files_dir}/', ''))
Path.ensure_dirs(position)
# Go through all files
for file in files_list:
# Make personalized changes to files
personalized_changes(file)
# Set symlink position ($HOME/filepath)
position = os.path.join(
Path.get_home(), file.replace(f'{files_dir}/', ''))
position = os.path.join(Path.get_home(),
file.replace(f'{files_dir}/', ''))
if symlink:
# Make symlink
@ -129,7 +143,9 @@ def main():
return False
choice = Input.multiple('Do you wish to create dotfiles', [
'symlinks (dotfiles/ dir will be required)', 'files (dotfiles/ dir can be removed afterwards)'])
'symlinks (dotfiles/ dir will be required)',
'files (dotfiles/ dir can be removed afterwards)'
])
# Create symlinks
if choice == 'symlinks (dotfiles/ dir will be required)':
init(symlink=True)
@ -139,9 +155,11 @@ def main():
Print.warning(
'Do not delete this floder, all dotfile symlinks are linked to it')
Print.warning(
'Deletion would cause errors on all your dotfiles managed by this program')
'Deletion would cause errors on all your dotfiles managed by this program'
)
Print.comment(
'If you wish to remove this floder, please select files instead of symlinks for dotfile creation')
'If you wish to remove this floder, please select files instead of symlinks for dotfile creation'
)
# Create files
elif choice == 'files (dotfiles/ dir can be removed afterwards)':