This repository was archived by the owner on Feb 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
347 lines (278 loc) · 12.1 KB
/
app.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# -----------------------------------------------------------------------------
# Copyright (c) 2019 Brennan Goewert
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# -----------------------------------------------------------------------------
import os
import tkinter as tk
import tkinter.filedialog as tkFileDialog
import tkinter.simpledialog as tkSimpleDialog
import logging
import sys
import ctypes
import platform
import time
import subprocess
import copyuserfiles as cuf
from hostnameselect import HostnameSelect
from usernameselect import UsernameSelect
# tkinter root vars
root = tk.Tk()
root.title('Copy User Files')
root.geometry('720x540')
root.minsize(300, 300)
root.resizable(True, True)
root.focus()
# tkinter string values
str_src_dir = tk.StringVar()
str_dest_dir = tk.StringVar()
str_aduser = tk.StringVar()
str_username = tk.StringVar()
str_hostname = tk.StringVar()
str_docs_loc = tk.StringVar()
# tkinter integers and booleans
bool_copy_downloads = tk.BooleanVar()
bool_documents_loc = tk.BooleanVar()
# ldap vars
host = HostnameSelect(root)
# Checks to see if user is administrator
def is_admin():
logging.info('Checking user privledges...')
try:
# Requests administrator permission for the python script
logging.info('Checking if user is admin...')
return ctypes.windll.shell32.IsUserAnAdmin()
except:
logging.info('User is not admin!')
return False
def cmd_select_dir(str_var):
""" Opens a file dialog to select a source directory."""
str_var.set(tkFileDialog.askdirectory())
def cmd_get_listselect(event):
w = event.widget
index = int(w.curselection()[0])
value = w.get(index)
return w.selection_set(0)
def cmd_select_hostname(frame):
host.get_name(frame, str_hostname)
def cmd_select_username(frame):
UsernameSelect(frame, str_username, str_hostname.get())
def cmd_start():
""" try:
start = subprocess.call(['py', 'copyuserfiles.py',
'-u', os.path.basename(str_src_dir.get()),
'-d', str_dest_dir.get(),
'-H', str_hostname.get(),
'-D', str_docs_loc.get()])
except:
logging.exception('Unable to run subprocess') """
cuf.copyuserfiles(str_dest_dir.get(),
str_src_dir.get(),
str_username.get(),
str_hostname.get())
def dir_select_label_group(frame):
# Directory selection group
grp_dirs = tk.LabelFrame(frame)
grp_dirs.grid(row=0, column=0, pady=(0, 5), sticky='we')
grp_dirs.grid_columnconfigure(2, weight=1)
# Remote hostname entry
rm_host_label = tk.Label(grp_dirs, text='Remote Hostname: ', )
rm_host_entry = tk.Entry(grp_dirs, textvariable=str_hostname)
rm_host_btn = tk.Button(grp_dirs,
text='Select Host',
command=lambda: cmd_select_hostname(frame))
rm_host_label.grid(row=0, column=1, sticky='e')
rm_host_entry.grid(row=0, column=2, sticky='we')
rm_host_btn.grid(row=0, column=0, sticky='we')
# Username
username_label = tk.Label(grp_dirs, text='Username: ', )
username_entry = tk.Entry(grp_dirs, textvariable=str_username)
username_btn = tk.Button(grp_dirs,
text='Select Username',
command=lambda: cmd_select_username(frame))
username_label.grid(row=1, column=1, sticky='e')
username_entry.grid(row=1, column=2, columnspan=2,
sticky='we', padx=(0, 5))
username_btn.grid(row=1, column=0, sticky='we')
# Source directory
src_dir_label = tk.Label(grp_dirs, text='Source Directory: ', )
src_dir_entry = tk.Entry(grp_dirs, textvariable=str_src_dir)
src_dir_btn = tk.Button(grp_dirs,
text='Select Source',
command=lambda: cmd_select_dir(str_src_dir))
src_dir_label.grid(row=2, column=1, sticky='e')
src_dir_entry.grid(row=2, column=2, columnspan=2, sticky='we', padx=(0, 5))
src_dir_btn.grid(row=2, column=0, sticky='we')
# Destination directory
dest_dir_label = tk.Label(grp_dirs, text='Destination Directory: ', )
dest_dir_entry = tk.Entry(grp_dirs, textvariable=str_dest_dir)
dest_dir_btn = tk.Button(grp_dirs,
text='Select Destintation',
command=lambda: cmd_select_dir(str_dest_dir))
dest_dir_label.grid(row=3, column=1, sticky='e')
dest_dir_entry.grid(row=3, column=2, columnspan=2, sticky='we',
padx=(0, 5))
dest_dir_btn.grid(row=3, column=0, sticky='we')
def remote_actions_label_group(frame):
# Remote actions frame
grp_remote = tk.LabelFrame(frame)
grp_remote.grid(row=1, column=0, pady=(0, 5), sticky='we')
grp_remote.columnconfigure(0, weight=1)
# Remote actions sub-frame
grp_remote_actions = tk.LabelFrame(grp_remote)
grp_remote_actions.grid(row=1, column=0, pady=(0, 5), sticky='we')
grp_remote_actions.columnconfigure(0, weight=0)
grp_remote_actions.columnconfigure(1, weight=1)
grp_remote_actions.config(bd=0)
# Remote documents folder location checkbox
chk_documents_loc = tk.Checkbutton(grp_remote,
text='Set remote documents folder' +
' location',
variable=bool_documents_loc,
command=lambda:
grp_remote_actions.grid() if
bool_documents_loc.get() else
grp_remote_actions.grid_remove())
chk_documents_loc.grid(row=0,
column=0,
sticky='w',
padx=(1, 15))
# Remote hostname entry
"""
rm_host_label = tk.Label(grp_remote_actions, text='Remote Hostname: ', )
rm_host_entry = tk.Entry(grp_remote_actions, textvariable=str_hostname)
rm_host_btn = tk.Button(grp_remote_actions,
text='Remote Hosts',
command=lambda: cmd_select_hostname(frame))
rm_host_label.grid(row=0, column=0, sticky='e')
rm_host_entry.grid(row=0, column=1, sticky='we')
rm_host_btn.grid(row=0, column=2, sticky='we')
"""
# Remote documents folder target location entry
rm_docs_label = tk.Label(grp_remote_actions, text='Documents ' +
'Target Location: ', )
rm_docs_entry = tk.Entry(grp_remote_actions, textvariable=str_docs_loc)
rm_docs_label.grid(row=1, column=0, sticky='e')
rm_docs_entry.grid(row=1, column=1, sticky='we')
grp_remote_actions.grid_remove()
def action_label_group(frame):
# Action button group
grp_actions = tk.LabelFrame(frame)
grp_actions.grid(row=2, column=0, pady=(0, 5), sticky='we')
# Start button
btn_start = tk.Button(grp_actions,
text='Start',
bg='#209920',
width=5)
btn_start.bind('<Button-1>',
lambda e: cmd_start())
btn_start.grid(row=0, column=0, sticky='we')
# Stop button
btn_stop = tk.Button(grp_actions,
text='Stop',
bg='#aa2020',
width=5,
command=cuf.stop)
btn_stop.grid(row=0, column=1, sticky='we')
# Copy Downloads checkbox
chk_copy_downloads = tk.Checkbutton(grp_actions,
text='Copy Downloads Folder (Limited' +
' to 50 most recent files)',
variable=bool_copy_downloads)
chk_copy_downloads.grid(row=0, column=2, sticky='w',
padx=(15, 15))
def header():
""" Header frame """
fra_header = tk.Frame(root)
fra_header.grid_columnconfigure(0, weight=1)
fra_header.grid_rowconfigure(0, weight=1)
dir_select_label_group(fra_header)
remote_actions_label_group(fra_header)
action_label_group(fra_header)
# Pack all the widgets
fra_header.pack(side='top', padx=10, pady=10, fill='x')
def body():
""" Body frame """
fra_body = tk.Frame(root)
fra_body.grid_columnconfigure(0, weight=1)
fra_body.grid_rowconfigure(0, weight=1)
txt_results = tk.Text(fra_body, width=20, height=5)
txt_results_scroll = tk.Scrollbar(fra_body,
orient='vertical',
command=txt_results.yview)
txt_results.config(state='disabled', yscrollcommand=txt_results_scroll.set)
txt_results.grid(row=0, column=0, sticky='nswe')
# Pack all the widgets
fra_body.pack(side='top', fill='both', expand=True)
def footer():
""" Footer frame """
fra_footer = tk.Frame(root, height=15)
fra_footer.grid_columnconfigure(0, weight=1)
fra_footer.grid_rowconfigure(0, weight=1)
lbl_version = tk.Label(fra_footer, text='Ver: ' +
cuf.__version__)
lbl_version.grid(row=0, column=0)
# Pack all the widgets
fra_footer.pack(side='bottom')
def check_python():
if sys.version_info[0] is 3:
logging.info('Running correct version of python!')
else:
logging.warning('Running older version of python!')
logging.warning('Some features might not work with older versions!')
def init():
logging.info('Started application at: ' + time.ctime())
logging.info('-===========- [ INIT ] -===========-')
logging.info('-==- [ SYS INFO ] -==-')
logging.info('system : ' + platform.system())
logging.info('release : ' + platform.release())
logging.info('version : ' + platform.version())
logging.info('node : ' + platform.node())
logging.info('machine : ' + platform.machine())
logging.info('processor : ' + platform.processor())
logging.info('-==- [ SYS INFO ] -==-')
logging.info('-==- [ APP INFO ] -==-')
logging.info('python : ' + platform.python_version())
logging.info('version : ' + cuf.__version__)
logging.info('-==- [ APP INFO ] -==-')
check_python()
if is_admin():
logging.info('User is admin! Proceeding through the application...')
logging.info('-===========- [ INIT ] -===========-')
main()
else:
logging.error('User is not admin! Prompting UAC elevation...')
logging.error('This will restart the application!')
logging.error('Stopped application at: ' + time.ctime())
logging.info('-===========- [ INIT ] -===========-')
ctypes.windll.shell32.ShellExecuteW(None, 'runas', sys.executable,
__file__, None, 1)
def app():
""" The main UI and application """
header()
logging.info('Header created!')
body()
logging.info('Body created!')
footer()
logging.info('Footer created!')
def main():
"""Main guts for the application and the process that keeps it alive"""
logging.info('Starting GUI...')
app()
logging.info('GUI started!')
root.mainloop()
if __name__ == "__main__":
init()