So I discovered that the previous patch wasn't exactly good in all
instances, It turns out that make can pass the jobserver-auth flag without
leaving the file-descriptors open.

I applied a check to see wether the file-descriptors was open before the
adding the descriptors fds to pass on. I opted to use os.stat() for this,
there may be a better way that I'm unaware of.

I've attached the patch this time instead
diff -Nru git-buildpackage-0.9.14/debian/changelog git-buildpackage-0.9.14+nmu1/debian/changelog
--- git-buildpackage-0.9.14/debian/changelog	2019-03-21 09:33:34.000000000 +0000
+++ git-buildpackage-0.9.14+nmu1/debian/changelog	2019-04-25 10:25:15.000000000 +0000
@@ -1,3 +1,11 @@
+git-buildpackage (0.9.14+nmu1) UNRELEASED; urgency=medium
+
+  * Non-maintainer upload.
+  * Made gbp buildpackage able to pass file-descriptors of an existing
+    GNU Make job-server.
+
+ -- Kristofer Hansson <kristofer.hans...@icomera.com>  Thu, 25 Apr 2019 10:25:15 +0000
+
 git-buildpackage (0.9.14) unstable; urgency=medium
 
   [ Michael Prokop ]
diff -Nru git-buildpackage-0.9.14/gbp/command_wrappers.py git-buildpackage-0.9.14+nmu1/gbp/command_wrappers.py
--- git-buildpackage-0.9.14/gbp/command_wrappers.py	2019-01-08 19:15:13.000000000 +0000
+++ git-buildpackage-0.9.14+nmu1/gbp/command_wrappers.py	2019-04-25 10:25:15.000000000 +0000
@@ -75,7 +75,7 @@
 
     If cmd doesn't contain a path component it will be looked up in $PATH.
     """
-    def __init__(self, cmd, args=[], shell=False, extra_env=None, cwd=None,
+    def __init__(self, cmd, args=[], shell=False, extra_env=None, cwd=None, pass_fds=(),
                  capture_stderr=False,
                  capture_stdout=False):
         self.cmd = cmd
@@ -86,6 +86,7 @@
         self.capture_stdout = capture_stdout
         self.capture_stderr = capture_stderr
         self.cwd = cwd
+        self.pass_fds = pass_fds
         if extra_env is not None:
             self.env = os.environ.copy()
             self.env.update(extra_env)
@@ -139,13 +140,15 @@
             stderr_arg = subprocess.PIPE if self.capture_stderr else stderr
 
             try:
+                print(self.pass_fds)
                 popen = subprocess.Popen(cmd,
                                          cwd=self.cwd,
                                          shell=self.shell,
                                          env=self.env,
                                          preexec_fn=default_sigpipe,
                                          stdout=stdout_arg,
-                                         stderr=stderr_arg)
+                                         stderr=stderr_arg,
+                                         pass_fds=self.pass_fds)
                 (self.stdout, self.stderr) = popen.communicate()
                 if self.stdout is not None:
                     self.stdout = self.stdout.decode()
diff -Nru git-buildpackage-0.9.14/gbp/scripts/buildpackage.py git-buildpackage-0.9.14+nmu1/gbp/scripts/buildpackage.py
--- git-buildpackage-0.9.14/gbp/scripts/buildpackage.py	2019-01-08 19:15:13.000000000 +0000
+++ git-buildpackage-0.9.14+nmu1/gbp/scripts/buildpackage.py	2019-04-25 10:25:15.000000000 +0000
@@ -335,6 +335,27 @@
     return branch
 
 
+def get_job_fds_from_env():
+    makeflags = None
+    try:
+        makeflags = os.environ['MAKEFLAGS']
+    except KeyError:
+        return ()
+
+    for flag in makeflags.split(' '):
+        if flag.startswith('--jobserver-auth='):
+            fds = flag.split('=')[1].split(',')
+            # Verify that the file-descriptor is open.
+            try:
+                for fd in fds:
+                    os.stat(fd)
+            except OSError:
+                return ()
+            return tuple(fds)
+    else:
+        return ()
+
+
 def build_parser(name, prefix=None):
     try:
         parser = GbpOptionParserDebian(command=os.path.basename(name), prefix=prefix)
@@ -510,6 +531,7 @@
             export_dir = os.path.join(output_dir, "%s-%s" % (source.sourcepkg, major))
             build_dir = export_dir if options.export_dir else repo.path
             changes_file = changes_file_name(source, build_dir, options.builder, dpkg_args)
+            job_fds = get_job_fds_from_env()
 
             # Run preexport hook
             if options.export_dir and options.preexport:
@@ -563,6 +585,7 @@
             RunAtCommand(options.builder,
                          [pipes.quote(arg) for arg in dpkg_args],
                          shell=True,
+                         pass_fds=job_fds,
                          extra_env=Hook.md(build_env,
                                            {'GBP_BUILD_DIR': build_dir})
                          )(dir=build_dir)

Reply via email to