132 lines
4.3 KiB
Python
132 lines
4.3 KiB
Python
"""
|
|
===
|
|
|
|
MIT License
|
|
|
|
Copyright (c) 2018 Dusty.P https://github.com/dustinpianalto
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
|
from io import StringIO
|
|
import sys
|
|
import asyncio
|
|
import discord
|
|
from discord.ext.commands.formatter import Paginator
|
|
import numpy as np
|
|
|
|
|
|
class Capturing(list):
|
|
def __enter__(self):
|
|
self._stdout = sys.stdout
|
|
sys.stdout = self._stringio = StringIO()
|
|
return self
|
|
|
|
def __exit__(self, *args):
|
|
self.extend(self._stringio.getvalue().splitlines())
|
|
del self._stringio # free up some memory
|
|
sys.stdout = self._stdout
|
|
|
|
|
|
def to_list_of_str(items, out: list=list(), level=1, recurse=0):
|
|
def rec_loop(item, key, out, level):
|
|
quote = '"'
|
|
if type(item) == list:
|
|
out.append(f'{" "*level}{quote+key+quote+": " if key else ""}[')
|
|
new_level = level + 1
|
|
out = to_list_of_str(item, out, new_level, 1)
|
|
out.append(f'{" "*level}]')
|
|
elif type(item) == dict:
|
|
out.append(f'{" "*level}{quote+key+quote+": " if key else ""}{{')
|
|
new_level = level + 1
|
|
out = to_list_of_str(item, out, new_level, 1)
|
|
out.append(f'{" "*level}}}')
|
|
else:
|
|
out.append(f'{" "*level}{quote+key+quote+": " if key else ""}{repr(item)},')
|
|
|
|
if type(items) == list:
|
|
if not recurse:
|
|
out = list()
|
|
out.append('[')
|
|
for item in items:
|
|
rec_loop(item, None, out, level)
|
|
if not recurse:
|
|
out.append(']')
|
|
elif type(items) == dict:
|
|
if not recurse:
|
|
out = list()
|
|
out.append('{')
|
|
for key in items:
|
|
rec_loop(items[key], key, out, level)
|
|
if not recurse:
|
|
out.append('}')
|
|
|
|
return out
|
|
|
|
|
|
def paginate(text, maxlen=1990):
|
|
paginator = Paginator(prefix='```py', max_size=maxlen+10)
|
|
if type(text) == list:
|
|
data = to_list_of_str(text)
|
|
elif type(text) == dict:
|
|
data = to_list_of_str(text)
|
|
else:
|
|
data = str(text).split('\n')
|
|
for line in data:
|
|
if len(line) > maxlen:
|
|
n = maxlen
|
|
for l in [line[i:i+n] for i in range(0, len(line), n)]:
|
|
paginator.add_line(l)
|
|
else:
|
|
paginator.add_line(line)
|
|
return paginator.pages
|
|
|
|
|
|
async def run_command(args):
|
|
# Create subprocess
|
|
process = await asyncio.create_subprocess_shell(
|
|
args,
|
|
# stdout must a pipe to be accessible as process.stdout
|
|
stdout=asyncio.subprocess.PIPE)
|
|
# Wait for the subprocess to finish
|
|
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'(cd {directory} && git add {file})')), 120)
|
|
|
|
|
|
async def git_commit(loop, directory, message):
|
|
return await asyncio.wait_for(loop.create_task(run_command(f'(cd {directory} && git commit -m {message})')), 120)
|
|
|
|
|
|
async def git_push(loop, directory):
|
|
result = await asyncio.wait_for(loop.create_task(run_command(f'(cd {directory} && git 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'(cd {directory} && git pull)')), 240)
|
|
return result if 'CONFLICT' in result else 'Completed'
|