44import prettytable
55from prettytable import MARKDOWN
66
7- import token , tokenize , logging , re
7+ import sys , logging , re , importlib . util , os
88from pathlib import Path
99
1010TinyMod : Client
@@ -27,22 +27,21 @@ async def ensure_curr_repo():
2727 await git_cmd ("fetch" )
2828 await git_cmd ("reset" , "--hard" , "origin/master" )
2929
30- def is_docstring (t ):
31- return t .type == token .STRING and t .string .startswith ('"""' ) and t .line .strip ().startswith ('"""' )
30+ _sz = None
31+ def load_sz ():
32+ """Loads tinygrad's own sz.py from the cloned repo so we reuse its line counting."""
33+ global _sz
34+ if _sz is None :
35+ repo = str (REPO_DIR .resolve ())
36+ if repo not in sys .path : sys .path .insert (0 , repo )
37+ spec = importlib .util .spec_from_file_location ("tinygrad_sz" , REPO_DIR / "sz.py" )
38+ _sz = importlib .util .module_from_spec (spec )
39+ spec .loader .exec_module (_sz )
40+ return _sz
3241
33- TOKEN_WHITELIST = [token .OP , token .NAME , token .NUMBER , token .STRING ]
34- PATH_BLACKLIST = ["autogen" ]
3542async def get_curr_metrics ():
3643 await ensure_curr_repo ()
37-
38- metrics = {}
39- for path in (REPO_DIR / "tinygrad" ).rglob ("*.py" ):
40- if any (blacklist in str (path ) for blacklist in PATH_BLACKLIST ): continue
41- with path .open ("r" ) as f :
42- tokens = [t for t in tokenize .generate_tokens (f .readline ) if t .type in TOKEN_WHITELIST and not is_docstring (t )]
43- line_count = len (set ([x for t in tokens for x in range (t .start [0 ], t .end [0 ]+ 1 )]))
44- if line_count > 0 : metrics [str (path .relative_to (REPO_DIR / "tinygrad" ))] = {"line_count" : line_count }
45- return metrics
44+ return load_sz ().gen_stats (str (REPO_DIR ))
4645
4746MAX_LINE_REGEX = re .compile (r"MAX_LINE_COUNT=(\d+)" )
4847async def get_curr_max_lines ():
@@ -60,25 +59,30 @@ async def line_count(client: Client, event):
6059 message = yield "calculating metrics..."
6160
6261 metrics = await get_curr_metrics ()
63- total_line_count = sum (m [ "line_count" ] for m in metrics . values () )
64- sorted_metrics = sorted (metrics . items () , key = lambda x : x [1 ][ "line_count" ] , reverse = True )[: 37 ]
62+ total_line_count = sum (row [ 1 ] for row in metrics )
63+ sorted_metrics = sorted (metrics , key = lambda x : x [1 ], reverse = True )
6564
6665 table = prettytable .PrettyTable ()
6766 table .set_style (MARKDOWN )
6867 table .field_names = ["File" , "Line Count" ]
69- for path , data in sorted_metrics : table .add_row ([path , data ["line_count" ]])
68+ def render (): return f"# Total line count: { total_line_count } \n \n **Largest Files:**\n ```{ table .get_string ()} ```"
69+ for path , line_count , _ in sorted_metrics :
70+ table .add_row ([path , line_count ])
71+ if len (render ()) > 1990 :
72+ table .del_row (len (table .rows ) - 1 )
73+ break
7074
71- yield InteractionResponse (content = f"# Total line count: { total_line_count } \n \n **Largest Files:** \n ``` { table . get_string () } ```" , message = message )
75+ yield InteractionResponse (content = render () , message = message )
7276
73- LINE_COUNT_CHANNEL = Channel .precreate (1068991125353939066 )
77+ LINE_COUNT_CHANNEL = Channel .precreate (os . getenv ( "LINE_COUNT_CHANNEL_ID" , 1068991125353939066 ) )
7478@TinyMod .interactions (guild = GUILD , show_for_invoking_user_only = True ) # type: ignore
7579async def update_line_count (client : Client , event ):
7680 """Updates the line count metrics."""
7781 if not event .user .has_role (ADMIN_ROLE ): return
7882 message = yield "updating metrics..."
7983
8084 metrics = await get_curr_metrics ()
81- total_line_count = sum (m [ "line_count" ] for m in metrics . values () )
85+ total_line_count = sum (row [ 1 ] for row in metrics )
8286 max_line_count = await get_curr_max_lines ()
8387 free_lines = max_line_count - total_line_count
8488
@@ -104,7 +108,7 @@ async def message_create(client: Client, message: Message):
104108 # update the line count
105109 logging .info ("Updating line count topic..." )
106110 metrics = await get_curr_metrics ()
107- total_line_count = sum (m [ "line_count" ] for m in metrics . values () )
111+ total_line_count = sum (row [ 1 ] for row in metrics )
108112 max_line_count = await get_curr_max_lines ()
109113 free_lines = max_line_count - total_line_count
110114
0 commit comments