Skip to content

Commit 07e51b4

Browse files
committed
Use asyncio subprocess
1 parent 90d78cf commit 07e51b4

File tree

4 files changed

+18
-26
lines changed

4 files changed

+18
-26
lines changed

.github/workflows/lints.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
python -m pip install bandit==1.6.2 pylint black==19.10b0
2626
continue-on-error: true
2727
- name: Bandit syntax check
28-
run: bandit ./bot.py cogs/*.py core/*.py -b .bandit_baseline.json
28+
run: bandit -r . -b .bandit_baseline.json
2929
- name: Pylint
3030
run: pylint ./bot.py cogs/*.py core/*.py --disable=import-error --exit-zero -r y
3131
continue-on-error: true

bot.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
import logging
77
import os
88
import re
9-
import subprocess
109
import sys
1110
import typing
1211
from datetime import datetime
12+
from subprocess import PIPE
1313
from types import SimpleNamespace
1414

1515
import discord
@@ -1466,15 +1466,9 @@ async def autoupdate(self):
14661466
await channel.send(embed=embed)
14671467
else:
14681468
command = "git pull"
1469-
1470-
cmd = subprocess.run(
1471-
command,
1472-
cwd=os.getcwd(),
1473-
stderr=subprocess.PIPE,
1474-
stdout=subprocess.PIPE,
1475-
shell=True,
1476-
)
1477-
res = cmd.stdout.decode("utf-8").strip()
1469+
proc = await asyncio.create_subprocess_shell(command, stderr=PIPE, stdout=PIPE,)
1470+
res = await proc.stdout.read()
1471+
res = res.decode("utf-8").rstrip()
14781472

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

1502-
if not self.config.get("github_token"):
1496+
if not self.config.get("github_token") and self.hosting_method == HostingMethod.HEROKU:
15031497
logger.warning("GitHub access token not found.")
15041498
logger.warning("Autoupdates disabled.")
15051499
self.autoupdate_loop.cancel()

cogs/utility.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from io import BytesIO, StringIO
1111
from itertools import takewhile, zip_longest
1212
from json import JSONDecodeError, loads
13-
import subprocess
13+
from subprocess import PIPE
1414
from textwrap import indent
1515
from types import SimpleNamespace
1616
from typing import Union
@@ -1844,9 +1844,9 @@ async def github(self, ctx):
18441844
embed.set_thumbnail(url=user["avatar_url"])
18451845
await ctx.send(embed=embed)
18461846
else:
1847-
await ctx.send(embed=discord.Embed(
1848-
title="Invalid Github Token", color=self.bot.error_color
1849-
))
1847+
await ctx.send(
1848+
embed=discord.Embed(title="Invalid Github Token", color=self.bot.error_color)
1849+
)
18501850

18511851
@commands.command()
18521852
@checks.has_permissions(PermissionLevel.OWNER)
@@ -1920,14 +1920,9 @@ async def update(self, ctx, *, flag: str = ""):
19201920
else:
19211921
command = "git pull"
19221922

1923-
cmd = subprocess.run(
1924-
command,
1925-
cwd=os.getcwd(),
1926-
stderr=subprocess.PIPE,
1927-
stdout=subprocess.PIPE,
1928-
shell=True,
1929-
)
1930-
res = cmd.stdout.decode("utf-8").strip()
1923+
proc = await asyncio.create_subprocess_shell(command, stderr=PIPE, stdout=PIPE,)
1924+
res = await proc.stdout.read()
1925+
res = res.decode("utf-8").rstrip()
19311926

19321927
if res != "Already up to date.":
19331928
logger.info("Bot has been updated.")

core/clients.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ async def login(cls, bot) -> "GitHub":
211211
raise InvalidConfigError("Invalid github token")
212212

213213

214-
215214
class ApiClient:
216215
"""
217216
This class represents the general request class for all type of clients.
@@ -664,7 +663,11 @@ async def get_user_info(self) -> dict:
664663
return None
665664
else:
666665
return {
667-
"user": {"username": user.username, "avatar_url": user.avatar_url, "url": user.url,}
666+
"user": {
667+
"username": user.username,
668+
"avatar_url": user.avatar_url,
669+
"url": user.url,
670+
}
668671
}
669672

670673

0 commit comments

Comments
 (0)