findmissing/synchronize: Reduce noise when running tool commands

We don't want to display log commands while running tool commands.
At the same time, we do want to see activity logs if the synchronization
script runs on GCE. To address the problem, introduce logging facility
and start using it for database synchronization. Set the log level for
local commands to WARNING and for commands executed on gce to INFO.

BUG=None
TEST=Run scripts on target

Change-Id: Ia15cbde84bc810e8340234930565158acfb81ebd
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/dev-util/+/2189517
Reviewed-by: Curtis Malainey <cujomalainey@chromium.org>
Commit-Queue: Guenter Roeck <groeck@chromium.org>
Tested-by: Guenter Roeck <groeck@chromium.org>
diff --git a/contrib/findmissing/main.py b/contrib/findmissing/main.py
index eb96f69..5f3e954 100755
--- a/contrib/findmissing/main.py
+++ b/contrib/findmissing/main.py
@@ -12,6 +12,7 @@
 
 from __future__ import print_function
 
+import logging
 import os
 import subprocess
 import sys
@@ -64,8 +65,13 @@
         def wrapped_preliminary_check(*args):
             """Sanity checks on state of environment before executing decorated function."""
             if is_gce:
+                level = logging.INFO
                 # Ensures we have service account credentials to connect to cloudsql (GCP)
                 check_service_key_secret_exists()
+            else:
+                level = logging.WARNING
+
+            logging.basicConfig(format='%(levelname)s: %(message)s', level=level)
 
             # Ensure cloudsql proxy is running to allow connection
             check_cloud_sql_proxy_running()
diff --git a/contrib/findmissing/synchronize.py b/contrib/findmissing/synchronize.py
index 4eb8e8c..ca161a4 100755
--- a/contrib/findmissing/synchronize.py
+++ b/contrib/findmissing/synchronize.py
@@ -8,6 +8,8 @@
 """Setup module containing script to Synchronize kernel repositories + database."""
 
 from __future__ import print_function
+
+import logging
 import os
 import subprocess
 import MySQLdb
@@ -27,13 +29,13 @@
     repo = upstream_kernel_metadata.repo
 
     if not os.path.exists(destdir):
-        print('Cloning %s into %s' % (repo, destdir))
+        logging.info('Cloning %s into %s', repo, destdir)
         clone = ['git', 'clone', '-q', repo, destdir]
         subprocess.run(clone, check=True)
     else:
         os.chdir(destdir)
 
-        print('Pulling latest changes in %s into %s' % (repo, destdir))
+        logging.info('Pulling latest changes in %s into %s', repo, destdir)
         pull = ['git', 'pull', '-q']
         git_interface.checkout_and_clean(destdir, 'master')
         subprocess.run(pull, check=True)
@@ -50,7 +52,7 @@
     get_branch_name = custom_kernel_metadata.get_kernel_branch
 
     if not os.path.exists(destdir):
-        print('Cloning %s into %s' % (repo, destdir))
+        logging.info('Cloning %s into %s', repo, destdir)
         clone = ['git', 'clone', '-q', repo, destdir]
         subprocess.run(clone, check=True)
 
@@ -58,11 +60,11 @@
         for branch in custom_kernel_metadata.branches:
             branch_name = get_branch_name(branch)
 
-            print('Creating local branch %s in destdir %s' % (branch_name, destdir))
+            logging.info('Creating local branch %s in destdir %s', branch_name, destdir)
             checkout_branch = ['git', 'checkout', '-q', branch_name]
             subprocess.run(checkout_branch, check=True)
 
-        print('Add remote upstream %s to destdir %s' % (upstream_destdir, destdir))
+        logging.info('Add remote upstream %s to destdir %s', upstream_destdir, destdir)
         add_upstream_remote = ['git', 'remote', 'add', 'upstream', upstream_destdir]
         fetch_upstream = ['git', 'fetch', '-q', 'upstream']
         subprocess.run(add_upstream_remote, check=True)
@@ -70,7 +72,7 @@
     else:
         os.chdir(destdir)
 
-        print('Updating %s into %s' % (repo, destdir))
+        logging.info('Updating %s into %s', repo, destdir)
         fetch_origin = ['git', 'fetch', '-q', 'origin']
         fetch_upstream = ['git', 'fetch', '-q', 'upstream']
         subprocess.run(fetch_origin, check=True)
@@ -78,7 +80,7 @@
 
         for branch in custom_kernel_metadata.branches:
             branch_name = get_branch_name(branch)
-            print('Updating local branch %s in destdir %s' % (branch_name, destdir))
+            logging.info('Updating local branch %s in destdir %s', branch_name, destdir)
             pull = ['git', 'pull', '-q']
             git_interface.checkout_and_clean(destdir, branch_name)
             subprocess.run(pull, check=True)
@@ -133,7 +135,8 @@
                 status = gerrit_status_to_db_status(gerrit_status)
                 cloudsql_interface.update_change_status(db, fixes_table, fix_change_id, status)
             except KeyError as e:
-                print('Skipping syncing change-id %s with gerrit' % fix_change_id, e)
+                logging.warning('Skipping syncing change-id %s with gerrit (%s)',
+                                fix_change_id, e)
 
     db.close()