#!/usr/bin/env python import os import glob import asyncio import subprocess INTSTALL_DIR=os.path.expandvars("$HOME/.vim/pack/vendor") class Package(object): def __init__(self, name, location, type='opt', posthook=None): self.name = name self.location = location self.type = type self.posthook = posthook self.path = self.get_path() def fail(self): print("%s : FAIL" % self.name) def get_path(self): return os.path.expandvars("$HOME/.vim/pack/vendor/%s/%s" % (self.type, self.name)) async def update(self): cmd = "cd %s && git pull" % self.path print("%s : Updating ..." % self.name) if not os.path.exists(self.path): os.mkdir(self.path) cmd = "git clone --depth 1 %s %s" % (self.location, self.path) if not os.path.isdir(self.path): print("ERROR: package directory error: %s" % self.name) self.fail() return process = await asyncio.create_subprocess_shell( cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, ) stdout, stderr = await process.communicate() if process.returncode != 0: print("ERROR: git command error") print(stdout) print(stderr) self.fail() return if self.posthook: self.posthook() print("%s : Done" % (self.name)) return "" packages = [ Package( "nnn.vim", "git@github.com:mcchrish/nnn.vim.git", "opt", ), ] def update_packages(packages): tasks = [ package.update() for package in packages ] loop = asyncio.get_event_loop() commands = asyncio.gather(*tasks) result = loop.run_until_complete(commands) loop.close() # print(result) def clean(packages): paths = [ package.path for package in packages ] to_clean = [ path for path in glob.glob(os.path.expandvars("$HOME/.vim/pack/vendor/*/*")) if path not in paths ] if not to_clean: return print("The following directory will be cleaned: [y/N]") print(to_clean) if input() not in ["y", "Yes", "yes"]: return cmd = ["rm", "-rf"] + to_clean result = subprocess.run(cmd, capture_output=True, shell=False) print(' '.join(cmd)) if result.returncode != 0: print("FAIL on cleaning the following directory:") print(result.stdout) print(result.stderr) else: print("DONE") def main(): update_packages(packages) clean(packages) if __name__ == "__main__": main()