-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCopyFileDirectoryRm.py
167 lines (142 loc) · 5.6 KB
/
CopyFileDirectoryRm.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
import numpy as np
import sys, os, string, inspect, types, glob, fnmatch, re, copy, shutil
import signal, time, errno, socket, platform, tempfile, traceback, stat
import distutils.spawn, operator, tarfile
from optparse import OptionParser
import math
import json
import random
#-----------------------------------------------------------------------------
# makedir()
#-----------------------------------------------------------------------------
def makedir( path ):
if ( not os.path.exists( path ) ):
os.makedirs( path )
else:
if ( not os.path.isdir( path ) ):
rm( path )
os.makedirs( path )
#-----------------------------------------------------------------------------
# copyFile()
#-----------------------------------------------------------------------------
def copyFile( source,
dest,
symlinks=False,
copyAsSymlinks=False ):
if not os.path.exists( source ):
print ( source,
'does not exist' )
return
if os.path.islink( dest ):
os.remove( dest )
elif os.path.exists( dest ):
os.chmod( dest,
stat.S_IWRITE | stat.S_IREAD )
os.remove( dest )
else:
destDir = os.path.dirname( dest )
if not os.path.isdir( destDir ):
os.makedirs( destDir )
if symlinks or copyAsSymlinks:
if os.path.islink( source ) and not os.path.isabs( os.readlink( source ) ):
os.symlink( os.readlink( source ),
dest )
elif copyAsSymlinks and systemname != 'windows':
os.symlink( source,
dest )
else:
shutil.copy2( source,
dest )
else:
shutil.copy2( source,
dest )
#-----------------------------------------------------------------------------
# copyDirectory()
#-----------------------------------------------------------------------------
def copyDirectory( sourceDir, destinationDir,
symlinks = False,
copyAsSymlinks = False,
include = None,
exclude = None,
copyCallback = None,
keepExistingDirs = True ):
if not os.path.exists( sourceDir ):
return
stack = os.listdir( sourceDir )
while stack:
relSource = stack.pop()
source = os.path.join( sourceDir,
relSource )
dest = os.path.join( destinationDir,
relSource )
if os.path.islink( source ) or not os.path.isdir( source ):
if ( include is not None and not include.match( relSource ) ) or \
( exclude is not None and exclude.match( relSource ) ):
continue
if copyCallback is None or copyCallback( source,
dest ):
copyFile( source,
dest,
symlinks=symlinks,
copyAsSymlinks=copyAsSymlinks )
elif os.path.isdir( source ):
if os.path.islink( dest ):
os.unlink( dest )
elif os.path.exists( dest ):
if not keepExistingDirs:
rm( dest )
else:
os.makedirs( dest )
stack += [os.path.join( relSource,
f ) for f in os.listdir( source )]
else:
stack += [os.path.join( relSource,
f ) for f in os.listdir( source )]
#-----------------------------------------------------------------------------
# rm()
#-----------------------------------------------------------------------------
def rm( *args ):
sources = []
for pattern in args:
sources += glob.glob( pattern )
sys.stdout.flush()
for path in sources:
if not os.path.islink( path ):
os.chmod( path,
'0777' )
if os.path.isdir( path ) and not os.path.islink( path ):
rm( os.path.join( path,
'*' ) )
os.rmdir( path )
else:
os.remove( path )
#-----------------------------------------------------------------------------
# runGinkgo()
#-----------------------------------------------------------------------------
def runGinkgo( parameterValues, outputWorkDirectory ):
parameterValues[ 'outputWorkDirectory' ] = outputWorkDirectory
makedir( outputWorkDirectory )
algorithmName = parameterValues[ '_algorithmName' ]
parameterFileName = os.path.join( outputWorkDirectory + os.sep + algorithmName + '.py' )
with open( parameterFileName,
'w' ) as f:
json.dump( parameterValues,
f,
sort_keys=True,
indent=2,
separators=( ',',
' : ' ) )
command = 'ginkgo ' + \
' --batch ' + \
' -p ' + parameterFileName + \
' -d single-threading'
print( command )
os.system( command )
#-----------------------------------------------------------------------------
# Directory FastScan Function
#-----------------------------------------------------------------------------
def fast_scandir(dirname):
subfolders= [f.path for f in os.scandir(dirname) if f.is_dir()]
for dirname in list(subfolders):
subfolders.extend(fast_scandir(dirname))
return subfolders