Skip to content

Commit 2edfd44

Browse files
committed
feat(Groupbot): Add !gh update command to pull issue updates.
This pulls from the backup, so it'll only be as up-to-date as the backup repo.
1 parent 4dd924f commit 2edfd44

File tree

8 files changed

+100
-15
lines changed

8 files changed

+100
-15
lines changed

pytox/toxcore/tox.pxd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,9 @@ cdef extern from "tox/tox.h":
476476
cdef void tox_options_set_hole_punching_enabled(Tox_Options* self, bool hole_punching_enabled)
477477
cdef Tox_Savedata_Type tox_options_get_savedata_type(const Tox_Options* self)
478478
cdef void tox_options_set_savedata_type(Tox_Options* self, Tox_Savedata_Type savedata_type)
479-
cdef size_t tox_options_get_savedata_length(const Tox_Options* self)
480-
cdef const uint8_t* tox_options_get_savedata_data(const Tox_Options* self)
481-
cdef bool tox_options_set_savedata_data(Tox_Options* self, const uint8_t* savedata_data, size_t length)
479+
cdef bool tox_options_get_savedata(const Tox_Options* self, uint8_t* savedata)
480+
cdef size_t tox_options_get_savedata_size(const Tox_Options* self)
481+
cdef bool tox_options_set_savedata(Tox_Options* self, const uint8_t* savedata_data, size_t length)
482482
cdef bool tox_options_get_experimental_owned_data(const Tox_Options* self)
483483
cdef void tox_options_set_experimental_owned_data(Tox_Options* self, bool experimental_owned_data)
484484
cdef bool tox_options_get_experimental_thread_safety(const Tox_Options* self)

pytox/toxcore/tox.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class Tox_Options_Ptr:
100100
proxy_host: str
101101
proxy_port: int
102102
proxy_type: Tox_Proxy_Type
103-
savedata_data: bytes
103+
savedata: bytes
104104
savedata_type: Tox_Savedata_Type
105105
start_port: int
106106
tcp_port: int

pytox/toxcore/tox.pyx

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,12 +320,20 @@ cdef class Tox_Options_Ptr:
320320
tox_options_set_savedata_type(self._get(), savedata_type)
321321

322322
@property
323-
def savedata_data(self) -> bytes:
324-
return tox_options_get_savedata_data(self._get())[:tox_options_get_savedata_length(self._get())]
323+
def savedata(self) -> bytes:
324+
cdef size_t size = tox_options_get_savedata_size(self._get())
325+
cdef uint8_t* data = <uint8_t*> malloc(size)
326+
if data is NULL:
327+
raise MemoryError()
328+
try:
329+
tox_options_get_savedata(self._get(), data)
330+
return bytes(data[:size])
331+
finally:
332+
free(data)
325333

326-
@savedata_data.setter
327-
def savedata_data(self, savedata_data: bytes):
328-
tox_options_set_savedata_data(self._get(), savedata_data, len(savedata_data))
334+
@savedata.setter
335+
def savedata(self, savedata: bytes):
336+
tox_options_set_savedata(self._get(), savedata, len(savedata))
329337

