Compare commits
6 Commits
75fcbb4452
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4a4fb621ad | ||
|
|
1cac1d2e66 | ||
| 06d3098940 | |||
|
|
cbcd90732d | ||
| 6c7eafb8d3 | |||
|
|
cd60dd5653 |
5
.env.example
Normal file
5
.env.example
Normal file
@@ -0,0 +1,5 @@
|
||||
# Copy this file to .env and fill in your actual tokens
|
||||
# The .env file is gitignored and will be used for local development
|
||||
|
||||
PICABLE_DISCORD_TOKEN=your_discord_token_here
|
||||
PICABLE_GITHUB_TOKEN=your_github_token_here
|
||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,3 +1,7 @@
|
||||
**/__pycache__/
|
||||
**/*_tok
|
||||
**/.idea/
|
||||
|
||||
# Environment variables and local development overrides
|
||||
.env
|
||||
docker-compose.override.yml
|
||||
|
||||
18
Dockerfile
18
Dockerfile
@@ -1,15 +1,21 @@
|
||||
FROM python:3.14-slim
|
||||
|
||||
# Copy your code
|
||||
RUN set -eux; \
|
||||
apt-get update; \
|
||||
apt-get install -y --no-install-recommends cloc git; \
|
||||
apt-get clean; \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
|
||||
WORKDIR /app
|
||||
RUN pip install --no-cache-dir discord asyncio requests
|
||||
RUN pip install --no-cache-dir discord.py requests
|
||||
|
||||
COPY main.py .
|
||||
COPY PICable.py .
|
||||
|
||||
# Drop privileges (optional but good practice)
|
||||
RUN useradd -m appuser
|
||||
RUN useradd -m appuser && \
|
||||
mkdir -p /temp && \
|
||||
chown appuser:appuser /temp
|
||||
|
||||
USER appuser
|
||||
|
||||
# Start the scheduler script
|
||||
CMD ["python", "main.py"]
|
||||
CMD ["python", "-u", "main.py"]
|
||||
|
||||
28
PICable.py
28
PICable.py
@@ -1,15 +1,15 @@
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import requests
|
||||
|
||||
LIMIT_COMMITS_30_DAYS = 50
|
||||
LIMIT_STARS = 200
|
||||
LIMIT_NLOC = 100000
|
||||
LIMIT_COMMITS_30_DAYS = int(os.getenv("LIMIT_COMMITS_30_DAYS", "50"))
|
||||
LIMIT_STARS = int(os.getenv("LIMIT_STARS", "200"))
|
||||
LIMIT_NLOC = int(os.getenv("LIMIT_NLOC", "100000"))
|
||||
|
||||
MARGIN = 0.1
|
||||
CLONE_DIR = "~/temp/PICable"
|
||||
CLONE_DIR = "/temp"
|
||||
EXCLUDED_LANGUAGES = ["CSV", "diff", "INI", "JSON", "Markdown", "reStructuredText", "SVG", "Text", "YAML"]
|
||||
|
||||
|
||||
@@ -53,20 +53,28 @@ def get_lines_of_code(owner, repository):
|
||||
repository_clone_dir = os.path.expanduser(f"{parent_dir}/{repository}")
|
||||
|
||||
try:
|
||||
print(f"Cloning {repository_url}...")
|
||||
subprocess.run(
|
||||
["git", "clone", "--depth", "1", repository_url, repository_clone_dir],
|
||||
["git", "clone", "--depth", "1", "--progress", repository_url, repository_clone_dir],
|
||||
check=True,
|
||||
stdout=sys.stdout,
|
||||
stderr=sys.stderr,
|
||||
)
|
||||
print("Clone complete. Running cloc...")
|
||||
result = subprocess.run(
|
||||
["cloc", f"--exclude-lang={','.join(EXCLUDED_LANGUAGES)}", repository_clone_dir],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True,
|
||||
)
|
||||
number_of_lines_of_code = int(result.stdout.split("\n")[-3].split()[-1])
|
||||
except Exception:
|
||||
|
||||
# Find the SUM line and extract the last column (code lines)
|
||||
for line in result.stdout.split("\n"):
|
||||
if line.strip().startswith("SUM:"):
|
||||
number_of_lines_of_code = int(line.split()[-1])
|
||||
break
|
||||
else:
|
||||
raise ValueError("Could not find SUM line in cloc output")
|
||||
except Exception as e:
|
||||
print(f"Error occurred: {e}")
|
||||
return -1
|
||||
finally:
|
||||
subprocess.run(["rm", "-rf", parent_dir], check=True)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
services:
|
||||
picable:
|
||||
build: .
|
||||
image: didas72/picable:latest
|
||||
container_name: picable
|
||||
environment:
|
||||
- PICABLE_DISCORD_TOKEN=yourdiscordtoken
|
||||
- PICABLE_GITHUB_TOKEN=yourgithubtoken
|
||||
- PICABLE_DISCORD_TOKEN=${PICABLE_DISCORD_TOKEN}
|
||||
- PICABLE_GITHUB_TOKEN=${PICABLE_GITHUB_TOKEN}
|
||||
restart: unless-stopped
|
||||
|
||||
212
main.py
212
main.py
@@ -1,6 +1,5 @@
|
||||
import os
|
||||
from asyncio import Condition, Lock, create_task, shield, to_thread, wait_for
|
||||
from sys import argv
|
||||
from os import getenv
|
||||
|
||||
import requests
|
||||
from discord import Game, Intents, Interaction, app_commands
|
||||
@@ -8,109 +7,110 @@ from discord.ext import commands
|
||||
|
||||
from PICable import PICable
|
||||
|
||||
MAX_STORAGE_KB = 10 * 1024 * 1024 # 10GB
|
||||
MAX_STORAGE_KB = 50 * 1024 * 1024 # 50GB
|
||||
DISCORD_TOKEN = os.getenv("PICABLE_DISCORD_TOKEN")
|
||||
GITHUB_TOKEN = os.getenv("PICABLE_GITHUB_TOKEN")
|
||||
|
||||
current_storage_kb = 0
|
||||
storage_condition = Condition()
|
||||
current_repositories = set()
|
||||
current_repositories_lock = Lock()
|
||||
|
||||
if not DISCORD_TOKEN or not GITHUB_TOKEN:
|
||||
print("Discord or Github tokens not set in env. Use variables PICABLE_DISCORD_TOKEN and PICABLE_GITHUB_TOKEN")
|
||||
exit(1)
|
||||
|
||||
intents = Intents.default()
|
||||
intents.message_content = True
|
||||
client = commands.Bot(command_prefix="/", intents=intents)
|
||||
|
||||
|
||||
@client.event
|
||||
async def on_ready():
|
||||
print(f"We have logged in as {client.user}")
|
||||
await client.change_presence(activity=Game("with PIC ideas"))
|
||||
try:
|
||||
synced = await client.tree.sync()
|
||||
print(f"Synced {len(synced)} command(s)")
|
||||
except Exception as e:
|
||||
print(f"Failed to sync commands: {e}")
|
||||
|
||||
|
||||
@client.tree.command(name="ping", description="Check the bot's latency.")
|
||||
async def ping(interaction: Interaction):
|
||||
latency = client.latency * 1000
|
||||
await interaction.response.send_message(f"Pong! Latency: {latency:.2f}ms")
|
||||
|
||||
|
||||
async def handle_picable(owner: str, repository: str, repository_size_kb: int, github_token: str):
|
||||
global current_storage_kb
|
||||
async with storage_condition:
|
||||
await storage_condition.wait_for(lambda: current_storage_kb + repository_size_kb <= MAX_STORAGE_KB)
|
||||
current_storage_kb += repository_size_kb
|
||||
|
||||
print(f"Storage is {current_storage_kb / MAX_STORAGE_KB * 100:.2f}% full.")
|
||||
return await to_thread(PICable, owner, repository, github_token)
|
||||
|
||||
|
||||
@client.tree.command(name="picable", description="Check if a Github repository is eligible for PIC.")
|
||||
@app_commands.describe(owner="The owner of the repository", repository="The name of the repository")
|
||||
async def picable(interaction: Interaction, owner: str, repository: str):
|
||||
await interaction.response.defer(thinking=True)
|
||||
|
||||
repository_full_name = f"{owner}/{repository}"
|
||||
async with current_repositories_lock:
|
||||
if repository_full_name in current_repositories:
|
||||
await interaction.followup.send(
|
||||
f"The repository {repository_full_name} is already being analyzed. Please wait for the current analysis to complete. :warning:"
|
||||
)
|
||||
return
|
||||
|
||||
current_repositories.add(repository_full_name)
|
||||
|
||||
url = f"https://api.github.com/repos/{owner}/{repository}"
|
||||
headers = {"Authorization": f"token {GITHUB_TOKEN}"}
|
||||
response = requests.get(url, headers=headers)
|
||||
|
||||
if not response.status_code == 200:
|
||||
await interaction.followup.send(
|
||||
"Invalid Repository. Please check the repository name and owner and try again. :x:"
|
||||
)
|
||||
return
|
||||
|
||||
response.raise_for_status()
|
||||
repo_info = response.json()
|
||||
repository_size_kb = repo_info["size"]
|
||||
print(f"Repository size: {repository_size_kb / 1024} MB")
|
||||
|
||||
if repository_size_kb > MAX_STORAGE_KB:
|
||||
await interaction.followup.send(
|
||||
f"The repository {repository_full_name} is too large to analyze. Please try a smaller repository. :warning:"
|
||||
)
|
||||
current_repositories.remove(repository_full_name)
|
||||
return
|
||||
|
||||
global current_storage_kb
|
||||
try:
|
||||
result_future = create_task(handle_picable(owner, repository, repository_size_kb, GITHUB_TOKEN))
|
||||
try:
|
||||
result = await wait_for(shield(result_future), timeout=600)
|
||||
await interaction.followup.send(result)
|
||||
except TimeoutError:
|
||||
await interaction.followup.send(
|
||||
f"The analysis for {repository_full_name} is still taking place. The results will be posted here once the analysis is complete. :clock4:"
|
||||
)
|
||||
result = await result_future
|
||||
await interaction.channel.send(f"{interaction.user.mention}\n{result}")
|
||||
except Exception as e:
|
||||
print(f"Error processing PICable check: {e}")
|
||||
await interaction.channel.send("An error occurred while processing your request. Please try again later.")
|
||||
finally:
|
||||
async with current_repositories_lock:
|
||||
current_repositories.remove(repository_full_name)
|
||||
async with storage_condition:
|
||||
current_storage_kb -= repository_size_kb
|
||||
storage_condition.notify_all()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
current_storage_kb = 0
|
||||
storage_condition = Condition()
|
||||
current_repositories = set()
|
||||
current_repositories_lock = Lock()
|
||||
|
||||
discord_token = getenv("PICABLE_DISCORD_TOKEN")
|
||||
github_token = getenv("PICABLE_GITHUB_TOKEN")
|
||||
|
||||
if not discord_token or not github_token:
|
||||
print("Discord or Github tokens not set in env. Use variables PICABLE_DISCORD_TOKEN and PICABLE_GITHUB_TOKEN")
|
||||
exit(1)
|
||||
|
||||
intents = Intents.default()
|
||||
intents.message_content = True
|
||||
client = commands.Bot(command_prefix="/", intents=intents)
|
||||
client.run(discord_token)
|
||||
|
||||
|
||||
@client.event
|
||||
async def on_ready():
|
||||
print(f"We have logged in as {client.user}")
|
||||
await client.change_presence(activity=Game("with PIC ideas"))
|
||||
try:
|
||||
synced = await client.tree.sync()
|
||||
print(f"Synced {len(synced)} command(s)")
|
||||
except Exception as e:
|
||||
print(f"Failed to sync commands: {e}")
|
||||
|
||||
|
||||
@client.tree.command(name="ping", description="Check the bot's latency.")
|
||||
async def ping(interaction: Interaction):
|
||||
latency = client.latency * 1000
|
||||
await interaction.response.send_message(f"Pong! Latency: {latency:.2f}ms")
|
||||
|
||||
|
||||
async def handle_picable(owner: str, repository: str, repository_size_kb: int, github_token: str):
|
||||
global current_storage_kb
|
||||
async with storage_condition:
|
||||
await storage_condition.wait_for(lambda: current_storage_kb + repository_size_kb <= MAX_STORAGE_KB)
|
||||
current_storage_kb += repository_size_kb
|
||||
|
||||
print(f"Storage is {current_storage_kb / MAX_STORAGE_KB * 100:.2f}% full.")
|
||||
return await to_thread(PICable, owner, repository, github_token)
|
||||
|
||||
|
||||
@client.tree.command(name="picable", description="Check if a Github repository is eligible for PIC.")
|
||||
@app_commands.describe(owner="The owner of the repository", repository="The name of the repository")
|
||||
async def picable(interaction: Interaction, owner: str, repository: str):
|
||||
await interaction.response.defer(thinking=True)
|
||||
|
||||
repository_full_name = f"{owner}/{repository}"
|
||||
async with current_repositories_lock:
|
||||
if repository_full_name in current_repositories:
|
||||
await interaction.followup.send(
|
||||
f"The repository {repository_full_name} is already being analyzed. Please wait for the current analysis to complete. :warning:"
|
||||
)
|
||||
return
|
||||
|
||||
current_repositories.add(repository_full_name)
|
||||
|
||||
url = f"https://api.github.com/repos/{owner}/{repository}"
|
||||
headers = {"Authorization": f"token {github_token}"}
|
||||
response = requests.get(url, headers=headers)
|
||||
|
||||
if not response.status_code == 200:
|
||||
await interaction.followup.send(
|
||||
f"Invalid Repository. Please check the repository name and owner and try again. :x:"
|
||||
)
|
||||
return
|
||||
|
||||
response.raise_for_status()
|
||||
repo_info = response.json()
|
||||
repository_size_kb = repo_info["size"]
|
||||
print(f"Repository size: {repository_size_kb / 1024} MB")
|
||||
|
||||
if repository_size_kb > MAX_STORAGE_KB:
|
||||
await interaction.followup.send(
|
||||
f"The repository {repository_full_name} is too large to analyze. Please try a smaller repository. :warning:"
|
||||
)
|
||||
current_repositories.remove(repository_full_name)
|
||||
return
|
||||
|
||||
global current_storage_kb
|
||||
try:
|
||||
result_future = create_task(handle_picable(owner, repository, repository_size_kb, github_token))
|
||||
try:
|
||||
result = await wait_for(shield(result_future), timeout=600)
|
||||
await interaction.followup.send(result)
|
||||
except TimeoutError:
|
||||
await interaction.followup.send(
|
||||
f"The analysis for {repository_full_name} is still taking place. The results will be posted here once the analysis is complete. :clock4:"
|
||||
)
|
||||
result = await result_future
|
||||
await interaction.channel.send(f"{interaction.user.mention}\n{result}")
|
||||
except Exception as e:
|
||||
print(f"Error processing PICable check: {e}")
|
||||
await interaction.channel.send("An error occurred while processing your request. Please try again later.")
|
||||
finally:
|
||||
async with current_repositories_lock:
|
||||
current_repositories.remove(repository_full_name)
|
||||
async with storage_condition:
|
||||
current_storage_kb -= repository_size_kb
|
||||
storage_condition.notify_all()
|
||||
client.run(DISCORD_TOKEN)
|
||||
|
||||
Reference in New Issue
Block a user