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 |
26 __docformat__ = "restructuredtext en" | 29 __docformat__ = "restructuredtext en" |
27 | 30 |
28 import sys | 31 import sys |
29 import shutil | 32 import shutil |
30 import mimetypes | 33 import mimetypes |
31 from os.path import isabs, isdir, islink, split, exists, normpath, join | 34 from os.path import isabs, isdir, islink, split, exists, normpath, join |
32 from os.path import abspath | 35 from os.path import abspath |
33 from os import sep, mkdir, remove, listdir, stat, chmod, walk | 36 from os import sep, mkdir, remove, listdir, stat, chmod, walk |
34 from stat import ST_MODE, S_IWRITE | 37 from stat import ST_MODE, S_IWRITE |
35 from cStringIO import StringIO | |
36 | 38 |
37 from logilab.common import STD_BLACKLIST as BASE_BLACKLIST, IGNORED_EXTENSIONS | 39 from logilab.common import STD_BLACKLIST as BASE_BLACKLIST, IGNORED_EXTENSIONS |
38 from logilab.common.shellutils import find | 40 from logilab.common.shellutils import find |
39 from logilab.common.deprecation import deprecated | 41 from logilab.common.deprecation import deprecated |
40 from logilab.common.compat import FileIO, any | 42 from logilab.common.compat import FileIO |
41 | 43 |
42 def first_level_directory(path): | 44 def first_level_directory(path): |
43 """Return the first level directory of a path. | 45 """Return the first level directory of a path. |
44 | 46 |
45 >>> first_level_directory('home/syt/work') | 47 >>> first_level_directory('home/syt/work') |
46 'home' | 48 'home' |
47 >>> first_level_directory('/home/syt/work') | 49 >>> first_level_directory('/home/syt/work') |
48 '/' | 50 '/' |
49 >>> first_level_directory('work') | 51 >>> first_level_directory('work') |
50 'work' | 52 'work' |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 if not exists(dest): | 370 if not exists(dest): |
369 mkdir(dest) | 371 mkdir(dest) |
370 for filename in filenames: | 372 for filename in filenames: |
371 # don't include binary files | 373 # don't include binary files |
372 # endswith does not accept tuple in 2.4 | 374 # endswith does not accept tuple in 2.4 |
373 if any([filename.endswith(ext) for ext in ignore_ext]): | 375 if any([filename.endswith(ext) for ext in ignore_ext]): |
374 continue | 376 continue |
375 src = join(directory, filename) | 377 src = join(directory, filename) |
376 dest = to_dir + src[len(from_dir):] | 378 dest = to_dir + src[len(from_dir):] |
377 if verbose: | 379 if verbose: |
378 print >> sys.stderr, src, '->', dest | 380 print(src, '->', dest, file=sys.stderr) |
379 if exists(dest): | 381 if exists(dest): |
380 remove(dest) | 382 remove(dest) |
381 shutil.copy2(src, dest) | 383 shutil.copy2(src, dest) |
382 | 384 |
383 | 385 |
384 def remove_dead_links(directory, verbose=0): | 386 def remove_dead_links(directory, verbose=0): |
385 """Recursively traverse directory and remove all dead links. | 387 """Recursively traverse directory and remove all dead links. |
386 | 388 |
387 :type directory: str | 389 :type directory: str |
388 :param directory: directory to cleanup | 390 :param directory: directory to cleanup |
389 | 391 |
390 :type verbose: bool | 392 :type verbose: bool |
391 :param verbose: | 393 :param verbose: |
392 flag indicating whether information about deleted links should be | 394 flag indicating whether information about deleted links should be |
393 printed to stderr, default to False | 395 printed to stderr, default to False |
394 """ | 396 """ |
395 for dirpath, dirname, filenames in walk(directory): | 397 for dirpath, dirname, filenames in walk(directory): |
396 for filename in dirnames + filenames: | 398 for filename in dirnames + filenames: |
397 src = join(dirpath, filename) | 399 src = join(dirpath, filename) |
398 if islink(src) and not exists(src): | 400 if islink(src) and not exists(src): |
399 if verbose: | 401 if verbose: |
400 print 'remove dead link', src | 402 print('remove dead link', src) |
401 remove(src) | 403 remove(src) |
402 | 404 |
OLD | NEW |