svn-gvsig-desktop / branches / v2_0_0_prep / build / buildman / bin / bmcore / BMUtil.py @ 27363
History | View | Annotate | Download (4.21 KB)
1 |
import os |
---|---|
2 |
import os.path |
3 |
import zipfile |
4 |
import difflib |
5 |
import tarfile |
6 |
import md5 |
7 |
import urllib2 |
8 |
import sys |
9 |
import shutil |
10 |
import re |
11 |
|
12 |
class BMUtil: |
13 |
def buildmanPath(self): |
14 |
dr = os.getenv("BUILDMAN_PATH")
|
15 |
if not dr is None: |
16 |
return dr
|
17 |
if sys.platform == "linux2" or sys.platform == "linux1": |
18 |
dr = os.getenv("HOME")+os.path.sep+".buildman" |
19 |
if sys.platform == "win32" or sys.platform == "win64": |
20 |
#ex: c:\documents and setting\user\DepMan
|
21 |
dr = os.getenv("USERPROFILE")+os.path.sep+"BuildMan" |
22 |
if sys.platform == "darwin": |
23 |
dr = os.path.sep + "Developer" + os.path.sep + "BuildMan" |
24 |
if dr is None: |
25 |
return None |
26 |
else:
|
27 |
return dr
|
28 |
|
29 |
def buildmanPlugInPath(self): |
30 |
dr = self.buildmanPath()
|
31 |
if dr is None: |
32 |
return None |
33 |
else:
|
34 |
return os.path.join(dr,'plugins') |
35 |
|
36 |
|
37 |
def mkdir(self,newdir): |
38 |
"""works the way a good mkdir should :)
|
39 |
- already exists, silently complete
|
40 |
- regular file in the way, raise an exception
|
41 |
- parent directory(ies) does not exist, make them as well
|
42 |
"""
|
43 |
if os.path.isdir(newdir):
|
44 |
pass
|
45 |
elif os.path.isfile(newdir):
|
46 |
raise OSError("a file with the same name as the desired " \ |
47 |
"dir, '%s', already exists." % newdir)
|
48 |
else:
|
49 |
head, tail = os.path.split(newdir) |
50 |
if head and not os.path.isdir(head): |
51 |
self.mkdir(head)
|
52 |
if tail:
|
53 |
os.mkdir(newdir) |
54 |
|
55 |
#recursively directory tree removing
|
56 |
def rmdir(self,path): |
57 |
if not os.path.isdir(path): |
58 |
return
|
59 |
|
60 |
shutil.rmtree(path) |
61 |
#files=os.listdir(path)
|
62 |
|
63 |
#for x in files:
|
64 |
# fullpath=os.path.join(path, x)
|
65 |
|
66 |
# if os.path.isfile(fullpath):
|
67 |
# os.remove(fullpath)
|
68 |
|
69 |
# elif os.path.isdir(fullpath):
|
70 |
# rmdir(fullpath)
|
71 |
# os.rmdir(fullpath)
|
72 |
|
73 |
|
74 |
def unzip(self,file, dir): |
75 |
mkdir(dir)
|
76 |
zfobj = zipfile.ZipFile(open(file)) |
77 |
for name in zfobj.namelist(): |
78 |
if name.endswith('/'): |
79 |
mkdir(os.path.join(dir, name))
|
80 |
else:
|
81 |
outfile = open(os.path.join(dir, name), 'wb') |
82 |
outfile.write(zfobj.read(name)) |
83 |
outfile.close() |
84 |
|
85 |
def targz(self,file, dir): |
86 |
cwd = os.getcwd() |
87 |
os.chdir(dir)
|
88 |
dirs = os.listdir(".")
|
89 |
if ".svn" in dirs: |
90 |
dirs.remove(".svn")
|
91 |
if ".cvsignore" in dirs: |
92 |
dirs.remove(".cvsignore")
|
93 |
tar = tarfile.open(str(file),"w|gz") |
94 |
for f in dirs: |
95 |
tar.add(f) |
96 |
tar.close() |
97 |
os.chdir(cwd) |
98 |
|
99 |
|
100 |
|
101 |
def untargz(self,file, dir): |
102 |
tar = tarfile.open(file)
|
103 |
for tarinfo in tar: |
104 |
tar.extract(tarinfo,dir)
|
105 |
tar.close() |
106 |
|
107 |
def untargzFiltered(self,file,dir,filter): |
108 |
filter=filter.replace("*","(\S)*") |
109 |
filter="^(\S)*"+filter+"$" |
110 |
#print "filter: ",filter
|
111 |
tar = tarfile.open(file)
|
112 |
for tarinfo in tar: |
113 |
if re.match(filter,tarinfo.name): |
114 |
print "[file] ",tarinfo.name |
115 |
tar.extract(tarinfo,dir)
|
116 |
|
117 |
tar.close() |
118 |
|
119 |
def diff(self,f1name, f2name): |
120 |
try:
|
121 |
f1 = open(f1name)
|
122 |
f2 = open(f2name)
|
123 |
a = f1.readlines(); f1.close() |
124 |
b = f2.readlines(); f2.close() |
125 |
for line in difflib.unified_diff(a, b): |
126 |
print line,
|
127 |
if line != "": |
128 |
return True |
129 |
return False |
130 |
except:
|
131 |
return True |
132 |
|
133 |
#creates the given fouput md5 file from the finput file
|
134 |
def createMD5(self,finput,foutput): |
135 |
#input file
|
136 |
file_in=open(finput,"rb") |
137 |
md5cpm=md5.new() |
138 |
|
139 |
while True: |
140 |
data = file_in.read(8096)
|
141 |
if not data: |
142 |
break
|
143 |
md5cpm.update(data) |
144 |
|
145 |
file_in.close() |
146 |
|
147 |
|
148 |
md5string=md5cpm.hexdigest() |
149 |
|
150 |
file_out=open(foutput,"w") |
151 |
file_out.write(md5string) |
152 |
file_out.close() |
153 |
|
154 |
|
155 |
|
156 |
def checkUrl(self,uri): |
157 |
try:
|
158 |
fd=urllib2.urlopen(uri) |
159 |
fd.close() |
160 |
return True |
161 |
except:
|
162 |
return False |
163 |
|
164 |
|
165 |
def download(self,uri,fout): |
166 |
try:
|
167 |
fdes=urllib2.urlopen(uri) |
168 |
foutdes=open(fout,"wb") |
169 |
uinfo=fdes.info() |
170 |
size = uinfo.get('content-length')
|
171 |
size=int(size)
|
172 |
|
173 |
uristr=uri[uri.rfind("/")+1:] |
174 |
|
175 |
#lets read the file in 1kbyte blocks
|
176 |
completed=0
|
177 |
x=completed*100/size
|
178 |
tstr="* downloading ["+str(x)+" %] "+uristr |
179 |
sys.stdout.write(tstr) |
180 |
while True: |
181 |
block=fdes.read(1024)
|
182 |
if not block: |
183 |
break
|
184 |
foutdes.write(block) |
185 |
|
186 |
completed=completed+len(block)
|
187 |
x=completed*100/size
|
188 |
sys.stdout.write("\r")
|
189 |
tstr="* downloading ["+str(x)+" %] "+uristr |
190 |
sys.stdout.write(tstr) |
191 |
#insert write op here
|
192 |
|
193 |
foutdes.close() |
194 |
|
195 |
sys.stdout.write("\n")
|
196 |
|
197 |
#print "* downloaded ",uri," [",size,"] bytes"
|
198 |
return True |
199 |
except:
|
200 |
return False |
201 |
|
202 |
|