flake8 fixes for filesystem.py

This commit is contained in:
Denis Davydov 2016-06-15 18:31:10 +02:00
parent d0d83cfe1e
commit 3d3a26cd9a

View file

@ -22,28 +22,28 @@
# License along with this program; if not, write to the Free Software # License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
############################################################################## ##############################################################################
__all__ = ['set_install_permissions', 'install', 'install_tree', 'traverse_tree',
'expand_user', 'working_dir', 'touch', 'touchp', 'mkdirp',
'force_remove', 'join_path', 'ancestor', 'can_access', 'filter_file',
'FileFilter', 'change_sed_delimiter', 'is_exe', 'force_symlink',
'set_executable', 'copy_mode', 'unset_executable_mode',
'remove_dead_links', 'remove_linked_tree', 'find_library_path',
'fix_darwin_install_name','to_link_flags']
import os import os
import glob import glob
import sys
import re import re
import shutil import shutil
import stat import stat
import errno import errno
import getpass import getpass
from contextlib import contextmanager, closing from contextlib import contextmanager, closing
from tempfile import NamedTemporaryFile
import subprocess import subprocess
import llnl.util.tty as tty import llnl.util.tty as tty
from spack.util.compression import ALLOWED_ARCHIVE_TYPES
__all__ = ['set_install_permissions', 'install', 'install_tree',
'traverse_tree',
'expand_user', 'working_dir', 'touch', 'touchp', 'mkdirp',
'force_remove', 'join_path', 'ancestor', 'can_access',
'filter_file',
'FileFilter', 'change_sed_delimiter', 'is_exe', 'force_symlink',
'set_executable', 'copy_mode', 'unset_executable_mode',
'remove_dead_links', 'remove_linked_tree', 'find_library_path',
'fix_darwin_install_name', 'to_link_flags']
def filter_file(regex, repl, *filenames, **kwargs): def filter_file(regex, repl, *filenames, **kwargs):
"""Like sed, but uses python regular expressions. """Like sed, but uses python regular expressions.
@ -69,6 +69,7 @@ def filter_file(regex, repl, *filenames, **kwargs):
# Allow strings to use \1, \2, etc. for replacement, like sed # Allow strings to use \1, \2, etc. for replacement, like sed
if not callable(repl): if not callable(repl):
unescaped = repl.replace(r'\\', '\\') unescaped = repl.replace(r'\\', '\\')
def replace_groups_with_groupid(m): def replace_groups_with_groupid(m):
def groupid_to_group(x): def groupid_to_group(x):
return m.group(int(x.group(1))) return m.group(int(x.group(1)))
@ -157,9 +158,12 @@ def set_install_permissions(path):
def copy_mode(src, dest): def copy_mode(src, dest):
src_mode = os.stat(src).st_mode src_mode = os.stat(src).st_mode
dest_mode = os.stat(dest).st_mode dest_mode = os.stat(dest).st_mode
if src_mode & stat.S_IXUSR: dest_mode |= stat.S_IXUSR if src_mode & stat.S_IXUSR:
if src_mode & stat.S_IXGRP: dest_mode |= stat.S_IXGRP dest_mode |= stat.S_IXUSR
if src_mode & stat.S_IXOTH: dest_mode |= stat.S_IXOTH if src_mode & stat.S_IXGRP:
dest_mode |= stat.S_IXGRP
if src_mode & stat.S_IXOTH:
dest_mode |= stat.S_IXOTH
os.chmod(dest, dest_mode) os.chmod(dest, dest_mode)
@ -224,9 +228,10 @@ def force_remove(*paths):
for path in paths: for path in paths:
try: try:
os.remove(path) os.remove(path)
except OSError, e: except OSError:
pass pass
@contextmanager @contextmanager
def working_dir(dirname, **kwargs): def working_dir(dirname, **kwargs):
if kwargs.get('create', False): if kwargs.get('create', False):
@ -240,7 +245,7 @@ def working_dir(dirname, **kwargs):
def touch(path): def touch(path):
"""Creates an empty file at the specified path.""" """Creates an empty file at the specified path."""
with open(path, 'a') as file: with open(path, 'a'):
os.utime(path, None) os.utime(path, None)
@ -253,7 +258,7 @@ def touchp(path):
def force_symlink(src, dest): def force_symlink(src, dest):
try: try:
os.symlink(src, dest) os.symlink(src, dest)
except OSError as e: except OSError:
os.remove(dest) os.remove(dest)
os.symlink(src, dest) os.symlink(src, dest)
@ -348,8 +353,9 @@ def traverse_tree(source_root, dest_root, rel_path='', **kwargs):
# When follow_nonexisting isn't set, don't descend into dirs # When follow_nonexisting isn't set, don't descend into dirs
# in source that do not exist in dest # in source that do not exist in dest
if follow_nonexisting or os.path.exists(dest_child): if follow_nonexisting or os.path.exists(dest_child):
tuples = traverse_tree(source_root, dest_root, rel_child, **kwargs) tuples = traverse_tree(source_root, dest_root, rel_child, **kwargs) # NOQA: ignore=E501
for t in tuples: yield t for t in tuples:
yield t
# Treat as a file. # Treat as a file.
elif not ignore(os.path.join(rel_path, f)): elif not ignore(os.path.join(rel_path, f)):
@ -379,6 +385,7 @@ def remove_dead_links(root):
if not os.path.exists(real_path): if not os.path.exists(real_path):
os.unlink(path) os.unlink(path)
def remove_linked_tree(path): def remove_linked_tree(path):
""" """
Removes a directory and its contents. If the directory is a Removes a directory and its contents. If the directory is a
@ -402,9 +409,9 @@ def fix_darwin_install_name(path):
Fix install name of dynamic libraries on Darwin to have full path. Fix install name of dynamic libraries on Darwin to have full path.
There are two parts of this task: There are two parts of this task:
(i) use install_name('-id',...) to change install name of a single lib; (i) use install_name('-id',...) to change install name of a single lib;
(ii) use install_name('-change',...) to change the cross linking between libs. (ii) use install_name('-change',...) to change the cross linking between
The function assumes that all libraries are in one folder and currently won't libs. The function assumes that all libraries are in one folder and
follow subfolders. currently won't follow subfolders.
Args: Args:
path: directory in which .dylib files are alocated path: directory in which .dylib files are alocated
@ -413,14 +420,14 @@ def fix_darwin_install_name(path):
libs = glob.glob(join_path(path, "*.dylib")) libs = glob.glob(join_path(path, "*.dylib"))
for lib in libs: for lib in libs:
# fix install name first: # fix install name first:
subprocess.Popen(["install_name_tool", "-id",lib,lib], stdout=subprocess.PIPE).communicate()[0] subprocess.Popen(["install_name_tool", "-id", lib, lib], stdout=subprocess.PIPE).communicate()[0] # NOQA: ignore=E501
long_deps = subprocess.Popen(["otool", "-L",lib], stdout=subprocess.PIPE).communicate()[0].split('\n') long_deps = subprocess.Popen(["otool", "-L", lib], stdout=subprocess.PIPE).communicate()[0].split('\n') # NOQA: ignore=E501
deps = [dep.partition(' ')[0][1::] for dep in long_deps[2:-1]] deps = [dep.partition(' ')[0][1::] for dep in long_deps[2:-1]]
# fix all dependencies: # fix all dependencies:
for dep in deps: for dep in deps:
for loc in libs: for loc in libs:
if dep == os.path.basename(loc): if dep == os.path.basename(loc):
subprocess.Popen(["install_name_tool", "-change",dep,loc,lib], stdout=subprocess.PIPE).communicate()[0] subprocess.Popen(["install_name_tool", "-change", dep, loc, lib], stdout=subprocess.PIPE).communicate()[0] # NOQA: ignore=E501
break break