-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcopy_bot.py
executable file
·401 lines (327 loc) · 14.1 KB
/
copy_bot.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim: set expandtab tabstop=4 shiftwidth=4 :
# copy_bot.py - Bot written in Python that copies one or more files from one
# location to another on the host the bot is running on. This is a tool bot,
# the uses of which are left up to the user.
#
# This is part of the Exocortex Halo project
# (https://github.com/virtadpt/exocortex-halo/).
# By: The Doctor <drwho at virtadpt dot net>
# 0x807B17C1 / 7960 1CDC 85C9 0B63 8D9F DD89 3BD8 FF2B 807B 17C1
# License: GPLv3
# v2.1 - Reworked the startup logic so that being unable to immediately
# connect to either the message bus or the intended service is a
# terminal state. Instead, it loops and sleeps until it connects and
# alerts the user appropriately.
# - Changed logger.warn() to logger.warning().
# v2.0 - Ported to Python 3.
# v1.0 - Initial release.
# TO-DO:
# - Refactor the bot to split out the file copying stuff.
# - Figure out how to specify a destination filename for single file copy.
# That take a few more neurons than I have online at the moment.
# Load modules.
import argparse
import configparser
import json
import logging
import os
import os.path
import requests
import shutil
import sys
import time
import parser
# Global variables.
# Handle to a logging object.
logger = ""
# Path to and name of the configuration file.
config_file = ""
# Loglevel for the bot.
loglevel = logging.INFO
# The "http://system:port/" part of the message queue URL.
server = ""
# URL to the message queue to take marching orders from.
message_queue = ""
# The name the search bot will respond to. The idea is, this bot can be
# instantiated any number of times with different config files to use
# different search engines on different networks.
bot_name = ""
# How often to poll the message queues for orders.
polling_time = 30
# Handle to a copy request from the user.
copy_request = None
# Functions.
# set_loglevel(): Turn a string into a numerical value which Python's logging
# module can use because.
def set_loglevel(loglevel):
if loglevel == "critical":
return 50
if loglevel == "error":
return 40
if loglevel == "warning":
return 30
if loglevel == "info":
return 20
if loglevel == "debug":
return 10
if loglevel == "notset":
return 0
# normalize_file_path(): Normalizes a full path in the file system. Takes one
# argument, a string representing a full or partial path to a file. Returns
# a string representing a fully normalized path to a file.
def normalize_file_path(file):
logger.debug("Entered function normalize_file_path().")
logger.debug("Value of file: " + str(file))
norm_path = os.path.expanduser(file)
norm_path = os.path.abspath(norm_path)
norm_path = os.path.normpath(norm_path)
logger.debug("Normalized file path: " + str(norm_path))
return norm_path
# ensure_exists(): Helper function that ensure that a file or directory
# actually exists. Returns a True or False.
def ensure_exists(file):
logger.debug("Entered function ensure_exists().")
if not os.path.exists(file):
logger.debug("The file path " + str(file) + " does not exist.")
return False
else:
logger.debug("The file path " + str(file) + " exists.")
return True
# ensure_readable(): Helper function that ensure that a file or directory is
# readable (has the +r bit). Returns a True or False.
def ensure_readable(file):
logger.debug("Entered function ensure_readable().")
if not os.access(file, os.R_OK):
logger.debug("The file path " + str(file) + " cannot be read.")
return False
else:
logger.debug("The file path " + str(file) + " is readable.")
return True
# ensure_writable(): Helper function that ensure that a file or directory is
# writable (has the +2 bit). Returns a True or False.
def ensure_writable(file):
logger.debug("Entered function ensure_writable().")
if not os.access(file, os.W_OK):
logger.debug("The file path " + str(file) + " cannot be written to.")
return False
else:
logger.debug("The file path " + str(file) + " is writable.")
return True
# copy_files(): Function that takes as its argument a hash table containing two
# filespecs, one a set of one or more files to copy (or a directory to copy
# the contents of), the other a directory to copy them into. Returns
# a message to the user.
def copy_files(filespecs):
logger.debug("Entered function copy_files().")
# Message for the user.
message = ""
# List of files in the source directory to copy.
source_files = []
# List of files that could not be copied.
uncopied_files = []
# Normalize the file paths so they are internally consistent.
source_path = normalize_file_path(filespecs['from'])
destination_path = normalize_file_path(filespecs['to'])
# Ensure the source filepath exists.
if not ensure_exists(source_path):
message = "ERROR: The source path " + str(source_path) + " does not exist. I can't do anything."
return message
# Ensure the source can be read. Bounce if it isn't.
if not ensure_readable(source_path):
message = "ERROR: The source path " + str(source_path) + " is not readable."
return message
# Ensure the destination directory exists.
if not ensure_exists(destination_path):
message = "ERROR: The destination path " + str(destination_path) + " does not exist. I can't do anything."
return message
# Ensure that the destination is writable. Note that it doesn't have to be
# readable. Bounce if it isn't.
if not ensure_writable(destination_path):
message = "ERROR: The destination path " + str(destination_path) + " cannot be written to."
return message
# Build a list of one or more files to copy in the source directory.
if os.path.isfile(source_path):
source_files.append(source_path)
else:
source_files = os.listdir(source_path)
# Roll through the list of files, test them, and copy them. If they can't
# be copied push them onto the list of files that errored out. We don't
# test if the files exist because we pulled their filenames out of the
# directory listing.
for file in source_files:
source_file = os.path.join(source_path, file)
if not ensure_readable(source_file):
uncopied_files.append(file)
continue
try:
logger.debug("Attempting to copy " + str(file) + " to " + str(destination_path) + ".")
shutil.copy2(source_file, destination_path)
except:
uncopied_files.append(file)
# Build the message to return to the user.
message = "Contents of directory " + source_path + " are as copied as I can make them."
if uncopied_files:
message = message + "\nI was unable to copy the following files:\n"
message = message + str(uncopied_files)
logger.debug("Files that could not be copied: " + str(uncopied_files))
# Return the message.
return message
# send_message_to_user(): Function that does the work of sending messages back
# to the user by way of the XMPP bridge. Takes one argument, the message to
# send to the user. Returns a True or False which delineates whether or not
# it worked.
def send_message_to_user(message):
# Headers the XMPP bridge looks for for the message to be valid.
headers = {"Content-type": "application/json"}
# Set up a hash table of stuff that is used to build the HTTP request to
# the XMPP bridge.
reply = {}
reply["name"] = bot_name
reply["reply"] = message
# Send an HTTP request to the XMPP bridge containing the message for the
# user.
request = requests.put(server + "replies", headers=headers,
data=json.dumps(reply))
# online_help(): Function that returns text - online help - to the user. Takes
# no arguments, returns a complex string.
def online_help():
logger.debug("Entered the function online_help().")
message = "My name is " + bot_name + " and I am an instance of " + sys.argv[0] + ".\n"
message = message + """
I am designed to remotely copy one or more files from one location on the hot I am running on to another location on the host. When specifying files, please ensure that you specify as full a path as possible to the source file, because this bot defaults to its current working directory, which may not be what you want. This is a potentially useful bot if used in a clever fashion. The interactive commands I currently suppot are:
help - Display this online help.
Individual files:
copy /path/to/foo.txt /another/path/to
copy /path/to/foo.txt to /another/path/to
copy /path/to/foo.txt into /another/path/to
copy from /path/to/foo.txt to /another/path/to
copy from /path/to/foo.txt into /another/path/to
Contents of a directory:
copy /path/to/ to /another/path
copy /path/to into /another/path/to
copy everything in /path/to/foo to /another/path
copy everything in /path/to/foo into /another/path
copy * in /path/to/foo to /another/path
copy * in /path/to/foo into /another/path
copy all files in /path/to/foo to /another/path
copy all files in /path/to/foo into /another/path
"""
return message
# Core code...
# Set up the command line argument parser.
argparser = argparse.ArgumentParser(description="A toolbot that accepts two filespecs from the user, one for one or more files to copy from, the other a filename or destination directory to copy into.")
# Set the default config file and the option to set a new one.
argparser.add_argument("--config", action="store",
default="./copy_bot.conf")
# Loglevels: critical, error, warning, info, debug, notset.
argparser.add_argument("--loglevel", action="store",
help="Valid log levels: critical, error, warning, info, debug, notset. Defaults to info.")
# Time (in seconds) between polling the message queues.
argparser.add_argument("--polling", action="store", help="Default: 30 seconds")
# Parse the command line arguments.
args = argparser.parse_args()
if args.config:
config_file = args.config
# Read the options in the configuration file before processing overrides on the
# command line.
config = configparser.ConfigParser()
if not os.path.exists(config_file):
logging.error("Unable to find or open configuration file " +
config_file + ".")
sys.exit(1)
config.read(config_file)
# Get the URL of the message queue to contact.
server = config.get("DEFAULT", "queue")
# Get the names of the message queues to report to.
bot_name = config.get("DEFAULT", "bot_name")
# Construct the full message queue URL.
message_queue = server + bot_name
# Get the default loglevel of the bot.
config_log = config.get("DEFAULT", "loglevel").lower()
if config_log:
loglevel = set_loglevel(config_log)
# Set the number of seconds to wait in between polling runs on the message
# queues.
try:
polling_time = config.get("DEFAULT", "polling_time")
except:
# Nothing to do here, it's an optional configuration setting.
pass
# Set the loglevel from the override on the command line.
if args.loglevel:
loglevel = set_loglevel(args.loglevel.lower())
# Configure the logger.
logging.basicConfig(level=loglevel, format="%(levelname)s: %(message)s")
logger = logging.getLogger(__name__)
# Set the message queue polling time from override on the command line.
if args.polling:
polling_time = args.polling
# Debugging output, if required.
logger.info("Everything is set up.")
logger.debug("Values of configuration variables as of right now:")
logger.debug("Configuration file: " + config_file)
logger.debug("Server to report to: " + server)
logger.debug("Message queue to report to: " + message_queue)
logger.debug("Bot name to respond to search requests with: " + bot_name)
logger.debug("Time in seconds for polling the message queue: " +
str(polling_time))
# Try to contact the XMPP bridge. Keep trying until you reach it or the
# system shuts down.
logger.info("Trying to contact XMPP message bridge...")
while True:
try:
send_message_to_user(bot_name + " now online.")
break
except:
logger.warning("Unable to reach message bus. Going to try again in %s seconds." % polling_time)
time.sleep(float(polling_time))
# Go into a loop in which the bot polls the configured message queue with each
# of its configured names to see if it has any download requests waiting for it.
logger.debug("Entering main loop to handle requests.")
while True:
command = None
# Check the message queue for download requests.
try:
logger.debug("Contacting message queue: " + message_queue)
request = requests.get(message_queue)
except:
logging.warning("Connection attempt to message queue timed out or failed. Going back to sleep to try again later.")
time.sleep(float(polling_time))
continue
# Test the HTTP response code.
# Success.
if request.status_code == 200:
logger.debug("Message queue " + bot_name + " found.")
# Extract the command.
command = json.loads(request.text)
command = command["command"]
# Parse the command.
command = parser.parse_command(command)
logger.debug("Parsed command: " + str(command))
# If the command was empty, return to the top of the loop.
if not command:
logger.debug("Empty command.")
time.sleep(float(polling_time))
continue
if command == "unknown":
message = "I didn't recognize that command."
send_message_to_user(message)
continue
# If the user is requesting online help...
if command == "help":
send_message_to_user(online_help())
continue
# If the user is requesting a multiple file copy.
if command['type'] == "copy":
message = copy_files(command)
send_message_to_user(message)
continue
# Message queue not found.
if request.status_code == 404:
logger.info("Message queue " + bot_name + " does not exist.")
# Sleep for the configured amount of time.
time.sleep(float(polling_time))
# Fin.
sys.exit(0)