Skip to content

Commit

Permalink
Refactoring with inits
Browse files Browse the repository at this point in the history
  • Loading branch information
ptichoid committed Aug 23, 2024
1 parent a48a266 commit c6f6506
Show file tree
Hide file tree
Showing 26 changed files with 404 additions and 275 deletions.
Empty file added __init__.py
Empty file.
9 changes: 9 additions & 0 deletions config/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from .classes import EnvVariables, Database, Timer
import logging


def setup_logging():
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')


__all__ = ['EnvVariables', 'Database', 'Timer']
Binary file added config/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file added config/__pycache__/classes.cpython-38.pyc
Binary file not shown.
78 changes: 78 additions & 0 deletions config/classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""
This script contains data classes for code reusing
"""
import logging
import time
import os

import psycopg2


class EnvVariables:
required_env_vars = [
"DB_HOST", "DB_PORT", "DB_CSV", "DB_USER", "DB_ORPH", "DB_ZUUL", "DB_PASSWORD", "GITEA_TOKEN", "GITHUB_TOKEN",
"GITHUB_FALLBACK_TOKEN"
]

def __init__(self):
self.db_host = os.getenv("DB_HOST")
self.db_port = os.getenv("DB_PORT")
self.db_csv = os.getenv("DB_CSV") # main postgres db, open PRs tables for public and hybrid clouds are stored
self.db_user = os.getenv("DB_USER")
self.db_orph = os.getenv("DB_ORPH")
self.db_zuul = os.getenv("DB_ZUUL")
self.db_password = os.getenv("DB_PASSWORD")
self.gitea_token = os.getenv("GITEA_TOKEN")
self.github_token = os.getenv("GITHUB_TOKEN")
self.github_fallback_token = os.getenv("GITHUB_FALLBACK_TOKEN")
self.api_key = os.getenv("OTC_BOT_API")
self.check_env_variables()

def check_env_variables(self):
for var in self.required_env_vars:
if os.getenv(var) is None:
raise Exception("Missing environment variable: %s" % var)


class Database:
def __init__(self, env):
self.db_host = env.db_host
self.db_port = env.db_port
self.db_user = env.db_user
self.db_password = env.db_password

def connect_to_db(self, db_name):
logging.info("Connecting to Postgres (%s)...", db_name)
try:
return psycopg2.connect(
host=self.db_host,
port=self.db_port,
dbname=db_name,
user=self.db_user,
password=self.db_password
)
except psycopg2.Error as e:
logging.error("Connecting to Postgres: an error occurred while trying to connect to the database: %s", e)
return None


class Timer:
def __init__(self):
self.start_time = None
self.end_time = None

def start(self):
self.start_time = time.time()

def stop(self):
self.end_time = time.time()
self.report()

def report(self):
if self.start_time and self.end_time:
execution_time = self.end_time - self.start_time
minutes, seconds = divmod(execution_time, 60)
logging.info(f"Script executed in {int(minutes)} minutes {int(seconds)} seconds! Let's go drink some beer "
":)")
else:
logging.error("Timer was not properly started or stopped")
45 changes: 45 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
This script is an entry point for all other modules included in Eyes-on-Docs
"""

import argparse
import sys
from scripts import eod_1_otc_services_dict, eod_2_gitea_info, eod_3_github_info, eod_4_failed_zuul,\
eod_5_open_issues, eod_6_last_commit_info, eod_7_request_changes, eod_8_ecosystem_issues, eod_9_scheduler


def main():
parser = argparse.ArgumentParser(description="Eyes-on-Docs scripts run")
parser.add_argument('--eod1', action='store_true', help='OTC services dict')
parser.add_argument('--eod2', action='store_true', help='Gitea info')
parser.add_argument('--eod3', action='store_true', help='Github info')
parser.add_argument('--eod4', action='store_true', help='Failed Zuul')
parser.add_argument('--eod5', action='store_true', help='Open issues')
parser.add_argument('--eod6', action='store_true', help='Last commit info')
parser.add_argument('--eod7', action='store_true', help='Request changes')
parser.add_argument('--eod8', action='store_true', help='Ecosystem issues')
parser.add_argument('--eod9', action='store_true', help='Scheduler')
args = parser.parse_args()

if args.eod1:
eod_1_otc_services_dict.run()
if args.eod2:
eod_2_gitea_info.run()
if args.eod3:
eod_3_github_info.run()
if args.eod4:
eod_4_failed_zuul.run()
if args.eod5:
eod_5_open_issues.run()
if args.eod6:
eod_6_last_commit_info.run()
if args.eod7:
eod_7_request_changes.run()
if args.eod8:
eod_8_ecosystem_issues.run()
if args.eod9:
eod_9_scheduler.run()


if __name__ == "__main__":
main()
Empty file added scripts/__init__.py
Empty file.
Binary file added scripts/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
31 changes: 15 additions & 16 deletions 1_otc_services_dict.py → scripts/eod_1_otc_services_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,12 @@
service postgres tables, to match repo names, service full names and its squads
"""

import base64
import logging
import time

import psycopg2
import base64
import requests
import yaml

from classes import Database, EnvVariables

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

start_time = time.time()

logging.info("-------------------------OTC SERVICES DICT SCRIPT IS RUNNING-------------------------")
from config import Database, EnvVariables, setup_logging, Timer

BASE_URL = "https://gitea.eco.tsi-dev.otc-service.com/api/v1"

Expand Down Expand Up @@ -281,7 +272,14 @@ def main(base_dir, rtctable, doctable, styring_path):
conn_csv.close()


if __name__ == "__main__":
def run():
timer = Timer()
timer.start()

setup_logging()

logging.info("-------------------------OTC SERVICES DICT SCRIPT IS RUNNING-------------------------")

BASE_DIR_SWISS = "/repos/infra/otc-metadata-swiss/contents/"
BASE_DIR_REGULAR = "/repos/infra/otc-metadata/contents/"
STYRING_URL_REGULAR = "/repos/infra/gitstyring/contents/data/github/orgs/opentelekomcloud-docs/data.yaml?token="
Expand All @@ -297,7 +295,8 @@ def main(base_dir, rtctable, doctable, styring_path):
conn_csv.commit()
conn_csv.close()

end_time = time.time()
execution_time = end_time - start_time
minutes, seconds = divmod(execution_time, 60)
logging.info("Script executed in %s minutes %s seconds! Let's go drink some beer :)", int(minutes), int(seconds))
timer.stop()


if __name__ == "__main__":
run()
27 changes: 13 additions & 14 deletions 2_gitea_info.py → scripts/eod_2_gitea_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,12 @@
import logging
import pathlib
import re
import time

import psycopg2
import requests
from github import Github

from classes import Database, EnvVariables

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

start_time = time.time()

logging.info("-------------------------OPEN PRs SCRIPT IS RUNNING-------------------------")
from config import Database, EnvVariables, setup_logging, Timer

GITEA_API_ENDPOINT = "https://gitea.eco.tsi-dev.otc-service.com/api/v1"
session = requests.Session()
Expand Down Expand Up @@ -498,7 +491,6 @@ def update_squad_and_title(cursors, conns, rtctable, opentable):


def main(org, gh_org, rtctable, opentable, string, token):

csv_erase(["proposalbot_prs.csv", "doc_exports_prs.csv", "orphaned_prs.csv"])

conn_csv = database.connect_to_db(env_vars.db_csv)
Expand Down Expand Up @@ -538,7 +530,13 @@ def main(org, gh_org, rtctable, opentable, string, token):
conn.close()


if __name__ == "__main__":
def run():
timer = Timer()
timer.start()

setup_logging()
logging.info("-------------------------OPEN PRs SCRIPT IS RUNNING-------------------------")

RTC_TABLE = "repo_title_category"
OPEN_TABLE = "open_prs"
ORG_STRING = "docs"
Expand All @@ -561,7 +559,8 @@ def main(org, gh_org, rtctable, opentable, string, token):
logging.info("Github operations successfully done!")

csv_erase(["proposalbot_prs.csv", "doc_exports_prs.csv", "orphaned_prs.csv"])
end_time = time.time()
execution_time = end_time - start_time
minutes, seconds = divmod(execution_time, 60)
logging.info("Script executed in %s minutes %s seconds! Let's go drink some beer :)", int(minutes), int(seconds))
timer.stop()


if __name__ == "__main__":
run()
Loading

0 comments on commit c6f6506

Please sign in to comment.