-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDirectoryTools.py
More file actions
154 lines (118 loc) · 4.52 KB
/
DirectoryTools.py
File metadata and controls
154 lines (118 loc) · 4.52 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
# DirectoryTools for Flame v1.0
# Copyright (c) 2019 Bob Maple (bobm-matchbox [at] idolum.com)
#
# For Flame 2020 and above - place in /opt/Autodesk/shared/python/
#
# Adds a submenu to the MediaHub context menu in Files mode to tar and zip
# a selected directory or directories (makes separate files for each)
#
# Select a directory in the browser and right-click to access
##### First some Qt helper-monkeys
#####
def ask_yesno( dlg_msg, dlg_title ) :
from PySide2.QtWidgets import QMessageBox
qtMsgBox = QMessageBox()
qtMsgBox.setWindowTitle( dlg_title )
qtMsgBox.setText( dlg_msg )
qtMsgBox.setStandardButtons( QMessageBox.Yes | QMessageBox.Cancel )
qtMsgBox.setDefaultButton( QMessageBox.Yes )
res = qtMsgBox.exec_()
if res == QMessageBox.Cancel :
return( False )
else:
return( True )
#
def ask_password() :
from PySide2 import QtWidgets, QtGui
encPW, okBut = QtWidgets.QInputDialog.getText( None, "Password", "Encryption Password (leave blank or Cancel for none):" )
if okBut and encPW:
return( True, encPW )
else:
return( False, False )
##### Main Flame hook handler
#####
def get_mediahub_files_custom_ui_actions():
import os, subprocess, time, tempfile
def menu_enabled(sel) :
if os.path.isdir( sel[0].path ) :
return True
else :
return False
#
def tardir_go(sel) :
for curitem in sel :
if os.path.isdir( curitem.path ) :
archive_dest, archive_dir, archive_file, archive_pathname = make_paths( curitem, ".tar" )
do_it = True
if ask_yesno( "Create " + archive_pathname + " ?", "tar directory" ) :
if os.path.isfile( archive_pathname ) :
if not ask_yesno( "Overwrite " + archive_pathname + " ?", archive_file + " exists" ) :
do_it = False
else:
do_it = False
if do_it :
tmpFP, tmpName = tempfile.mkstemp( ".sh", "flametar" )
os.write( tmpFP, '#!/bin/sh\n' )
os.write( tmpFP, 'cd "' + archive_dest + '"\n' )
os.write( tmpFP, 'tar -cf "' + archive_file + '_busy" "' + archive_dir + '"\n' )
os.write( tmpFP, 'tar -tf "' + archive_file + '_busy" > "' + archive_file + '.list"\n' )
os.write( tmpFP, 'mv "' + archive_file + '_busy" "' + archive_file + '"\n' )
os.write( tmpFP, 'rm "' + tmpName + '"\n' )
os.close( tmpFP )
tar_cmd = [ "/bin/sh", tmpName ]
rc = subprocess.Popen( tar_cmd )
#
def zipdir_go(sel) :
for curitem in sel :
if os.path.isdir( curitem.path ) :
archive_dest, archive_dir, archive_file, archive_pathname = make_paths( curitem, ".zip" )
do_it = True
if ask_yesno( "Create " + archive_pathname + " ?", "zip directory" ) :
if os.path.isfile( archive_pathname ) :
if not ask_yesno( "Overwrite " + archive_pathname + " ?", archive_file + " exists" ) :
do_it = False
else:
do_it = False
if do_it :
doPW, usePW = ask_password()
tmpFP, tmpName = tempfile.mkstemp( ".sh", "flamezip" )
os.write( tmpFP, '#!/bin/sh\n' )
os.write( tmpFP, 'cd "' + archive_dest + '"\n' )
os.write( tmpFP, 'zip -r -q ' + (('-e -P "' + usePW + '" ') if doPW else '') )
os.write( tmpFP, '"' + archive_file + '_busy" "' + archive_dir + '"\n' )
os.write( tmpFP, 'mv "' + archive_file + '_busy" "' + archive_file + '"\n' )
os.write( tmpFP, 'rm "' + tmpName + '"\n' )
os.close( tmpFP )
zip_cmd = [ "/bin/sh", tmpName ]
rc = subprocess.Popen( zip_cmd )
#
def make_paths( item, extension ) :
# Takes a Flame object and returns a tuple:
# destination path, directory to archive, filename of archive, full pathname of archive
cur_dir, junk = os.path.split( item.path )
archive_dest, archive_dir = os.path.split( cur_dir )
archive_file = archive_dir + extension
archive_path = os.path.join( archive_dest, archive_file )
# print( "archive_dest: " + archive_dest )
# print( "archive_dir : " + archive_dir )
# print( "archive_file: " + archive_file )
# print( "archive_path: " + archive_path )
return( archive_dest, archive_dir, archive_file, archive_path )
#
return [
{
"name": "Directory Tools",
"actions": [
{
"name": "TAR Directory",
"isEnabled": menu_enabled,
"execute": tardir_go
},
{
"name": "ZIP Directory",
"isEnabled": menu_enabled,
"execute": zipdir_go
}
]
}
]