Finished script

This commit is contained in:
Guilhem Lavaux 2024-05-15 13:41:03 +02:00
parent 9e3eb3d5cc
commit 2934c6fb8d
2 changed files with 144 additions and 0 deletions

View File

@ -3,11 +3,18 @@ name = "buguser"
version = "0.1.0"
description = ""
authors = ["Guilhem Lavaux <guilhem.lavaux@iap.fr>"]
license = "MIT"
packages = []
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.12"
click = "^8.1.7"
requests = "^2.31.0"
bs4 = "^0.0.2"
[tool.poetry.scripts]
buguser = "buguser:main"
[build-system]
requires = ["poetry-core"]

137
src/buguser.py Normal file
View File

@ -0,0 +1,137 @@
import textwrap
import smtplib
import requests
import click
import bs4
import os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
email_template = """Dear {name},
I am writing to tell you that you are currently overusing the nodes on Infinity.
Infinity is a shared platform which relies on the cooperation of all users to ensure that everyone has access to the resources they need.
Could you please check your jobs and make sure that you are not using more nodes than you need? For example could you enforce to put your work on only a few nodes at a time?
If you have any questions or need help, please do not hesitate to contact me.
Best regards,
{complain_name}.
"""
url = "https://infinity-users.projet-horizon.fr/index.php"
@click.command()
@click.argument("to_bug")# help="The login of the user to bug")
def main(to_bug):
"""Bug a user on the Infinity platform.
Args:
to_bug (str): The login of the user to bug.
"""
if to_bug is None:
print("Please provide the login of the user to bug")
raise SystemExit(1)
with requests.session() as s:
response = s.post(url, data={"authpw": "astrorizon"})
if response.status_code != 200:
print("Connection failure")
raise SystemExit(1)
r = s.get(url, params={"n": "Site.CurrentUsers"})
soup = bs4.BeautifulSoup(r.text, "html.parser")
users = soup.find("pre")
# Define an empty list to store user data as dictionaries
users_tbl = []
# Iterate through each row in the table and add the data as a dictionary to users list
for row in users.get_text().split("\n")[3:]:
if len(row.strip()) == 0:
continue
# Split each row into columns using space as delimiter
columns = list(map(lambda x: x.strip(), row.split(" | ")[:]))
# Initialize an empty dictionary with keys as column headers
user_data = {
"name": "",
"status": "",
"last_login": "",
"email": "",
"login": "",
"mentor": "",
}
# Assign values to each key in the dictionary from corresponding columns
(
user_data["login"],
user_data["name"],
user_data["status"],
user_data["last_login"],
user_data["email"],
user_data["mentor"],
) = columns
user_data["mentor"] = user_data["mentor"][:-1].strip()
user_data["login"] = user_data["login"][1:].strip()
# Append the populated dictionary to users list
users_tbl.append(user_data)
# Print the parsed data
mapped_login = {user["login"]: user for user in users_tbl}
# Get the current unix login of the user using python os
current_login = os.getlogin()
if not current_login in mapped_login:
print(f"User {current_login} not found")
raise SystemExit(1)
from_user = mapped_login[current_login]
if not to_bug in mapped_login:
print(f"User {to_bug} not found")
raise SystemExit(1)
to_user = mapped_login[to_bug]
email_body = email_template.format(
name=to_user["name"], complain_name=from_user["name"]
)
# Create a multipart message
msg = MIMEMultipart()
msg["From"] = from_user["email"]
msg["To"] = to_user["email"]
msg["CC"] = from_user["email"]
msg["Subject"] = "Your usage of Infinity nodes"
msg.attach(MIMEText(email_body, "plain"))
text = msg.as_string()
print(textwrap.dedent(f"""
Here is the draft of the email that is going to be sent:
-----------------------------------------------------
{text}
-----------------------------------------------------
If you agree with it, please enter your login name to confirm the sending of the email.
"""))
if input("Do you want to send the email? ") != current_login:
print("Email not sent")
raise SystemExit(0)
print("Sending email...")
server = smtplib.SMTP('smtp.iap.fr', 587)
server.sendmail(from_user["email"], [to_user["email"], from_user["email"]], text)
server.quit()
if __name__ == "__main__":
main()