forked from scottchiefbaker/dool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.py
executable file
·115 lines (84 loc) · 3.3 KB
/
install.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/python3
###############################################################################
# Simple install script that copies files to various paths and sets
# permissions on files. Nothing fancy here, just copying files around
#
# 2022-09-28 - Scott Baker
###############################################################################
import sys
import glob
import os
import shutil
import pathlib
base_dir = os.path.dirname(__file__)
base_dir = base_dir or "."
# Are you running as root?
am_root = os.getuid() == 0
# Pull out the ARGV so we can check for somethings
argv = sys.argv[1:]
args = " ".join(argv)
# Set some global variables
force_root_install = args.__contains__("--root")
force_user_install = args.__contains__("--user")
verbose = args.__contains__("--verbose")
# --user overrides --root
if (force_user_install):
force_root_install = False
am_root = False
# Glob the files we need to install
binaries = glob.glob(base_dir + "/dool")
plugins = glob.glob(base_dir + "/plugins/*.py")
manpages = glob.glob(base_dir + "/docs/dool.1")
homedir = os.path.expanduser("~/")
############################################################
def main():
if (force_root_install or am_root):
print("You are %s, doing a local install\n" % color(9, "root"))
bin_dir = "/usr/bin/"
plugin_dir = "/usr/share/dool/"
manpage_dir = "/usr/share/man/man1/"
print("Installing binaries to %s" % color(15, bin_dir))
copy_files(binaries, bin_dir, 0o755)
print("Installing plugins to %s" % color(15, plugin_dir))
copy_files(plugins , plugin_dir, 0o644)
print("Installing manpages to %s" % color(15, manpage_dir))
copy_files(manpages, manpage_dir, 0o644)
else:
print("You are a %s user, doing a local install\n" % color(227, "normal"))
bin_dir = (homedir + "/bin/").replace("//", "/")
plugin_dir = (homedir + "/.dool/").replace("//", "/")
manpage_dir = ""
print("Installing binaries to %s" % color(15, bin_dir))
copy_files(binaries, bin_dir, 0o755)
print("Installing plugins to %s" % color(15, plugin_dir))
copy_files(plugins , plugin_dir, 0o644)
dool_path = (bin_dir + "/dool").replace("//", "/")
#print(os.path.exists(dool_path))
print("")
print("Install complete. Dool installed to %s" % (color(84, dool_path)))
############################################################
# Print a string wrapped in an ANSI color and RESET
def color(num, mystr):
reset = '\033[0;0m'
ret = "\033[38;5;" + str(num) + "m" + mystr + reset
return ret
# Copy an array of files to a destination dir and chmod each file to mode
def copy_files(files, dest_dir, mode):
ok = os.makedirs(dest_dir, exist_ok=True)
count = 0
for x in files:
basename = os.path.basename(x)
dest_file = dest_dir + "/" + basename
dest_file = dest_file.replace("//", "/")
# Copy file
ok = shutil.copyfile(x, dest_file)
# Chmod the file after it's copied
os.chmod(dest_file, mode)
if verbose:
print("%40s => %s" % (x, dest_file))
if (ok):
count += 1
return ok
################################################################
if __name__ == "__main__":
main()