autoland: refactor: fix logging (
bug 1349997); r?gps
Instead of passing 'logger' to functions, follow python conventions and declare
a global.
MozReview-Commit-ID: 14GlGANIstT
--- a/autoland/autoland/autoland.py
+++ b/autoland/autoland/autoland.py
@@ -28,18 +28,20 @@ MAX_TRANSPLANT_ATTEMPTS = 50
MOZREVIEW_COMMENT_LIMIT = 10
# time to wait before attempting to update MozReview after a failure to post
MOZREVIEW_RETRY_DELAY = datetime.timedelta(minutes=5)
# time to wait before retrying a transplant
TRANSPLANT_RETRY_DELAY = datetime.timedelta(minutes=5)
+logger = logging.getLogger('autoland')
-def handle_pending_transplants(logger, dbconn):
+
+def handle_pending_transplants(dbconn):
cursor = dbconn.cursor()
now = datetime.datetime.now()
query = """
SELECT id, destination, request
FROM Transplant
WHERE landed IS NULL
AND (last_updated IS NULL OR last_updated<=%(time)s)
ORDER BY created
@@ -92,17 +94,17 @@ def handle_pending_transplants(logger, d
requester = request['ldap_username']
tree = request['tree'].encode('ascii')
rev = request['rev'].encode('ascii')
trysyntax = request.get('trysyntax', '')
push_bookmark = request.get('push_bookmark', '').encode('ascii')
pingback_url = request.get('pingback_url', '').encode('ascii')
commit_descriptions = request.get('commit_descriptions')
tree_open = current_treestatus.setdefault(
- destination, treestatus.tree_is_open(logger, destination))
+ destination, treestatus.tree_is_open(destination))
if not tree_open:
handle_treeclosed(transplant_id, tree, rev, destination,
trysyntax, pingback_url)
continue
attempts = 0
started = datetime.datetime.now()
@@ -112,18 +114,17 @@ def handle_pending_transplants(logger, d
tree, rev, destination, attempts + 1))
# TODO: We should break the transplant call into two steps, one
# to pull down the commits to transplant, and another
# one to rebase it and attempt to push so we don't
# duplicate work unnecessarily if we have to rebase more
# than once.
os.environ['AUTOLAND_REQUEST_USER'] = requester
- landed, result = transplant.transplant(logger, tree,
- destination, rev,
+ landed, result = transplant.transplant(tree, destination, rev,
trysyntax, push_bookmark,
commit_descriptions)
del os.environ['AUTOLAND_REQUEST_USER']
logger.info('transplant from tree: %s rev: %s attempt: %s: %s' % (
tree, rev, attempts + 1, result))
if landed or 'abort: push creates new remote head' not in result:
@@ -206,17 +207,17 @@ def handle_pending_transplants(logger, d
query = """
insert into MozreviewUpdate(transplant_id,data)
values(%s,%s)
"""
cursor.executemany(query, mozreview_updates)
dbconn.commit()
-def handle_pending_mozreview_updates(logger, dbconn):
+def handle_pending_mozreview_updates(dbconn):
"""Attempt to post updates to mozreview"""
cursor = dbconn.cursor()
query = """
select MozreviewUpdate.id,transplant_id,request,data
from MozreviewUpdate inner join Transplant
on (Transplant.id = MozreviewUpdate.transplant_id)
limit %(limit)s
@@ -277,34 +278,33 @@ def main():
parser.add_argument('--dsn', default=dsn,
help='Postgresql DSN connection string')
args = parser.parse_args()
logging.basicConfig(format='%(asctime)s,%(msecs)d %(name)s '
'%(levelname)s %(message)s',
datefmt='%H:%M:%S',
level=logging.DEBUG)
- logger = logging.getLogger('autoland')
stdout_handler = logging.StreamHandler(sys.stdout)
logger.addHandler(stdout_handler)
logger.info('starting autoland')
dbconn = get_dbconn(args.dsn)
last_error_msg = None
next_mozreview_update = datetime.datetime.now()
while True:
try:
- handle_pending_transplants(logger, dbconn)
+ handle_pending_transplants(dbconn)
# TODO: In normal configuration, all updates will be posted to the
# same MozReview instance, so we don't bother tracking failure to
# post for individual urls. In the future, we might need to
# support this.
if datetime.datetime.now() > next_mozreview_update:
- ok = handle_pending_mozreview_updates(logger, dbconn)
+ ok = handle_pending_mozreview_updates(dbconn)
if ok:
next_mozreview_update += datetime.timedelta(seconds=1)
else:
next_mozreview_update += MOZREVIEW_RETRY_DELAY
time.sleep(0.1)
except KeyboardInterrupt:
break
--- a/autoland/autoland/transplant.py
+++ b/autoland/autoland/transplant.py
@@ -1,30 +1,33 @@
import config
import hglib
import json
+import logging
import os
import re
import tempfile
REPO_CONFIG = {}
+logger = logging.getLogger('autoland')
+
def get_repo_path(tree):
return config.get('repos').get(tree,
os.path.join(os.path.sep, 'repos', tree))
def formulate_hg_error(cmd, output):
# we want to strip out any sensitive --config options
cmd = map(lambda x: x if not x.startswith('bugzilla') else 'xxx', cmd)
return 'hg error in cmd: ' + ' '.join(cmd) + ': ' + output
-def transplant(logger, tree, destination, rev, trysyntax=None,
+def transplant(tree, destination, rev, trysyntax=None,
push_bookmark=False, commit_descriptions=None):
"""Transplant a specified revision and ancestors to the specified tree.
If ``trysyntax`` is specified, a Try commit will be created using the
syntax specified.
"""
# These values can appear in command arguments. Don't let unicode leak
# into these.
@@ -32,22 +35,22 @@ def transplant(logger, tree, destination
assert isinstance(destination, str)
assert isinstance(rev, str)
if push_bookmark:
assert isinstance(push_bookmark, str)
path = get_repo_path(tree)
configs = ['ui.interactive=False']
with hglib.open(path, encoding='utf-8', configs=configs) as client:
- return _transplant(logger, client, tree, destination, rev,
+ return _transplant(client, tree, destination, rev,
trysyntax=trysyntax, push_bookmark=push_bookmark,
commit_descriptions=commit_descriptions)
-def _transplant(logger, client, tree, destination, rev, trysyntax=None,
+def _transplant(client, tree, destination, rev, trysyntax=None,
push_bookmark=False, commit_descriptions=None):
landed = True
result = ''
def run_hg(args):
logger.info('rev: %s: executing: %s' % (rev, args))
out = hglib.util.BytesIO()
out_channels = {b'o': out.write, b'e': out.write}
--- a/autoland/autoland/treestatus.py
+++ b/autoland/autoland/treestatus.py
@@ -1,14 +1,16 @@
import config
+import logging
import re
import requests
TREESTATUS_URL = 'https://treestatus.mozilla-releng.net/trees/'
+logger = logging.getLogger('autoland')
def tree_is_open(logger, tree):
# treestatus running in dev/CI is an older version, with slightly
# different request and response structures.
is_test_env = config.testing()
# Map integration branches to their short form name
m = re.match('ssh://hg\.mozilla\.org/integration/([^/]+)', tree)