-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbcopy.py
234 lines (214 loc) · 7.05 KB
/
bcopy.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# -*- coding:utf-8 -*-
import shutil
import os
import sys
try:
from PyQt4 import QtCore, QtGui, uic
from PyQt4.QtGui import QApplication, QMainWindow, QPushButton, QWidget
except ImportError as a:
from PySide import QtCore, QtGui
from PySide.QtGui import QApplication, QMainWindow, QPushButton, QWidget
import pyside_uicfix
import sys
# from threading import Thread
class porgressCopy(QWidget):
def __init__(self, parent=None):
super(porgressCopy, self).__init__(parent)
uipath = '%s/ui/uploadCheck.ui' % self.parent().env.WhAppPath
try:
pyside_uicfix.loadUi(uipath, self)
except NameError:
uic.loadUi(uipath, self)
def copygui(self, copyinfo):
if copyinfo['idx']:
print 'change file'
else:
print 'keep going'
print copyinfo
return copyinfo
def copyfile(self, source,target,callback,length=10485760):
"""copy data from file-like object source to file-like object target"""
source = unicode(source)
target = unicode(target)
if not os.path.exists(os.path.dirname(target)):
os.makedirs(unicode(os.path.dirname(target)))
fsrc = file(source, 'rb')
fdst = file(target, 'wb')
copyedsize = 0
filesize = os.path.getsize(source)
copyinfo = {}
try:
while 1:
copyinfo['progressValue'] = 0
copyinfo['filename'] = target
copyinfo['idx'] = False
buf = fsrc.read(length)
if not buf:
break
copyedsize += length
fdst.write(buf)
if filesize > copyedsize:
# progressValue = int(float(copyedsize) / float(filesize) * 100)
copyinfo['progressValue'] = int(float(copyedsize) / float(filesize) * 100)
elif filesize <= copyedsize:
# progressValue = 100
copyinfo['progressValue'] = 100
copyinfo['idx'] = True
callback(copyinfo)
fsrc.close()
fdst.close()
except:
fsrc.close()
fdst.close()
def copytree(self, source, target, symlinks=False, ignore=None):
source = unicode(source)
target = unicode(target)
names = os.listdir(source)
file_counter = sum([len(files) for r, d, files in os.walk(source)])
copyed = 0
if ignore is not None:
ignored_names = ignore(source, names)
else:
ignored_names = set()
if not os.path.exists(target):
os.makedirs(target)
errors = []
for name in names:
if name in ignored_names:
continue
srcname = os.path.join(source, name)
dstname = os.path.join(target, name)
try:
if symlinks and os.path.islink(srcname):
linkto = os.readlink(srcname)
os.symlink(linkto, dstname)
elif os.path.isdir(srcname):
self.copytree(srcname, dstname, symlinks, ignore)
else:
self.copyfile(srcname, dstname, self.copygui)
copyed += 1
# XXX What about devices, sockets etc.?
except (IOError, os.error) as why:
errors.append((srcname, dstname, str(why)))
# catch the Error from the recursive copytree so that we can
# continue with other files
try:
shutil.copystat(source, target)
except WindowsError:
# can't copy file access times on Windows
pass
except OSError as why:
errors.extend((source, target, str(why)))
#
#
# def test(copyinfo):
#
# if copyinfo['idx']:
# print 'change file'
# else:
# print 'keep going'
# print copyinfo
# return copyinfo
#
# def copyfile( source,target,callback,length=10485760):
# # def copyfile( source,target,callback,length=500):
# '''
# :param source:
# :param target:
# :param callback:
# :param length:
# :return:
# '''
#
# """copy data from file-like object source to file-like object target"""
# source = unicode(source)
# target = unicode(target)
# if not os.path.exists(os.path.dirname(target)):
# os.makedirs(unicode(os.path.dirname(target)))
# fsrc = file(source, 'rb')
# fdst = file(target, 'wb')
# copyedsize = 0
# filesize = os.path.getsize(source)
# copyinfo = {}
#
# try:
# while 1:
#
# copyinfo['progressValue'] = 0
# copyinfo['filename'] = target
# copyinfo['idx'] = False
#
# buf = fsrc.read(length)
# if not buf:
# break
# copyedsize += length
# fdst.write(buf)
# if filesize > copyedsize:
# # progressValue = int(float(copyedsize) / float(filesize) * 100)
# copyinfo['progressValue'] = int(float(copyedsize) / float(filesize) * 100)
# elif filesize <= copyedsize:
# # progressValue = 100
# copyinfo['progressValue'] =100
# copyinfo['idx'] = True
#
# callback(copyinfo)
# fsrc.close()
# fdst.close()
#
#
# except:
# fsrc.close()
# fdst.close()
#
#
# # #
# # source = "D:/temp/tree/arrow.png"
# # target = "D:/temp/arrow.png"
# # copyfile(source,target,test)
#
# def copytree(source, target, symlinks=False, ignore=None):
# source = unicode(source)
# target = unicode(target)
# names = os.listdir(source)
# file_counter = sum([len(files) for r, d, files in os.walk(source)])
# copyed = 0
#
# if ignore is not None:
# ignored_names = ignore(source, names)
# else:
# ignored_names = set()
# if not os.path.exists(target):
# os.makedirs(target)
#
# errors = []
# for name in names:
# if name in ignored_names:
# continue
# srcname = os.path.join(source, name)
# dstname = os.path.join(target, name)
# try:
# if symlinks and os.path.islink(srcname):
# linkto = os.readlink(srcname)
# os.symlink(linkto, dstname)
# elif os.path.isdir(srcname):
# copytree(srcname, dstname, symlinks, ignore)
# else:
# copyfile(srcname, dstname, test)
# copyed += 1
#
# # XXX What about devices, sockets etc.?
# except (IOError, os.error) as why:
# errors.append((srcname, dstname, str(why)))
# # catch the Error from the recursive copytree so that we can
# # continue with other files
#
# try:
# shutil.copystat(source, target)
# except WindowsError:
# # can't copy file access times on Windows
# pass
# except OSError as why:
# errors.extend((source, target, str(why)))
#
#
# copytree("D:\\tmp\\wormholeTest","D:\\tmp\\pub_core2")