buildframework/helium/sf/python/pythoncore/lib/fileutils.py.orig
author lorewang
Wed, 01 Dec 2010 16:05:36 +0800
changeset 715 e0739b8406dd
parent 645 b8d81fa19e7d
permissions -rw-r--r--
Specify extenal tool with path
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
645
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
     1
#============================================================================ 
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
     2
#Name        : fileutils.py 
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
     3
#Part of     : Helium 
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
     4
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
     5
#Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
     6
#All rights reserved.
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
     7
#This component and the accompanying materials are made available
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
     8
#under the terms of the License "Eclipse Public License v1.0"
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
     9
#which accompanies this distribution, and is available
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    10
#at the URL "http://www.eclipse.org/legal/epl-v10.html".
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    11
#
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    12
#Initial Contributors:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    13
#Nokia Corporation - initial contribution.
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    14
#
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    15
#Contributors:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    16
#
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    17
#Description:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    18
#===============================================================================
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    19
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    20
"""
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    21
File manipulation related functionalities:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    22
 * Filescanner
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    23
 * rmtree (fixed version)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    24
 * move (fixed version)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    25
"""
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    26
import codecs
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    27
import locale
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    28
import logging
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    29
import os
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    30
import re
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    31
import sys
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    32
import shutil
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    33
import hashlib
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    34
import subprocess
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    35
import string
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    36
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    37
import pathaddition.match
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    38
import stat
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    39
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    40
if os.name == 'nt':
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    41
    import win32api
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    42
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    43
LOGGER = logging.getLogger('fileutils')
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    44
LOGGER_LOCK = logging.getLogger('fileutils.lock')
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    45
#LOGGER.addHandler(logging.FileHandler('default.log'))
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    46
#logging.basicConfig(level=logging.DEBUG)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    47
#LOGGER.setLevel(logging.DEBUG)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    48
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    49
class AbstractScanner(object):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    50
    """ This class implements all the required infrastructure for filescanning. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    51
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    52
    def __init__(self):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    53
        """ Initialization. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    54
        self.includes = []
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    55
        self.excludes = []
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    56
        self.includes_files = []
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    57
        self.excludes_files = []
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    58
        self.selectors = []
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    59
        self.filetypes = []
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    60
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    61
    def add_include(self, include):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    62
        """ Adds an include path to the scanner. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    63
        if include.endswith('/') or include.endswith('\\'):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    64
            include = include + '**'
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    65
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    66
        self.includes.append(include)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    67
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    68
    def add_exclude(self, exclude):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    69
        """ Adds an exclude path to the scanner. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    70
        if exclude.endswith('/') or exclude.endswith('\\'):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    71
            exclude = exclude + '**'
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    72
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    73
        self.excludes.append(exclude)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    74
        
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    75
    def add_exclude_file(self, exclude):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    76
        """ Adds an exclude file to the scanner. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    77
        self.excludes_files.append(exclude)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    78
    
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    79
    def add_selector(self, selector):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    80
        """ Add selector to the scanner. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    81
        self.selectors.append(selector)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    82
        
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    83
    def add_filetype(self, filetype):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    84
        """ Adds a filetype selection to the scanner. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    85
        self.filetypes.append(filetype)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    86
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    87
    def is_included(self, path):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    88
        """ Returns if path is included by the scanner. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    89
        LOGGER.debug("is_included: path = " + path)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    90
        if path.replace('\\', '/') in self.includes_files or path in self.includes_files:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    91
            return True
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    92
        for inc in self.includes:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    93
            if self.match(path, inc):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    94
                LOGGER.debug("Included: " + path + " by " + inc)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    95
                return True
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    96
        return False
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    97
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    98
    def is_excluded(self, path):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
    99
        """ Returns if path is excluded by the scanner. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   100
        LOGGER.debug("is_excluded: path = " + path)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   101
        if path.replace('\\', '/') in self.excludes_files or path in self.excludes_files:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   102
            return True
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   103
        for ex in self.excludes:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   104
            if self.match(path, ex):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   105
                LOGGER.debug("Excluded: " + path + " by " + ex)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   106
                return True
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   107
        return False
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   108
    
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   109
    def is_selected(self, path):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   110
        """ Returns if path is selected by all selectors in the scanner. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   111
        LOGGER.debug("is_selected: path = " + path)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   112
        for selector in self.selectors:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   113
            if not selector.is_selected(path):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   114
                return False
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   115
        LOGGER.debug("Selected: " + path)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   116
        return True
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   117
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   118
    def is_filetype(self, path):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   119
        """ Test if a file matches one filetype. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   120
        if len(self.filetypes) == 0:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   121
            return True
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   122
        LOGGER.debug("is_filetype: path = " + path)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   123
        for filetype in self.filetypes:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   124
            if self.match(path, filetype):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   125
                LOGGER.debug("Filetype: " + path + " by " + filetype)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   126
                return True
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   127
        return False
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   128
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   129
    def match(self, filename, pattern):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   130
        """ Is filename matching pattern? """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   131
        return pathaddition.match.ant_match(filename, pattern, casesensitive=(os.sep != '\\'))
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   132
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   133
    def test_path(self, root, relpath):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   134
        """ Test if a path matches filetype, include, exclude, and selection process."""
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   135
        return self.is_filetype(relpath) and self.is_included(relpath) \
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   136
                         and not self.is_excluded(relpath) and \
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   137
                         self.is_selected(os.path.join(root, relpath))
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   138
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   139
    def __str__(self):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   140
        """ Returns a string representing this instance. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   141
        content = []
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   142
        for inc in self.includes:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   143
            content.append('include:' + os.path.normpath(inc))
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   144
        for ex in self.excludes:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   145
            content.append('exclude:' + os.path.normpath(ex))
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   146
        return ';'.join(content)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   147
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   148
    def __repr__(self):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   149
        """ Returns a string representing this instance. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   150
        return self.__str__()
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   151
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   152
    def scan(self):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   153
        """ Abstract method which much be overriden to implement the scanning process. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   154
        raise Exception("scan method must be overriden")
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   155
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   156
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   157
class FileScanner(AbstractScanner):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   158
    """Scans the filesystem for files that match the selection paths.
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   159
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   160
    The scanner is configured with a root directory. Any number of include
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   161
    and exclude paths can be added. The scan() method is a generator that
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   162
    returns matching files one at a time when called as an iterator.
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   163
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   164
    This is a revisited implementation of the filescanner. It now relies on
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   165
    the module pathaddition.match that implements a Ant-like regular expression matcher.
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   166
    
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   167
    Rules:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   168
    - Includes and excludes should not start with *
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   169
    - Includes and excludes should not have wildcard searches ending with ** (e.g. wildcard**)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   170
    
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   171
    Supported includes and excludes:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   172
    - filename.txt
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   173
    - filename.*
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   174
    - dir/
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   175
    - dir/*
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   176
    - dir/**    
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   177
    """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   178
    def __init__(self, root_dir):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   179
        """ Initialization. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   180
        AbstractScanner.__init__(self)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   181
        self.root_dir = os.path.normpath(root_dir)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   182
        if not self.root_dir.endswith(os.sep):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   183
            self.root_dir = self.root_dir + os.sep
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   184
        # Add 1 so the final path separator is removed
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   185
        #self.root_dirLength = len(self.root_dir) + 1
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   186
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   187
    def scan(self):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   188
        """ Scans the files required to zip"""
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   189
        #paths_cache = []
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   190
        
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   191
        excludescopy = self.excludes[:]
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   192
        for f_file in excludescopy:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   193
            if os.path.exists(os.path.normpath(os.path.join(self.root_dir, f_file))):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   194
                self.excludes_files.append(f_file)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   195
                self.excludes.remove(f_file)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   196
        
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   197
        includescopy = self.includes[:]
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   198
        for f_file in includescopy:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   199
            if os.path.exists(os.path.normpath(os.path.join(self.root_dir, f_file))):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   200
                self.includes_files.append(f_file)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   201
                self.includes.remove(f_file)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   202
        
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   203
        LOGGER.debug('Scanning sub-root directories')
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   204
        for root_dir in self.find_subroots():
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   205
            for dirpath, subdirs, files in os.walk(unicode(root_dir)):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   206
                # Let's save the len before it's getting modified.
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   207
                subdirsLen = len(subdirs)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   208
                subroot = dirpath[len(self.root_dir):]
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   209
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   210
                dirs_to_remove = []
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   211
                for subdir in subdirs:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   212
                    if self.is_excluded(os.path.join(subroot, subdir)):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   213
                        dirs_to_remove.append(subdir)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   214
                
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   215
                for dir_remove in dirs_to_remove:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   216
                    subdirs.remove(dir_remove)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   217
                
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   218
                LOGGER.debug('Scanning directory: ' + dirpath)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   219
                for file_ in files:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   220
                    path = os.path.join(subroot, file_)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   221
                    if self.is_filetype(path) and self.is_included(path) and \
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   222
                        self.is_selected(os.path.join(dirpath, file_)) and not self.is_excluded(path):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   223
                        ret_path = os.path.join(dirpath, file_)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   224
                        yield ret_path
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   225
            
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   226
                LOGGER.debug('Checking for empty directory: ' + dirpath)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   227
                # Check for including empty directories
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   228
                if self.is_included(subroot) and not self.is_excluded(subroot):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   229
                    if len(files) == 0 and subdirsLen == 0:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   230
                        LOGGER.debug('Including empty dir: ' + dirpath)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   231
                        yield dirpath
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   232
                    
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   233
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   234
    def find_subroots(self):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   235
        """Finds all the subdirectory roots based on the include paths.
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   236
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   237
        Often large archive operations define a number of archives from the root
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   238
        of the drive. Walking the tree from the root is very time-consuming, so
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   239
        selecting more specific subdirectory roots improves performance.
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   240
        """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   241
        def splitpath(path):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   242
            """ Returns the splitted path"""
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   243
            return path.split(os.sep)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   244
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   245
        root_dirs = []
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   246
        
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   247
        # Look for includes that start with wildcards.
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   248
        subdirs_not_usable = False
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   249
        for inc in self.includes + self.includes_files:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   250
            first_path_segment = splitpath(os.path.normpath(inc))[0]
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   251
            if first_path_segment.find('*') != -1:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   252
                subdirs_not_usable = True
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   253
                
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   254
        # Parse all includes for sub-roots
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   255
        if not subdirs_not_usable:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   256
            for inc in self.includes + self.includes_files:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   257
                include = None
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   258
                LOGGER.debug("===> inc %s" % inc)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   259
                contains_globs = False                
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   260
                for pathcomp in splitpath(os.path.normpath(inc)):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   261
                    if pathcomp.find('*') != -1:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   262
                        contains_globs = True
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   263
                        break
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   264
                    else:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   265
                        if include == None:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   266
                            include = pathcomp
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   267
                        else:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   268
                            include = os.path.join(include, pathcomp)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   269
                if not contains_globs:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   270
                    include = os.path.dirname(include) 
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   271
    
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   272
                LOGGER.debug("include %s" % include)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   273
                if include != None:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   274
                    root_dir = os.path.normpath(os.path.join(self.root_dir, include))
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   275
                    is_new_root = True
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   276
                    for root in root_dirs[:]:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   277
                        if destinsrc(root, root_dir):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   278
                            LOGGER.debug("root contains include, skip it")
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   279
                            is_new_root = False
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   280
                            break
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   281
                        if destinsrc(root_dir, root):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   282
                            LOGGER.debug("include contains root, so remove root")
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   283
                            root_dirs.remove(root)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   284
                    if is_new_root:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   285
                        root_dirs.append(root_dir)    
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   286
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   287
        if len(root_dirs) == 0:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   288
            root_dirs = [os.path.normpath(self.root_dir)]
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   289
        LOGGER.debug('Roots = ' + str(root_dirs))
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   290
        return root_dirs
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   291
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   292
    def __str__(self):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   293
        return os.path.normpath(self.root_dir) + ';' + AbstractScanner.__str__(self) 
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   294
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   295
    def __repr__(self):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   296
        return self.__str__()
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   297
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   298
        
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   299
def move(src, dst):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   300
    """Recursively move a file or directory to another location.
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   301
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   302
    If the destination is on our current filesystem, then simply use
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   303
    rename.  Otherwise, copy src to the dst and then remove src.
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   304
    A lot more could be done here...  A look at a mv.c shows a lot of
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   305
    the issues this implementation glosses over.
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   306
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   307
    """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   308
    try:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   309
        os.rename(src, dst)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   310
    except OSError:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   311
        if os.path.isdir(src):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   312
            if destinsrc(src, dst):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   313
                raise Exception, "Cannot move a directory '%s' into itself '%s'." % (src, dst)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   314
            shutil.copytree(src, dst, symlinks=True)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   315
            rmtree(src)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   316
        else:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   317
            shutil.copy2(src, dst)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   318
            os.unlink(src)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   319
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   320
def rmtree(rootdir):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   321
    """ Catch shutil.rmtree failures on Windows when files are read-only. Thanks Google!""" 
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   322
    if sys.platform == 'win32':
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   323
        rootdir = os.path.normpath(rootdir)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   324
        if not os.path.isabs(rootdir):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   325
            rootdir = os.path.join(os.path.abspath('.'), rootdir)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   326
        if not rootdir.startswith('\\\\'):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   327
            rootdir = u"\\\\?\\" + rootdir
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   328
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   329
    def cb_handle_error(fcn, path, excinfo):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   330
        """ Error handler, removing readonly and deleting the file. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   331
        os.chmod(path, 0666)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   332
        if os.path.isdir(path):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   333
            rmdir(path)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   334
        elif os.path.isfile(path):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   335
            remove(path)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   336
        else:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   337
            fcn(path)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   338
    
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   339
    if 'java' in sys.platform:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   340
        import java.io
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   341
        import org.apache.commons.io.FileUtils
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   342
        f_file = java.io.File(rootdir)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   343
        org.apache.commons.io.FileUtils.deleteDirectory(f_file)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   344
    else:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   345
        return shutil.rmtree(rootdir, onerror=cb_handle_error)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   346
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   347
def destinsrc(src, dst):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   348
    """ Fixed version of destinscr, that doesn't match dst with same root name."""
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   349
    if os.sep == '\\':
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   350
        src = src.lower()
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   351
        dst = dst.lower()
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   352
    src = os.path.abspath(src)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   353
    dst = os.path.abspath(dst)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   354
    if not src.endswith(os.path.sep):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   355
        src += os.path.sep
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   356
    if not dst.endswith(os.path.sep):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   357
        dst += os.path.sep
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   358
    return dst.startswith(src)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   359
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   360
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   361
def which(executable):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   362
    """ Search for executable in the PATH."""
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   363
    pathlist = os.environ['PATH'].split(os.pathsep)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   364
    pathexts = ['']
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   365
    if os.sep == '\\':
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   366
        pathexts = os.environ['PATHEXT'].split(os.pathsep)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   367
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   368
    for folder in pathlist:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   369
        for pathext in pathexts:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   370
            exename = executable
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   371
            if os.sep == '\\' and not exename.lower().endswith(pathext.lower()):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   372
                exename = exename + pathext
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   373
            filename = os.path.join(folder, exename)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   374
            try:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   375
                status = os.stat(filename)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   376
            except os.error:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   377
                continue
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   378
            # Check if the path is a regular file
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   379
            if stat.S_ISREG(status[stat.ST_MODE]):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   380
                mode = stat.S_IMODE(status[stat.ST_MODE])
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   381
                if mode & 0111:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   382
                    return os.path.normpath(filename)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   383
    return None
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   384
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   385
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   386
def read_policy_content(filename):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   387
    """ Read the policy number from the policy file.
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   388
        strict allows to activate the new policy scanning.
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   389
    """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   390
    value = None
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   391
    error = ""
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   392
    try:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   393
        LOGGER.debug('Opening policy file: ' + filename)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   394
        policy_data = load_policy_content(filename)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   395
        match = re.match(r'^((?:\d+)|(?:0842[0-9a-zA-Z]{3}))\s*$', policy_data, re.M|re.DOTALL)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   396
        if match != None:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   397
            value = match.group(1)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   398
        else:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   399
            error = "Content of '%s' doesn't match r'^\d+|0842[0-9a-zA-Z]{3}\s*$'." % filename
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   400
    except Exception, exc:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   401
        error = str(exc)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   402
    if value is not None:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   403
        return value
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   404
    # worse case....
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   405
    raise Exception(error)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   406
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   407
def load_policy_content(filename):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   408
    """ Testing policy content loading. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   409
    data = ''
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   410
    try:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   411
        fileh = codecs.open(filename, 'r', 'ascii')
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   412
        data = fileh.read()
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   413
    except:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   414
        raise Exception("Error loading '%s' as an ASCII file." % filename)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   415
    finally:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   416
        fileh.close()
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   417
    return data
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   418
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   419
ENCODING_MATRIX = {
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   420
   codecs.BOM_UTF8: 'utf_8',
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   421
   codecs.BOM_UTF16: 'utf_16',
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   422
   codecs.BOM_UTF16_BE: 'utf_16_be',
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   423
   codecs.BOM_UTF16_LE: 'utf_16_le',
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   424
}
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   425
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   426
def guess_encoding(data):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   427
    """Given a byte string, guess the encoding.
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   428
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   429
    First it tries for UTF8/UTF16 BOM.
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   430
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   431
    Next it tries the standard 'UTF8', 'ISO-8859-1', and 'cp1252' encodings,
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   432
    Plus several gathered from locale information.
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   433
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   434
    The calling program *must* first call locale.setlocale(locale.LC_ALL, '')
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   435
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   436
    If successful it returns (decoded_unicode, successful_encoding)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   437
    If unsuccessful it raises a ``UnicodeError``.
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   438
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   439
    This was taken from http://www.voidspace.org.uk/python/articles/guessing_encoding.shtml
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   440
    """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   441
    for bom, enc in ENCODING_MATRIX.items():
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   442
        if data.startswith(bom):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   443
            return data.decode(enc), enc
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   444
    encodings = ['ascii', 'UTF-8']
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   445
    successful_encoding = None
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   446
    try:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   447
        encodings.append(locale.getlocale()[1])
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   448
    except (AttributeError, IndexError):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   449
        pass
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   450
    try:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   451
        encodings.append(locale.getdefaultlocale()[1])
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   452
    except (AttributeError, IndexError):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   453
        pass
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   454
    # latin-1
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   455
    encodings.append('ISO8859-1')
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   456
    encodings.append('cp1252')
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   457
    for enc in encodings:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   458
        if not enc:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   459
            continue
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   460
        try:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   461
            decoded = unicode(data, enc)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   462
            successful_encoding = enc
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   463
            break
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   464
        except (UnicodeError, LookupError):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   465
            pass
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   466
    if successful_encoding is None:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   467
        raise UnicodeError('Unable to decode input data. Tried the'
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   468
                           ' following encodings: %s.' %
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   469
                           ', '.join([repr(enc) for enc in encodings if enc]))
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   470
    else:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   471
        if successful_encoding == 'ascii':
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   472
            # our default ascii encoding
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   473
            successful_encoding = 'ISO8859-1'
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   474
        return (decoded, successful_encoding)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   475
        
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   476
def getmd5(fullpath, chunk_size=2**16):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   477
    """ returns the md5 value"""
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   478
    file_handle = open(fullpath, "rb")
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   479
    file_handle.seek(0, os.SEEK_END)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   480
    size = file_handle.tell()
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   481
    file_handle.seek(0, os.SEEK_SET)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   482
    md5 = hashlib.md5()
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   483
    while size > 0:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   484
        toread = chunk_size
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   485
        if size < chunk_size:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   486
            toread = size
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   487
        chunk = file_handle.read(toread)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   488
        size = size - len(chunk)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   489
        md5.update(chunk)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   490
    file_handle.close()
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   491
    return md5.hexdigest()
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   492
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   493
def read_symbian_policy_content(filename):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   494
    """ Read the policy category from the policy file. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   495
    value = None
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   496
    error = ""
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   497
    try:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   498
        LOGGER.debug('Opening symbian policy file: ' + filename)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   499
        try:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   500
            fileh = codecs.open(filename, 'r', 'ascii')
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   501
        except:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   502
            raise Exception("Error loading '%s' as an ASCII file." % filename)        
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   503
        for line in fileh:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   504
            match = re.match(r'^Category\s+([A-Z])\s*$', line, re.M|re.DOTALL)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   505
            if match != None:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   506
                value = match.group(1)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   507
                fileh.close()
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   508
                return value
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   509
        fileh.close()
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   510
        if match == None:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   511
            error = "Content of '%s' doesn't match r'^Category\s+([A-Z])\s*$'." % filename
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   512
    except Exception, exc:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   513
        error = str(exc)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   514
    if value is not None:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   515
        return value
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   516
    # worse case....
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   517
    raise Exception(error)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   518
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   519
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   520
class LockFailedException(Exception):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   521
    """ This class is used to indicate the failure in obtaining a Lock. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   522
    pass
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   523
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   524
if os.name == 'nt':
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   525
    import win32file
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   526
    import win32con
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   527
    import winerror
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   528
    import time
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   529
    import win32netcon
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   530
    import win32wnet
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   531
    
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   532
    class Lock:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   533
        """ This object implement file locking for windows. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   534
        
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   535
        def __init__(self, filename):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   536
            LOGGER_LOCK.debug("__init__")
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   537
            self._filename = filename
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   538
            self.f_desc = None
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   539
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   540
        def lock(self, wait=False):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   541
            """lock the file"""
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   542
            LOGGER_LOCK.debug("lock")
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   543
            # Open the file
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   544
            if self.f_desc == None:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   545
                self.f_desc = open(self._filename, "w+")
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   546
            wfd = win32file._get_osfhandle(self.f_desc.fileno())
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   547
            if not wait:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   548
                try:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   549
                    win32file.LockFile(wfd, 0, 0, 0xffff, 0)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   550
                except:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   551
                    raise LockFailedException()
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   552
            else:    
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   553
                while True:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   554
                    try:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   555
                        win32file.LockFile(wfd, 0, 0, 0xffff, 0)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   556
                        break
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   557
                    except win32file.error, exc:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   558
                        if exc[0] != winerror.ERROR_LOCK_VIOLATION:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   559
                            raise exc
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   560
                    LOGGER_LOCK.debug("waiting")
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   561
                    time.sleep(1)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   562
                    
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   563
        def unlock(self):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   564
            """unlock the file"""
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   565
            LOGGER_LOCK.debug("unlock")
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   566
            if self.f_desc == None:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   567
                LOGGER_LOCK.debug("already unlocked")
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   568
                return
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   569
            wfd = win32file._get_osfhandle(self.f_desc.fileno())
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   570
            try:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   571
                # pylint: disable-msg=E1101
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   572
                win32file.UnlockFile(wfd, 0 , 0, 0xffff, 0)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   573
                self.f_desc.close()
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   574
                self.f_desc = None
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   575
            except win32file.error, exc:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   576
                if exc[0] != 158:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   577
                    raise
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   578
            
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   579
            
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   580
        def __del__(self):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   581
            LOGGER_LOCK.debug("__del__")
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   582
            self.unlock()
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   583
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   584
    def rmdir(path):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   585
        """ Catch os.rmdir failures on Windows when path is too long (more than 256 chars)."""
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   586
        path = win32api.GetShortPathName(path)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   587
        win32file.RemoveDirectory(path)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   588
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   589
    def remove(filename):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   590
        """ Catch os.rmdir failures on Windows when path is too long (more than 256 chars)."""
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   591
        filename = win32api.GetShortPathName(filename)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   592
        filename = filename.lstrip("\\\\?\\")
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   593
        os.remove(filename)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   594
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   595
    def mount(drive, unc, username=None, password=None, persistent=False):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   596
        """ Windows helper function to map a network drive. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   597
        flags = 0
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   598
        if persistent:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   599
            flags = win32netcon.CONNECT_UPDATE_PROFILE
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   600
        win32wnet.WNetAddConnection2(win32netcon.RESOURCETYPE_DISK, drive, unc, None, username, password, flags)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   601
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   602
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   603
    def umount(drive):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   604
        """ Windows helper function to map a network drive. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   605
        drive_type = win32file.GetDriveType(drive)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   606
        if drive_type == win32con.DRIVE_REMOTE:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   607
            win32wnet.WNetCancelConnection2(drive, win32netcon.CONNECT_UPDATE_PROFILE, 1)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   608
        else:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   609
            raise Exception("%s couldn't be umount." % drive)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   610
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   611
else:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   612
    def rmdir(path):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   613
        """remove directory"""
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   614
        return os.rmdir(path)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   615
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   616
    def remove(path):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   617
        """remove the files and folders"""
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   618
        return os.remove(path)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   619
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   620
    class Lock:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   621
        """ This class represents a dummy lock """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   622
        def __init__(self, filename):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   623
            pass
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   624
        def lock(self, wait=False):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   625
            """lock file - do nothing """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   626
            pass
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   627
        def unlock(self):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   628
            """un lock file -do nothing """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   629
            pass
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   630
            
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   631
if os.sep == '\\':
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   632
    def get_next_free_drive():
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   633
        """ Return the first free drive found else it raise an exception. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   634
        if os.name == 'nt':
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   635
            drive_labels = sorted(list(set(string.ascii_uppercase) - set(win32api.GetLogicalDriveStrings())), reverse=True)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   636
            if len(drive_labels) != 0 :
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   637
                return drive_labels[0] + ":"
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   638
            raise OSError("No free drive left.")
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   639
        if 'java' in sys.platform:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   640
            import java.io
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   641
            used = []
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   642
            for _xx in java.io.File.listRoots():
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   643
                used.append(str(_xx).replace(':\\', ''))
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   644
            drive_labels = sorted(list(set(string.ascii_uppercase) - set(used)), reverse=True)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   645
            if len(drive_labels) != 0 :
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   646
                return drive_labels[0] + ":"
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   647
            raise OSError("No free drive left.")
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   648
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   649
    def subst(drive, path):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   650
        """ Substing path as a drive. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   651
        path = os.path.normpath(path)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   652
        p_subst = subprocess.Popen("subst %s %s" % (drive, path),  shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   653
        errmsg = p_subst.communicate()[0]
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   654
        if p_subst.returncode != 0:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   655
            raise Exception("Error substing '%s' under '%s': %s" % (path, drive, errmsg))
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   656
    
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   657
    def unsubst(drive):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   658
        """ Unsubsting the drive. """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   659
        p_subst = subprocess.Popen("subst /D %s" % (drive), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   660
        errmsg = p_subst.communicate()[0]
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   661
        if p_subst.returncode != 0:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   662
            raise Exception("Error unsubsting '%s': %s" % (drive, errmsg))
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   663
    
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   664
    def getSubstedDrives():
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   665
        """get substituted drive"""
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   666
        driveInformation = {}
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   667
        subStedDriveList = []
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   668
        p_subst = subprocess.Popen("subst",  shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   669
        subStedDriveList = re.split('\\n', p_subst.communicate()[0])
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   670
        del subStedDriveList[len(subStedDriveList)-1]
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   671
        for path in subStedDriveList:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   672
            subStedDrivePath = []
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   673
            if(re.search(r'UNC', path) is not None):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   674
                subStedDrivePath = re.split('=>', path)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   675
                (drive_to_unsubst, _) = os.path.splitdrive(os.path.normpath(subStedDrivePath[0]))
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   676
                uncPath = re.sub('UNC', r'\\', subStedDrivePath[1].strip())
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   677
                if(uncPath != subStedDrivePath[1].strip()):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   678
                    driveInformation[drive_to_unsubst] = uncPath
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   679
            else:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   680
                subStedDrivePath = re.split('=>', path)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   681
                (drive_to_unsubst, _) = os.path.splitdrive(os.path.normpath(subStedDrivePath[0]))
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   682
                driveInformation[drive_to_unsubst] = os.path.normpath(subStedDrivePath[1].strip())
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   683
    
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   684
        return driveInformation
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   685
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   686
def touch(srcdir):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   687
    """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   688
    Recursively touches all the files in the source path mentioned.
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   689
    It does not touch the directories.
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   690
    """
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   691
    srcnames = os.listdir(srcdir)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   692
    for name in srcnames:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   693
        srcfname = os.path.join(srcdir, name)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   694
        if os.path.isdir(srcfname):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   695
            touch(srcfname)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   696
        else:
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   697
            if os.path.exists(srcfname):
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents:
diff changeset
   698
                os.utime(srcfname, None)