From 4f55dcae2b265b6241c6e28fe141bb2c9d552030 Mon Sep 17 00:00:00 2001 From: pac23 Date: Jul 20 2019 10:49:54 +0000 Subject: added logging functionality --- diff --git a/accept.py b/accept.py index 62f8597..d0ea8a9 100644 --- a/accept.py +++ b/accept.py @@ -106,6 +106,8 @@ def accept(args): service_id = config['TAIGA']['SERVICE_ID'] # service id service_token = config['TAIGA']['SERVICE_TOKEN'] project_id = config['TAIGA']['PROJECT_ID'] + log = config['LOG']['LOCATION'] + setup_logging_to_file(log) value = args.issue for x in value: # iteratingover name space object try: @@ -138,4 +140,5 @@ def accept(args): print(issuel) except Exception as e: print(e, "Unable to accept the Change Proposal") + log_exception(e) diff --git a/bz/bugzilla_auth.py b/bz/bugzilla_auth.py index 5cdb4ab..b654965 100644 --- a/bz/bugzilla_auth.py +++ b/bz/bugzilla_auth.py @@ -34,6 +34,8 @@ def reset(args): service_id = config['BUGZILLA']['SERVICE_ID'] # service id service_token = config['BUGZILLA']['SERVICE_TOKEN'] # service d for auth bz_url = config['BUGZILLA']['BUGZILLA_URL'] # taiga server note + log = config['LOG']['LOCATION'] + setup_logging_to_file(log) print("You User Id and password will be securly store in the Keyring") username = input("Enter your user id for bugzilla: ") password = getpass.getpass("Enter your password for bugzilla: ") @@ -49,3 +51,4 @@ def reset(args): print("Password and api key reset succesfully") except (keyring.errors.PasswordSetError, Exception) as e: print("Failed to reset password & api key", e) + log_exception(e) diff --git a/bz/pgba.py b/bz/pgba.py index 606dac2..1cd4d30 100644 --- a/bz/pgba.py +++ b/bz/pgba.py @@ -130,28 +130,38 @@ def pgtobz(value, userstoryid, configpath): except requests.exceptions.RequestException as e: print("Failed to fetch attributes of the issue") print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) except requests.exceptions.RequestException as e: print("Failed to fetch attributes of the issue") print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) except requests.exceptions.RequestException as e: print("Failed to fetch attributes of the issue") print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) else: print("Cant fetch attributes from Taiga") except requests.exceptions.RequestException as e: print("Failed to fetch attributes of the issue") print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) except requests.exceptions.RequestException as e: print("Failed to fetch attributes of the issue") print(e) + log_exception(e) except Exception as e: - print(e) + print(e) + log_exception(e) diff --git a/config.ini b/config.ini index a7f21b2..be9a071 100644 --- a/config.ini +++ b/config.ini @@ -76,3 +76,6 @@ PORT = 465 SMTP_SERVER = smtp.gmail.com ; TEST = yes ; 1892835 + +[LOG] +LOCATION = "main.log" \ No newline at end of file diff --git a/mail/climail.py b/mail/climail.py index 30eafad..ba969c5 100644 --- a/mail/climail.py +++ b/mail/climail.py @@ -55,6 +55,8 @@ def mails(args): service_id = config['EMAIL']['SERVICE_ID'] sender = config['EMAIL']['SENDER_EMAIL_ID'] reciver = config['EMAIL']['LIST'] + log = config['LOG']['LOCATION'] + setup_logging_to_file(log) print(args) value = args.ml for x in value: @@ -102,11 +104,14 @@ def mails(args): print("Mail was not sent check logs for errors") except Exception as e: print(e) + log_exception(e) else: print("check logs") except requests.exceptions.RequestException as e: print(e) + log_exception(e) else: print("unable to send emails check logs") except requests.exceptions.RequestException as e: print(e) + log_exception(e) diff --git a/mail/mailauth.py b/mail/mailauth.py index 23df271..45c695a 100644 --- a/mail/mailauth.py +++ b/mail/mailauth.py @@ -28,3 +28,4 @@ def reset(args): print("Password reset succesfully") except (keyring.errors.PasswordSetError, Exception) as e: print("Failed to reset password", e) + log_exception(e) diff --git a/mail/maillist.py b/mail/maillist.py index 79fa0ed..e0c84a6 100644 --- a/mail/maillist.py +++ b/mail/maillist.py @@ -91,3 +91,4 @@ def email_send(subject, replyto, config): return True except Exception as e: print(e) + log_exception(e) diff --git a/pgure/fesco.py b/pgure/fesco.py index 73dac8d..8caaa17 100644 --- a/pgure/fesco.py +++ b/pgure/fesco.py @@ -27,6 +27,8 @@ def fesissue(args): service_token = config['TAIGA']['SERVICE_TOKEN'] project_id = config['TAIGA']['PROJECT_ID'] value = args.fesco + log = config['LOG']['LOCATION'] + setup_logging_to_file(log) for x in value: # iteratingover name space object try: r = tgaissue.get_issue(x, config) # x is user story refrence id @@ -95,13 +97,17 @@ def fesissue(args): print("Check logs for errors",json.loads(r.content)) except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) else: print("Unable to post to fesco") except requests.exceptions.RequestException as e: print(e) + log_exception(e) else: print("Unable to fetch details from taiga for issue creation") except requests.exceptions.RequestException as e: print(e) + log_exception(e) diff --git a/pgure/issues.py b/pgure/issues.py index 9b8131c..86e8162 100644 --- a/pgure/issues.py +++ b/pgure/issues.py @@ -31,8 +31,10 @@ def list_issue(config): return r except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) def get_issue(issueid, config): @@ -55,8 +57,10 @@ def get_issue(issueid, config): return r except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) def post_issue(payload, repo, config): @@ -81,5 +85,7 @@ def post_issue(payload, repo, config): return r except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: - print(e) \ No newline at end of file + print(e) + log_exception(e) \ No newline at end of file diff --git a/pgure/pagure_auth.py b/pgure/pagure_auth.py index 3d5d547..6654f62 100644 --- a/pgure/pagure_auth.py +++ b/pgure/pagure_auth.py @@ -15,6 +15,8 @@ def reset(args): print("Your auth token will be securly store in the Keyring") # username = input("Enter your user id for taiga: ") service_id = config['PAGURE']['SERVICE_ID'] + log = config['LOG']['LOCATION'] + setup_logging_to_file(log) #repo_name = input("Enter the repo name you want to set api key for: ") fescotoken = getpass.getpass("Enter your token for" + ' ' + "fesco" + ' ' "repository: ") releasetoken = getpass.getpass("Enter your token for" + ' ' + "release-notes" + ' ' "repository: ") @@ -27,4 +29,5 @@ def reset(args): except (keyring.errors.PasswordSetError, Exception) as e: print("Failed to reset token") print(e) + log_exception(e) # print(e + "Contact the developers asap") diff --git a/tga/convert.py b/tga/convert.py index 76dbed4..dfc6036 100644 --- a/tga/convert.py +++ b/tga/convert.py @@ -36,6 +36,8 @@ def convert(value, configpath): service_token = config['TAIGA']['SERVICE_TOKEN'] project_id = config['TAIGA']['PROJECT_ID'] taiga_url = config['TAIGA']['TAIGA_URL'] + log = config['LOG']['LOCATION'] + setup_logging_to_file(log) #value = args.taiga for x in value: try: @@ -208,25 +210,32 @@ def convert(value, configpath): print("Unnsucessful to map to epics,change user story status to accepted and issue status to processed") except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) else: print("Unsuccesful Conversion") else: print("Unsuccesful conversion") except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: - print(e) + print(e) + log_exception(e) else: print("Unsuccesful Conversion") except requests.exceptions.RequestException as e: print("Failed to fetch attributes of the issue") print(e) + log_exception(e) else: print("Unable to Convert Issue,Check logs and contact the developer", json.loads(r.content)) except Exception as e: print(e) + log_exception(e) except requests.exceptions.RequestException as e: print(e) + log_exception(e) diff --git a/tga/epics.py b/tga/epics.py index 69a0a9b..042ada6 100644 --- a/tga/epics.py +++ b/tga/epics.py @@ -48,8 +48,10 @@ def list_epic(config): return r except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) def post_relatedus(USERSTORY_id, epicid, epicref, config): @@ -75,8 +77,10 @@ def post_relatedus(USERSTORY_id, epicid, epicref, config): return r except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) def listrelepic_userstory(epicid, config): #url = str(config['TAIGA']['TAIGA_URL']) + "/api/v1/userstories?project=" + str(config['TAIGA']['PROJECT_ID']) + "&epic=" + epicid @@ -95,5 +99,7 @@ def listrelepic_userstory(epicid, config): return r except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: - print(e) \ No newline at end of file + print(e) + log_exception(e) \ No newline at end of file diff --git a/tga/issues.py b/tga/issues.py index 1ff6f6f..eaf7fc4 100644 --- a/tga/issues.py +++ b/tga/issues.py @@ -78,13 +78,17 @@ def list_issue(args): print(r.content) except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) def get_issue(x, config): + try: """ config = configparser.ConfigParser() @@ -105,8 +109,10 @@ def get_issue(x, config): return url, r except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) def caget_issue(x, config): """ @@ -131,8 +137,10 @@ def caget_issue(x, config): return r except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) def changestatus_issue(x, payload, config): @@ -161,8 +169,10 @@ def changestatus_issue(x, payload, config): return r except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) def edit_issue(x, id, config): #url = str(config['TAIGA']['TAIGA_URL']) + "/api/v1/issues?project=" + config['TAIGA']['PROJECT_ID'] + "&status=" + str(config['TAIGA']['ISSUE_STATUS_NEW']) @@ -183,5 +193,7 @@ def edit_issue(x, id, config): return r except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: - print(e) \ No newline at end of file + print(e) + log_exception(e) \ No newline at end of file diff --git a/tga/taiga_auth.py b/tga/taiga_auth.py index 3716499..d772c92 100644 --- a/tga/taiga_auth.py +++ b/tga/taiga_auth.py @@ -39,6 +39,8 @@ def refresh_token(args): configpath = args.config config = configparser.ConfigParser() config.read(configpath) + log = config['LOG']['LOCATION'] + setup_logging_to_file(log) headers = { 'Content-Type': 'application/json', } @@ -76,14 +78,18 @@ def refresh_token(args): print("auth error") except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) def reset(args): configpath = args.config config = configparser.ConfigParser() config.read(configpath) + log = config['LOG']['LOCATION'] + setup_logging_to_file(log) headers = { 'Content-Type': 'application/json', } @@ -108,6 +114,8 @@ def reset(args): print("Password reset succesfully") except (keyring.errors.PasswordSetError, Exception) as e: print("Failed to reset password") + log_exception(e) except Exception as e: print(e) + log_exception(e) diff --git a/tga/userstory.py b/tga/userstory.py index e3cdb64..901d8f0 100644 --- a/tga/userstory.py +++ b/tga/userstory.py @@ -76,10 +76,13 @@ def list_us(args): except requests.exceptions.RequestException as e: print(e) print(r.content) + log_exception(e) except Exception as e: print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) def get_us(usid, config): @@ -104,8 +107,10 @@ def get_us(usid, config): return r except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) def caget_us(usid, config): @@ -122,8 +127,10 @@ def caget_us(usid, config): return r except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) def post_us(x, config): @@ -145,8 +152,10 @@ def post_us(x, config): return r except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) def post_usca(x, id, config): @@ -176,8 +185,10 @@ def post_usca(x, id, config): return r except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) def changestatus_userstory(x, payload, config): @@ -207,8 +218,10 @@ def changestatus_userstory(x, payload, config): return r except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) def listrelepic_userstory(epicid, config): @@ -221,8 +234,10 @@ def listrelepic_userstory(epicid, config): return r except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) """ curl -X GET \ -H "Content-Type: application/json" \ diff --git a/update.py b/update.py index 29b48d0..7b841e9 100644 --- a/update.py +++ b/update.py @@ -21,12 +21,13 @@ from pgure import issues as pgaissues from bz import bugs as bzbugs from urllib.parse import urlparse from collections import OrderedDict +from logger import setup_logging_to_file, log_exception def sync(x, config): # fvid is fedora version - configpath = args.config - config = configparser.ConfigParser() - config.read(configpath) + #configpath = args.config + #config = configparser.ConfigParser() + #config.read(configpath) #fvid = args.sync_status bzapi = bugzilla.Bugzilla(config['BUGZILLA']['BUGZILLA_URL']) #for x in fvid: @@ -96,124 +97,135 @@ def sync(x, config): print("Check logs for errors,Unable to Sync",r.content, r.status_code) except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) else: print("Error in fetching user story") except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) else: print("Unable to fetch epic related user stories,check logs for errors") else: print("Unable to Sync Check logs", r.content) except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) def report(x,config): try: - r = tgaepic.list_epic(config) - changes = [] - if r.status_code == 200: - data = json.loads(r.content) - #print(data) - for i in data: - print(i['subject']) - if int(i['subject']) == x: - epicid = i['id'] - epicref = i['ref'] - break - - r = tgaepic.listrelepic_userstory(epicid, config) + r = tgaepic.list_epic(config) + changes = [] if r.status_code == 200: data = json.loads(r.content) + #print(data) for i in data: - userstoryid = i['user_story'] - print(userstoryid) + print(i['subject']) + if int(i['subject']) == x: + epicid = i['id'] + epicref = i['ref'] + break + + r = tgaepic.listrelepic_userstory(epicid, config) + if r.status_code == 200: + data = json.loads(r.content) + for i in data: + userstoryid = i['user_story'] + print(userstoryid) + try: + r = tgauserstory.get_us(userstoryid, config) + if r.status_code == 200: + userstorydata = json.loads(r.content) + userstory_version = userstorydata['version'] + userstoryref = userstorydata['ref'] + description = userstorydata['description'] + title = userstorydata['subject'] + r = tgauserstory.caget_us(userstoryid, config) + userstorycadata = json.loads(r.content) + bztracker = userstorycadata['attributes_values'][config['TAIGA']['USERSTORY_CA_BUGZILLA_TRACKER']] + summary = userstorycadata['attributes_values'][config['TAIGA']['USERSTORY_CA_SUMMARY']] + #owner = userstorycadata['attributes_values'][config['TAIGA']['USERSTORY_CA_OWNERS']] + email = userstorycadata['attributes_values'][config['TAIGA']['USERSTORY_CA_BUGZILLA_CONTACT']] + target = x + type = userstorycadata['attributes_values'][config['TAIGA']['USERSTORY_CA_SYSTEM_WIDE_CHANGE']] + status = userstorydata['status_extra_info']['name'] + subject = userstorydata['subject'] + print(type, "this is type") + #record['Updated'] = page.get("last updated") + + status = userstorydata['status'] + #changes.append(userstorycadata) + """ + Json Rebuilding is required because + config cant pe passed to the template + and since the user story endpoint only + returns the main userstory and we have + tp call another userstoyr custom attr + endpoint to retrive teh custom atirbutes + all this needs to be clubbed into one json + to be appended to the jinja2 html template + """ + reportjson = {"summary": summary, "contact": email, + "fedora_version": x, "bugzillatracker": bztracker, "description": description, + "type": type, "status": status, "title": subject } + #rjson = json.dumps(reportjson) + """ + types = {'sys_change': [], 'self_change': []} + for n in reportjson: + if n['type'] == True: + types['sys_change'].append(n) + else: + if n['type'] == False: + types['self_change'].append(n) + """ + changes.append(reportjson) + else: + print("Unable to fetch data to generate report, check logs") + except requests.exceptions.RequestException as e: + print(e) + log_exception(e) + except Exception as e: + print(e) + log_exception(e) + """ + for n in changes: + #print(n['attributes_values'][config['TAIGA']['USERSTORY_CA_SUMMARY']]) + print(n) + """ + #print(changes) try: - r = tgauserstory.get_us(userstoryid, config) - if r.status_code == 200: - userstorydata = json.loads(r.content) - userstory_version = userstorydata['version'] - userstoryref = userstorydata['ref'] - description = userstorydata['description'] - title = userstorydata['subject'] - r = tgauserstory.caget_us(userstoryid, config) - userstorycadata = json.loads(r.content) - bztracker = userstorycadata['attributes_values'][config['TAIGA']['USERSTORY_CA_BUGZILLA_TRACKER']] - summary = userstorycadata['attributes_values'][config['TAIGA']['USERSTORY_CA_SUMMARY']] - #owner = userstorycadata['attributes_values'][config['TAIGA']['USERSTORY_CA_OWNERS']] - email = userstorycadata['attributes_values'][config['TAIGA']['USERSTORY_CA_BUGZILLA_CONTACT']] - target = x - type = userstorycadata['attributes_values'][config['TAIGA']['USERSTORY_CA_SYSTEM_WIDE_CHANGE']] - status = userstorydata['status_extra_info']['name'] - subject = userstorydata['subject'] - print(type, "this is type") - #record['Updated'] = page.get("last updated") - - status = userstorydata['status'] - #changes.append(userstorycadata) - """ - Json Rebuilding is required because - config cant pe passed to the template - and since the user story endpoint only - returns the main userstory and we have - tp call another userstoyr custom attr - endpoint to retrive teh custom atirbutes - all this needs to be clubbed into one json - to be appended to the jinja2 html template - """ - reportjson = {"summary": summary, "contact": email, - "fedora_version": x, "bugzillatracker": bztracker, "description": description, - "type": type, "status": status, "title": subject } - #rjson = json.dumps(reportjson) - """ - types = {'sys_change': [], 'self_change': []} - for n in reportjson: - if n['type'] == True: - types['sys_change'].append(n) - else: - if n['type'] == False: - types['self_change'].append(n) - """ - changes.append(reportjson) - else: - print("Unable to fetch data to generate report, check logs") - except requests.exceptions.RequestException as e: - print(e) + templateLoader = jinja2.FileSystemLoader(searchpath="./" ) + print("this step first before that") + templateEnv = jinja2.Environment( loader=templateLoader ) + template = templateEnv.get_template("tplate1.html") + print("this step") + templateVars = {'changes': changes, 'fedoraversion': x} + print(templateVars) + outputText = template.render(templateVars) + print(outputText) + with open("test.html", "w") as rhtml: + rhtml.write(outputText) except Exception as e: print(e) - """ - for n in changes: - #print(n['attributes_values'][config['TAIGA']['USERSTORY_CA_SUMMARY']]) - print(n) - """ - #print(changes) - try: - templateLoader = jinja2.FileSystemLoader(searchpath="./" ) - print("this step first before that") - templateEnv = jinja2.Environment( loader=templateLoader ) - template = templateEnv.get_template("tplate1.html") - print("this step") - templateVars = {'changes': changes, 'fedoraversion': x} - print(templateVars) - outputText = template.render(templateVars) - print(outputText) - with open("test.html", "w") as rhtml: - rhtml.write(outputText) - except Exception as e: - print(e) + log_exception(e) + else: + print("Failed to fetch data from taiga to generate report") else: - print("Failed to fetch data from taiga to generate report") - else: - print("Failed to fetch epics and realted userstories") + print("Failed to fetch epics and realted userstories") except requests.exceptions.RequestException as e: print(e) + log_exception(e) except Exception as e: print(e) + log_exception(e) """ @@ -238,6 +250,8 @@ def update(args): service_id = config['TAIGA']['SERVICE_ID'] # service id service_token = config['TAIGA']['SERVICE_TOKEN'] project_id = config['TAIGA']['PROJECT_ID'] + log = config['LOG']['LOCATION'] + setup_logging_to_file(log) fedora_version = args.release for x in fedora_version: try: @@ -252,6 +266,7 @@ def update(args): print("Failed to Generate Report") except Exception as e: print(e) + log_exception(e) """ if __name__ == "__main__":