| OLD | NEW |
| 1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. | 1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
| 2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr | 2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
| 3 # | 3 # |
| 4 # This file is part of logilab-common. | 4 # This file is part of logilab-common. |
| 5 # | 5 # |
| 6 # logilab-common is free software: you can redistribute it and/or modify it unde
r | 6 # logilab-common is free software: you can redistribute it and/or modify it unde
r |
| 7 # the terms of the GNU Lesser General Public License as published by the Free | 7 # the terms of the GNU Lesser General Public License as published by the Free |
| 8 # Software Foundation, either version 2.1 of the License, or (at your option) an
y | 8 # Software Foundation, either version 2.1 of the License, or (at your option) an
y |
| 9 # later version. | 9 # later version. |
| 10 # | 10 # |
| 11 # logilab-common is distributed in the hope that it will be useful, but WITHOUT | 11 # logilab-common is distributed in the hope that it will be useful, but WITHOUT |
| 12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | 12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | 13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 14 # details. | 14 # details. |
| 15 # | 15 # |
| 16 # You should have received a copy of the GNU Lesser General Public License along | 16 # You should have received a copy of the GNU Lesser General Public License along |
| 17 # with logilab-common. If not, see <http://www.gnu.org/licenses/>. | 17 # with logilab-common. If not, see <http://www.gnu.org/licenses/>. |
| 18 """File and file-path manipulation utilities. | 18 """File and file-path manipulation utilities. |
| 19 | 19 |
| 20 :group path manipulation: first_level_directory, relative_path, is_binary,\ | 20 :group path manipulation: first_level_directory, relative_path, is_binary,\ |
| 21 get_by_ext, remove_dead_links | 21 get_by_ext, remove_dead_links |
| 22 :group file manipulation: norm_read, norm_open, lines, stream_lines, lines,\ | 22 :group file manipulation: norm_read, norm_open, lines, stream_lines, lines,\ |
| 23 write_open_mode, ensure_fs_mode, export | 23 write_open_mode, ensure_fs_mode, export |
| 24 :sort: path manipulation, file manipulation | 24 :sort: path manipulation, file manipulation |
| 25 """ | 25 """ |
| 26 | |
| 27 from __future__ import print_function | |
| 28 | |
| 29 __docformat__ = "restructuredtext en" | 26 __docformat__ = "restructuredtext en" |
| 30 | 27 |
| 31 import sys | 28 import sys |
| 32 import shutil | 29 import shutil |
| 33 import mimetypes | 30 import mimetypes |
| 34 from os.path import isabs, isdir, islink, split, exists, normpath, join | 31 from os.path import isabs, isdir, islink, split, exists, normpath, join |
| 35 from os.path import abspath | 32 from os.path import abspath |
| 36 from os import sep, mkdir, remove, listdir, stat, chmod, walk | 33 from os import sep, mkdir, remove, listdir, stat, chmod, walk |
| 37 from stat import ST_MODE, S_IWRITE | 34 from stat import ST_MODE, S_IWRITE |
| 35 from cStringIO import StringIO |
| 38 | 36 |
| 39 from logilab.common import STD_BLACKLIST as BASE_BLACKLIST, IGNORED_EXTENSIONS | 37 from logilab.common import STD_BLACKLIST as BASE_BLACKLIST, IGNORED_EXTENSIONS |
| 40 from logilab.common.shellutils import find | 38 from logilab.common.shellutils import find |
| 41 from logilab.common.deprecation import deprecated | 39 from logilab.common.deprecation import deprecated |
| 42 from logilab.common.compat import FileIO | 40 from logilab.common.compat import FileIO, any |
| 43 | 41 |
| 44 def first_level_directory(path): | 42 def first_level_directory(path): |
| 45 """Return the first level directory of a path. | 43 """Return the first level directory of a path. |
| 46 | 44 |
| 47 >>> first_level_directory('home/syt/work') | 45 >>> first_level_directory('home/syt/work') |
| 48 'home' | 46 'home' |
| 49 >>> first_level_directory('/home/syt/work') | 47 >>> first_level_directory('/home/syt/work') |
| 50 '/' | 48 '/' |
| 51 >>> first_level_directory('work') | 49 >>> first_level_directory('work') |
| 52 'work' | 50 'work' |
| (...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 if not exists(dest): | 368 if not exists(dest): |
| 371 mkdir(dest) | 369 mkdir(dest) |
| 372 for filename in filenames: | 370 for filename in filenames: |
| 373 # don't include binary files | 371 # don't include binary files |
| 374 # endswith does not accept tuple in 2.4 | 372 # endswith does not accept tuple in 2.4 |
| 375 if any([filename.endswith(ext) for ext in ignore_ext]): | 373 if any([filename.endswith(ext) for ext in ignore_ext]): |
| 376 continue | 374 continue |
| 377 src = join(directory, filename) | 375 src = join(directory, filename) |
| 378 dest = to_dir + src[len(from_dir):] | 376 dest = to_dir + src[len(from_dir):] |
| 379 if verbose: | 377 if verbose: |
| 380 print(src, '->', dest, file=sys.stderr) | 378 print >> sys.stderr, src, '->', dest |
| 381 if exists(dest): | 379 if exists(dest): |
| 382 remove(dest) | 380 remove(dest) |
| 383 shutil.copy2(src, dest) | 381 shutil.copy2(src, dest) |
| 384 | 382 |
| 385 | 383 |
| 386 def remove_dead_links(directory, verbose=0): | 384 def remove_dead_links(directory, verbose=0): |
| 387 """Recursively traverse directory and remove all dead links. | 385 """Recursively traverse directory and remove all dead links. |
| 388 | 386 |
| 389 :type directory: str | 387 :type directory: str |
| 390 :param directory: directory to cleanup | 388 :param directory: directory to cleanup |
| 391 | 389 |
| 392 :type verbose: bool | 390 :type verbose: bool |
| 393 :param verbose: | 391 :param verbose: |
| 394 flag indicating whether information about deleted links should be | 392 flag indicating whether information about deleted links should be |
| 395 printed to stderr, default to False | 393 printed to stderr, default to False |
| 396 """ | 394 """ |
| 397 for dirpath, dirname, filenames in walk(directory): | 395 for dirpath, dirname, filenames in walk(directory): |
| 398 for filename in dirnames + filenames: | 396 for filename in dirnames + filenames: |
| 399 src = join(dirpath, filename) | 397 src = join(dirpath, filename) |
| 400 if islink(src) and not exists(src): | 398 if islink(src) and not exists(src): |
| 401 if verbose: | 399 if verbose: |
| 402 print('remove dead link', src) | 400 print 'remove dead link', src |
| 403 remove(src) | 401 remove(src) |
| 404 | 402 |
| OLD | NEW |