Skip to content

Commit

Permalink
Use asyncio subprocess
Browse files Browse the repository at this point in the history
  • Loading branch information
fourjr committed Nov 12, 2020
1 parent 90d78cf commit 07e51b4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lints.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
python -m pip install bandit==1.6.2 pylint black==19.10b0
continue-on-error: true
- name: Bandit syntax check
run: bandit ./bot.py cogs/*.py core/*.py -b .bandit_baseline.json
run: bandit -r . -b .bandit_baseline.json
- name: Pylint
run: pylint ./bot.py cogs/*.py core/*.py --disable=import-error --exit-zero -r y
continue-on-error: true
Expand Down
16 changes: 5 additions & 11 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import logging
import os
import re
import subprocess
import sys
import typing
from datetime import datetime
from subprocess import PIPE
from types import SimpleNamespace

import discord
Expand Down Expand Up @@ -1466,15 +1466,9 @@ async def autoupdate(self):
await channel.send(embed=embed)
else:
command = "git pull"

cmd = subprocess.run(
command,
cwd=os.getcwd(),
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
shell=True,
)
res = cmd.stdout.decode("utf-8").strip()
proc = await asyncio.create_subprocess_shell(command, stderr=PIPE, stdout=PIPE,)
res = await proc.stdout.read()
res = res.decode("utf-8").rstrip()

if res != "Already up to date.":
logger.info("Bot has been updated.")
Expand All @@ -1499,7 +1493,7 @@ async def before_autoupdate(self):
logger.warning("Autoupdates disabled.")
self.autoupdate_loop.cancel()

if not self.config.get("github_token"):
if not self.config.get("github_token") and self.hosting_method == HostingMethod.HEROKU:
logger.warning("GitHub access token not found.")
logger.warning("Autoupdates disabled.")
self.autoupdate_loop.cancel()
Expand Down
19 changes: 7 additions & 12 deletions cogs/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from io import BytesIO, StringIO
from itertools import takewhile, zip_longest
from json import JSONDecodeError, loads
import subprocess
from subprocess import PIPE
from textwrap import indent
from types import SimpleNamespace
from typing import Union
Expand Down Expand Up @@ -1844,9 +1844,9 @@ async def github(self, ctx):
embed.set_thumbnail(url=user["avatar_url"])
await ctx.send(embed=embed)
else:
await ctx.send(embed=discord.Embed(
title="Invalid Github Token", color=self.bot.error_color
))
await ctx.send(
embed=discord.Embed(title="Invalid Github Token", color=self.bot.error_color)
)

@commands.command()
@checks.has_permissions(PermissionLevel.OWNER)
Expand Down Expand Up @@ -1920,14 +1920,9 @@ async def update(self, ctx, *, flag: str = ""):
else:
command = "git pull"

cmd = subprocess.run(
command,
cwd=os.getcwd(),
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
shell=True,
)
res = cmd.stdout.decode("utf-8").strip()
proc = await asyncio.create_subprocess_shell(command, stderr=PIPE, stdout=PIPE,)
res = await proc.stdout.read()
res = res.decode("utf-8").rstrip()

if res != "Already up to date.":
logger.info("Bot has been updated.")
Expand Down
7 changes: 5 additions & 2 deletions core/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ async def login(cls, bot) -> "GitHub":
raise InvalidConfigError("Invalid github token")



class ApiClient:
"""
This class represents the general request class for all type of clients.
Expand Down Expand Up @@ -664,7 +663,11 @@ async def get_user_info(self) -> dict:
return None
else:
return {
"user": {"username": user.username, "avatar_url": user.avatar_url, "url": user.url,}
"user": {
"username": user.username,
"avatar_url": user.avatar_url,
"url": user.url,
}
}


Expand Down

0 comments on commit 07e51b4

Please sign in to comment.