dotfiles
This commit is contained in:
parent
786ac39033
commit
75325f3baa
1 changed files with 19 additions and 11 deletions
|
@ -15,13 +15,14 @@ import sys
|
||||||
|
|
||||||
basedir = os.path.expanduser("~/.dotfiles")
|
basedir = os.path.expanduser("~/.dotfiles")
|
||||||
|
|
||||||
|
|
||||||
def getpaths(path):
|
def getpaths(path):
|
||||||
absolutpath = os.path.abspath(os.path.expanduser(path))
|
absolutpath = os.path.abspath(os.path.expanduser(path))
|
||||||
dotfilepath = basedir + absolutpath[len(os.path.expanduser("~")):]
|
dotfilepath = basedir + absolutpath[len(os.path.expanduser("~")) :]
|
||||||
|
|
||||||
if absolutpath.startswith(basedir):
|
if absolutpath.startswith(basedir):
|
||||||
dotfilepath = absolutpath
|
dotfilepath = absolutpath
|
||||||
absolutpath = os.path.expanduser("~" + absolutpath[len(basedir):])
|
absolutpath = os.path.expanduser("~" + absolutpath[len(basedir) :])
|
||||||
|
|
||||||
return absolutpath, dotfilepath
|
return absolutpath, dotfilepath
|
||||||
|
|
||||||
|
@ -30,11 +31,14 @@ def link(path, force):
|
||||||
absolutpath, dotfilepath = getpaths(path)
|
absolutpath, dotfilepath = getpaths(path)
|
||||||
|
|
||||||
if not absolutpath.startswith(os.path.expanduser("~")):
|
if not absolutpath.startswith(os.path.expanduser("~")):
|
||||||
print(f"Could not add {path} to repo, only files in your home directory are supported", file=sys.stderr)
|
print(
|
||||||
|
f"Could not add {path} to repo, only files in your home directory are supported",
|
||||||
|
file=sys.stderr,
|
||||||
|
)
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
|
|
||||||
if absolutpath == os.path.expanduser("~"):
|
if absolutpath == os.path.expanduser("~"):
|
||||||
print(f"Cannot add home directory to dotfiles", file=sys.stderr)
|
print("Cannot add home directory to dotfiles", file=sys.stderr)
|
||||||
sys.exit(3)
|
sys.exit(3)
|
||||||
|
|
||||||
if absolutpath.startswith(basedir):
|
if absolutpath.startswith(basedir):
|
||||||
|
@ -55,6 +59,7 @@ def link(path, force):
|
||||||
os.system(f"git -C {basedir} add {dotfilepath}")
|
os.system(f"git -C {basedir} add {dotfilepath}")
|
||||||
os.symlink(dotfilepath, absolutpath)
|
os.symlink(dotfilepath, absolutpath)
|
||||||
|
|
||||||
|
|
||||||
def unlink(path):
|
def unlink(path):
|
||||||
absolutpath, dotfilepath = getpaths(path)
|
absolutpath, dotfilepath = getpaths(path)
|
||||||
|
|
||||||
|
@ -89,12 +94,14 @@ def apply(path, force):
|
||||||
shutil.rmtree(absolutpath)
|
shutil.rmtree(absolutpath)
|
||||||
|
|
||||||
os.makedirs(os.path.dirname(absolutpath), exist_ok=True)
|
os.makedirs(os.path.dirname(absolutpath), exist_ok=True)
|
||||||
|
|
||||||
|
print(f"{dotfilepath} -> {absolutpath}")
|
||||||
os.link(dotfilepath, absolutpath)
|
os.link(dotfilepath, absolutpath)
|
||||||
|
|
||||||
|
|
||||||
def sync():
|
def sync():
|
||||||
os.system(f"git -C {basedir} pull")
|
os.system(f"git -C {basedir} pull")
|
||||||
os.system(f"git -C {basedir} commit -am \"dotfiles\"")
|
os.system(f'git -C {basedir} commit -am "dotfiles"')
|
||||||
os.system(f"git -C {basedir} push")
|
os.system(f"git -C {basedir} push")
|
||||||
|
|
||||||
|
|
||||||
|
@ -112,11 +119,11 @@ def main():
|
||||||
symlink_parser = subparsers.add_parser("apply")
|
symlink_parser = subparsers.add_parser("apply")
|
||||||
sync_parser = subparsers.add_parser("sync")
|
sync_parser = subparsers.add_parser("sync")
|
||||||
|
|
||||||
add_parser.add_argument('--force', '-f', action="store_true")
|
add_parser.add_argument("--force", "-f", action="store_true")
|
||||||
add_parser.add_argument('path')
|
add_parser.add_argument("path")
|
||||||
unlink_parser.add_argument('path')
|
unlink_parser.add_argument("path")
|
||||||
symlink_parser.add_argument('--force', '-f', action="store_true")
|
symlink_parser.add_argument("--force", "-f", action="store_true")
|
||||||
symlink_parser.add_argument('path')
|
symlink_parser.add_argument("path")
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
@ -130,5 +137,6 @@ def main():
|
||||||
case "sync":
|
case "sync":
|
||||||
sync()
|
sync()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Add table
Reference in a new issue