Repository: spack/spack

Issue:
glib doesn't build with python ~pythoncmd
glib requires python3 to compile. The glib package does replacements to use python, which doesn't work if the spack python3 was installed with ~pythoncmd.

```
==> Error: ProcessError: Command exited with status 2:
    'make' '-j16'

5 errors found in build log:
     1098      GEN      stamp-testmarshal.h
     1099    'import site' failed; use -v for traceback
     1100      File "../../gobject/glib-genmarshal", line 97
     1101        'g_cclosure_marshal_VOID__VOID',
     1102                                       ^
     1103    SyntaxError: invalid syntax
  >> 1104    make[4]: *** [stamp-testmarshal.h] Error 1
     1105    make[4]: Leaving directory `/tmp/andy.wettstein/spack-stage/spack-
             stage-vrRci5/spack-src/tests/gobject'
  >> 1106    make[3]: *** [all-recursive] Error 1
     1107    make[3]: Leaving directory `/tmp/andy.wettstein/spack-stage/spack-
             stage-vrRci5/spack-src/tests'
  >> 1108    make[2]: *** [all] Error 2
     1109    make[2]: Leaving directory `/tmp/andy.wettstein/spack-stage/spack-
             stage-vrRci5/spack-src/tests'
  >> 1110    make[1]: *** [all-recursive] Error 1
     1111    make[1]: Leaving directory `/tmp/andy.wettstein/spack-stage/spack-
             stage-vrRci5/spack-src'
  >> 1112    make: *** [all] Error 2
```

I did this, but maybe there is something better:
```
diff --git a/var/spack/repos/builtin/packages/glib/package.py b/var/spack/repos/builtin/packages/glib/package.py
index 0414c0619..f0c536376 100644
--- a/var/spack/repos/builtin/packages/glib/package.py
+++ b/var/spack/repos/builtin/packages/glib/package.py
@@ -100,7 +100,7 @@ class Glib(AutotoolsPackage):
         files = ['gobject/glib-genmarshal.in', 'gobject/glib-mkenums.in']
 
         filter_file('^#!/usr/bin/env @PYTHON@',
-                    '#!/usr/bin/env python',
+                    '#!/usr/bin/env python3',
                     *files)
 
     @run_before('configure')
@@ -119,7 +119,7 @@ class Glib(AutotoolsPackage):
             copy(dtrace, dtrace_copy)
             filter_file(
                 '^#!/usr/bin/python',
-                '#!/usr/bin/env python',
+                '#!/usr/bin/env python3',
                 dtrace_copy
             )
 
@@ -136,7 +136,8 @@ class Glib(AutotoolsPackage):
         # to rebuild files as filter_file updates the timestamps)
         if self.spec.satisfies('@2.53.4:'):
             pattern = '^#!/usr/bin/env python'
-            repl = '#!{0}'.format(self.spec['python'].command.path)
+            #repl = '#!{0}'.format(self.spec['python'].command.path)
+            repl = '^#!/usr/bin/env python3'
             files = ['glib-genmarshal', 'glib-mkenums']
         else:
             pattern = '^#! /usr/bin/perl'
```


Discussion:
You could extract the correct executable name from `self.spec['python'].command.path`.
Yes, I think just changing to python3 instead of python works ok:

```
diff --git a/var/spack/repos/builtin/packages/glib/package.py b/var/spack/repos/builtin/packages/glib/package.py
index 0414c0619..940f0fa2c 100644
--- a/var/spack/repos/builtin/packages/glib/package.py
+++ b/var/spack/repos/builtin/packages/glib/package.py
@@ -100,7 +100,7 @@ class Glib(AutotoolsPackage):
         files = ['gobject/glib-genmarshal.in', 'gobject/glib-mkenums.in']
 
         filter_file('^#!/usr/bin/env @PYTHON@',
-                    '#!/usr/bin/env python',
+                    '#!/usr/bin/env python3',
                     *files)
 
     @run_before('configure')

@@ -119,7 +119,7 @@ class Glib(AutotoolsPackage):
             copy(dtrace, dtrace_copy)
             filter_file(
                 '^#!/usr/bin/python',
-                '#!/usr/bin/env python',
+                '#!/usr/bin/env python3',
                 dtrace_copy
             )
 
@@ -135,7 +135,7 @@ class Glib(AutotoolsPackage):
         # this after install because otherwise the install target will try
         # to rebuild files as filter_file updates the timestamps)
         if self.spec.satisfies('@2.53.4:'):
-            pattern = '^#!/usr/bin/env python'
+            pattern = '^#!/usr/bin/env python3'
             repl = '#!{0}'.format(self.spec['python'].command.path)
             files = ['glib-genmarshal', 'glib-mkenums']
         else:
```
That only works if you're building with Python 3.

> glib requires python3 to compile.

The `depends_on` says any Python version will work. Is this no longer the case?
I checked the release notes and it looks like python2 is unsupported in 2.59, so I guess 2.56.3 should still be ok with python2.

With ~pythoncmd set, the build would have used the system python (which is python 2.7.5 on centos7), but spack would have loaded the spack python environment so that possibly messed up the system python environment since the build environment sets PYTHONHOME?

```
     1098      GEN      stamp-testmarshal.h
     1099    'import site' failed; use -v for traceback
     1100      File "../../gobject/glib-genmarshal", line 97
     1101        'g_cclosure_marshal_VOID__VOID',
     1102                                       ^
     1103    SyntaxError: invalid syntax
```
The configure stage uses --with-python set to the spack python (it is --with-python=python3.7 for my build), but the filter has already replaced @PYTHON@ with python (or python3 with the change I made), so I'm not sure why that is actually necessary.
@michaelkuhn it looks like you did most of the work on sbang filtering in the `glib` package back in #6910. Can you take a look at this issue? We likely just need to replace `'python'` with `spec['python'].command.path` in a few places. Python 3 will soon be the default version of Python for Spack, so it's important we clean up these lingering Python 3 bugs.

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/glib/package.py b/var/spack/repos/builtin/packages/glib/package.py
--- a/var/spack/repos/builtin/packages/glib/package.py
+++ b/var/spack/repos/builtin/packages/glib/package.py
@@ -100,7 +100,8 @@ def fix_python_path(self):
         files = ['gobject/glib-genmarshal.in', 'gobject/glib-mkenums.in']
 
         filter_file('^#!/usr/bin/env @PYTHON@',
-                    '#!/usr/bin/env python',
+                    '#!/usr/bin/env {0}'.format(
+                        os.path.basename(self.spec['python'].command.path)),
                     *files)
 
     @run_before('configure')
@@ -119,7 +120,8 @@ def fix_dtrace_usr_bin_path(self):
             copy(dtrace, dtrace_copy)
             filter_file(
                 '^#!/usr/bin/python',
-                '#!/usr/bin/env python',
+                '#!/usr/bin/env {0}'.format(
+                    os.path.basename(self.spec['python'].command.path)),
                 dtrace_copy
             )
 
@@ -135,7 +137,8 @@ def filter_sbang(self):
         # this after install because otherwise the install target will try
         # to rebuild files as filter_file updates the timestamps)
         if self.spec.satisfies('@2.53.4:'):
-            pattern = '^#!/usr/bin/env python'
+            pattern = '^#!/usr/bin/env {0}'.format(
+                os.path.basename(self.spec['python'].command.path))
             repl = '#!{0}'.format(self.spec['python'].command.path)
             files = ['glib-genmarshal', 'glib-mkenums']
         else:


Test patch: