-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall
executable file
·125 lines (104 loc) · 3.96 KB
/
install
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author:: Lincoln Clarete <[email protected]>
# Copyright:: Copyright (c) 2013, Lincoln Clarete
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Except as contained in this notice, the name(s) of the above copyright
# holders shall not be used in advertising or otherwise to promote the
# sale, use or other dealings in this Software without prior written
# authorization.
"""Small tool to install links for my dotfiles
"""
import os
import sys
import argparse
import shutil
try:
import couleur
couleur.proxy(sys.stdout).enable()
except ImportError:
pass
AVOID_LIST = (
os.path.basename(__file__),
'requirements.txt',
'.git',
'.gitignore',
'.gitmodule',
'README.md',
)
def files_to_copy():
"""Retrieves a list of files that should be copied"""
files = [
i for i in os.listdir(os.path.dirname(__file__))
if i not in AVOID_LIST]
return files
def backup(args):
"""Backs up any existing file"""
# Just making sure that our backup folder is empty
if os.path.exists(args.bkpdir):
print('#{red}Not good!#{normal} Your backup folder is not empty')
prompt = input(' Should I remove it and continue? (y, n): ')
if not prompt.lower() in ('y', 'yes'):
print(' Backup your stuff and try again')
exit(-1)
print(" Aaaaaaand it's gone")
shutil.rmtree(args.bkpdir)
# Saving a list of files to be backed up
files_to_backup = []
for i in files_to_copy():
current = os.path.join(args.home, i)
# It's just a link, we can send it to hell
if os.path.islink(current):
os.unlink(current)
if os.path.exists(current):
files_to_backup.append(current)
# Actuall backup
if files_to_backup:
os.mkdir(args.bkpdir, 0o700)
for i in files_to_backup:
shutil.move(i, args.bkpdir)
def link(args):
"""Links the files in the checkout folder to the destdir"""
print("#{green}Linking the files!#{normal}")
for i in files_to_copy():
src = os.path.join(os.path.abspath(os.path.dirname(__file__)), i)
dest = os.path.join(args.home, i)
os.symlink(src, dest)
print((' * {} #{{green}} ✔ #{{normal}}'.format(i)))
def main():
"""Processes the command line args and calls the backup and
linking functions"""
home = os.path.expanduser('~')
bkpdir = os.path.join(home, '.dotfiles.bkp')
parser = argparse.ArgumentParser(description='dotfile installer')
parser.add_argument(
'--destdir', dest='home', default=home, action='store',
help='Target folder to install files, default to your home')
parser.add_argument(
'--bkpdir', dest='bkpdir', default=bkpdir, action='store',
help='Folder used to copy old files before linking, defaults '
'to ~/.dotfiles.bkp')
args = parser.parse_args()
backup(args)
link(args)
if __name__ == '__main__':
main()