Skip to content

Commit

Permalink
Try detecting git commit from existing git repo
Browse files Browse the repository at this point in the history
Move process invocation to separate function
  • Loading branch information
evgfilim1 committed Apr 3, 2023
1 parent ad52b0f commit a7d37b8
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 41 deletions.
62 changes: 33 additions & 29 deletions locales/evgfilim1-userbot.pot
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ msgid ""
msgstr ""
"Project-Id-Version: evgfilim1/userbot 0.6.x\n"
"Report-Msgid-Bugs-To: https://github.com/evgfilim1/userbot/issues\n"
"POT-Creation-Date: 2023-03-30 10:19+0500\n"
"POT-Creation-Date: 2023-04-03 14:27+0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand All @@ -21,18 +21,18 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.11.0\n"

#: userbot/commands/content_converters.py:161 userbot/commands/content_converters.py:198
#: userbot/commands/content_converters.py:157 userbot/commands/content_converters.py:194
#: userbot/hooks.py:73
#, python-brace-format
msgid "{icon} <i>Transcription failed, maybe the message has no recognizable voice?</i>"
msgstr ""

#: userbot/commands/content_converters.py:166 userbot/hooks.py:80
#: userbot/commands/content_converters.py:162 userbot/hooks.py:80
#, python-brace-format
msgid "{icon} <i>Transcription is pending...</i>"
msgstr ""

#: userbot/commands/content_converters.py:167 userbot/commands/content_converters.py:202
#: userbot/commands/content_converters.py:163 userbot/commands/content_converters.py:198
#: userbot/hooks.py:85
#, python-brace-format
msgid ""
Expand All @@ -57,97 +57,101 @@ msgid ""
"To get more info about a command, send <code>{prefix}help {command}</code>."
msgstr ""

#: userbot/commands/about.py:32
#: userbot/commands/about.py:40
msgid "(dirty)"
msgstr ""

#: userbot/commands/about.py:42
msgid "staging"
msgstr ""

#: userbot/commands/about.py:38
#: userbot/commands/about.py:48
msgid "<i>Repo:</i>"
msgstr ""

#: userbot/commands/about.py:39
#: userbot/commands/about.py:49
msgid "<i>Commit:</i>"
msgstr ""

#: userbot/commands/about.py:40
#: userbot/commands/about.py:50
#, python-brace-format
msgid "{icon} <b>About userbot</b>"
msgstr ""

#: userbot/commands/about.py:46
#: userbot/commands/about.py:56
#, python-brace-format
msgid "(<a href='{base_url}/deployments'>deployments</a>)"
msgstr ""

#: userbot/commands/about.py:49
#: userbot/commands/about.py:59
#, python-brace-format
msgid "{icon} <a href='{url}'>Contribute userbot translations</a>"
msgstr ""

#: userbot/commands/about.py:59
#: userbot/commands/about.py:69
msgid "<i>Collecting stats...</i>"
msgstr ""

#: userbot/commands/about.py:120
#: userbot/commands/about.py:130
msgid "<b>Statistics:</b>"
msgstr ""

#: userbot/commands/about.py:121
#: userbot/commands/about.py:131
#, python-brace-format
msgid "{icon} Uptime: {uptime}"
msgstr ""

#: userbot/commands/about.py:125
#: userbot/commands/about.py:135
#, python-brace-format
msgid "{icon} Commands used: {count}"
msgstr ""

#: userbot/commands/about.py:134
#: userbot/commands/about.py:144
#, python-brace-format
msgid "{icon} Total chats: {count}"
msgstr ""

#: userbot/commands/about.py:138
#: userbot/commands/about.py:148
#, python-brace-format
msgid "• {icon} Private: {count}"
msgstr ""

#: userbot/commands/about.py:142
#: userbot/commands/about.py:152
#, python-brace-format
msgid "• {icon} Bots: {count}"
msgstr ""

#: userbot/commands/about.py:146
#: userbot/commands/about.py:156
#, python-brace-format
msgid "• {icon} Groups and supergroups: {count}"
msgstr ""

#: userbot/commands/about.py:150
#: userbot/commands/about.py:160
#, python-brace-format
msgid "• {icon} Channels: {count}"
msgstr ""

#: userbot/commands/about.py:154
#: userbot/commands/about.py:164
#, python-brace-format
msgid "• {icon} Archived: {count}"
msgstr ""

#: userbot/commands/about.py:168
#: userbot/commands/about.py:178
#, python-brace-format
msgid "{icon} GIFs: {count}/{total}"
msgstr ""

#: userbot/commands/about.py:173
#: userbot/commands/about.py:183
#, python-brace-format
msgid "{icon} Stickers: {count}/{total}"
msgstr ""

#: userbot/commands/about.py:178
#: userbot/commands/about.py:188
#, python-brace-format
msgid "{icon} Archived stickers: {count}"
msgstr ""

#: userbot/commands/about.py:182
#: userbot/commands/about.py:192
#, python-brace-format
msgid "{icon} Custom emoji: {count}"
msgstr ""
Expand Down Expand Up @@ -318,24 +322,24 @@ msgstr ""
msgid "{icon} Color of the user {user} is {c}"
msgstr ""

