-
Notifications
You must be signed in to change notification settings - Fork 2
/
place.py
executable file
·105 lines (87 loc) · 2.88 KB
/
place.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
#!/usr/bin/env python3
'''
This script places every dot file to their destination.
'''
DATE = "Saturday, May 25 2013"
AUTHOR = "Yagnesh Raghava Yakkala"
WEBSITE = "http://yagnesh.org"
LICENSE = "GPL v3 or later"
import os
import inspect
import re
import shutil
import logging
import sys
def _get_logger():
LOG_FORMAT = '%(levelname)-6s: %(message)s'
logger = logging.getLogger('wrfout')
logger.setLevel(logging.INFO)
if not logger.handlers:
hlr = logging.StreamHandler()
hlr.setFormatter(logging.Formatter(LOG_FORMAT))
logger.addHandler(hlr)
return logger
this_file_path = os.path.abspath(os.path.split(inspect.getfile(
inspect.currentframe()))[0])
ignores = [".git", "README.org", ".gitignore"]
lgr = _get_logger()
def find_attri(f):
n = d = None
with open(f, encoding="utf8") as fh:
for line in fh:
if re.match(r'.*\+DEST=(.*)', line):
d = re.sub(r'.*\+DEST=(.*)', r'\1', line)
elif re.match(r'.*\+FNAME=(.*)', line):
n = re.sub(r'.*\+FNAME=(.*)', r'\1', line)
elif d and n:
break
return [n, d]
def link_file(f, dest):
"""
Check if the destination is writable or not. Backup if there is already a
destination file(don't bother about links) exists.
Finally link `f` to `dest`.
"""
dest_dir = os.path.dirname(dest)
if not os.path.exists(dest_dir) or not os.access(
os.path.dirname(dest), os.W_OK):
lgr.error('Destination "%s" doesn\'t exits or not writable' % dest_dir)
return False
else:
if os.path.exists(dest):
if not os.path.islink(dest):
lgr.info('Backing up original to %s' % dest + '.orig')
shutil.move(dest, dest + '.orig')
try:
os.unlink(dest)
except OSError:
lgr.error('*FAILED*: deleting %s' % dest)
try:
# return os.symlink(os.path.abspath(f), dest)
return shutil.copyfile(os.path.abspath(f), dest)
except OSError:
lgr.error('*FAILED*: linking %s => %s' % (f, dest))
def place_file(f):
[fname, d] = find_attri(f)
if fname and d:
dest = os.path.expandvars(d.rstrip()+fname.rstrip())
lgr.info('Placing %s => %s' % (f, dest))
return link_file(f, dest)
else:
lgr.error('*FAILED*: %s => ???' % f)
def main():
if len(sys.argv) < 2:
os.chdir(this_file_path)
reg = re.compile("|".join(map(re.escape, ignores)))
for f in os.listdir(this_file_path):
if f == os.path.split(__file__)[1] or reg.search(f):
continue
place_file(f)
else:
for f in sys.argv[1:]:
if os.path.exists(f):
place_file(f)
else:
print(f + ": no such file to place")
if __name__ == '__main__':
main()