forked from BhallaLab/moose-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
140 lines (126 loc) · 3.76 KB
/
utils.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
# utils.py ---
#
# Filename: utils.py
# Description:
# Author:
# Maintainer:
# Created: Sat Sep 22 15:19:09 2012 (+0530)
# Version:
# Last-Updated: Wed Sep 26 16:35:38 2012 (+0530)
# By: subha
# Update #: 50
# URL:
# Keywords:
# Compatibility:
#
#
# Commentary:
#
#
#
#
# Change log:
#
#
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street, Fifth
# Floor, Boston, MA 02110-1301, USA.
#
#
# Code:
import shutil
import os
from PyQt4 import QtGui, QtCore
def resizeWidthToContents(lineEdit):
"""Resize a QLineEdit object to fit its content."""
text = lineEdit.text()
fm = lineEdit.fontMetrics()
w = fm.boundingRect(text).width()
lineEdit.resize(w, lineEdit.height())
def copyTree(src, dst, progressDialog=None):
"""Copy the contents of source directory recursively into
destination directory recursively. Display the progress in
progressDialog. This does not overwrite any existing files or
directories.
Parameters
----------
src: str
path of the source directory
dst: str
path of the destination directory
progressDialog: QProgressDialog
Qt object to display the progress of the copying.
Return
------
list containing all the errors encountered while copying.
"""
src = src.strip()
dst = dst.strip()
errors = []
if not os.access(src, os.R_OK + os.X_OK):
print 'Failed to access directory', src
return
print 'Copying %s to : %s' % (src, dst)
if not os.access(src, os.R_OK + os.X_OK):
try:
os.makedirs(dst)
except OSError, e:
# print e
errors.append(e)
totalsize = 0
for dirpath, dirnames, filenames in os.walk(src):
for fname in filenames:
srcname = os.path.join(dirpath, fname)
try:
totalsize += os.path.getsize(srcname)
except OSError, e:
# print e
errors.append(e)
if progressDialog:
progressDialog.setMaximum(totalsize)
size = 0
for dirpath, dirnames, filenames in os.walk(src):
dstdir = os.path.join(dst, dirpath[len(src)+1:])
# print 'Destination dir', dstdir, dirpath[len(src)+1:]
try:
os.makedirs(dstdir)
except OSError, e:
# print e
errors.append(e)
# print 'Copying files from %s to %s' % (dirpath, dstdir)
for fname in filenames:
srcname = os.path.join(dirpath, fname)
dstname = os.path.join(dstdir, fname)
# print 'Copying:', srcname, 'to', dstname
try:
shutil.copy2(srcname, dstname)
except IOError, e:
# print e
errors.append(e)
size += os.path.getsize(srcname)
if progressDialog:
progressDialog.setValue(size)
if progressDialog.wasCanceled():
return errors
else:
print 'Copied %d bytes of %d.' % (size, totalsize)
if progressDialog:
progressDialog.close()
else:
print 'Finished.'
return errors
#
# utils.py ends here