#: userbot/commands/content_converters.py:62
#: userbot/commands/content_converters.py:58
msgid "<i>Converting to mpeg4gif...</i>"
msgstr ""

#: userbot/commands/content_converters.py:73 userbot/commands/content_converters.py:129
#: userbot/commands/content_converters.py:69 userbot/commands/content_converters.py:125
#, python-brace-format
msgid "{icon} No video found"
msgstr ""

#: userbot/commands/content_converters.py:85
#: userbot/commands/content_converters.py:81
msgid "<i>Converting to sticker...</i>"
msgstr ""

#: userbot/commands/content_converters.py:118
#: userbot/commands/content_converters.py:114
msgid "<i>Extracting audio...</i>"
msgstr ""

#: userbot/commands/content_converters.py:158
#: userbot/commands/content_converters.py:154
#, python-brace-format
msgid "{icon} No voice or video note found"
msgstr ""
Expand Down
14 changes: 12 additions & 2 deletions userbot/commands/about.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
DialogCount,
StatsController,
Translation,
call_subprocess,
format_timedelta,
get_dialogs_count,
gettext,
Expand All @@ -28,8 +29,17 @@ async def about(client: Client, tr: Translation) -> str:
"""Shows information about this userbot."""
_ = tr.gettext
base_url = "https://github.com/evgfilim1/userbot"
# TODO (2022-07-13): get this from the git repo also
commit = __git_commit__ if __git_commit__ else _("staging")
if __git_commit__:
commit = __git_commit__
else:
is_git_installed = bool(await call_subprocess("git", "--version"))
if is_git_installed:
commit = (await call_subprocess("git", "rev-parse", "HEAD")).stdout.decode().strip()
is_clean = bool(await call_subprocess("git", "diff", "--quiet"))
if not is_clean:
commit += " " + _("(dirty)")
else:
commit = _("staging")
me = await client.get_me()
if me.is_premium:
github_icon = Icons.GITHUB
Expand Down
16 changes: 6 additions & 10 deletions userbot/commands/content_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"transcribed_audio_raw_handler",
]

import asyncio
from io import BytesIO
from os import path
from tempfile import NamedTemporaryFile
Expand All @@ -19,7 +18,7 @@
from ..meta.modules import CommandsModule
from ..middlewares import CommandObject
from ..storage import Storage
from ..utils import Translation, gettext
from ..utils import Translation, call_subprocess, gettext
from ..utils.premium import transcribe_message

commands = CommandsModule("Content converters")
Expand All @@ -31,22 +30,19 @@ async def _call_ffmpeg(
*args: str,
) -> None:
"""Calls ffmpeg with the given arguments."""
proc = await asyncio.subprocess.create_subprocess_exec(
"/usr/bin/env",
result = await call_subprocess(
"ffmpeg",
"-hide_banner",
"-i",
input_file,
*args,
"-y",
output_file,
stdin=asyncio.subprocess.DEVNULL,
stdout=asyncio.subprocess.DEVNULL,
stderr=asyncio.subprocess.PIPE,
)
__, stderr = await proc.communicate()
if proc.returncode != 0:
raise RuntimeError(f"Process finished with error code {proc.returncode}\n{stderr.decode()}")
if not result:
raise RuntimeError(
f"Process finished with error code {result.return_code}\n{result.stderr.decode()}"
)


def _convert_to_sticker(photo: BinaryIO, fmt: str) -> BytesIO:
Expand Down
3 changes: 3 additions & 0 deletions userbot/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"AppLimits",
"AppLimitsController",
"async_partial",
"call_subprocess",
"DialogCount",
"edit_replied_or_reply",
"fetch_stickers",
Expand All @@ -22,6 +23,7 @@
"StatsController",
"StickerFilter",
"StickerInfo",
"SubprocessResult",
"Translation",
"Unset",
]
Expand All @@ -37,6 +39,7 @@
is_my_message,
)
from .misc import SecretValue, StatsController, Unset, async_partial
from .os import SubprocessResult, call_subprocess
from .stickers import StickerInfo, fetch_stickers
from .telegram_json import json_value_to_python
from .time import format_timedelta, parse_timespec
Expand Down
36 changes: 36 additions & 0 deletions userbot/utils/os.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
__all__ = [
"call_subprocess",
"SubprocessResult",
]

import asyncio
from dataclasses import dataclass


@dataclass()
class SubprocessResult:
"""Represents the result of a subprocess call."""

return_code: int
stdout: bytes
stderr: bytes

def __bool__(self) -> bool:
return self.return_code == 0


async def call_subprocess(
executable: str,
*args: str,
) -> SubprocessResult:
"""Calls the given executable with the given arguments."""
proc = await asyncio.subprocess.create_subprocess_exec(
"/usr/bin/env",
executable,
*args,
stdin=asyncio.subprocess.DEVNULL,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
out, err = await proc.communicate()
return SubprocessResult(proc.returncode, out, err)

0 comments on commit a7d37b8

Please sign in to comment.