Skip to content

Commit

Permalink
Fix: adjust message format for Fedora Messaging
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Moura <[email protected]>
  • Loading branch information
phsmoura committed Sep 5, 2024
1 parent 527854a commit 4311a16
Showing 1 changed file with 72 additions and 66 deletions.
138 changes: 72 additions & 66 deletions pluto/build_planet.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@


build_dir = "/pluto/build"
fedora_planet_url_prod = "planet.apps.ocp.fedoraproject.org"
fedora_planet_url_stg = "planet.apps.ocp.stg.fedoraproject.org"
fedora_planet_url_prod = "fedoraplanet.org"
fedora_planet_url_stg = "stg.fedoraplanet.org"

if os.environ.get('OPENSHIFT_BUILD_REFERENCE') == 'staging':
fedora_planet_url = fedora_planet_url_stg
env = "STG."
if os.environ.get("OPENSHIFT_BUILD_REFERENCE") == "staging":
fedora_planet_url = fedora_planet_url_stg
env = "STG."
else:
fedora_planet_url = fedora_planet_url_prod
env = ""
fedora_planet_url = fedora_planet_url_prod
env = ""

std_people_ini_content = f"""
[fedorauniversity]
Expand Down Expand Up @@ -61,85 +61,91 @@

# Reset directories
if not os.path.exists("/var/www/html/images-v2"):
shutil.move("/pluto/images-v2","/var/www/html/")
shutil.move("/pluto/images-v2", "/var/www/html/")
if not os.path.exists("/var/www/html/css-v2"):
shutil.move("/pluto/css-v2","/var/www/html/")
shutil.move("/pluto/css-v2", "/var/www/html/")

subprocess.call('rm -rf ' + build_dir, shell=True)
subprocess.run(['mkdir', '-p', build_dir])
subprocess.call("rm -rf " + build_dir, shell=True)
subprocess.run(["mkdir", "-p", build_dir])

# Kerberos login
subprocess.call(f'kinit -kt {os.environ.get("KRB5_CLIENT_KTNAME")} HTTP/{fedora_planet_url}@{env}FEDORAPROJECT.ORG', shell=True)
subprocess.call(
f'kinit -kt {os.environ.get("KRB5_CLIENT_KTNAME")} HTTP/{fedora_planet_url}@{env}FEDORAPROJECT.ORG',
shell=True,
)

# Get data from fasjson
fasjson = fasjson_client.Client(f"https://fasjson.{env.lower()}fedoraproject.org")
fasjson_response = fasjson.search(
rssurl="*",
group=["fedora-contributor"],
_request_options={
"headers": {"X-Fields": ["username", "human_name", "websites", "rssurls", "emails"]}
}
"headers": {
"X-Fields": ["username", "human_name", "websites", "rssurls", "emails"]
}
},
)

# writing headers ini file -> pluto use this to create tables in SQLite
with open(f"{build_dir}/planet.ini", "a") as f:
f.write(f"title = Fedora People\n")
f.write(f"url = {fedora_planet_url_prod}\n\n")
f.write(std_people_ini_content + "\n")

with open(f"{build_dir}/planet.json","w") as f:
f.write("{\n ")
f.write(f"title = Fedora People\n")
f.write(f"url = {fedora_planet_url_prod}\n\n")
f.write(std_people_ini_content + "\n")

# append users blog in ini file
while True:
for user in fasjson_response.result:
for rssindex, rssurl in enumerate(user["rssurls"]):
if rssurl.startswith("http://"):
print(f"User {user['username']} has a bad RSS URL: {rssurl}")
continue
try:
r = requests.get(rssurl)

if r.status_code==200:
with open(f"{build_dir}/planet.ini","a") as f:
f.write(f"[{user['username']}_{rssindex + 1}]\n ")
f.write(f"name = {user['human_name']}\n ")
try:
f.write(f"link = {user['websites'][0]}\n ")
except (TypeError, IndexError):
# It can be either None (TypeError) or an empty list (IndexError)
print(f"User {user['username']} has a RSS URL but no website.")
f.write(f"feed = {rssurl}\n ")
f.write(f"avatar = https://www.libravatar.org/avatar/{hashlib.md5(user['emails'][0].encode()).hexdigest()}\n ")
f.write(f"author = {user['username']}\n\n")

# Creating json for fedora messaging
with open(f"{build_dir}/planet.json","a") as f:
f.write(f"{user['username']}_{rssindex + 1}]\n ")
f.write(f"name = {user['human_name']}\n ")
for user in fasjson_response.result:
for rssindex, rssurl in enumerate(user["rssurls"]):
if rssurl.startswith("http://"):
print(f"User {user['username']} has a bad RSS URL: {rssurl}")
continue
try:
f.write(f"link = {user['websites'][0]}\n ")
except (TypeError, IndexError):
# It can be either None (TypeError) or an empty list (IndexError)
print(f"User {user['username']} has a RSS URL but no website.")
f.write(f"feed = {rssurl}\n ")
f.write(f"avatar = https://www.libravatar.org/avatar/{hashlib.md5(user['emails'][0].encode()).hexdigest()}\n ")
f.write(f"author = {user['username']}\n\n")
except Exception as e:
print(e)
try:
fasjson_response = fasjson_response.next_page()
except fasjson_client.response.PaginationError:
break

with open(f"{build_dir}/planet.json","a") as f:
f.write("}")
r = requests.get(rssurl)

if r.status_code == 200:
with open(f"{build_dir}/planet.ini", "a") as f:
f.write(f"[{user['username']}_{rssindex + 1}]\n ")
f.write(f"name = {user['human_name']}\n ")
try:
f.write(f"link = {user['websites'][0]}\n ")
except (TypeError, IndexError):
# It can be either None (TypeError) or an empty list (IndexError)
print(
f"User {user['username']} has a RSS URL but no website."
)
f.write(f"feed = {rssurl}\n ")
f.write(
f"avatar = https://www.libravatar.org/avatar/{hashlib.md5(user['emails'][0].encode()).hexdigest()}\n "
)
f.write(f"author = {user['username']}\n\n")
except Exception as e:
print(e)
try:
fasjson_response = fasjson_response.next_page()
except fasjson_client.response.PaginationError:
break

# build planet with pluto
subprocess.call(f'cd /pluto; pluto build {build_dir}/planet.ini -o /var/www/html -d /var/www/html -t planet', shell=True)
subprocess.call(
f"cd /pluto; pluto build {build_dir}/planet.ini -o /var/www/html -d /var/www/html -t planet",
shell=True,
)

# send to fedora messaging
with open(f'{build_dir}/planet.json','r') as f:
planet_users = json.load(f)

api.publish(api.Message(topic="Planet build", body={"Users": planet_users}))
planet_users = dict()
username = None
with open(f"{build_dir}/planet.ini", "r") as f:
for line in f:
line = line.strip()
if not line:
continue
if line.startswith("["):
username = line[1:-1]
planet_users[username] = dict()
elif username is not None:
key, value = line.split("=", 1)
planet_users[username][key.strip()] = value.strip()

api.publish(
api.Message(topic=f"{fedora_planet_url}.build", body={"Users": planet_users})
)

0 comments on commit 4311a16

Please sign in to comment.