InstallChecks update

- Better code readability
- Added vim_vundle check + Vundle Installation
This commit is contained in:
koumakpet 2020-04-06 00:38:29 +02:00
parent 3fd8308768
commit 6c41f7b1c8
3 changed files with 152 additions and 76 deletions

View file

@ -2,83 +2,114 @@ from util import Path, Print, Install, Input
from datetime import datetime from datetime import datetime
class InstallationChecks: class InstallChecks:
@staticmethod @staticmethod
def installation_error(package): def installation_error(package):
Print.err(f'Dotfiles installation cancelled - {package} not installed') Print.err(f'Dotfiles installation cancelled - {package} not installed')
raise Install.InstallationError(f'{package} not installed') raise Install.InstallationError(f'{package} not installed')
@staticmethod @staticmethod
def get_installation_path(standard_paths): def get_installation_path(standard_paths, file_end=False):
dir_exists, path = Path.check_dir_exists(standard_paths) if file_end:
if dir_exists: path_exists, path = Path.check_file_exists(standard_paths)
path = path.replace('~', '$HOME') print(f'Installation path -> {path_exists}, {path}')
else:
path_exists, path = Path.check_dir_exists(standard_paths)
return dir_exists, path return path
@staticmethod @staticmethod
def install_package(package, standard_paths, status, use_aur=False): def _make_install_text(status):
if not Install.package( return f'default + (This is {status} for dotfiles to work)'
package,
f'default + (This is {status} for dotfiles to work)',
aur=use_aur):
return False, None
else:
installation_path = InstallationChecks.get_installation_path(
standard_paths)
if not installation_path: # Package was not found in standard paths after installation
Print.err(
f'Installation location of {package} has changed, please contact the developer'
)
return False, None
else:
return True, installation_path
@staticmethod @staticmethod
def check_zsh(): def zsh():
if Install.check_not_installed('zsh'): if Install.check_not_installed('zsh'):
if not Install.package( if not Install.package(
'zsh', 'zsh',
'default + (This is REQUIRED shell for dotfiles to work)'): 'default + (This is REQUIRED shell for dotfiles to work)'):
return False InstallChecks.installation_error('zsh')
return True
@staticmethod @staticmethod
def check_oh_my_zsh(): def oh_my_zsh(dotfiles):
standard_paths = [ standard_paths = [
'~/.oh-my-zsh', '~/oh-my-zsh', '~/ohmyzsh', '~/.config/oh-my-zsh', '~/.oh-my-zsh', '~/oh-my-zsh', '~/ohmyzsh', '~/.config/oh-my-zsh',
'/usr/share/oh-my-zsh' '/usr/share/oh-my-zsh'
] ]
oh_my_zsh_path = InstallationChecks.get_installation_path(
oh_my_zsh_path = InstallChecks.get_installation_path(standard_paths)
# Check if package was found in standard paths
if oh_my_zsh_path:
dotfiles.oh_my_zsh = True
dotfiles.oh_my_zsh_path = oh_my_zsh_path
# Package wasn't found, try to install it
else:
install_text = InstallChecks._make_install_text(
'REQUIRED zsh package')
dotfiles.oh_my_zsh = Install.package('oh-my-zsh',
install_text,
aur=True)
dotfiles.oh_my_zsh_path = InstallChecks.get_installation_path(
standard_paths) standard_paths)
if oh_my_zsh_path[0]: # Check if package was found in standard paths if not dotfiles.oh_my_zsh:
return True, oh_my_zsh_path[1] InstallChecks.installation_error('oh-my-zsh')
else: # Package wasn't found, try to install it
return InstallationChecks.install_package('oh-my-zsh',
standard_paths,
'REQUIRED zsh package',
use_aur=True)
@staticmethod @staticmethod
def check_zsh_highlight(): def zsh_highlight(dotfiles):
standard_paths = [ standard_paths = [
'/usr/share/zsh/plugins/zsh-syntax-highlighting', '/usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh',
'/usr/share/zsh/zsh-syntax-highlighting', '/usr/share/zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh',
'/usr/share/zsh-syntax-highlighting' '/usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh'
] ]
zsh_highlight_path = InstallationChecks.get_installation_path( zsh_highlight_path = InstallChecks.get_installation_path(
standard_paths) standard_paths, file_end=True)
if zsh_highlight_path[ # Check if package was found in standard paths
0]: # Check if package was found in standard paths if zsh_highlight_path:
return True, zsh_highlight_path[1] dotfiles.zsh_highlight = True
else: # Package wasn't found, try to install it dotfiles.zsh_highlight_path = zsh_highlight_path
return InstallationChecks.install_package( # Package wasn't found, try to install it
'zsh-syntax-highlighting', standard_paths, else:
install_text = InstallChecks._make_install_text(
'RECOMMENDED zsh extension') 'RECOMMENDED zsh extension')
dotfiles.zsh_highlight = Install.package('zsh-syntax-highlighting',
install_text,
aur=False)
dotfiles.zsh_highlight_path = InstallChecks.get_installation_path(
standard_paths, file_end=True)
if not dotfiles.zsh_highlight:
Print.comment('Proceeding without zsh-syntax-highlighting')
@staticmethod
def vim_vundle(dotfiles, installation_path='~/.vim/bundle/Vundle.vim'):
standard_paths = [
'~/.vim/bundle/Vundle.vim', '~/.local/share/vim/bundle/Vundle.vim'
]
vim_vundle_path = InstallChecks.get_installation_path(standard_paths)
# Check if package was found in standard paths
if vim_vundle_path:
dotfiles.vim_vundle = True
dotfiles.vim_vundle_path = vim_vundle_path
# Package wasn't found, try to install it
else:
install_text = InstallChecks._make_install_text(
'RECOMMENDED Vim Package Manager')
dotfiles.vim_vundle = Install.git_install(
'https://github.com/VundleVim/Vundle.vim.git',
installation_path, install_text)
if dotfiles.vim_vundle:
dotfiles.vim_vundle_path = installation_path
else:
dotfiles.vim_vundle_path = None
if not dotfiles.vim_vundle:
Print.comment('Proceeding without zsh-syntax-highlighting')
class PersonalizedChanges: class PersonalizedChanges:
@staticmethod @staticmethod
@ -90,11 +121,12 @@ class PersonalizedChanges:
filedata = filedata.replace('"$HOME/.config/oh-my-zsh"', filedata = filedata.replace('"$HOME/.config/oh-my-zsh"',
f'"{dotfiles.oh_my_zsh_path}"') f'"{dotfiles.oh_my_zsh_path}"')
# Set path to zsh-color-highlighting # Set zsh-color-highlighting
if dotfiles.zsh_syntax_highlighting_installed: InstallChecks.zsh_highlight(dotfiles)
if dotfiles.zsh_highlight:
filedata = filedata.replace( filedata = filedata.replace(
'/usr/share/zsh/plugins/zsh-syntax-highlighting', '/usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh',
dotfiles.zsh_syntax_highlighting_path) dotfiles.zsh_highlight_path)
else: else:
filedata = filedata.replace( filedata = filedata.replace(
'# Load zsh-syntax-highlighting (should be last)', '') '# Load zsh-syntax-highlighting (should be last)', '')
@ -110,15 +142,33 @@ class PersonalizedChanges:
return True return True
@staticmethod @staticmethod
def vimrc(file): def vimrc(dotfiles, file):
if Input.yes_no( if Input.yes_no(
'Do you wish to use .vimrc (If you choose yes, please install Vundle or adjust .vimrc file (without Vundle multiple errors will occur)' 'Do you wish to follow XDG Standard for vim (.vim floder in .local/share/vim instead of home floder)'
):
# Ensure XDG Directories
Path.ensure_dirs('~/.local/share/vim/bundle')
Path.ensure_dirs('~/.local/share/vim/swap')
Path.ensure_dirs('~/.local/share/vim/undo')
Path.ensure_dirs('~/.local/share/vim/backup')
InstallChecks.vim_vundle(dotfiles,
'~/.local/share/vim/bundle/Vundle.vim')
else:
# TODO: Change vimrc not to follow XDG
InstallChecks.vim_vundle(dotfiles)
Print.warning(
'vim will produce multiple errors, please adjust paths not to use XDG Standard'
)
if not dotfiles.vim_vundle:
if Input.yes_no(
'Do you wish to proceed without Vundle (.vimrc will produce multiple errors without Vundle )'
): ):
# TODO: Vundle installation
# TODO: XDG Standard following + dirs creation
return True return True
else: else:
return False return False
return True
@staticmethod @staticmethod
def gitconfig(file): def gitconfig(file):
@ -188,16 +238,8 @@ class Dotfiles:
return True return True
def initial_checks(self): def initial_checks(self):
if not InstallationChecks.check_zsh(): InstallChecks.zsh()
InstallationChecks.installation_error('zsh') InstallChecks.oh_my_zsh(self)
self.oh_my_zsh_installed, self.oh_my_zsh_path = InstallationChecks.check_oh_my_zsh(
)
if not self.oh_my_zsh_installed:
InstallationChecks.installation_error('oh-my-zsh')
self.zsh_syntax_highlighting_installed, self.zsh_syntax_highlighting_path = InstallationChecks.check_zsh_highlight(
)
def make_backup(self): def make_backup(self):
Print.action('Creating current dotfiles backup') Print.action('Creating current dotfiles backup')
@ -217,7 +259,7 @@ class Dotfiles:
to_pos = Path.join(backup_dir, file_blank) to_pos = Path.join(backup_dir, file_blank)
# If file is in home directory, back it up # If file is in home directory, back it up
if Path.check_file_exists(from_pos): if Path.check_file_exists(from_pos)[0]:
Path.copy(from_pos, to_pos) Path.copy(from_pos, to_pos)
Print.action('Backup complete') Print.action('Backup complete')
@ -226,7 +268,7 @@ class Dotfiles:
if '.zshrc' in file: if '.zshrc' in file:
return PersonalizedChanges.zshrc(self, file) return PersonalizedChanges.zshrc(self, file)
elif 'vimrc' in file: elif 'vimrc' in file:
return PersonalizedChanges.vimrc(file) return PersonalizedChanges.vimrc(self, file)
elif '.gitconfig' in file: elif '.gitconfig' in file:
return PersonalizedChanges.gitconfig(file) return PersonalizedChanges.gitconfig(file)
else: else:

View file

@ -9,15 +9,21 @@ class InstallationError(Exception):
pass pass
def _generate_install_text(install_text, package_name, yay=False, git=False): def _generate_install_text(install_text,
package_name,
yay=False,
git=False,
git_aur=False):
if install_text == 'default': if install_text == 'default':
install_text = f'Do you wish to install {package_name}?' install_text = f'Do you wish to install {package_name}?'
if install_text[:10] == 'default + ': if install_text[:10] == 'default + ':
install_text = f'Do you wish to install {package_name}? {install_text[10:]}' install_text = f'Do you wish to install {package_name}? {install_text[10:]}'
if yay: if yay:
install_text += '[AUR (YAY) Package]' install_text += '[AUR (YAY) Package]'
if git: if git_aur:
install_text += '[AUR (GIT+MAKEPKG) Package]' install_text += '[AUR (GIT+MAKEPKG) Package]'
if git:
install_text += '[GIT Package]'
return install_text return install_text
@ -67,6 +73,34 @@ def check_not_installed(package_name):
return True return True
def git_install(url,
destination,
install_text='default',
package_name='default'):
'''Install package directly using GIT
Arguments:
url {string} -- url for installation
destination {string} -- installation path
Keyword Arguments:
install_text {str} -- text presented to the user before installing (default: {'default' -> 'Do you wish to install [repository]'})
package_name {str} -- name of package displayed to user (default: {'default' -> Last part of url})
Returns:
bool -- installed
'''
if package_name == 'default':
package_name = url.split(' ')[-1].replace('.git', '')
install_text = _generate_install_text(install_text, package_name, git=True)
if Input.yes_no(install_text):
Command.execute(f'git clone {url} {destination}')
return True
else:
Print.cancel('Skipping...')
return False
def git_aur(repository, install_text='default', force=False): def git_aur(repository, install_text='default', force=False):
'''Install package directly from AUR using only git and makepkg '''Install package directly from AUR using only git and makepkg
@ -97,7 +131,7 @@ def git_aur(repository, install_text='default', force=False):
if check_not_installed(repository) or force: if check_not_installed(repository) or force:
install_text = _generate_install_text(install_text, install_text = _generate_install_text(install_text,
repository, repository,
git=True) git_aur=True)
if Input.yes_no(install_text): if Input.yes_no(install_text):
url = f'https://aur.archlinux.org/{repository}.git' url = f'https://aur.archlinux.org/{repository}.git'
Command.execute(f'git clone {url}') Command.execute(f'git clone {url}')

View file

@ -51,10 +51,10 @@ def check_file_exists(paths):
for file_path in paths: for file_path in paths:
file_path = os.path.expanduser(file_path) file_path = os.path.expanduser(file_path)
if os.path.isfile(file_path): if os.path.isfile(file_path):
return True return True, file_path
break break
else: else:
return False return False, None
def get_parent(file_path): def get_parent(file_path):