330338
@property
331339
def experimental_owned_data(self) -> bool:
@@ -475,6 +483,8 @@ cdef class Tox_Ptr:
475483
def savedata(self) -> bytes:
476484
cdef size_t size = tox_get_savedata_size(self._get())
477485
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
486+
if data is NULL:
487+
raise MemoryError()
478488
try:
479489
tox_get_savedata(self._get(), data)
480490
return data[:size]
@@ -508,6 +518,8 @@ cdef class Tox_Ptr:
508518
def address(self) -> bytes:
509519
cdef size_t size = tox_address_size()
510520
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
521+
if data is NULL:
522+
raise MemoryError()
511523
try:
512524
tox_self_get_address(self._get(), data)
513525
return data[:size]
@@ -518,6 +530,8 @@ cdef class Tox_Ptr:
518530
def public_key(self) -> bytes:
519531
cdef size_t size = tox_public_key_size()
520532
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
533+
if data is NULL:
534+
raise MemoryError()
521535
try:
522536
tox_self_get_public_key(self._get(), data)
523537
return data[:tox_public_key_size()]
@@ -528,6 +542,8 @@ cdef class Tox_Ptr:
528542
def dht_id(self) -> bytes:
529543
cdef size_t size = tox_public_key_size()
530544
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
545+
if data is NULL:
546+
raise MemoryError()
531547
try:
532548
tox_self_get_dht_id(self._get(), data)
533549
return data[:tox_public_key_size()]
@@ -554,6 +570,8 @@ cdef class Tox_Ptr:
554570
def secret_key(self) -> bytes:
555571
cdef size_t size = tox_secret_key_size()
556572
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
573+
if data is NULL:
574+
raise MemoryError()
557575
try:
558576
tox_self_get_secret_key(self._get(), data)
559577
return data[:tox_secret_key_size()]
@@ -564,6 +582,8 @@ cdef class Tox_Ptr:
564582
def name(self) -> bytes:
565583
cdef size_t size = tox_self_get_name_size(self._get())
566584
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
585+
if data is NULL:
586+
raise MemoryError()
567587
try:
568588
tox_self_get_name(self._get(), data)
569589
return data[:size]
@@ -581,6 +601,8 @@ cdef class Tox_Ptr:
581601
def status_message(self) -> bytes:
582602
cdef size_t size = tox_self_get_status_message_size(self._get())
583603
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
604+
if data is NULL:
605+
raise MemoryError()
584606
try:
585607
tox_self_get_status_message(self._get(), data)
586608
return data[:size]
@@ -631,6 +653,8 @@ cdef class Tox_Ptr:
631653
def friend_list(self) -> list[Tox_Friend_Number]:
632654
cdef size_t size = tox_self_get_friend_list_size(self._get())
633655
cdef Tox_Friend_Number *data = <Tox_Friend_Number*> malloc(size * sizeof(Tox_Friend_Number))
656+
if data is NULL:
657+
raise MemoryError()
634658
try:
635659
tox_self_get_friend_list(self._get(), data)
636660
return [data[i] for i in range(size)]
@@ -640,6 +664,8 @@ cdef class Tox_Ptr:
640664
def friend_get_public_key(self, friend_number: Tox_Friend_Number) -> bytes:
641665
cdef size_t size = tox_public_key_size()
642666
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
667+
if data is NULL:
668+
raise MemoryError()
643669
cdef Tox_Err_Friend_Get_Public_Key err = TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK
644670
try:
645671
tox_friend_get_public_key(self._get(), friend_number, data, &err)
@@ -662,6 +688,8 @@ cdef class Tox_Ptr:
662688
if err:
663689
raise ApiException(Tox_Err_Friend_Query(err))
664690
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
691+
if data is NULL:
692+
raise MemoryError()
665693
try:
666694
tox_friend_get_status_message(self._get(), friend_number, data, &err)
667695
if err:
@@ -676,6 +704,8 @@ cdef class Tox_Ptr:
676704
if err:
677705
raise ApiException(Tox_Err_Friend_Query(err))
678706
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
707+
if data is NULL:
708+
raise MemoryError()
679709
try:
680710
tox_friend_get_name(self._get(), friend_number, data, &err)
681711
if err:
@@ -734,6 +764,8 @@ cdef class Tox_Ptr:
734764
cdef Tox_Err_File_Get err = TOX_ERR_FILE_GET_OK
735765
cdef size_t size = tox_file_id_length()
736766
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
767+
if data is NULL:
768+
raise MemoryError()
737769
try:
738770
tox_file_get_file_id(self._get(), friend_number, file_number, data, &err)
739771
if err:
@@ -806,6 +838,8 @@ cdef class Tox_Ptr:
806838
if err:
807839
raise ApiException(Tox_Err_Conference_Title(err))
808840
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
841+
if data is NULL:
842+
raise MemoryError()
809843
try:
810844
tox_conference_get_title(self._get(), conference_number, data, &err)
811845
if err:
@@ -846,6 +880,8 @@ cdef class Tox_Ptr:
846880
if err:
847881
raise ApiException(Tox_Err_Conference_Peer_Query(err))
848882
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
883+
if data is NULL:
884+
raise MemoryError()
849885
try:
850886
tox_conference_peer_get_name(self._get(), conference_number, peer_number, data, &err)
851887
if err:
@@ -860,6 +896,8 @@ cdef class Tox_Ptr:
860896
if err:
861897
raise ApiException(Tox_Err_Conference_Peer_Query(err))
862898
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
899+
if data is NULL:
900+
raise MemoryError()
863901
try:
864902
tox_conference_offline_peer_get_name(self._get(), conference_number, offline_peer_number, data, &err)
865903
if err:
@@ -872,6 +910,8 @@ cdef class Tox_Ptr:
872910
cdef Tox_Err_Conference_Peer_Query err = TOX_ERR_CONFERENCE_PEER_QUERY_OK
873911
cdef size_t size = tox_public_key_size()
874912
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
913+
if data is NULL:
914+
raise MemoryError()
875915
try:
876916
tox_conference_peer_get_public_key(self._get(), conference_number, peer_number, data, &err)
877917
if err:
@@ -884,6 +924,8 @@ cdef class Tox_Ptr:
884924
cdef Tox_Err_Conference_Peer_Query err = TOX_ERR_CONFERENCE_PEER_QUERY_OK
885925
cdef size_t size = tox_public_key_size()
886926
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
927+
if data is NULL:
928+
raise MemoryError()
887929
try:
888930
tox_conference_offline_peer_get_public_key(self._get(), conference_number, offline_peer_number, data, &err)
889931
if err:
@@ -910,6 +952,8 @@ cdef class Tox_Ptr:
910952
def conference_chatlist(self) -> list[Tox_Conference_Number]:
911953
cdef size_t size = tox_conference_get_chatlist_size(self._get())
912954
cdef Tox_Conference_Number *data = <Tox_Conference_Number*> malloc(size * sizeof(Tox_Conference_Number))
955+
if data is NULL:
956+
raise MemoryError()
913957
try:
914958
tox_conference_get_chatlist(self._get(), data)
915959
return [data[i] for i in range(size)]
@@ -919,6 +963,8 @@ cdef class Tox_Ptr:
919963
def conference_get_id(self, conference_number: Tox_Conference_Number) -> bytes:
920964
cdef size_t size = tox_conference_id_size()
921965
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
966+
if data is NULL:
967+
raise MemoryError()
922968
try:
923969
if not tox_conference_get_id(self._get(), conference_number, data):
924970
raise ApiException(0) # TODO(iphydf): There's no error enum for this. Make one.
@@ -947,6 +993,8 @@ cdef class Tox_Ptr:
947993
if err:
948994
raise ApiException(Tox_Err_Group_Self_Query(err))
949995
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
996+
if data is NULL:
997+
raise MemoryError()
950998
try:
951999
tox_group_self_get_name(self._get(), group_number, data, &err)
9521000
if err:
@@ -992,6 +1040,8 @@ cdef class Tox_Ptr:
9921040
cdef Tox_Err_Group_Self_Query err = TOX_ERR_GROUP_SELF_QUERY_OK
9931041
cdef size_t size = tox_public_key_size()
9941042
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
1043+
if data is NULL:
1044+
raise MemoryError()
9951045
try:
9961046
tox_group_self_get_public_key(self._get(), group_number, data, &err)
9971047
if err:
@@ -1006,6 +1056,8 @@ cdef class Tox_Ptr:
10061056
if err:
10071057
raise ApiException(Tox_Err_Group_Peer_Query(err))
10081058
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
1059+
if data is NULL:
1060+
raise MemoryError()
10091061
try:
10101062
tox_group_peer_get_name(self._get(), group_number, peer_id, data, &err)
10111063
if err:
@@ -1045,6 +1097,8 @@ cdef class Tox_Ptr:
10451097
cdef Tox_Err_Group_Peer_Query err = TOX_ERR_GROUP_PEER_QUERY_OK
10461098
cdef size_t size = tox_public_key_size()
10471099
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
1100+
if data is NULL:
1101+
raise MemoryError()
10481102
try:
10491103
tox_group_peer_get_public_key(self._get(), group_number, peer_id, data, &err)
10501104
if err:
@@ -1059,6 +1113,8 @@ cdef class Tox_Ptr:
10591113
if err:
10601114
raise ApiException(Tox_Err_Group_State_Query(err))
10611115
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
1116+
if data is NULL:
1117+
raise MemoryError()
10621118
try:
10631119
tox_group_get_topic(self._get(), group_number, data, &err)
10641120
if err:
@@ -1079,6 +1135,8 @@ cdef class Tox_Ptr:
10791135
if err:
10801136
raise ApiException(Tox_Err_Group_State_Query(err))
10811137
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
1138+
if data is NULL:
1139+
raise MemoryError()
10821140
try:
10831141
tox_group_get_name(self._get(), group_number, data, &err)
10841142
if err:
@@ -1091,6 +1149,8 @@ cdef class Tox_Ptr:
10911149
cdef Tox_Err_Group_State_Query err = TOX_ERR_GROUP_STATE_QUERY_OK
10921150
cdef size_t size = tox_group_chat_id_size()
10931151
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
1152+
if data is NULL:
1153+
raise MemoryError()
10941154
try:
10951155
tox_group_get_chat_id(self._get(), group_number, data, &err)
10961156
if err:
@@ -1160,6 +1220,8 @@ cdef class Tox_Ptr:
11601220
if err:
11611221
raise ApiException(Tox_Err_Group_State_Query(err))
11621222
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
1223+
if data is NULL:
1224+
raise MemoryError()
11631225
try:
11641226
tox_group_get_password(self._get(), group_number, data, &err)
11651227
if err:

test/tox_options_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ def test_options(self) -> None:
5454
opts.savedata_type = c.TOX_SAVEDATA_TYPE_TOX_SAVE
5555
self.assertEqual(opts.savedata_type, c.TOX_SAVEDATA_TYPE_TOX_SAVE)
5656

57-
opts.savedata_data = b"test"
58-
self.assertEqual(opts.savedata_data, b"test")
57+
opts.savedata = b"test"
58+
self.assertEqual(opts.savedata, b"test")
5959

6060
self.assertFalse(opts.experimental_thread_safety)
6161
opts.experimental_thread_safety = True

tools/groupbot/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def parse_args() -> Config:
7171
default=(os.path.join(
7272
BUILD_WORKSPACE_DIRECTORY,
7373
"tools",
74-
"toktok-backup",
74+
"backup",
7575
) if BUILD_WORKSPACE_DIRECTORY is not None else None),
7676
)
7777
return Config(**vars(parser.parse_args()))

tools/groupbot/groupbot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def __init__(self, config: api.Config) -> None:
4747
options.proxy_port = 9050
4848
options.proxy_type = core.TOX_PROXY_TYPE_SOCKS5
4949
if data:
50-
options.savedata_data = data
50+
options.savedata = data
5151
options.savedata_type = core.TOX_SAVEDATA_TYPE_TOX_SAVE
5252
super().__init__(config, options)
5353

