I'm currently making a Disocrd bot, and one feature of it is a very rapid update (to minimise downtime). In order to do so, inside the reboot script we have code that checks if a usb is mounted, and if it is, the files on it need to be moved into the bots folder and in turn updates the bot.
async def cmd_restart(self, channel):
await self.safe_send_message(channel, ":wave:")
await self.disconnect_all_voice_clients()
if os.path.exists(/dev/[usb ID]/Update.zip):
zip_ref = zipfile.ZipFile(/dev/[usb ID]/Update.zip, 'r')
zip_ref.extractall(/root/MusicBot/musicbot)
zip_ref.close()
raise exceptions.RestartSignal
The issue is, as the bot has grown the files have migrated into multiple folders, and this script no longer does the job. We need the files to be extracted to different locations, depending on what they are called.
For example 'bot.py' would go into the musicbot folder, while config.ini would go into the config folder, and they would both replace their counterparts in the folders.
Because of the nature of the bot, no new files are ever going to be created using this, only existing files get replaced.
Forgive me for the rambling, It feels overly complicated to explain.
First, you need to get a list of all filenames. Then you can check those and put the files in their respective directories.
Should look something like this:
import os
import shutil
async def cmd_restart(self, channel):
await self.safe_send_message(channel, ":wave:")
await self.disconnect_all_voice_clients()
if os.path.exists("/dev/[usb ID]/Update.zip"):
zip_ref = zipfile.ZipFile("/dev/[usb ID]/Update.zip", 'r')
zip_ref.extractall("/root/MusicBot/musicbot/tmpExtract")
zip_ref.close()
files = os.listdir("/root/MusicBot/musicbot/tmpExtract") # get a list of all files that were in the zip
for filename in files: #check every file
if (filename == "meetsYourCondition.conf"):
shutil.move("/root/MusicBot/musicbot/tmpExtract/meetsYourCondition.conf", "/root/MusicBot/musicbot/DirectoryYouWantItToGoTo")
#elif (filename == "meetsYourNextCondition.conf"):
#...
# you get the idea
raise exceptions.RestartSignal