# Buggy code
def get_package_dir_permissions(spec):
    """Return the permissions configured for the spec.

    Include the GID bit if group permissions are on. This makes the group
    attribute sticky for the directory. Package-specific settings take
    precedent over settings for ``all``"""
    perms = get_package_permissions(spec)
    if perms & stat.S_IRWXG:
        perms |= stat.S_ISGID
    return perms


# Traceback (PermissionError)
lib/spack/spack/cmd/__init__.py:102 ==> [2020-01-08-18:01:06.241182] Imported install from built-in commands
lib/spack/spack/config.py:706 ==> [2020-01-08-18:01:06.247268] Reading config file /afs/cern.ch/work/r/razumov/spack_vanilla/etc/spack/defaults/config.yaml
lib/spack/spack/cmd/__init__.py:102 ==> [2020-01-08-18:01:06.271299] Imported install from built-in commands
lib/spack/spack/config.py:706 ==> [2020-01-08-18:01:06.276303] Reading config file /afs/cern.ch/work/r/razumov/spack_vanilla/etc/spack/defaults/repos.yaml
lib/spack/spack/config.py:706 ==> [2020-01-08-18:01:10.210033] Reading config file /afs/cern.ch/work/r/razumov/spack_vanilla/etc/spack/defaults/packages.yaml
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.240640] READ LOCK: /afs/cern.ch/user/r/razumov/.spack/cache/providers/.builtin-index.json.lock[0:0] [Acquiring]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.242159] READ LOCK: /afs/cern.ch/user/r/razumov/.spack/cache/providers/.builtin-index.json.lock[0:0] [Acquired]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.271533] READ LOCK: /afs/cern.ch/user/r/razumov/.spack/cache/providers/.builtin-index.json.lock[0:0] [Released]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.277038] READ LOCK: /afs/cern.ch/user/r/razumov/.spack/cache/tags/.builtin-index.json.lock[0:0] [Acquiring]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.278357] READ LOCK: /afs/cern.ch/user/r/razumov/.spack/cache/tags/.builtin-index.json.lock[0:0] [Acquired]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.279135] READ LOCK: /afs/cern.ch/user/r/razumov/.spack/cache/tags/.builtin-index.json.lock[0:0] [Released]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.283876] READ LOCK: /afs/cern.ch/user/r/razumov/.spack/cache/patches/.builtin-index.json.lock[0:0] [Acquiring]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.285200] READ LOCK: /afs/cern.ch/user/r/razumov/.spack/cache/patches/.builtin-index.json.lock[0:0] [Acquired]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.299337] READ LOCK: /afs/cern.ch/user/r/razumov/.spack/cache/patches/.builtin-index.json.lock[0:0] [Released]
lib/spack/spack/config.py:706 ==> [2020-01-08-18:01:10.312009] Reading config file /afs/cern.ch/user/r/razumov/.spack/linux/compilers.yaml
lib/spack/spack/concretize.py:565 ==> [2020-01-08-18:01:10.331186] Warning: gcc@4.8.5 cannot build optimized binaries for "broadwell". Using best target possible: "haswell"
lib/spack/spack/database.py:310 ==> [2020-01-08-18:01:10.356590] DATABASE LOCK TIMEOUT: 120s
lib/spack/spack/database.py:314 ==> [2020-01-08-18:01:10.356940] PACKAGE LOCK TIMEOUT: No timeout
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.357586] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquiring]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.359717] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquired]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.368517] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Released]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.372162] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquiring]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.373495] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquired]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.374094] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Released]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.376033] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquiring]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.377234] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquired]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.377834] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Released]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.379224] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquiring]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.380414] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquired]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.381048] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Released]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.382854] WRITE LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/prefix_lock[2895703049217635441:1] [Acquiring]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.383386] WRITE LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/prefix_lock[2895703049217635441:1] [Acquired]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.383745] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquiring]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.384759] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquired]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.385310] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Released]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.386315] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquiring]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.387456] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquired]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.388098] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Released]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.390128] WRITE LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/prefix_lock[2895703049217635441:1] [Released]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.392482] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/prefix_lock[2895703049217635441:1] [Acquiring]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.392943] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/prefix_lock[2895703049217635441:1] [Acquired]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.393335] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquiring]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.394152] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquired]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.394689] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Released]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.395787] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/prefix_lock[2895703049217635441:1] [Released]
lib/spack/spack/package.py:1645 ==> [2020-01-08-18:01:10.396123] Installing expat dependencies
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.396777] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquiring]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.397757] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquired]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.398291] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Released]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.399129] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquiring]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.400046] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquired]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.400590] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Released]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.401353] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquiring]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.402320] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquired]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.402862] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Released]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.404483] WRITE LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/prefix_lock[7237090093046132987:1] [Acquiring]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.404948] WRITE LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/prefix_lock[7237090093046132987:1] [Acquired]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.405323] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquiring]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.406199] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquired]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.406925] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Released]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.407781] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquiring]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.408839] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquired]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.409403] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Released]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.410740] WRITE LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/prefix_lock[7237090093046132987:1] [Released]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.422206] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/prefix_lock[7237090093046132987:1] [Acquiring]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.422705] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/prefix_lock[7237090093046132987:1] [Acquired]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.424183] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquiring]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.425561] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquired]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.426151] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Released]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.427703] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/prefix_lock[7237090093046132987:1] [Released]
lib/spack/spack/package.py:1662 ==> [2020-01-08-18:01:10.428156] Installing libbsd
lib/spack/spack/package.py:1509 ==> [2020-01-08-18:01:10.428409] Searching for binary cache of libbsd
lib/spack/spack/binary_distribution.py:682 ==> [2020-01-08-18:01:10.429257] No Spack mirrors are currently configured
lib/spack/spack/package.py:1675 ==> [2020-01-08-18:01:10.430136] No binary for libbsd found: installing from source
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.430565] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquiring]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.431556] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquired]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.432112] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Released]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.485290] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquiring]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.486355] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Acquired]
lib/spack/llnl/util/lock.py:368 ==> [2020-01-08-18:01:10.486996] READ LOCK: /afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/.spack-db/lock[0:0] [Released]
Traceback (most recent call last):
File "/afs/cern.ch/work/r/razumov/spack_vanilla/bin/spack", line 64, in <module>
sys.exit(spack.main.main())
File "/afs/cern.ch/work/r/razumov/spack_vanilla/lib/spack/spack/main.py", line 711, in main
return _invoke_command(command, parser, args, unknown)
File "/afs/cern.ch/work/r/razumov/spack_vanilla/lib/spack/spack/main.py", line 456, in _invoke_command
return_val = command(parser, args)
File "/afs/cern.ch/work/r/razumov/spack_vanilla/lib/spack/spack/cmd/install.py", line 373, in install
install_spec(args, kwargs, abstract, concrete)
File "/afs/cern.ch/work/r/razumov/spack_vanilla/lib/spack/spack/cmd/install.py", line 234, in install_spec
spec.package.do_install(**kwargs)
File "/afs/cern.ch/work/r/razumov/spack_vanilla/lib/spack/spack/package.py", line 1652, in do_install
dep.package.do_install(**dep_kwargs)
File "/afs/cern.ch/work/r/razumov/spack_vanilla/lib/spack/spack/package.py", line 1774, in do_install
spack.store.layout.create_install_directory(self.spec)
File "/afs/cern.ch/work/r/razumov/spack_vanilla/lib/spack/spack/directory_layout.py", line 299, in create_install_directory
mkdirp(spec.prefix, mode=perms, group=group, default_perms='parents')
File "/afs/cern.ch/work/r/razumov/spack_vanilla/lib/spack/llnl/util/filesystem.py", line 583, in mkdirp
raise e
File "/afs/cern.ch/work/r/razumov/spack_vanilla/lib/spack/llnl/util/filesystem.py", line 550, in mkdirp
os.chmod(path, mode)
PermissionError: [Errno 13] Permission denied: '/afs/cern.ch/work/r/razumov/spack_vanilla/opt/spack/linux-centos7-haswell/gcc-4.8.5/libbsd-0.10.0-zdpj4mixa3q7p2b3zgojligmsnnw54ne'

# Fixed code
def get_package_dir_permissions(spec):
    """Return the permissions configured for the spec.

    Include the GID bit if group permissions are on. This makes the group
    attribute sticky for the directory. Package-specific settings take
    precedent over settings for ``all``"""
    perms = get_package_permissions(spec)
    if perms & stat.S_IRWXG and spack.config.get("config:allow_sgid", True):
        perms |= stat.S_ISGID
    return perms