dashboards.txt

master
SLW\ARNAUA 2023-07-13 21:23:59 +02:00
parent e06353151e
commit a8557b1fcc
1 changed files with 98 additions and 23 deletions

121
main.py
View File

@ -4,6 +4,8 @@ import glob
import logging import logging
import os import os
import pandas as pd import pandas as pd
import shutil
import stat
import time; import time;
import yaml import yaml
@ -15,6 +17,74 @@ from pathlib import Path
t = time.strftime("%Y%m%d-%H%M%S") t = time.strftime("%Y%m%d-%H%M%S")
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
def format_block(string, max):
string_length = len(string)
string = (f'{" "*(max-string_length)}{string}')
return string
def onerror(func, path, exc_info):
"""
Error handler for ``shutil.rmtree``.
If the error is due to an access error (read only file)
it attempts to add write permission and then retries.
If the error is for another reason it re-raises the error.
Usage : ``shutil.rmtree(path, onerror=onerror)``
"""
import stat
# Is the error an access error?
if not os.access(path, os.W_OK):
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise
def delete_directory(path):
logging.info("cleaning up...")
try:
shutil.rmtree(path, onerror=onerror)
logging.info("%s directory successfully deleted", str(path))
except OSError as e:
print("Error: %s - %s." % (e.filename, e.strerror))
def checkout_master(repo):
logging.info("master branch name is: %s", str(repo.heads.master.name))
logging.info("checking active branch ...")
if repo.active_branch.name != repo.heads.master.name:
logging.info("active branch name is: %s", str(repo.active_branch.name))
if repo.active_branch.is_detached:
logging.info("active branch (%s) is detached: %s",
str(repo.active_branch.name),
str(repo.active_branch.is_detached))
logging.info("checking out master...")
repo.git.checkout("master")
logging.info("checkout to master successful")
logging.info("active branch is %s and is detached: %s",
str(repo.active_branch.name),
str(repo.active_branch.is_detached))
else:
# repo.heads.master.checkout()
logging.info("active branch (%s) is detached: %s",
str(repo.active_branch.name),
str(repo.active_branch.is_detached))
logging.info("checking out master...")
repo.git.checkout("master")
logging.info("checkout to master successful")
logging.info("active branch is %s and is detached: %s",
str(repo.active_branch.name),
str(repo.active_branch.is_detached))
else:
logging.info("active branch is already master (%s) and is detached: %s",
str(repo.active_branch.name),
str(repo.active_branch.is_detached))
return repo
def fetch_branches(repo): def fetch_branches(repo):
logging.info("fetching branches...") logging.info("fetching branches...")
@ -26,11 +96,12 @@ def fetch_branches(repo):
def fetch_repository(REPOSITORY_URL, REPOSITORY_PATH): def fetch_repository(REPOSITORY_URL, REPOSITORY_PATH):
logging.info("fetching repository %s", str(REPOSITORY_URL)) # logging.info("fetching repository %s", str(REPOSITORY_URL))
# git.Repo(Path(config(REPOSITORY_PATH))) # repo = git.Repo.clone_from(REPOSITORY_URL,
repo = git.Repo.clone_from(REPOSITORY_URL, # Path("../coco_apm_terraform_onboarding"))
Path("../coco_apm_terraform_onboarding")) logging.info("repository path %s", str(REPOSITORY_PATH))
repo = git.Repo(Path(REPOSITORY_PATH))
return repo return repo
@ -174,30 +245,34 @@ if __name__ == "__main__":
# data = adaptDataStructure(dashboards, metric_queries) # data = adaptDataStructure(dashboards, metric_queries)
# result = evaluate(env, data) # result = evaluate(env, data)
# # writeToExcel(env, t, result) # # writeToExcel(env, t, result)
# all_data[env] = result # all_data[env] = result
target_dirs = ["EMEA_PROD", "EMEA_PREPROD","NA_PROD", "NA_PPREPROD", target_dirs = ["EMEA_PROD", "EMEA_PREPROD","NA_PROD", "NA_PPREPROD",
"CN_PROD", "CN_PREPROD"] "CN_PROD", "CN_PREPROD"]
repo = fetch_repository(config("REPOSITORY_URL"), config("REPOSITORY")) repo = fetch_repository(config("REPOSITORY_URL"), config("REPOSITORY_PATH"))
list_branches = fetch_branches(repo) list_branches = fetch_branches(repo)
list_branches.remove("HEAD") list_branches.remove("HEAD")
list_branches.remove("master")
list_branches.remove("template")
list_branches.remove("CD_TS-CMS") # do it manually
if repo.active_branch.name != repo.heads.master.name: # repo_ = checkout_master(repo)
if not repo.active_branch.is_detached: repo_ = repo
repo.git.checkout("master") wd = Path(repo_.git.working_dir)
else: list_excluded_files = ["providers.tf", "data_source.tf"]
# repo.heads.master.checkout()
repo.git.checkout("master")
try: try:
for i, branch in enumerate(list_branches): with open(Path("./dashboards.txt"), "a+", encoding="utf-8") as f:
repo.git.checkout(branch) for i, branch in enumerate(list_branches):
print("i: ", i, " ", branch) repo_.git.checkout(branch)
# with open("out.txt", "a+") as f: logging.info("%d - branch: %s", i, str(branch))
# for file in Path(repo.git.working_dir).glob('**/dashboard/*.tf'): for file in glob.glob(str(wd) + '/**/dashboard/*.tf', recursive=True):
# f.write("%s | %s\n" % (branch, file)) if os.path.basename(file) not in list_excluded_files:
f.write("%s | %s\n" % (format_block(branch, 50), file))
except Exception as e: except Exception as e:
print(e) print("Exception:", e)
print("finished") # delete_directory(Path(config("REPOSITORY_PATH")))
logging.info("finished")