Skip to content

Commit

Permalink
Flake8 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tgamblin committed Aug 9, 2016
1 parent 102ac7b commit 05f222a
Show file tree
Hide file tree
Showing 16 changed files with 294 additions and 330 deletions.
18 changes: 10 additions & 8 deletions lib/spack/llnl/util/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,20 @@


class Lock(object):
def __init__(self,file_path):
"""This is an implementation of a filesystem lock using Python's lockf.
In Python, `lockf` actually calls `fcntl`, so this should work with any
filesystem implementation that supports locking through the fcntl calls.
This includes distributed filesystems like Lustre (when flock is enabled)
and recent NFS versions.
"""
def __init__(self, file_path):
self._file_path = file_path
self._fd = None
self._reads = 0
self._writes = 0


def _lock(self, op, timeout):
"""This takes a lock using POSIX locks (``fnctl.lockf``).
Expand Down Expand Up @@ -80,19 +87,17 @@ def _lock(self, op, timeout):

raise LockError("Timed out waiting for lock.")


def _unlock(self):
"""Releases a lock using POSIX locks (``fcntl.lockf``)
Releases the lock regardless of mode. Note that read locks may
be masquerading as write locks, but this removes either.
"""
fcntl.lockf(self._fd,fcntl.LOCK_UN)
fcntl.lockf(self._fd, fcntl.LOCK_UN)
os.close(self._fd)
self._fd = None


def acquire_read(self, timeout=_default_timeout):
"""Acquires a recursive, shared lock for reading.
Expand All @@ -112,7 +117,6 @@ def acquire_read(self, timeout=_default_timeout):
self._reads += 1
return False


def acquire_write(self, timeout=_default_timeout):
"""Acquires a recursive, exclusive lock for writing.
Expand All @@ -132,7 +136,6 @@ def acquire_write(self, timeout=_default_timeout):
self._writes += 1
return False


def release_read(self):
"""Releases a read lock.
Expand All @@ -153,7 +156,6 @@ def release_read(self):
self._reads -= 1
return False


