-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_quick_script.py
More file actions
executable file
·55 lines (44 loc) · 1.44 KB
/
Copy pathadd_quick_script.py
File metadata and controls
executable file
·55 lines (44 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/python3
"""
Quickly add a script to `~/.local/bin` as a symbolic link
"""
import os
import stat
from pathlib import Path
YES = ("Y", "y")
RENAME = ("R", "r")
BIN = Path("~/.local/bin").expanduser()
def add_script_to_path(script_path: str) -> int:
proper_path = Path(script_path).expanduser().resolve()
final_path = BIN / Path(script_path).stem
if not proper_path.exists():
raise Exception(f"{proper_path} is not a valid path!")
if final_path.exists():
raise Exception(f"{final_path} already exists!")
if not os.access(proper_path, os.X_OK):
# Give the file the proper access
proper_path.chmod(proper_path.stat().st_mode | stat.S_IEXEC)
return io_helper(proper_path, final_path)
def io_helper(proper_path: Path, final_path: Path):
# Finally, create symlink
print("Creating the following Symlink:")
print(f"{proper_path} -> {final_path}")
print("[Y/y]?")
print("[R/r] rename?")
inpt = input()
if inpt in YES:
os.symlink(proper_path, final_path)
return 0
if inpt not in RENAME:
print("Goodbye")
return 1
return io_helper(
proper_path,
BIN / input("Rename the final bit to:\n"),
)
if __name__ == "__main__":
import sys
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("script", help="File to symlink")
sys.exit(add_script_to_path(parser.parse_args().script))