manage_projects: exit with error code

 * Currently, manage_projects always perform
   `exit 0`, even if some errors were.
 * Patch would add `exit 1` beh., in
   case any issues were during processing.
   It would affect only exit code , and will not
   stop processing.
 * Misc: perform auto-ident

Change-Id: I50d425e5a9e02441a50f7b1d32219485f80e6632
diff --git a/jeepyb/cmd/manage_projects.py b/jeepyb/cmd/manage_projects.py
index 2c19c3a..0a3cba2 100644
--- a/jeepyb/cmd/manage_projects.py
+++ b/jeepyb/cmd/manage_projects.py
@@ -59,6 +59,7 @@
 import os
 import re
 import shutil
+import sys
 import time
 
 import gerritlib.gerrit
@@ -92,6 +93,14 @@
     pass
 
 
+class ProcessACLException(Exception):
+    pass
+
+
+class PushToGerritException(Exception):
+    pass
+
+
 class CreateGroupException(Exception):
     pass
 
@@ -127,7 +136,7 @@
         else:
             status, output = u.git_command_output(
                 repo_path, "ls-files --with-tree=remotes/gerrit-meta/config "
-                "project.config", env)
+                           "project.config", env)
         if output.strip() != "project.config" or status != 0:
             log.debug("Failed to find project.config for project: %s" %
                       project)
@@ -358,6 +367,7 @@
     except Exception:
         log.exception(
             "Error pushing %s to Gerrit." % project)
+        raise PushToGerritException()
 
 
 def process_acls(acl_config, project, ACL_DIR, section,
@@ -375,6 +385,7 @@
     except Exception:
         log.exception(
             "Exception processing ACLS for %s." % project)
+        raise ProcessACLException()
     finally:
         u.git_command(repo_path, 'reset --hard')
         u.git_command(repo_path, 'checkout master')
@@ -395,7 +406,6 @@
 
 def create_local_mirror(local_git_dir, project_git,
                         gerrit_system_user, gerrit_system_group):
-
     git_mirror_path = os.path.join(local_git_dir, project_git)
     if not os.path.exists(git_mirror_path):
         (ret, output) = u.run_command_status(
@@ -462,6 +472,8 @@
     project_list = gerrit.listProjects()
     ssh_env = u.make_ssh_wrapper(GERRIT_USER, GERRIT_KEY)
     try:
+        # Collect processed errors,if any
+        process_errors = []
         for section in registry.configs_list:
             project = section['project']
             if args.projects and project not in args.projects:
@@ -569,8 +581,9 @@
                     project_cache[project]['created-in-github'] = created
 
             except Exception:
-                log.exception(
-                    "Problems creating %s, moving on." % project)
+                msg = "Problems creating %s, moving on." % project
+                log.exception(msg)
+                process_errors.append(msg)
                 continue
             finally:
                 # Clean up after ourselves - this repo has no use
@@ -582,6 +595,11 @@
             cache_out.write(json.dumps(
                 project_cache, sort_keys=True, indent=2))
         os.unlink(ssh_env['GIT_SSH'])
+        if len(process_errors) > 0:
+            log.error("%d problems has been caught during run:\n %s" % (
+                len(process_errors), process_errors))
+            sys.exit(1)
+
 
 if __name__ == "__main__":
     main()