-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdot_files.py
executable file
·163 lines (150 loc) · 6.78 KB
/
dot_files.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python3
# Usage: python dot_files.py [update|recover|init]
# Update Function: Back up $HOME/file to ./backup,
# append all the contents of ./file to $HOME/file or
# copy ./file to $HOME/
# Recover Function: copy ./backup/file to $HOME/
# No Args: this will recover, backup and update.
# NOTE: any operation reference to update will overwrite the backup files,
# so use ./dot_files.py with no args are strongly recommended.
# NOTE: do not run this code as root, even using sudo, otherwise, this will install in
# user root's HOME DIRECTORY, which is usually set with /root
# Update the list to ignore those files you don't want to update or recover
# NOTE: items must start with ./ and end with no / at the end,
# for example, ./backup means a file named backup in current directory or
# a directory named backup in current directory
# NOTE: functionality of converting vimwiki to html needed pandoc,
# and tasks_list are supported after pandoc 2.6,
# you can use conda install pandoc for your activated conda environment,
# if you do it, make sure when you want to convert files,
# your conda environment is where you install pandoc,
# if you never convert vimwiki to html files, you can ignore this.
# NOTE: make sure you conda environments are all deactived before runnint with init
ignore_file = set(["./.git", "./LICENSE", "./README.md", './.gitmodules',
"./dot_files.py", "./.gitignore", "./replace_md_image.py",
"./installer", "./vscode-setting", "./README.assets", "./markdownBackup",
"./.bashrc", "./.config/fish", "./.config/nvim/lua/plugin_config"])
# Update the list to let those files to be copied to $HOME
# In short, if your $HOME has no the file or directory,
# Add it to the variable.
copy_file = set(["./.tmux.conf", "./.config", "./.local", "./.p10k.zsh",])
# Update the string to specify where you want to store the backed-up files
# or where you want to recover from
backup_dir = "./backup"
import os
import shutil
import sys
# We don't backup backup directory
ignore_file.add(backup_dir)
backup_dir += '/'
home_dir = os.environ["HOME"] + '/'
def backup_files(home_current_dir : str, current_dir):
global backup_dir, ignore_file, home_dir
if not os.path.exists(backup_dir):
os.mkdir(backup_dir)
home_file_set = set(os.listdir(home_current_dir))
cur_file_set = set(os.listdir(current_dir))
backup_file_set = home_file_set & cur_file_set
for fileOrDir in backup_file_set:
if fileOrDir == '.' or fileOrDir == "..":
continue
if current_dir + fileOrDir in ignore_file:
continue
if os.path.isfile(home_current_dir + fileOrDir):
shutil.copy(home_current_dir + fileOrDir,
backup_dir + fileOrDir)
else:
fileOrDir += '/'
backup_dir += fileOrDir
backup_files(home_current_dir + fileOrDir,
current_dir + fileOrDir)
backup_dir = backup_dir[:-len(fileOrDir)]
def update_dot_files(home_current_dir : str, current_dir : str):
if not os.path.exists(home_current_dir):
os.mkdir(home_current_dir)
cur_file_set = set(os.listdir(current_dir))
for fileOrDir in cur_file_set:
if fileOrDir == '.' or fileOrDir == "..":
continue
if current_dir + fileOrDir in ignore_file:
continue
if os.path.isfile(current_dir + fileOrDir):
if current_dir + fileOrDir in copy_file:
shutil.copy(current_dir + fileOrDir,
home_current_dir + fileOrDir)
else:
os.system(f"cat {os.getcwd() + '/' + current_dir + fileOrDir} "
f">> {home_current_dir + fileOrDir}")
elif current_dir + fileOrDir in copy_file:
if not os.path.exists(home_current_dir + fileOrDir):
os.mkdir(home_current_dir + fileOrDir)
os.system("cp -r "
f"{os.getcwd() + '/' + current_dir + fileOrDir + '/*'} "
f"{home_current_dir + fileOrDir}")
else:
fileOrDir += '/'
update_dot_files(home_current_dir + fileOrDir,
current_dir + fileOrDir)
def recover_dot_files(home_current_dir, current_dir):
global backup_dir, ignore_file, home_dir
# this is to invent the backup_dir doesn't exists.
if not os.path.exists(backup_dir + current_dir):
return
if not os.path.exists(home_current_dir):
os.mkdir(home_current_dir)
recover_file_set = set(os.listdir(backup_dir + current_dir))
for recover_file in recover_file_set:
if recover_file == '.' or recover_file == "..":
continue
if current_dir + recover_file in ignore_file:
continue
if os.path.isfile(backup_dir + current_dir + recover_file):
shutil.copy(backup_dir + current_dir + recover_file,
home_current_dir)
else:
recover_file += '/'
recover_dot_files(home_current_dir + recover_file,
current_dir + recover_file)
def install_useful_softwares():
os.system("cd installer && bash installer.sh")
def init_submodule():
os.system("git submodule update --init --recursive")
def operate_dot_files(home_current_dir : str, current_dir : str,
opcode : str):
if opcode == "update":
backup_files(home_current_dir, current_dir)
update_dot_files(home_current_dir, current_dir)
elif opcode == "recover":
recover_dot_files(home_current_dir, current_dir)
elif opcode == "init":
init_submodule();
recover_dot_files(home_current_dir, current_dir)
backup_files(home_current_dir, current_dir)
update_dot_files(home_current_dir, current_dir)
install_useful_softwares()
elif opcode == "":
recover_dot_files(home_current_dir, current_dir)
backup_files(home_current_dir, current_dir)
update_dot_files(home_current_dir, current_dir)
else:
print("Usage: ./dot_files.py [update|recover|init]")
exit(1)
if __name__ == "__main__":
if os.getuid() == 0:
while True:
print("Warning: you are installing for user root,"
"which is uncommon, are you sure to continue?[y/N]", end = '')
confirm = input().lower()
if confirm == 'n':
exit(0)
elif confirm == 'y':
break
if len(sys.argv) == 2:
opcode = sys.argv[1]
elif len(sys.argv) > 2:
print("Usage: ./dot_files.py [update|recover|init]")
exit(1)
else:
opcode = ""
home_dir = home_dir.replace("//", "/")
operate_dot_files(home_dir, "./", opcode)