Files
dotfiles/install_files.py

108 lines
3.3 KiB
Python
Raw Normal View History

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