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 os
import pandas as pd
import shutil
import stat
import time;
import yaml
@ -15,6 +17,74 @@ from pathlib import Path
t = time.strftime("%Y%m%d-%H%M%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):
logging.info("fetching branches...")
@ -26,11 +96,12 @@ def fetch_branches(repo):
def fetch_repository(REPOSITORY_URL, REPOSITORY_PATH):
logging.info("fetching repository %s", str(REPOSITORY_URL))
# git.Repo(Path(config(REPOSITORY_PATH)))
repo = git.Repo.clone_from(REPOSITORY_URL,
Path("../coco_apm_terraform_onboarding"))
# logging.info("fetching repository %s", str(REPOSITORY_URL))
# repo = git.Repo.clone_from(REPOSITORY_URL,
# Path("../coco_apm_terraform_onboarding"))
logging.info("repository path %s", str(REPOSITORY_PATH))
repo = git.Repo(Path(REPOSITORY_PATH))
return repo
@ -174,30 +245,34 @@ if __name__ == "__main__":
# data = adaptDataStructure(dashboards, metric_queries)
# result = evaluate(env, data)
# # writeToExcel(env, t, result)
# all_data[env] = result
target_dirs = ["EMEA_PROD", "EMEA_PREPROD","NA_PROD", "NA_PPREPROD",
"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.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:
if not repo.active_branch.is_detached:
repo.git.checkout("master")
else:
# repo.heads.master.checkout()
repo.git.checkout("master")
# repo_ = checkout_master(repo)
repo_ = repo
wd = Path(repo_.git.working_dir)
list_excluded_files = ["providers.tf", "data_source.tf"]
try:
for i, branch in enumerate(list_branches):
repo.git.checkout(branch)
print("i: ", i, " ", branch)
# with open("out.txt", "a+") as f:
# for file in Path(repo.git.working_dir).glob('**/dashboard/*.tf'):
# f.write("%s | %s\n" % (branch, file))
try:
with open(Path("./dashboards.txt"), "a+", encoding="utf-8") as f:
for i, branch in enumerate(list_branches):
repo_.git.checkout(branch)
logging.info("%d - branch: %s", i, str(branch))
for file in glob.glob(str(wd) + '/**/dashboard/*.tf', recursive=True):
if os.path.basename(file) not in list_excluded_files:
f.write("%s | %s\n" % (format_block(branch, 50), file))
except Exception as e:
print(e)
print("finished")
print("Exception:", e)
# delete_directory(Path(config("REPOSITORY_PATH")))
logging.info("finished")