-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtkFs.py
More file actions
221 lines (173 loc) · 7.72 KB
/
tkFs.py
File metadata and controls
221 lines (173 loc) · 7.72 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
"""
------------------------- LICENCE INFORMATION -------------------------------
This file is part of Toonkit Module Lite, Python Maya library and module.
Authors : Cyril GIBAUD - Toonkit, Stephane Bonnot - Parallel Dev
Copyright (C) 2014-2017 Toonkit
http://toonkit-studio.com/
Toonkit Module Lite is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Toonkit Module Lite 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Toonkit Module Lite. If not, see <http://www.gnu.org/licenses/>
-------------------------------------------------------------------------------
"""
__author__ = "Cyril GIBAUD - Toonkit"
from subprocess import call
import os
import sys
import shutil
from . import tkCore as tc
from . import tkLogger
from .tkProjects import tkContext as tkcx
@tc.verbosed
def makedirs(inPath):
"""Create directory structure is it does not exists already (can take a file path)"""
#Manage case where a filename is given
dirPath, filePath = os.path.split(inPath)
if os.extsep in filePath:
inPath = dirPath
try:
os.makedirs(inPath)
except OSError:
if not os.path.isdir(inPath):
raise
@tc.verbosed
def copy(inInPath, inOutPath=None, inOutDir=None, inUseRobocopy=True):
"""Copy file or directory"""
assert not inOutPath is None or not inOutDir is None, "Output path and output directory cannot both be None !"
if os.path.isfile(inInPath):
if inOutPath is None:
inOutPath = os.join(inOutDir, os.path.basename(filePath))
#makedirs(inOutPath)
if inUseRobocopy:
inInFolderPath, inInFileName = os.path.split(inInPath)
inOutFolderPath, inOutFileName = os.path.split(inOutPath)
call(["robocopy", inInFolderPath, inOutFolderPath, inInFileName])
if inInFileName != inOutFileName:
notRenamedPath = os.path.join(inOutFolderPath, inInFileName)
os.rename(notRenamedPath, inOutPath)
else:
shutil.copy2(inInPath, inOutPath)
return inOutPath
if os.path.isdir(inInPath):
outDir = inOutPath or inOutDir
shutil.copytree(inInPath, outDir)
return outDir
else:
raise IOError("copy : File or directory '{0}' does not exists !".format(inInPath))
return None
@tc.verbosed
def copyTranslated( inSourcePatterns, inDestinationPatterns, inFileList=None, inMove=False, inUseRobocopy=True,
inAddVariables=None, inVariablesTranslator=None, inAllowDifferent=None, inUseDifferent=False):
"""Copy or move file(s) from a hierarchy pattern to another"""
if not isinstance(inSourcePatterns, dict):
inSourcePatterns = {"Unknown":inSourcePatterns}
if not isinstance(inDestinationPatterns, dict):
inDestinationPatterns = {"Unknown":inDestinationPatterns}
results = []
if inFileList is None:
inFileList = [f[0] for f in tkcx.collectPath(inSourcePattern[0])]
elif not isinstance(inFileList, (list, tuple)):
inFileList = (inFileList,)
for srcFile in inFileList:
if not os.path.isfile(srcFile):
tkLogger.debug("File '{}' does not exists, skip...".format(srcFile))
continue
matched = True
for fileType, pattern in inSourcePatterns.items():
variables = {}
if tkcx.match(pattern, srcFile, variables):
matched = True
destinations = inDestinationPatterns[fileType]
if not isinstance(destinations, (list, tuple)):
destinations = (destinations, )
for destination in destinations:
destPath = tkcx.translate( srcFile, pattern, destination, inAcceptUndefinedResults=True,
inAddVariables=inAddVariables, inVariablesTranslator=inVariablesTranslator, inAllowDifferent=inAllowDifferent, inUseDifferent=inUseDifferent)
if not destPath is None:
results.append(destPath)
if inMove and destination == destinations[-1]:
if inUseRobocopy and srcFile[:3] != destPath[:3]:
inInFolderPath, inInFileName = os.path.split(srcFile)
inOutFolderPath, inOutFileName = os.path.split(destPath)
call(["robocopy", inInFolderPath, inOutFolderPath, inInFileName])
os.remove(srcFile)
else:
shutil.move(srcFile, destPath)
tkLogger.debug("copyTranslated : File '{0}' moved to '{1}'".format(srcFile, destPath))
else:
copy(srcFile, destPath, inUseRobocopy=inUseRobocopy)
tkLogger.debug("copyTranslated : File '{0}' copied to '{1}'".format(srcFile, destPath))
break
if not matched:
tkLogger.warning("File '{}' does not match any patterns !".format(srcFile))
return results
@tc.verbosed
def cleanPycs(inPath, inRecursive=True, inDryRun=False):
"""Delete '.pyc's if the corresponding '.py' exists"""
deletedFiles = []
if os.path.isfile(inPath):
pass
else:
for elem in os.listdir(inPath):
elemPath = os.path.join(inPath, elem)
if os.path.isfile(elemPath):
if elemPath.endswith(".pyc"):
if os.path.isfile(elemPath.replace(".pyc", ".py")):
deletedFiles.append(elemPath)
if not inDryRun:
os.remove(elemPath)
tkLogger.debug("{}Removed {}".format("DRYRUN " if inDryRun else "", elem, elemPath))
else:
tkLogger.warning("WARNING : {} have no '.py' equivalent ({}) !".format(elem, elemPath))
elif inRecursive:
cleanPycs(elemPath, inRecursive=True)
return deletedFiles
@tc.verbosed
def getModuleFromPath(inPath):
mod = None
if inPath.endswith(".py"):
if os.path.isfile(inPath):
inPath = inPath[:-3]
else:
raise Exception("The file {} doesn't exist !".format(inPath))
else:
if not os.path.isfile(inPath + ".py"):
raise Exception("The file {} doesn't exist !".format(inPath))
dirName = os.path.dirname(inPath)
if not dirName in sys.path:
sys.path.append(dirName)
moduleName = os.path.split(inPath)[-1]
if sys.version_info >= (2,7):
import importlib
from imp import reload
try:
mod = importlib.import_module(moduleName)
reload(mod)
except Exception as e:
tkLogger.error(str(e))
raise e
else:
try:
mod = __import__(moduleName)
except:
tkLogger.error(str(e))
raise e
if dirName in sys.path:
sys.path.remove(dirName)
return mod
@tc.verbosed
def getFileContents(inPath, readlignes=False):
content = None
if os.path.isfile(inPath):
with open(inPath, "r") as file:
if readlignes:
content = [x.rstrip(os.linesep) for x in file.readlines()]
else:
content = file.read()
return content