Auto push to github

This commit is contained in:
Dustin Pianalto 2018-05-09 13:55:16 -08:00
parent ae90dcde84
commit b73bb47445
4 changed files with 89 additions and 30 deletions

View File

@ -2,15 +2,21 @@
<dictionary name="Dustin.Pianalto">
<words>
<w>aiohttp</w>
<w>aren</w>
<w>asctime</w>
<w>chans</w>
<w>coro</w>
<w>customsearch</w>
<w>dinos</w>
<w>emojis</w>
<w>exts</w>
<w>levelname</w>
<w>lockdown</w>
<w>msecs</w>
<w>shootergame</w>
<w>shootergamemode</w>
<w>singleplayer</w>
<w>strftime</w>
</words>
</dictionary>
</component>

View File

@ -112,3 +112,13 @@ def generate_dino_files(dino_data, directory):
int(dino['Dino Data']['DinoID2']))
with open(f'{directory}/{filename}', 'w') as f:
dino.write(f, space_around_delimiters=False)
def generate_files(storage_dir, ctx, filename, game_ini, dinos_data, mods):
if not os.path.isdir(f'{storage_dir}/{ctx.author.id}'):
os.mkdir(f'{storage_dir}/{ctx.author.id}')
directory = f'{storage_dir}/{ctx.author.id}/{filename}_' \
f'{ctx.message.created_at.strftime("%Y%m%dT%H%M%S")}'
os.mkdir(directory)
generate_game_ini(game_ini, mods, directory)
generate_dino_files(dinos_data, directory)

View File

@ -82,3 +82,21 @@ async def run_command(args):
stdout, stderr = await process.communicate()
# Return stdout
return stdout.decode().strip()
async def git_add(loop, directory, file):
return await asyncio.wait_for(loop.create_task(run_command(f'git -C {directory} add {file}')), 120)
async def git_commit(loop, directory, message):
return await asyncio.wait_for(loop.create_task(run_command(f'git -C {directory} commit -m {message}')), 120)
async def git_push(loop, directory):
result = await asyncio.wait_for(loop.create_task(run_command(f'git -C {directory} push')), 240)
return result if 'error: failed' in result else 'Completed'
async def git_pull(loop, directory):
result = await asyncio.wait_for(loop.create_task(run_command(f'git -C {directory} pull')), 240)
return result if 'CONFLICT' in result else 'Completed'

View File

@ -1,11 +1,12 @@
import discord
from discord.ext import commands
from io import BytesIO
from .imports import process_files
from .imports import process_files, utils
from configparser import ConfigParser
import os
storage_dir = '../ASB_dino_submissions'
owner_id = 351794468870946827
class Uploader:
@ -14,40 +15,64 @@ class Uploader:
@commands.command(name='upload', aliases=['submit'])
async def upload_dino(self, ctx, official: str='unofficial', singleplayer: bool=False):
msg = await ctx.send('Processing... Please Wait')
if ctx.message.attachments:
attachment = ctx.message.attachments[0]
if attachment.filename.endswith('.zip'):
async with ctx.typing():
with BytesIO() as file:
await attachment.save(file)
unzipped = process_files.load_zip(file)
game_ini, dinos_data, mods = process_files.process_files(unzipped)
if dinos_data == dict():
await ctx.send('There aren\'t any DinoExport files in the zip file attached.\n'
await msg.edit(content='There aren\'t any DinoExport files in the zip file attached.\n'
'Please make sure the files have not been renamed.')
else:
if official == 'unofficial' and game_ini == ConfigParser():
await ctx.send('Game.ini is missing or is not valid.')
await msg.edit(content='Game.ini is missing or is not valid.')
return
elif official == 'official' and game_ini == ConfigParser():
if singleplayer:
game_ini.add_section('/script/shootergame.shootergamemode')
game_ini.set('/script/shootergame.shootergamemode', 'bUseSingleplayerSettings', True)
game_ini.set('/script/shootergame.shootergamemode',
'bUseSingleplayerSettings',
True)
elif official not in ['official', 'unofficial']:
await ctx.send(f'{official} is not a valid option. Please specify "official" or '
f'"unofficial" or leave it blank to default to "unofficial"')
await msg.edit(content=f'{official} is not a valid option. Please specify "official" '
f'or "unofficial" or leave it blank to default to "unofficial"')
return
if not os.path.isdir(f'{storage_dir}/{ctx.author.id}'):
os.mkdir(f'{storage_dir}/{ctx.author.id}')
directory = f'{storage_dir}/{ctx.author.id}/{attachment.filename}_' \
f'{ctx.message.created_at.strftime("%Y%m%dT%H%M%S")}'
os.mkdir(directory)
process_files.generate_game_ini(game_ini, mods, directory)
process_files.generate_dino_files(dinos_data, directory)
await ctx.send('Upload complete.')
await msg.edit('Processing... Syncing with GitHub')
pull_status = utils.git_pull(self.bot.loop, storage_dir)
if pull_status == 'Completed':
await msg.edit('Processing... Sync complete... Generating new files')
process_files.generate_files(storage_dir, ctx, attachment.filename,
game_ini, dinos_data, mods)
await msg.edit('Processing... Files generated... Committing changes')
utils.git_add(self.bot.loop, storage_dir, '*')
utils.git_commit(self.bot.loop,
storage_dir,
f'Uploaded {len(dinos_data)} dinos {official}')
await msg.edit('Processing... Committed... Pushing files to GitHub')
push_status = utils.git_push(self.bot.loop, storage_dir)
if push_status == 'Completed':
await msg.edit(content=f'{ctx.author.mention} Upload complete.')
else:
await ctx.send('Please attach a zip file to the command.')
await self.bot.get_user(owner_id).send(f'There was an error with git push'
f'\n{push_status}')
await msg.edit(content='There was an error pushing the files to GitHub\n'
'Dusty.P has been notified and will get this fixed')
else:
await ctx.send('Please attach a zip file to the command.')
await self.bot.get_user(owner_id).send(f'There was an error with git pull\n'
f'{pull_status}')
process_files.generate_files('submissions_temp', ctx, attachment.filename,
game_ini, dinos_data, mods)
await msg.edit(content='Could not sync with GitHub.\n'
'Dusty.P has been notified and your files are stored in a '
'temporary location')
else:
await msg.edit(content='Please attach a zip file to the command.')
else:
await msg.edit(content='Please attach a zip file to the command.')
def setup(bot):