Project

General

Profile

migrate_dot_files.py

prototype script. - Marc Mengel, 04/26/2016 10:05 AM

 
1
#!/usr/bin/env python
2

    
3
import re
4
import os
5

    
6
match={}
7
repl={}
8

    
9
repl[".profile"] = """
10
for pa in /grid/fermiapp/products/common/etc /local/ups/etc /fnal/ups/etc
11
do
12
  if [  -r "$pa/setups.sh" ]
13
  then
14
    . "$pa/setups.sh"
15
    if ups exist login
16
    then
17
        setup login
18
    fi
19
  fi
20
done
21
"""
22

    
23
repl[".login"] = """
24
foreach pa (/grid/fermiapp/products/common/etc /local/ups/etc /fnal/ups/etc)
25

26
  if ( -f "$pa/setups.csh" ) then
27
    source "$pa/setups.csh"
28
    if ( { ups exist login } ) then
29
        setup login
30
    endif
31
    break
32
  endif
33

34
end
35

36
"""
37

    
38
repl[".cshrc"] = """
39
foreach pa (/grid/fermiapp/products/common/etc /local/ups/etc /fnal/ups/etc)
40

41
  if ( -f "$pa/setups.csh" ) then
42
    source "$pa/setups.csh"
43
    break
44
  endif
45

46
end
47

48
if ( { ups exist shrc } ) then
49
    setup shrc
50
endif
51
"""
52

    
53
repl[".shrc"] = """
54
for pa in /grid/fermiapp/products/common/etc /local/ups/etc /fnal/ups/etc
55
do
56
  if [  -r "$pa/setups.sh" ]
57
  then
58
    . "$pa/setups.sh"
59
    break
60
  fi
61
done
62

63
if ups exist shrc
64
then
65
    setup shrc
66
fi
67
"""
68

    
69

    
70
match[".profile"] = """if\s+\[\s+-f\s+"/afs/fnal.gov/ups/etc/setups.sh"\s+\].\s*then.\s*\.\s*"/afs/fnal.gov/ups/etc/setups.sh".\s*if\s+ups\s+exist\s+login.\s*then.\s*setup\s+login.\s*fi.\s*fi"""
71

    
72
match[".login"] = """if\s+\(\s+-f\s+"/afs/fnal.gov/ups/etc/setups.csh"\s+\)\s+then.*\s*source\s+"/afs/fnal.gov/ups/etc/setups.csh".*\s*if\s+\(\s+\{\s+ups\s+exist\s+login\s+\}\s+\)\s+then.\s*setup\s+login.\s*endif.*endif"""
73

    
74
match[".cshrc"] = """if\s+\(\s+-f\s+"/afs/fnal.gov/ups/etc/setups.csh"\s+\)\s+then.\s*source\s+"/afs/fnal.gov/ups/etc/setups.csh".\s*endif.*if\s+\(\s+{\s+ups\s+exist\s+shrc\s+}\s+\)\s+then.\s*setup\s+shrc.endif"""
75

    
76
match[".shrc"] = """if\s+\[\s+-f\s+"/afs/fnal.gov/ups/etc/setups.sh"\s+\].\s*then.\s*\.\s+"/afs/fnal.gov/ups/etc/setups.sh".fi..if\s+ups\s+exist\s+shrc.then.\s*setup\s+shrc.fi"""
77

    
78
def do_replacements():
79

    
80
    for f in [".profile", ".login", ".shrc", ".cshrc"]:
81

    
82
        print "doing: ", f
83

    
84
        fd = open(f,"r")
85
        contents = fd.read()
86
        fd.close()
87
        m =  re.search(match[f],  contents, re.M+re.S )
88
        if m:
89
           print "saw match in ", f, "matched at:", m.start(0), "..", m.end(0)
90
           newcontents = contents[:m.start(0)] + repl[f] + contents[m.end(0):]
91

    
92
           print "substitution changed something..."
93
           os.rename(f, f+'.afs_setups')
94
           fd = open(f,"w")       
95
           fd.write(newcontents)
96
           fd.close()
97
     
98

    
99
if __name__ == '__main__':
100

    
101
    do_replacements()