Files
dotfiles/install_files.py

106 lines
3.2 KiB
Python
Executable File

#! /usr/bin/env python3
import os
import shutil
from pathlib import Path
from docopt import docopt
from subprocess import check_call
HELPTEXT = """{progname}.
Installs symlinks, plugins, plugin managers, ...
Usage:
{progname} [-f] [-p <dir>] install all
{progname} [-f] [-p <dir>] install vim
{progname} [-f] [-p <dir>] install tmux
{progname} (-h | --help)
Options:
-h --help Show this screen.
-f --force Force install if files look already installed
-p --path=<dir> Path to the dotfiles repo [default: .]
""".format(progname="install_files")
def install_vim(pdotfiles, force=False):
phome = Path(os.path.expanduser("~")).resolve()
phome_vimrc = phome / Path(".vimrc")
prelvimrc = Path("vim/vimrc")
try:
pdotfiles_vimrc = pdotfiles.resolve().relative_to(phome.resolve()) / prelvimrc
except ValueError:
pdotfiles_vimrc = pdotfiles / prelvimrc
if force and phome_vimrc.is_symlink():
phome_vimrc.unlink() # only kill symlinks
if phome_vimrc.exists():
print('.vimrc already there!')
else:
print("Symlinking {} to {}".format(phome_vimrc, pdotfiles_vimrc))
phome_vimrc.symlink_to(pdotfiles_vimrc)
# vundle
pvundle = phome / Path(".vim/bundle/Vundle.vim")
if not force and pvundle.exists():
print("Vundle already installed?")
else:
if force and pvundle.exists():
shutil.rmtree(pvundle.as_posix())
print("Install Vundle")
check_call(
["git", "clone", "https://github.com/VundleVim/Vundle.vim.git",
pvundle.as_posix()])
print("Install vim plugins")
check_call("vim -X -- +PluginInstall +qall")
def install_tmux(pdotfiles, force=False):
phome = Path(os.path.expanduser("~")).resolve()
phome_tmuxrc = phome / Path(".tmux.conf")
preltmuxrc = Path("tmux/tmux.conf")
try:
pdotfiles_tmuxrc = pdotfiles.resolve().relative_to(phome.resolve()) / preltmuxrc
except ValueError:
pdotfiles_tmuxrc = pdotfiles / preltmuxrc
if force and phome_tmuxrc.is_symlink():
phome_tmuxrc.unlink() # only kill symlinks
if phome_tmuxrc.exists():
print('.tmux.conf already there!')
else:
print("Symlinking {} to {}".format(phome_tmuxrc, pdotfiles_tmuxrc))
phome_tmuxrc.symlink_to(pdotfiles_tmuxrc)
# tpm
tmux_plugins = phome / Path(".tmux/plugins")
tpm = tmux_plugins / Path("tpm")
if not force and tmux_plugins.exists():
print("plugin dir exits, tpm already installed?")
else:
if force and tmux_plugins.exists():
print("remove tmux plugin dir")
shutil.rmtree(tmux_plugins.as_posix())
print("Install tpm")
check_call(
["git", "clone", "https://github.com/tmux-plugins/tpm",
tpm.as_posix()])
check_call((tpm / Path("bin") / Path("install_plugins")).as_posix())
if __name__ == "__main__":
args = docopt(HELPTEXT, version='0.1')
if args["install"]:
if any((args["all"], args["vim"])):
install_vim(Path(args["--path"]).resolve(), args["--force"])
if any((args["all"], args["tmux"])):
install_tmux(Path(args["--path"]).resolve(), args["--force"])