forked from 900supersport/rkKitchen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
246 lines (203 loc) · 7.91 KB
/
Copy pathutils.py
File metadata and controls
246 lines (203 loc) · 7.91 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/python
#
#
# FreakTabKitchen www.freaktab.com
#
# Copyright 2013 Brian Mahoney brian@mahoneybrian.wanadoo.co.uk
#
# <version>2.0.1</version>
#
############################################################################
#
# FreakTabKitchen 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 of the License, or
# (at your option) any later version.
#
# FreakTabKitchen 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 FreakTabKitchen. If not, see <http://www.gnu.org/licenses/>.
#
############################################################################
import os
import pickle
import logging
import traceback
import zipfile
import zlib
# try:
#
# except Exception as e:
# logerror('utils::mountsystem ' , e, 1)
#
def zipfolder(sourcefolder,zipfilename):
'''creates a zipfolder containing as an archive of sourcefolder
if there are no files to be archived then the zip will be removed
'''
try:
if os.path.isdir(sourcefolder):
if os.path.isfile(zipfilename):
mode = 'a'
else:
mode = 'w'
zf = zipfile.ZipFile(zipfilename,mode,compression=zipfile.ZIP_DEFLATED)
hasdata = recursive_zip(zf,sourcefolder)
zf.close()
if hasdata == 0 and mode =='w' :
os.system('rm ' + zipfilename)
except Exception as e:
logerror('utils::zipfolder ',e,1)
def recursive_zip(zipf, directory, folder = ""):
'''adds directory and all sub directories to the zipfile zipf
'''
try:
append = 0
for item in os.listdir(directory):
if os.path.isfile(os.path.join(directory, item)):
zipf.write(os.path.join(directory, item), os.path.join(folder, item))
append = 1
elif os.path.isdir(os.path.join(directory, item)):
if recursive_zip(zipf, os.path.join(directory, item), os.path.join(folder, item)) == 1:
append = 1
return append
except Exception as e:
logerror('utils::recursive_zip ',e,1)
def GetCWD():
return os.getcwd()
def CheckMakeFolders(folderlist):
'''check the existance of and make any missing folders in the input list
'''
try:
for dp in folderlist:
CheckMakeFolderW(dp,0)
except Exception as e:
logerror('utils::CheckMakeFolders ' , e, 1)
def CheckMakeFoldersRoot(folderlist):
'''check the existance of and make any missing folders as root in the input list
'''
try:
for dp in folderlist:
CheckMakeFolderW(dp,1)
except Exception as e:
logerror('utils::CheckMakeFoldersRoot ' , e, 1)
def CheckMakeFolder(dirpath):
'''Check for existance of the folder and create
if the folder specified by dirpath does not exist then create it.
NB if recursion used if a a nested folder specified so all
folders in the path hierarchy and checked and created'''
try:
CheckMakeFolderW(dirpath,0)
except Exception as e:
logerror('utils::CheckMakeFolder ' , e, 1)
def CheckMakeFolderW(dirpath,asroot):
'''Check for existance of the folder and create as root if specified
if the folder specified by dirpath does not exist then create it.
NB if recursion used if a a nested folder specified so all
folders in the path hierarchy and checked and created'''
logging.debug('CheckMakeFolder start :' + dirpath)
try:
if not os.path.isdir(dirpath):
logging.debug('Making Folder :' + dirpath)
logging.debug('as root :' + str(asroot))
if asroot ==1:
rt,pt = os.path.split(dirpath)
if len(rt)>0:
logging.info(rt)
CheckMakeFolderW(rt,asroot)
logging.info( 'sudo mkdir ' + pt)
os.system('sudo mkdir ' + dirpath)
else:
logging.debug('making with os.makedirs ' + dirpath)
os.makedirs(dirpath + '/')
logging.debug('Made Folder :' + dirpath)
except Exception as e:
logerror('utils::CheckMakeFolderW ' , e, 1)
def QueryPickleLoad(classvar, filepath):
'''load a class from a pickle file
'''
try:
logging.debug('Start QueryPickleLoad')
logging.debug('Filepath :' + filepath)
if os.path.exists(filepath):
logging.debug('file exists')
with open(filepath,'r') as f:
logging.debug('file opened')
classvar = pickle.load(f)
logging.debug('file read')
else:
logging.info('utils::QueryPickleLoad Filepath not found :' + filepath)
#raise IOError
except Exception as e:
logerror('utils::QueryPickleLoad ' , e, 1)
finally:
logging.debug('End QueryPickleLoad')
return classvar
def PickleIt(classvar, filepath):
'''Pickle a class to a file
'''
try:
with open(filepath,'w') as f:
pickle.dump(classvar,f)
except IOError as e:
print "I/O error({0}): {1}"#.format(e.errno, e.strerror)
except Exception as e:
logerror('utils::PickleIt ' , e, 1)
def apply_sed(sedfilepath,applytopath,openforreview):
'''apply the sed statements from file sedfilepath to the file applytopath'''
try:
path =os.path.expanduser(sedfilepath)
with open(path,'r') as f:
for line in f:
cl = line.strip();
args = cl.split(',')
if cl[:1] <> '#' and len(cl) > 0:
logging.debug( 'sudo sed -i ' + cl + ' ' + applytopath )
os.system('sudo sed -i ' + cl + ' ' + applytopath )
if openforreview ==1:
os.system('sudo gedit ' + applytopath)
except Exception as e:
logerror('utils::apply_sed ' , e, 1)
def umount(mountpoint):
'''unmount the specified filesystem'''
try:
systemstring = 'sudo umount ' + mountpoint
os.system(systemstring)
except Exception as e:
logerror('utils::umount ' , e, 1)
def finalisefilesystemimage(mount, image):
'''unmount and check a mounted filebased filesystem'''
try:
umount(mount)
checkfsimage(image)
except Exception as e:
logerror('utils::finalisefilesystemimage ' , e, 1)
def mountfileasfilesystem(mountfile, filesystemtype, mountpoint):
'''mount a files system to a mount point of the specifie type'''
logging.debug('Start mountfileasfilesystem')
try:
CheckMakeFolder(mountpoint)
systemstring = 'sudo mount -t ' + filesystemtype + ' -o loop ' + mountfile + ' ' + mountpoint
os.system(systemstring)
except Exception as e:
logerror('utils::mountfileasfilesystem ' , e, 1)
logging.debug('End mountfileasfilesystem')
def checkfsimage(image):
'''call e2fscheck on a file based filesystem'''
os.system('sudo e2fsck -y -v ' + image)
def logerror(errortext, myerror, raiseit):
try:
err = traceback.format_exc()
logging.debug( 'bm' + err + 'bm')
logging.error(errortext)
logging.error(myerror)
discard = myerror.handled
except AttributeError as ae:
logging.error(err)
myerror.handled = 1
finally:
if raiseit == 1:
raise myerror