def release_write(self):
"""Releases a write lock.
Expand Down
22 changes: 12 additions & 10 deletions lib/spack/spack/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flake8: noqa
##############################################################################
# Copyright (c) 2013-2016, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory.
Expand Down Expand Up @@ -147,7 +148,7 @@
_tmp_candidates = (_default_tmp, '/nfs/tmp2', '/tmp', '/var/tmp')
for path in _tmp_candidates:
# don't add a second username if it's already unique by user.
if not _tmp_user in path:
if _tmp_user not in path:
tmp_dirs.append(join_path(path, '%u', 'spack-stage'))
else:
tmp_dirs.append(join_path(path, 'spack-stage'))
Expand Down Expand Up @@ -179,12 +180,13 @@
# Spack internal code should call 'import spack' and accesses other
# variables (spack.repo, paths, etc.) directly.
#
# TODO: maybe this should be separated out and should go in build_environment.py?
# TODO: it's not clear where all the stuff that needs to be included in packages
# should live. This file is overloaded for spack core vs. for packages.
# TODO: maybe this should be separated out to build_environment.py?
# TODO: it's not clear where all the stuff that needs to be included in
# packages should live. This file is overloaded for spack core vs.
# for packages.
#
__all__ = ['Package', 'StagedPackage', 'CMakePackage', \
'Version', 'when', 'ver', 'alldeps', 'nolink']
__all__ = ['Package', 'StagedPackage', 'CMakePackage',
'Version', 'when', 'ver', 'alldeps', 'nolink']
from spack.package import Package, ExtensionConflictError
from spack.package import StagedPackage, CMakePackage
from spack.version import Version, ver
Expand All @@ -204,8 +206,8 @@
__all__ += spack.util.executable.__all__

from spack.package import \
install_dependency_symlinks, flatten_dependencies, DependencyConflictError, \
InstallError, ExternalPackageError
install_dependency_symlinks, flatten_dependencies, \
DependencyConflictError, InstallError, ExternalPackageError
__all__ += [
'install_dependency_symlinks', 'flatten_dependencies', 'DependencyConflictError',
'InstallError', 'ExternalPackageError']
'install_dependency_symlinks', 'flatten_dependencies',
'DependencyConflictError', 'InstallError', 'ExternalPackageError']
4 changes: 2 additions & 2 deletions lib/spack/spack/cmd/purge.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ def setup_parser(subparser):

def purge(parser, args):
# Special case: no flags.
if not any((args.stage, args.cache, args.all)):
if not any((args.stage, args.downloads, args.user_cache, args.all)):
stage.purge()
return

# handle other flags with fall through.
if args.stage or args.all:
stage.purge()
if args.cache or args.all:
if args.downloads or args.all:
spack.fetch_cache.destroy()
if args.user_cache or args.all:
spack.user_cache.destroy()
9 changes: 5 additions & 4 deletions lib/spack/spack/cmd/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
import os
from pprint import pprint

from llnl.util.filesystem import join_path, mkdirp
from llnl.util.tty.colify import colify
from llnl.util.lang import list_modules

import spack
import spack.test
from spack.fetch_strategy import FetchError

description ="Run unit tests"
description = "Run unit tests"


def setup_parser(subparser):
subparser.add_argument(
'names', nargs='*', help="Names of tests to run.")
subparser.add_argument(
'-l', '--list', action='store_true', dest='list', help="Show available tests")
'-l', '--list', action='store_true', dest='list',
help="Show available tests")
subparser.add_argument(
'--createXmlOutput', action='store_true', dest='createXmlOutput',
help="Create JUnit XML from test results")
Expand Down Expand Up @@ -69,6 +69,7 @@ def fetch(self):
def __str__(self):
return "[mock fetcher]"


def test(parser, args):
if args.list:
print "Available tests:"
Expand Down
6 changes: 3 additions & 3 deletions lib/spack/spack/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,9 @@ def _write(self, type, value, traceback):
This is a helper function called by the WriteTransaction context
manager. If there is an exception while the write lock is active,
nothing will be written to the database file, but the in-memory database
*may* be left in an inconsistent state. It will be consistent after the
start of the next transaction, when it read from disk again.
nothing will be written to the database file, but the in-memory
database *may* be left in an inconsistent state. It will be consistent
after the start of the next transaction, when it read from disk again.
This routine does no locking.
Expand Down
18 changes: 10 additions & 8 deletions lib/spack/spack/file_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from llnl.util.filesystem import *
from llnl.util.lock import *

import spack
from spack.error import SpackError


Expand All @@ -54,11 +53,14 @@ def __init__(self, root):

self._locks = {}

def purge(self):
def destroy(self):
"""Remove all files under the cache root."""
for f in os.listdir(self.root):
path = join_path(self.root, f)
shutil.rmtree(f)
if os.path.isdir(path):
shutil.rmtree(path, True)
else:
os.remove(path)

def cache_path(self, key):
"""Path to the file in the cache for a particular key."""
Expand Down Expand Up @@ -92,15 +94,15 @@ def init_entry(self, key):
if not os.path.isfile(cache_path):
raise CacheError("Cache file is not a file: %s" % cache_path)

if not os.access(cache_path, os.R_OK|os.W_OK):
if not os.access(cache_path, os.R_OK | os.W_OK):
raise CacheError("Cannot access cache file: %s" % cache_path)
else:
# if the file is hierarchical, make parent directories
parent = os.path.dirname(cache_path)
if parent.rstrip(os.path.sep) != self.root:
mkdirp(parent)

if not os.access(parent, os.R_OK|os.W_OK):
if not os.access(parent, os.R_OK | os.W_OK):
raise CacheError("Cannot access cache directory: %s" % parent)

# ensure lock is created for this key
Expand Down Expand Up @@ -154,7 +156,6 @@ def __exit__(cm, type, value, traceback):

return WriteTransaction(self._get_lock(key), WriteContextManager)


def mtime(self, key):
"""Return modification time of cache file, or 0 if it does not exist.
Expand All @@ -168,7 +169,6 @@ def mtime(self, key):
sinfo = os.stat(self.cache_path(key))
return sinfo.st_mtime


def remove(self, key):
lock = self._get_lock(key)
try:
Expand All @@ -178,4 +178,6 @@ def remove(self, key):
lock.release_write()
os.unlink(self._lock_path(key))

class CacheError(SpackError): pass

class CacheError(SpackError):
pass
1 change: 1 addition & 0 deletions lib/spack/spack/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,7 @@ def use_cray_compiler_names():
os.environ['FC'] = 'ftn'
os.environ['F77'] = 'ftn'


def flatten_dependencies(spec, flat_dir):
"""Make each dependency of spec present in dir via symlink."""
for dep in spec.traverse(root=False):
Expand Down
Loading

0 comments on commit 05f222a

Please sign in to comment.