I am writing a script to automate the install and configuration of vim, zsh, ohmyzsh and tmux, on a Linux/Mac. I'd like to perform all operations in Python, rather than using subprocess
to streamline some of the operations (e.g. git clone).
I would also like to use this script as an example to myself of good Python coding practice for file system operations. Code clarity, reuse and pythonic style are my primary objectives. Before I write the remainder of the script I'd like some input on the methods I've chosen for low level operations, but all areas of the script are open for scrutiny.
from urllib.request import urlretrieve
from urllib.error import URLError
from zipfile import ZipFile
import sys
import os
import errno
import shutil
vim = ".vimtest"
vim_directories = [vim, (vim + "/bundle"), (vim + "/autoload"), (vim + "/colors")]
vim_files = [".vimrctest", vim + "/vimrc"]
flags = os.O_CREAT | os.O_EXCL | os.O_WRONLY
def vim_install():
# Create directories and files
for sub in vim_directories:
try:
os.mkdir(os.path.expanduser('~/' + sub))
print("mkkdir " + os.path.expanduser('~/' + sub))
except OSError as e:
if e.errno == errno.EEXIST:
print("Directory " + vim + sub + " already exists.")
sys.exit(1)
else:
raise
try:
file_handle = os.open(os.path.expanduser('~/' + vim_files[1]), flags)
except OSError as e:
if e.errno == errno.EEXIST:
print("File " + vim_files[1] + " already exists")
sys.exit(1)
else:
raise
with os.fdopen(file_handle, 'w') as file_obj:
file_obj.write("")
print("Created file " + os.path.expanduser('~/') + vim_files[1])
try:
os.symlink(os.path.expanduser('~/') + vim_files[1],
os.path.expanduser('~/') + vim_files[0])
print("ln -s " + os.path.expanduser('~/') + vim_files[0] + " -> " +
os.path.expanduser('~/') + vim_files[1])
except OSError:
print("Failed to make symbolic link.")
print("ln -s " + os.path.expanduser('~/') +
vim_files[0] + " -> " + os.path.expanduser('~/') +
vim_files[1])
sys.exit(1)
# Download Pathogen, NerdTree, unpack
try:
urlretrieve("https://tpo.pe/pathogen.vim",
os.path.expanduser('~/' + vim + "/autoload/pathogen.vim"))
print("Downloaded Pathogen")
urlretrieve("https://github.com/scrooloose/nerdtree/archive/master.zip",
os.path.expanduser('~/' + vim + "/bundle/master.zip"))
print("Downloaded NerdTree")
except URLError as e:
if hasattr(e, 'reason'):
print('Scrip failed to reach a server.')
print('Reason: ', e.reason)
elif hasattr(e, 'code'):
print('The server couldn\'t fulfill the request.')
print('Error code: ', e.code)
else:
raise
try:
zip_ref = ZipFile(os.path.expanduser('~/') + vim +
"/bundle/master.zip", 'r')
zip_ref.extractall(os.path.expanduser('~/') + vim + "/bundle/")
zip_ref.close()
print("Unziped NerdTree")
except OSError as e:
print("Could not unzip: {0}".format(e))
try:
os.rename(os.path.expanduser('~/' + vim + "/bundle/nerdtree-master"),
os.path.expanduser('~/' + vim + "/bundle/nerdtree"))
print("Renamed " + os.path.expanduser('~/') + vim +
"/bundle/nerdtree-master \n"
+ os.path.expanduser('~/') + vim + "/bundle/nerdtree")
except OSError as e:
print("OS error {0}".format(e))