我正在编写一个脚本,用于在Linux/Mac上自动化vim、zsh、ohmyzsh和tmux的安装和配置。我想用Python来执行所有的操作,而不是使用subprocess来简化一些操作(例如,git克隆)。
代码清晰、重用和pythonic风格是我的主要目标。在编写脚本的其余部分之前,我想要一些关于我为低级别操作选择的方法的输入,但是脚本的所有区域都是开放的。
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))发布于 2018-05-26 06:39:10
vim、vim_directories、vim_files和flags似乎是常量,所以您应该用它们的名称来应用命名约定。的第一部分
关于严肃的事情,我现在只谈你的代码的第一部分,稍后我可能会回来(但没有承诺):
try块中,您应该只测试实际容易引发异常的代码,而不是其他代码。如果应用此规则,将以编写短尝试块的方式结束。os.mkdir(os.path.expanduser('~/' + sub)),如果尝试失败,print()也不会执行。如果目录创建成功,else块应该负责打印任务。try块应该简洁干净sys.exit(1)在这里的必要性。老实说,在大多数情况下,它是无用的,在典型的情况下,我看不出它如何有用,而且您已经打印了一条关于执行失败的消息。errno.EEXIST,而不是其他错误代码?我以为你太细心了,所以你决定追踪errno模块的所有符号,但我错了。我认为这样写是公平的:除了OSError作为e: print(‘错误代码:{}'.format(e.errno))打印(’错误信息:{}'.format(e.strerror))errno.EEXIST太性感、太吸引人了,我仍然会选择一种不那么烦人的语法:FileExistsError而不是errno.EEXIST (请注意,后者恰好对应于FileExistsError,但正如您所看到的,它更容易读懂)https://codereview.stackexchange.com/questions/195198
复制相似问题