diff --git a/pagure-ci/pagure_ci_server.py b/pagure-ci/pagure_ci_server.py index 5e931c6..afc4f2a 100644 --- a/pagure-ci/pagure_ci_server.py +++ b/pagure-ci/pagure_ci_server.py @@ -25,7 +25,7 @@ import trollius import trollius_redis -LOG = logging.getLogger(__name__) +_log = logging.getLogger(__name__) if 'PAGURE_CONFIG' not in os.environ \ and os.path.exists('/etc/pagure/pagure.cfg'): @@ -59,7 +59,7 @@ def handle_messages(): # Inside a while loop, wait for incoming events. while True: reply = yield trollius.From(subscriber.next_published()) - LOG.info( + _log.info( 'Received: %s on channel: %s', repr(reply.value), reply.channel) data = json.loads(reply.value) @@ -67,19 +67,19 @@ def handle_messages(): pr_id = data['pr']['id'] pr_uid = data['pr']['uid'] branch = data['pr']['branch_from'] - LOG.info('Looking for PR: %s', pr_uid) + _log.info('Looking for PR: %s', pr_uid) session = pagure.lib.create_session(pagure.APP.config['DB_URL']) request = pagure.lib.get_request_by_uid(session, pr_uid) - LOG.info('PR retrieved: %s', request) + _log.info('PR retrieved: %s', request) if not request: - LOG.warning( + _log.warning( 'No request could be found from the message %s', data) session.close() continue - LOG.info( + _log.info( "Trigger on %s PR #%s from %s: %s", request.project.fullname, pr_id, request.project_from.fullname, branch) @@ -91,7 +91,7 @@ def handle_messages(): repo = '%s/%s' % ( pagure.APP.config['GIT_URL_GIT'].rstrip('/'), request.project_from.path) - LOG.info( + _log.info( 'Triggering the build at: %s, for repo: %s', url, repo) requests.post( url, @@ -103,10 +103,10 @@ def handle_messages(): } ) else: - LOG.warning('Un-supported CI type') + _log.warning('Un-supported CI type') session.close() - LOG.info('Ready for another') + _log.info('Ready for another') def main(): @@ -124,9 +124,9 @@ def main(): except trollius.ConnectionResetError: pass - LOG.info("End Connection") + _log.info("End Connection") loop.close() - LOG.info("End") + _log.info("End") if __name__ == '__main__': @@ -136,7 +136,7 @@ if __name__ == '__main__': logging.basicConfig(level=logging.DEBUG) # setup console logging - LOG.setLevel(logging.DEBUG) + _log.setLevel(logging.DEBUG) shellhandler = logging.StreamHandler() shellhandler.setLevel(logging.DEBUG) @@ -146,5 +146,5 @@ if __name__ == '__main__': aslog.setLevel(logging.DEBUG) shellhandler.setFormatter(formatter) - LOG.addHandler(shellhandler) + _log.addHandler(shellhandler) main() diff --git a/pagure/docs_server.py b/pagure/docs_server.py index 136a52f..ee58ea6 100644 --- a/pagure/docs_server.py +++ b/pagure/docs_server.py @@ -45,7 +45,7 @@ SHANDLER = logging.StreamHandler() SHANDLER.setLevel(APP.config.get('log_level', 'INFO')) APP.logger.addHandler(SHANDLER) -LOG = APP.logger +_log = logging.getLogger(__name__) TMPL_HTML = ''' @@ -185,7 +185,7 @@ def view_docs(repo, username=None, namespace=None, filename=None): except pagure.exceptions.FileNotFoundException as err: flask.flash(err.message, 'error') except Exception as err: - LOG.exception(err) + _log.exception(err) flask.abort(500, 'Unkown error encountered and reported') mimetype = None diff --git a/pagure/hooks/files/pagure_hook.py b/pagure/hooks/files/pagure_hook.py index d88bdd5..e28f5b9 100755 --- a/pagure/hooks/files/pagure_hook.py +++ b/pagure/hooks/files/pagure_hook.py @@ -5,6 +5,7 @@ relates to an issue. """ +import logging import os import sys @@ -22,6 +23,8 @@ import pagure.exceptions # noqa: E402 import pagure.lib.link # noqa: E402 +_log = logging.getLogger(__name__) + abspath = os.path.abspath(os.environ['GIT_DIR']) @@ -86,7 +89,7 @@ def relates_commit(commitid, issue, app_url=None): print err except SQLAlchemyError as err: # pragma: no cover pagure.SESSION.rollback() - pagure.APP.logger.exception(err) + _log.exception(err) def fixes_relation(commitid, relation, app_url=None): @@ -131,7 +134,7 @@ def fixes_relation(commitid, relation, app_url=None): print err except SQLAlchemyError as err: # pragma: no cover pagure.SESSION.rollback() - pagure.LOG.exception(err) + _log.exception(err) try: if relation.isa == 'issue': @@ -154,7 +157,7 @@ def fixes_relation(commitid, relation, app_url=None): except SQLAlchemyError as err: # pragma: no cover pagure.SESSION.rollback() print 'ERROR', err - pagure.LOG.exception(err) + _log.exception(err) def run_as_post_receive_hook(): diff --git a/pagure/lib/model.py b/pagure/lib/model.py index 5c68750..0c5d0bd 100644 --- a/pagure/lib/model.py +++ b/pagure/lib/model.py @@ -43,7 +43,7 @@ CONVENTION = { BASE = declarative_base(metadata=MetaData(naming_convention=CONVENTION)) -ERROR_LOG = logging.getLogger('pagure.model') +_log = logging.getLogger(__name__) # hit w/ all the id field we use # pylint: disable=invalid-name @@ -114,7 +114,7 @@ def create_default_status(session, acls=None): session.commit() except SQLAlchemyError: # pragma: no cover session.rollback() - ERROR_LOG.debug('Status %s could not be added', ticket_stat) + _log.debug('Status %s could not be added', ticket_stat) for status in ['Open', 'Closed', 'Merged']: pr_stat = StatusPullRequest(status=status) @@ -123,7 +123,7 @@ def create_default_status(session, acls=None): session.commit() except SQLAlchemyError: # pragma: no cover session.rollback() - ERROR_LOG.debug('Status %s could not be added', pr_stat) + _log.debug('Status %s could not be added', pr_stat) for grptype in ['user', 'admin']: grp_type = PagureGroupType(group_type=grptype) @@ -132,7 +132,7 @@ def create_default_status(session, acls=None): session.commit() except SQLAlchemyError: # pragma: no cover session.rollback() - ERROR_LOG.debug('Type %s could not be added', grptype) + _log.debug('Type %s could not be added', grptype) for acl in sorted(acls) or {}: item = ACL( @@ -144,7 +144,7 @@ def create_default_status(session, acls=None): session.commit() except SQLAlchemyError: # pragma: no cover session.rollback() - ERROR_LOG.debug('ACL %s could not be added', acl) + _log.debug('ACL %s could not be added', acl) for access in ['ticket', 'commit', 'admin']: access_obj = AccessLevels(access=access) @@ -153,7 +153,7 @@ def create_default_status(session, acls=None): session.commit() except SQLAlchemyError: session.rollback() - ERROR_LOG.debug('Access level %s could not be added', access) + _log.debug('Access level %s could not be added', access) class AccessLevels(BASE): diff --git a/pagure/lib/notify.py b/pagure/lib/notify.py index f7b2fd7..c6a2777 100644 --- a/pagure/lib/notify.py +++ b/pagure/lib/notify.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """ - (c) 2014-2016 - Copyright Red Hat Inc + (c) 2014-2017 - Copyright Red Hat Inc Authors: Pierre-Yves Chibon @@ -17,6 +17,7 @@ from __future__ import print_function import datetime import hashlib import json +import logging import urlparse import re import smtplib @@ -30,6 +31,9 @@ from email.header import Header from email.mime.text import MIMEText +_log = logging.getLogger(__name__) + + REPLY_MSG = 'To reply, visit the link below' if pagure.APP.config['EVENTSOURCE_SOURCE']: REPLY_MSG += ' or just reply to this email' @@ -286,7 +290,7 @@ def send_email(text, subject, to_mail, [mailto], msg.as_string()) except smtplib.SMTPException as err: - pagure.LOG.exception(err) + _log.exception(err) if smtp: smtp.quit() return msg diff --git a/pagure/lib/repo.py b/pagure/lib/repo.py index ef3c11b..851c360 100644 --- a/pagure/lib/repo.py +++ b/pagure/lib/repo.py @@ -1,13 +1,14 @@ # -*- coding: utf-8 -*- """ - (c) 2015-2016 - Copyright Red Hat Inc + (c) 2015-2017 - Copyright Red Hat Inc Authors: Pierre-Yves Chibon """ +import logging import os import subprocess @@ -17,6 +18,9 @@ import pagure import pagure.exceptions +_log = logging.getLogger(__name__) + + def get_pygit2_version(): ''' Return pygit2 version as a tuple of integers. This is needed for correct version comparison. @@ -74,7 +78,7 @@ class PagureRepo(pygit2.Repository): raise pagure.exceptions.GitConflictsException( 'Pulling remote changes leads to a conflict') else: - pagure.LOG.debug( + _log.debug( 'Un-expected merge result: %s' % ( pygit2.GIT_MERGE_ANALYSIS_NORMAL)) raise AssertionError('Unknown merge analysis result') diff --git a/pagure/ui/app.py b/pagure/ui/app.py index 2fded03..563180b 100644 --- a/pagure/ui/app.py +++ b/pagure/ui/app.py @@ -10,6 +10,7 @@ """ import datetime +import logging from math import ceil import flask @@ -24,6 +25,9 @@ from pagure import (APP, SESSION, login_required, authenticated, admin_session_timedout) +_log = logging.getLogger(__name__) + + @APP.route('/browse/projects', endpoint='browse_projects') @APP.route('/browse/projects/', endpoint='browse_projects') @APP.route('/') @@ -631,7 +635,7 @@ def remove_user_email(): flask.flash('Email removed') except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash('Email could not be removed', 'error') return flask.redirect(flask.url_for('.user_settings')) @@ -666,7 +670,7 @@ def add_user_email(): flask.flash(str(err), 'error') except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash('Email could not be added', 'error') return flask.render_template( @@ -711,7 +715,7 @@ def set_default_email(): flask.flash('Default email set to: %s' % email) except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash('Default email could not be set', 'error') return flask.redirect(flask.url_for('.user_settings')) @@ -743,7 +747,7 @@ def reconfirm_email(): flask.flash(str(err), 'error') except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash('Confirmation email could not be re-sent', 'error') return flask.redirect(flask.url_for('.user_settings')) @@ -772,7 +776,7 @@ def confirm_email(token): flask.flash( 'Could not set the account as active in the db, ' 'please report this error to an admin', 'error') - APP.logger.exception(err) + _log.exception(err) return flask.redirect(flask.url_for('.user_settings')) @@ -824,7 +828,7 @@ def add_api_user_token(): return flask.redirect(flask.url_for('.user_settings')) except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash('API key could not be added', 'error') # When form is displayed after an empty submission, show an error. @@ -868,7 +872,7 @@ def revoke_api_user_token(token_id): flask.flash('Token revoked') except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash( 'Token could not be revoked, please contact an admin', 'error') diff --git a/pagure/ui/fork.py b/pagure/ui/fork.py index 36ab1b1..35afc8c 100644 --- a/pagure/ui/fork.py +++ b/pagure/ui/fork.py @@ -16,10 +16,11 @@ # pylint: disable=too-many-lines -import flask +import logging import os from math import ceil +import flask import filelock import pygit2 from sqlalchemy.exc import SQLAlchemyError @@ -33,6 +34,9 @@ import pagure.forms from pagure import APP, SESSION, login_required, __get_file_in_tree +_log = logging.getLogger(__name__) + + def _get_parent_repo_path(repo): """ Return the path of the parent git repository corresponding to the provided Repository object from the DB. @@ -224,7 +228,7 @@ def request_pull(repo, requestid, username=None, namespace=None): namespace=namespace)) except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash( 'Could not update this pull-request in the database', 'error') @@ -309,7 +313,7 @@ def request_pull_patch(repo, requestid, username=None, namespace=None): namespace=namespace)) except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash( 'Could not update this pull-request in the database', 'error') @@ -375,7 +379,7 @@ def request_pull_edit(repo, requestid, username=None, namespace=None): flask.flash('Pull request edited!') except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash( 'Could not edit this pull-request in the database', 'error') @@ -463,14 +467,14 @@ def pull_request_add_comment( flask.flash(message) except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) if is_js: return 'error' else: flask.flash(str(err), 'error') except filelock.Timeout as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) if is_js: return 'error' else: @@ -562,7 +566,7 @@ def pull_request_drop_comment( flask.flash('Comment removed') except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.error(err) + _log.error(err) flask.flash( 'Could not remove the comment: %s' % commentid, 'error') @@ -632,7 +636,7 @@ def pull_request_edit_comment( flask.flash(message) except SQLAlchemyError, err: # pragma: no cover SESSION.rollback() - APP.logger.error(err) + _log.error(err) if is_js: return 'error' else: @@ -640,7 +644,7 @@ def pull_request_edit_comment( 'Could not edit the comment: %s' % commentid, 'error') except filelock.Timeout as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) if is_js: return 'error' else: @@ -790,13 +794,13 @@ def cancel_request_pull(repo, requestid, username=None, namespace=None): flask.flash('Pull request canceled!') except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash( 'Could not update this pull-request in the database', 'error') except filelock.Timeout as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash( 'We could not save all the info, please try again', 'error') @@ -857,11 +861,11 @@ def set_assignee_requests(repo, requestid, username=None, namespace=None): flask.flash(err.message, 'error') except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash(str(err), 'error') except filelock.Timeout as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash( 'We could not save all the info, please try again', 'error') @@ -1011,7 +1015,7 @@ def new_request_pull( SESSION.commit() except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash( 'Could not register this pull-request in the database', 'error') @@ -1037,7 +1041,7 @@ def new_request_pull( flask.flash(str(err), 'error') except filelock.Timeout as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash( 'We could not save all the info, please try again', 'error') @@ -1191,7 +1195,7 @@ def new_remote_request_pull(repo, username=None, namespace=None): flask.flash('Request created') except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash( 'Could not register this pull-request in ' 'the database', 'error') diff --git a/pagure/ui/groups.py b/pagure/ui/groups.py index b365ad1..df4b3f5 100644 --- a/pagure/ui/groups.py +++ b/pagure/ui/groups.py @@ -8,8 +8,9 @@ """ -import flask +import logging +import flask from sqlalchemy.exc import SQLAlchemyError import pagure @@ -18,6 +19,9 @@ import pagure.lib import pagure.lib.git +_log = logging.getLogger(__name__) + + @pagure.APP.route('/groups/') @pagure.APP.route('/groups') def group_lists(): @@ -93,10 +97,9 @@ def view_group(group): 'Could not add user `%s` to group `%s`.' % ( username, group.group_name), 'error') - pagure.APP.logger.debug( + _log.exception( 'Could not add user `%s` to group `%s`.' % ( username, group.group_name)) - pagure.APP.logger.exception(err) member = False if pagure.authenticated(): @@ -156,9 +159,8 @@ def edit_group(group): flask.flash( 'Could not edit group `%s`.' % (group.group_name), 'error') - pagure.APP.logger.debug( + _log.exception( 'Could not edit group `%s`.' % (group.group_name)) - pagure.APP.logger.exception(err) elif flask.request.method == 'GET': form.display_name.data = group.display_name form.description.data = group.description @@ -207,10 +209,9 @@ def group_user_delete(user, group): 'Could not remove user `%s` from the group `%s`.' % ( user.user, group), 'error') - pagure.APP.logger.debug( + _log.exception( 'Could not remove user `%s` from the group `%s`.' % ( user.user, group)) - pagure.APP.logger.exception(err) return flask.redirect(flask.url_for('.view_group', group=group)) @@ -314,8 +315,7 @@ def add_group(): except SQLAlchemyError as err: # pragma: no cover pagure.SESSION.rollback() flask.flash('Could not create group.') - pagure.APP.logger.debug('Could not create group.') - pagure.APP.logger.exception(err) + _log.exception('Could not create group.') return flask.render_template( 'add_group.html', diff --git a/pagure/ui/issues.py b/pagure/ui/issues.py index 7f5d493..32c44fe 100644 --- a/pagure/ui/issues.py +++ b/pagure/ui/issues.py @@ -15,12 +15,13 @@ import datetime -import flask +import logging import os import re from collections import defaultdict from math import ceil +import flask import filelock import pygit2 import werkzeug.datastructures @@ -39,6 +40,8 @@ from pagure import (APP, SESSION, __get_file_in_tree, login_required, authenticated, urlpattern) +_log = logging.getLogger(__name__) + # URLs @@ -136,7 +139,7 @@ def update_issue(repo, issueid, username=None, namespace=None): except SQLAlchemyError as err: # pragma: no cover is_js = False SESSION.rollback() - APP.logger.error(err) + _log.error(err) if not is_js: flask.flash( 'Could not remove the comment: %s' % commentid, @@ -340,12 +343,12 @@ def update_issue(repo, issueid, username=None, namespace=None): except SQLAlchemyError as err: # pragma: no cover is_js = False SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash(str(err), 'error') except filelock.Timeout as err: # pragma: no cover is_js = False SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash( 'We could not save all the info, please try again', 'error') @@ -428,7 +431,7 @@ def edit_tag(repo, tag, username=None, namespace=None): flask.flash(msg) except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.error(err) + _log.error(err) flask.flash('Could not edit tag: %s' % tag, 'error') return flask.redirect(flask.url_for( @@ -575,7 +578,7 @@ def remove_tag(repo, username=None, namespace=None): flask.flash(msg) except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.error(err) + _log.error(err) flask.flash( 'Could not remove tag: %s' % ','.join(tags), 'error') @@ -943,7 +946,7 @@ def new_issue(repo, username=None, namespace=None): flask.flash(str(err), 'error') except filelock.Timeout as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash( 'We could not save all the info, please try again', 'error') @@ -1092,7 +1095,7 @@ def delete_issue(repo, issueid, username=None, namespace=None): namespace=namespace)) except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash('Could not delete the issue', 'error') return flask.redirect(flask.url_for( @@ -1212,7 +1215,7 @@ def edit_issue(repo, issueid, username=None, namespace=None): flask.flash(str(err), 'error') except filelock.Timeout as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash( 'We could not save all the info, please try again', 'error') @@ -1278,7 +1281,7 @@ def upload_issue(repo, issueid, username=None, namespace=None): ) except filelock.Timeout as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash( 'We could not save all the info, please try again', 'error') @@ -1373,7 +1376,7 @@ def view_issue_raw_file( ktc.to_bytes(data)) except pagure.exceptions.PagureException: # We cannot decode the file, so bail but warn the admins - APP.logger.exception('File could not be decoded') + _log.exception('File could not be decoded') if encoding: mimetype += '; charset={encoding}'.format(encoding=encoding) @@ -1437,14 +1440,14 @@ def edit_comment_issue( flask.flash(message) except SQLAlchemyError, err: # pragma: no cover SESSION.rollback() - APP.logger.error(err) + _log.error(err) if is_js: return 'error' flask.flash( 'Could not edit the comment: %s' % commentid, 'error') except filelock.Timeout as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash( 'We could not save all the info, please try again', 'error') diff --git a/pagure/ui/login.py b/pagure/ui/login.py index cdb6757..bee6ae7 100644 --- a/pagure/ui/login.py +++ b/pagure/ui/login.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """ - (c) 2014-2016 - Copyright Red Hat Inc + (c) 2014-2017 - Copyright Red Hat Inc Authors: Pierre-Yves Chibon @@ -10,6 +10,7 @@ """ import datetime +import logging import urlparse import flask @@ -24,6 +25,9 @@ from pagure import APP, SESSION, login_required from pagure.lib.login import generate_hashed_value, check_password +_log = logging.getLogger(__name__) + + @APP.route('/user/new/', methods=['GET', 'POST']) @APP.route('/user/new', methods=['GET', 'POST']) def new_user(): @@ -62,11 +66,10 @@ def new_user(): flask.flash( 'User created, please check your email to activate the ' 'account') - except SQLAlchemyError as err: # pragma: no cover + except SQLAlchemyError: # pragma: no cover SESSION.rollback() flask.flash('Could not create user.') - APP.logger.debug('Could not create user.') - APP.logger.exception(err) + _log.exception('Could not create user.') return flask.redirect(flask.url_for('auth_login')) @@ -97,7 +100,7 @@ def do_login(): form.password.data, user_obj.password, seed=APP.config.get('PASSWORD_SEED', None)) except pagure.exceptions.PagureException as err: - APP.logger.exception(err) + _log.exception(err) flask.flash('Username or password of invalid format.', 'error') return flask.redirect(flask.url_for('auth_login')) @@ -137,7 +140,7 @@ def do_login(): flask.flash( 'Could not set the session in the db, ' 'please report this error to an admin', 'error') - APP.logger.exception(err) + _log.exception(err) return flask.redirect(next_url) else: @@ -165,7 +168,7 @@ def confirm_user(token): flask.flash( 'Could not set the account as active in the db, ' 'please report this error to an admin', 'error') - APP.logger.exception(err) + _log.exception(err) return flask.redirect(flask.url_for('index')) @@ -204,13 +207,12 @@ def lost_password(): send_lostpassword_email(user_obj) flask.flash( 'Check your email to finish changing your password') - except SQLAlchemyError as err: # pragma: no cover + except SQLAlchemyError: # pragma: no cover SESSION.rollback() flask.flash( 'Could not set the token allowing changing a password.', 'error') - APP.logger.debug('Password lost change - Error setting token.') - APP.logger.exception(err) + _log.exception('Password lost change - Error setting token.') return flask.redirect(flask.url_for('auth_login')) @@ -248,12 +250,11 @@ def reset_password(token): SESSION.commit() flask.flash( 'Password changed') - except SQLAlchemyError as err: # pragma: no cover + except SQLAlchemyError: # pragma: no cover SESSION.rollback() flask.flash('Could not set the new password.', 'error') - APP.logger.debug( + _log.exception( 'Password lost change - Error setting password.') - APP.logger.exception(err) return flask.redirect(flask.url_for('auth_login')) @@ -288,7 +289,7 @@ def change_password(): form.old_password.data, user_obj.password, seed=APP.config.get('PASSWORD_SEED', None)) except pagure.exceptions.PagureException as err: - APP.logger.exception(err) + _log.exception(err) flask.flash( 'Could not update your password, either user or password ' 'could not be checked', 'error') @@ -308,12 +309,11 @@ def change_password(): SESSION.commit() flask.flash( 'Password changed') - except SQLAlchemyError as err: # pragma: no cover + except SQLAlchemyError: # pragma: no cover SESSION.rollback() flask.flash('Could not set the new password.', 'error') - APP.logger.debug( + _log.exception( 'Password change - Error setting new password.') - APP.logger.exception(err) return flask.redirect(flask.url_for('auth_login')) @@ -435,7 +435,7 @@ def _check_session_cookie(): flask.flash( 'Could not prolong the session in the db, ' 'please report this error to an admin', 'error') - APP.logger.exception(err) + _log.exception(err) flask.g.fas_session_id = session_id flask.g.fas_user = user diff --git a/pagure/ui/plugins.py b/pagure/ui/plugins.py index 363a4b3..6447ab6 100644 --- a/pagure/ui/plugins.py +++ b/pagure/ui/plugins.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """ - (c) 2014-2016 - Copyright Red Hat Inc + (c) 2014-2017 - Copyright Red Hat Inc Authors: Pierre-Yves Chibon @@ -11,8 +11,9 @@ # pylint: disable=too-many-branches -import flask +import logging +import flask from sqlalchemy.exc import SQLAlchemyError import pagure.exceptions @@ -23,6 +24,9 @@ from pagure import APP, SESSION, login_required from pagure.exceptions import FileNotFoundException +_log = logging.getLogger(__name__) + + @APP.route('//settings//', methods=('GET', 'POST')) @APP.route('//settings/', methods=('GET', 'POST')) @APP.route('///settings//', methods=('GET', 'POST')) @@ -114,8 +118,7 @@ def view_plugin(repo, plugin, username=None, namespace=None, full=True): SESSION.flush() except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.debug('Could not add plugin %s', plugin.name) - APP.logger.exception(err) + _log.exception('Could not add plugin %s', plugin.name) flask.flash( 'Could not add plugin %s, please contact an admin' % plugin.name) @@ -139,14 +142,14 @@ def view_plugin(repo, plugin, username=None, namespace=None, full=True): plugin.install(repo, dbobj) flask.flash('Hook %s activated' % plugin.name) except FileNotFoundException as err: - pagure.APP.logger.exception(err) + _log.exception(err) flask.abort(404, 'No git repo found') else: try: plugin.remove(repo) flask.flash('Hook %s deactivated' % plugin.name) except FileNotFoundException as err: - pagure.APP.logger.exception(err) + _log.exception(err) flask.abort(404, 'No git repo found') SESSION.commit() diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index e7c5d1a..5a2c3c7 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -19,8 +19,10 @@ import datetime import json +import logging import shutil import os +from cStringIO import StringIO from math import ceil import flask @@ -28,7 +30,6 @@ import pygit2 import kitchen.text.converters as ktc import werkzeug -from cStringIO import StringIO from PIL import Image from pygments import highlight from pygments.formatters import HtmlFormatter @@ -54,6 +55,9 @@ from pagure import (APP, SESSION, __get_file_in_tree, login_required, from pagure.lib import encoding_utils +_log = logging.getLogger(__name__) + + @APP.route('/.git') @APP.route('//.git') @APP.route('/fork//.git') @@ -492,7 +496,7 @@ def view_file(repo, identifier, filename, username=None, namespace=None): Image.open(StringIO(content.data)) output_type = 'image' except IOError as err: - APP.logger.debug( + _log.debug( 'Failed to load image %s, error: %s', filename, err ) output_type = 'binary' @@ -658,7 +662,7 @@ def view_raw_file( encoding = encoding_utils.guess_encoding(ktc.to_bytes(data)) except pagure.exceptions.PagureException: # We cannot decode the file, so bail but warn the admins - APP.logger.exception('File could not be decoded') + _log.exception('File could not be decoded') if encoding: mimetype += '; charset={encoding}'.format(encoding=encoding) @@ -699,7 +703,7 @@ def view_blame_file(repo, filename, username=None, namespace=None): content = encoding_utils.decode(content.data) except pagure.exceptions.PagureException: # We cannot decode the file, so bail but warn the admins - APP.logger.exception('File could not be decoded') + _log.exception('File could not be decoded') flask.abort(500, 'File could not be decoded') lexer = TextLexer() @@ -959,7 +963,7 @@ def new_release(repo, username=None, namespace=None): except pagure.exceptions.PagureException as err: flask.flash(str(err), 'error') except Exception as err: # pragma: no cover - APP.logger.exception(err) + _log.exception(err) flask.flash('Upload failed', 'error') return flask.redirect(flask.url_for( 'view_tags', repo=repo.name, username=username, @@ -1378,7 +1382,7 @@ def change_ref_head(repo, username=None, namespace=None): repo_obj.set_head(reference.name) flask.flash('Default branch updated to %s' % branchname) except Exception as err: # pragma: no cover - APP.logger.exception(err) + _log.exception(err) return flask.redirect(flask.url_for( 'view_settings', username=username, repo=repo.name, @@ -1416,7 +1420,7 @@ def delete_repo(repo, username=None, namespace=None): SESSION.commit() except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash('Could not delete the project', 'error') repopath = os.path.join(APP.config['GIT_FOLDER'], repo.path) @@ -1430,7 +1434,7 @@ def delete_repo(repo, username=None, namespace=None): shutil.rmtree(ticketpath) shutil.rmtree(requestpath) except (OSError, IOError) as err: - APP.logger.exception(err) + _log.exception(err) flask.flash( 'Could not delete all the repos from the system', 'error') @@ -1475,7 +1479,7 @@ def new_repo_hook_token(repo, username=None, namespace=None): flask.flash('New hook token generated') except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash('Could not generate a new token for this project', 'error') return flask.redirect(flask.url_for( @@ -1535,7 +1539,7 @@ def remove_deploykey(repo, keyid, username=None, namespace=None): flask.flash('Deploy key removed') except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash('Deploy key could not be removed', 'error') return flask.redirect(flask.url_for( @@ -1593,7 +1597,7 @@ def remove_user(repo, userid, username=None, namespace=None): flask.flash('User removed') except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash('User could not be removed', 'error') return flask.redirect(flask.url_for( @@ -1656,7 +1660,7 @@ def add_deploykey(repo, username=None, namespace=None): flask.flash(msg, 'error') except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash('Deploy key could not be added', 'error') return flask.render_template( @@ -1734,7 +1738,7 @@ def add_user(repo, username=None, namespace=None): flask.flash(msg, 'error') except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash('User could not be added', 'error') access_levels = pagure.lib.get_access_levels(SESSION) @@ -1801,7 +1805,7 @@ def remove_group_project(repo, groupid, username=None, namespace=None): flask.flash('Group removed') except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash('Group could not be removed', 'error') return flask.redirect(flask.url_for( @@ -1878,7 +1882,7 @@ def add_group_project(repo, username=None, namespace=None): flask.flash(msg, 'error') except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash('Group could not be added', 'error') access_levels = pagure.lib.get_access_levels(SESSION) @@ -2006,7 +2010,7 @@ def add_token(repo, username=None, namespace=None): namespace=namespace)) except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash('User could not be added', 'error') # When form is displayed after an empty submission, show an error. @@ -2067,7 +2071,7 @@ def revoke_api_token(repo, token_id, username=None, namespace=None): flask.flash('Token revoked') except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() - APP.logger.exception(err) + _log.exception(err) flask.flash( 'Token could not be revoked, please contact an admin', 'error') @@ -2138,7 +2142,7 @@ def edit_file(repo, branchname, filename, username=None, namespace=None): namespace=namespace, branchname=form.branch.data) ) except pagure.exceptions.PagureException as err: # pragma: no cover - APP.logger.exception(err) + _log.exception(err) flask.flash('Commit could not be done', 'error') data = form.content.data elif flask.request.method == 'GET': @@ -2195,7 +2199,7 @@ def delete_branch(repo, branchname, username=None, namespace=None): branch.delete() flask.flash('Branch `%s` deleted' % branchname) except pygit2.GitError as err: - APP.logger.exception(err) + _log.exception(err) flask.flash('Could not delete `%s`' % branchname, 'error') return flask.redirect(flask.url_for( diff --git a/tests/test_pagure_flask_docs.py b/tests/test_pagure_flask_docs.py index 0715734..52cdbd4 100644 --- a/tests/test_pagure_flask_docs.py +++ b/tests/test_pagure_flask_docs.py @@ -38,7 +38,6 @@ class PagureFlaskDocstests(tests.Modeltests): """ Set up the environnment, ran before every tests. """ super(PagureFlaskDocstests, self).setUp() - pagure.docs_server.LOG.handlers = [] pagure.docs_server.APP.config['TESTING'] = True pagure.docs_server.SESSION = self.session