Bug 1530134 - Run clang-format without docker as a fallback, r=jcj
authorMartin Thomson <martin.thomson@gmail.com>
Tue, 26 Feb 2019 10:34:38 +1100
changeset 15033 ee8e4996a6b364a2d7c65fd734afcb63c865ac7b
parent 15032 a2b586f90e0ae2fdbe8bc6e942e188fa8f4a62c6
child 15034 68578ca0ba17f205e4f92512157368eaf1694eb3
push id3284
push usermartin.thomson@gmail.com
push dateThu, 28 Feb 2019 05:36:28 +0000
reviewersjcj
bugs1530134
Bug 1530134 - Run clang-format without docker as a fallback, r=jcj Running clang-format with a bad version is better than not running it at all. Reviewers: jcj Reviewed By: jcj Bug #: 1530134 Differential Revision: https://phabricator.services.mozilla.com/D20938
mach
--- a/mach
+++ b/mach
@@ -34,38 +34,43 @@ def run_tests(test, cycles="standard", e
     })
     os_env = os.environ
     os_env.update(env)
     command = cwd + "/tests/all.sh"
     stdout = stderr = DEVNULL if silent else None
     subprocess.check_call(command, env=os_env, stdout=stdout, stderr=stderr)
 
 class cfAction(argparse.Action):
-    docker_command = ["docker"]
+    docker_command = None
     restorecon = None
 
     def __call__(self, parser, args, values, option_string=None):
-        if not args.noroot:
-            self.setDockerCommand()
+        self.setDockerCommand(args)
 
         if values:
             files = [os.path.relpath(os.path.abspath(x), start=cwd) for x in values]
         else:
             files = self.modifiedFiles()
-        files = [os.path.join('/home/worker/nss', x) for x in files]
 
         # First check if we can run docker.
         try:
             with open(os.devnull, "w") as f:
                 subprocess.check_call(
                     self.docker_command + ["images"], stdout=f)
         except:
-            print("Please install docker and start the docker daemon.")
-            sys.exit(1)
+            self.docker_command = None
 
+        if self.docker_command is None:
+            print("warning: running clang-format directly, which isn't guaranteed to be correct")
+            command = [cwd + "/automation/clang-format/run_clang_format.sh"] + files
+            repr(command)
+            subprocess.call(command)
+            return
+
+        files = [os.path.join('/home/worker/nss', x) for x in files]
         docker_image = 'clang-format-service:latest'
         cf_docker_folder = cwd + "/automation/clang-format"
 
         # Build the image if necessary.
         if self.filesChanged(cf_docker_folder):
             self.buildImage(docker_image, cf_docker_folder)
 
         # Check if we have the docker image.
@@ -108,21 +113,27 @@ class cfAction(argparse.Action):
 
     def buildImage(self, docker_image, cf_docker_folder):
         command = self.docker_command + [
             "build", "-t", docker_image, cf_docker_folder
         ]
         subprocess.check_call(command)
         return
 
-    def setDockerCommand(self):
+    def setDockerCommand(self, args):
+        from distutils.spawn import find_executable
         if platform.system() == "Linux":
-            from distutils.spawn import find_executable
-            self.restorecon = find_executable('restorecon')
-            self.docker_command = ["sudo"] + self.docker_command
+            self.restorecon = find_executable("restorecon")
+        dcmd = find_executable("docker")
+        if dcmd is not None:
+            self.docker_command = [dcmd]
+            if not args.noroot:
+                self.docker_command = ["sudo"] + self.docker_command
+        else:
+            self.docker_command = None
 
     def modifiedFiles(self):
         files = []
         if os.path.exists(os.path.join(cwd, '.hg')):
             st = subprocess.Popen(['hg', 'status', '-m', '-a'],
                                   cwd=cwd, stdout=subprocess.PIPE, universal_newlines=True)
             for line in iter(st.stdout.readline, ''):
                 files += [line[2:].rstrip()]