tools/groupbot/groupbot.tox

4.48 KB
Binary file not shown.

tools/groupbot/plugins/github.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"""
99
import json
1010
import os
11+
import subprocess # nosec
12+
import time
1113
from dataclasses import dataclass
1214
from functools import cache as memoize
1315
from typing import Any
@@ -105,6 +107,7 @@ def fromJSON(path: IssuePath, issue: dict[str, Any]) -> "Issue":
105107
@dataclass
106108
class GitHub(api.Handler):
107109
path: str
110+
last_update: float = 0
108111

109112
@staticmethod
110113
def new(config: api.Config) -> "GitHub":
@@ -147,6 +150,8 @@ def handle_cli(self, message: tuple[str, ...]) -> Optional[api.Reply]:
147150
if len(message) == 1 and "#" in message[0]:
148151
repo_name, issue_id = message[0].split("#", 1)
149152
return self.handle_issue(repo_name, issue_id)
153+
if len(message) == 1 and message[0] == "update":
154+
return self.handle_update()
150155

151156
return None
152157

@@ -200,6 +205,25 @@ def handle_issue(self, repo_name: Optional[str],
200205
return api.Reply(f"{issue.emoji} {issue.title} by {issue.user.login} "
201206
f"({issue.repo}#{issue.number}, {issue.state})")
202207

208+
def handle_update(self) -> api.Reply:
209+
"""Pull the backup directory."""
210+
if time.time() - self.last_update < 60:
211+
return api.Reply("Not updating yet, try again later")
212+
self.last_update = time.time()
213+
output = subprocess.run( # nosec
214+
["git", "pull", "--rebase"],
215+
cwd=self.path,
216+
stdout=subprocess.PIPE,
217+
stderr=subprocess.STDOUT,
218+
).stdout.decode().split("\n")[0]
219+
self._clear_cache()
220+
return api.Reply(output)
221+
222+
def _clear_cache(self) -> None:
223+
"""Clear the memoization cache."""
224+
for func in (self.load_issue, self.issues, self.repos):
225+
func.cache_clear()
226+
203227
@memoize
204228
def load_issue(self, issue: IssuePath) -> Issue:
205229
"""Get the issue/PR information."""
@@ -247,8 +271,7 @@ def issues(self, repo: RepoPath) -> list[IssuePath]:
247271
api.BUILD_WORKSPACE_DIRECTORY = os.path.abspath(
248272
os.path.dirname(
249273
os.path.dirname(os.path.dirname(os.path.dirname(__file__)))))
250-
github_path = os.path.join(api.BUILD_WORKSPACE_DIRECTORY, "tools",
251-
"toktok-backup")
274+
github_path = os.path.join(api.BUILD_WORKSPACE_DIRECTORY, "tools", "backup")
252275

253276
gh = GitHub(github_path)
254277
reply = gh.handle_cli(tuple(sys.argv[1:]))

0 commit comments

Comments
 (0)