-
Notifications
You must be signed in to change notification settings - Fork 0
/
reclink
executable file
·153 lines (126 loc) · 4.09 KB
/
reclink
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
#!/usr/bin/env python3
import sys
if sys.version_info.major != 3:
print('please run with python 3')
sys.exit(1)
import os
import logging
import argparse
logging.basicConfig(format='%(levelname)s %(message)s', level=logging.INFO)
SOURCE_DIR = str()
TARGET_DIR = str()
IGNORE_SET = {os.path.basename(__file__)}
REPLACE = False
DRY = False
def parse_args():
parser = argparse.ArgumentParser(prog='reclink')
required = parser.add_argument_group('required arguments')
required.add_argument(
'-s', '--source',
action='store',
type=str,
required=True,
metavar='PATH',
help='path to source directory'
)
required.add_argument(
'-t', '--target',
action='store',
type=str,
required=True,
metavar='PATH',
help='path to target directory'
)
parser.add_argument(
'-i', '--ignore',
action='extend',
type=str,
nargs='+',
required=False,
metavar='PATH',
help='relative paths to be ignored'
)
parser.add_argument(
'-r', '--replace',
action='store_true',
required=False,
help='replace existing targets'
)
parser.add_argument(
'-q', '--quiet',
action='store_true',
required=False,
help='skip user confirmation'
)
parser.add_argument(
'-d', '--dry',
action='store_true',
required=False,
help='skip actual changes to filesystem'
)
return parser.parse_args()
def link_file(source: str):
# absolute path
target = os.path.abspath(TARGET_DIR + os.sep + source.removeprefix(SOURCE_DIR))
# check target
if os.path.exists(target):
if not REPLACE:
logging.info('skipping link, target already exists: ' + target)
return
if os.path.isdir(target):
logging.warning('skipping link, target is a directory: ' + target)
return
elif os.path.isfile(target) or os.path.islink(target):
if not DRY:
os.remove(target)
else:
if not os.path.isdir(os.path.abspath(target + os.sep + '..')):
logging.info('creating directories for file: ' + target)
if not DRY:
try:
os.makedirs(target.removesuffix(os.path.basename(os.sep + target)), mode=0o755, exist_ok=True)
except FileExistsError:
logging.warning('unable to create directory at: ' + os.path.abspath(target + os.sep + '..'))
return
logging.info('linking file: ' + source + ' to ' + target)
if not DRY:
os.symlink(source, target)
def is_ignored(abs_path: str):
rel_path = abs_path.removeprefix(SOURCE_DIR + os.sep)
for i in IGNORE_SET:
if rel_path == i or rel_path.startswith(i + os.sep):
return True
return False
if __name__ == '__main__':
args = parse_args()
SOURCE_DIR = os.path.abspath(args.source)
TARGET_DIR = os.path.abspath(args.target)
if args.ignore is not None:
for i in args.ignore:
IGNORE_SET.add(i)
REPLACE = args.replace
DRY = args.dry
if not args.quiet:
print('source: ' + str(SOURCE_DIR))
print('target: ' + str(TARGET_DIR))
print('ignore: ' + str(sorted(IGNORE_SET)))
input('press enter to confirm')
if not os.path.isdir(SOURCE_DIR):
logging.error('source is not a directory: ' + SOURCE_DIR)
sys.exit(2)
if not os.path.isdir(TARGET_DIR):
logging.error('target is not a directory: ' + TARGET_DIR)
sys.exit(2)
if SOURCE_DIR == TARGET_DIR:
logging.error('source and target directory can not be the same')
sys.exit(2)
for root, _, files in os.walk(SOURCE_DIR):
if is_ignored(root):
logging.debug('ignoring folder: ' + str(root))
continue
for f in files:
path = os.path.abspath(root + os.sep + f)
if is_ignored(path):
logging.debug('ignoring file: ' + str(path))
continue
link_file(path)