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
|
#!/usr/bin/python
#----------------
from optparse import OptionParser
import os
from sys import exit
import sys
import glob
def pathsplit(path):
return path.split(os.path.sep)
def commonpath(l1, l2, common=[]):
if len(l1) < 1: return (common, l1, l2)
if len(l2) < 1: return (common, l1, l2)
if l1[0] != l2[0]: return (common, l1, l2)
return commonpath(l1[1:], l2[1:], common+[l1[0]])
def relpath(p1, p2):
(common,l1,l2) = commonpath(pathsplit(p1), pathsplit(p2))
p = []
if len(l1) > 0:
p = [ '../' * len(l1) ]
p = p + l2
return os.path.join( *p )
def ask_confirmation(path):
while True:
print path+" exists, should I :"
print "1 : remove and recreate"
print "2 : update"
print "3 : cancel"
answer = sys.stdin.readline()
try:
answer = int(answer)
except ValueError:
print "Must be a number between 1 and 31\n"
continue
if answer >= 1 and answer <= 3:
return answer
print "Must be a number between 1 and 3\n"
def create_dir(dest):
if not os.path.exists(dest):
os.makedirs(dest)
return
return
reps = pathsplit(dest)
mkrep = ""
for rep in reps:
print rep + " rep!"
mkrep = os.path.join(mkrep, rep)
if os.path.exists(mkrep):
ans = ask_confirmation(mkrep)
if ans == 1:
for root, dirs, files in os.walk(mkrep, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
os.rmdir(root)
if ans == 2:
continue
if ans == 3:
exit()
os.mkdir(mkrep)
def get_h(source):
return glob.glob(source+"/*.h")
def create_h(source, destination):
files = get_h(source)
inc_path = relpath(os.path.abspath(destination), os.path.abspath(source))
create_dir(destination)
for h_file in files:
h_file = os.path.basename(h_file)
if os.path.exists(os.path.join(destination, h_file)):
ans = ask_confirmation(os.path.join(destination, h_file))
if ans == 1:
os.remove(os.path.join(destination, h_file))
f = open(os.path.join(destination, h_file), "w")
f.write("#include <" + os.path.join(inc_path,h_file) + ">\n")
if ans == 2:
f = open(os.path.join(destination, h_file), "r")
text = f.read()
f.close
os.remove(os.path.join(destination, h_file))
f = open(os.path.join(destination, h_file), "w")
f.write("#include <" + os.path.join(inc_path,h_file) + ">\n")
f.write(text)
if ans == 3:
exit()
else:
f = open(os.path.join(destination, h_file), "w")
f.write("#include <" + os.path.join(inc_path,h_file) + ">\n")
f.close
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-d", "--destination-dir", dest="destination", help="destination directory")
parser.add_option("-s", "--source-dir", dest="source", help="source directory")
(options, args) = parser.parse_args()
if (not options.destination) and (not options.source):
print "Destination and source required."
parser.print_help()
exit()
create_h(options.source, options.destination)
|