-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcollectFiles.py
247 lines (164 loc) · 6.87 KB
/
collectFiles.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
235
236
237
238
239
240
241
242
243
244
245
246
247
#This Source Code Form is subject to the terms of the Mozilla Public
#License, v. 2.0. If a copy of the MPL was not distributed with this
#file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#Created by Fabrice Fernandez on 19/06/2019.
import os
import string
import shutil
import subprocess
import NatronEngine
from NatronGui import *
# GATHERS COMP AND FILES IN ONE PLACE #
def collectFiles():
# get current Natron instance running in memory #
app = natron.getGuiInstance(0)
#################################################
# create dialog window #
dialog = app.createModalDialog()
# set dialog title #
dialog.setWindowTitle("Collect files")
# set dialog margins #
dialog.setContentsMargins(0, 0, 10, 10)
# set window size #
dialog.resize( 400, 100 )
# UI creation #
line01 = dialog.createStringParam("line01","")
line01.setType(NatronEngine.StringParam.TypeEnum.eStringTypeLabel)
line02 = dialog.createStringParam("line02","")
line02.setType(NatronEngine.StringParam.TypeEnum.eStringTypeLabel)
folderLocation = dialog.createPathParam("destFolder","Destination folder : ")
#################################################
# Refresh UI #
dialog.refreshUserParamsGUI()
if dialog.exec_():
# we grab current project name #
projectName = app.getProjectParam('projectName').get()
projectName = os.path.splitext(projectName)[0]
destinationFolder = folderLocation.getValue()
if destinationFolder == '' :
# Gives user's home directory
myUserPath = os.path.expanduser('~')
if destinationFolder != '' :
myUserPath = destinationFolder
# ---------------------------------------------------- #
# ---------------------- Windows --------------------- #
# ---------------------------------------------------- #
if natron.isWindows() == 1 :
# get diskCachePath from Preferences #
userDiskCachePath = NatronEngine.natron.getSettings().getParam('diskCachePath').get()
if destinationFolder == '' :
# build 'Localize' path #
if userDiskCachePath == '':
# if diskCachePath is empty (default) #
localizeFolderPath = str(myUserPath) + '\\AppData\\Local\\INRIA\\Natron\\Localize\\'
else :
# if diskCachePath is set to a custom folder #
localizeFolderPath = str(userDiskCachePath) + '\\Localize\\'
else :
localizeFolderPath = str(myUserPath) + '/'
# ---------------------------------------------------- #
# --------------------- Linux/OSX -------------------- #
# ---------------------------------------------------- #
if natron.isLinux() == 1 or natron.isMacOSX() == 1 :
# get diskCachePath from Preferences #
userDiskCachePath = NatronEngine.natron.getSettings().getParam('diskCachePath').get()
if destinationFolder == '' :
# build 'Localize' path #
if userDiskCachePath == '':
# if diskCachePath is empty (default) #
localizeFolderPath = str(myUserPath) + '/.cache/INRIA/Natron/Localize/'
else :
# if diskCachePath is set to a custom folder #
localizeFolderPath = str(userDiskCachePath) + '/Localize/'
else :
localizeFolderPath = str(myUserPath) + '/'
# check if output folder exists. If not, we create it #
if not os.path.exists(localizeFolderPath):
os.makedirs(localizeFolderPath)
# we select all nodes #
app.selectAllNodes()
# we get selected nodes #
selectedNodes = app.getSelectedNodes()
# we cycle all selected nodes #
for currentNode in selectedNodes:
# get current node ID #
nodeID = currentNode.getPluginID()
# if node is not a 'Read' node #
if nodeID == 'fr.inria.built-in.Read':
# we grab the complete file path #
fileCompletePath = currentNode.getParam('filename').get()
# we grab the file name only #
fileName = os.path.basename(fileCompletePath)
# we grab the file path #
filePath = fileCompletePath.replace(fileName,'')
# file name without the extension #
fileNameNoExt = os.path.splitext(fileName)[0]
# ------------------------- #
# IF FILE IS NOT A SEQUENCE #
# ------------------------- #
if '#' not in str(fileName):
# a created folder will be named as the file #
newFileFolder = str(localizeFolderPath) + str(projectName) + '_COLLECT' + '/Sources/' + str(fileNameNoExt)
# we create the folder #
if os.path.exists(newFileFolder):
pass
else:
os.makedirs(newFileFolder)
# we copy the file #
shutil.copy(fileCompletePath,newFileFolder)
# we get copied file path #
newPath = './Sources/'+ str(fileNameNoExt) + '/' + str(fileName)
# we grab the file path again #
fileCompletePath = currentNode.getParam('filename')
# we set the new file path #
fileCompletePath.set(newPath)
# --------------------- #
# IF FILE IS A SEQUENCE #
# --------------------- #
if '#' in str(fileName):
fileNameNoExt = fileNameNoExt.replace('#','')
fileNameNoExt = fileNameNoExt[:-1]
# a created folder will be named as the file #
newFileFolder = str(localizeFolderPath) + str(projectName) + '_COLLECT' + '/Sources/' + str(fileNameNoExt)
# we create the folder #
if os.path.exists(newFileFolder):
pass
else:
os.makedirs(newFileFolder)
os.chdir(filePath)
cwd = os.getcwd()
for basename in os.listdir(cwd):
if fileNameNoExt in basename:
shutil.copy(basename,newFileFolder)
# we get copied file path #
newPath = './Sources/'+ str(fileNameNoExt) + '/' + str(fileName)
# we grab the file path again #
fileCompletePath = currentNode.getParam('filename')
# we set the new file path #
fileCompletePath.set(newPath)
# we save the project #
rootFolder = str(localizeFolderPath) + str(projectName) + '_COLLECT' + '/'
if not os.path.exists(rootFolder):
os.makedirs(rootFolder)
newProjectName = str(projectName) + '_COLLECT'
os.chdir(rootFolder)
app.saveProjectAs(newProjectName)
# ---------------------------------------------------- #
# ---------------------- Windows --------------------- #
# ---------------------------------------------------- #
if natron.isWindows() == 1 :
os.chdir(rootFolder)
subprocess.Popen( ['explorer', '.'] , stdin = subprocess.PIPE, stdout = subprocess.PIPE)
# ---------------------------------------------------- #
# ----------------------- Linux ---------------------- #
# ---------------------------------------------------- #
if natron.isLinux() == 1 :
subprocess.Popen( ['nautilus', rootFolder] , stdin = subprocess.PIPE, stdout = subprocess.PIPE)
# ---------------------------------------------------- #
# ------------------------ OSX ----------------------- #
# ---------------------------------------------------- #
if natron.isMacOSX() == 1 :
subprocess.Popen( ['explorer', rootFolder] , stdin = subprocess.PIPE, stdout = subprocess.PIPE)
app.resetProject()
newNatronProject = rootFolder + newProjectName + '.ntp'
app.loadProject(newNatronProject)