Пример #1
0
 def assert_policy_file(self, filename, value=None, exception=False):
     if exception:
         try:
             fileutils.read_policy_content(filename)
             assert "Should fail while loading '%s'." % filename
         except:
             pass
     else:
         assert fileutils.read_policy_content(filename) == value
Пример #2
0
 def validate_content(self, filename):
     """  Validating the policy file content. If it cannot be decoded, 
         it reports an 'invalidencoding'.            
         Case 'notinidlist': value is not defined under the id list.
     """
     value = None
     try:
         value = fileutils.read_policy_content(filename)
     except Exception:
         yield ["invalidencoding", filename, None]
     if value is not None:                        
         if self._ids != None:
             if value not in self._ids:
                 yield ["notinidlist", filename, value]
Пример #3
0
 def get_dir_policy(self, dirname):
     """ Get policy value for a specific directory. """
     dirname = os.path.normpath(dirname)
     if not self._policy_cache.has_key(dirname):
         policyfile = None
         for name in self.get_policy_filenames():
             if os.sep != '\\':
                 for filename in os.listdir(dirname):
                     if filename.lower() == name.lower():
                         policyfile = os.path.join(dirname, filename)
                         break
             elif os.path.exists(os.path.join(dirname, name)): 
                 policyfile = os.path.join(dirname, name)
                 break
         
         value = self._config.get('policy.default.value', MISSING_POLICY)
         if policyfile != None:      #policy file present
             try:
                 value = fileutils.read_policy_content(policyfile)
                 if value not in self._binary.keys():    #check policy file is valid
                     _logger.error("POLICY_ERROR: policy file found %s but policy %s value not exists in csv" % (policyfile, value))
             except Exception, exc:
                 _logger.error("POLICY_ERROR: %s" % exc)         
                 value = self._config.get('policy.default.value', MISSING_POLICY)
         else:       #no policy file present
             filePresent = False
             dirPresent = False
             for ftype in os.listdir(dirname):   #see if files or directories are present
                 if os.path.isdir(os.path.join(dirname, ftype)):
                     dirPresent = True
                 if os.path.isfile(os.path.join(dirname, ftype)):
                     filePresent = True
                     
             if filePresent:    #files present : error     
                 _logger.error("POLICY_ERROR: could not find a policy file under: '%s'" % dirname)
             elif dirPresent and not filePresent:  #directories only : warning
                 _logger.error("POLICY_WARNING: no policy file, no files present, but sub-folder present in : '%s'" % dirname)
             else:       #no files no dirs : warning
                 _logger.error("POLICY_WARNING: empty directory at : '%s'" % dirname)
             
         # saving the policy value for that directory.
         self._policy_cache[dirname] = value
Пример #4
0
    def is_selected(self, path):
        """ Determines if the path is selected by this selector. """
        current_dir = os.path.abspath(os.path.dirname(path))
        logger.debug('is_selected: current dir = ' + current_dir + '  ' + str(os.path.exists(current_dir)))
        result = False
        policy_file = None
        # finding the distribution policy from the filelist.
        for filename in self._policy_files:
            #slow method on case sensitive system
            if os.sep != '\\':
                for f in os.listdir(current_dir):
                    if f.lower() == filename.lower():
                        policy_file = os.path.join(current_dir, f)
                        break
            elif os.path.exists(os.path.join(current_dir, filename)):            
                policy_file = os.path.join(current_dir, filename)
                logger.debug('Using Policy file: ' + policy_file)
                break

        policy_value = None
        if policy_file is None:
            if not self._ignoremissingpolicyfiles:
                logger.error("POLICY_ERROR: Policy file not found under '%s' using names [%s]" % (current_dir, ", ".join(self._policy_files)))
            policy_value = archive.mappers.MISSING_POLICY
        else:
            try:
                policy_value = fileutils.read_policy_content(policy_file)
            except Exception:
                logger.warning('POLICY_ERROR: Exception thrown parsing policy file: ' + policy_file)
                policy_value = archive.mappers.MISSING_POLICY
        # loop through the possible values
        for value in self.values:
            (val, negate) = self.get_value_and_negate(value)
            logger.debug('Policy value: ' + str(policy_value) + '  ' + val)
            if (not negate and policy_value == val) or (negate and policy_value != val):
                return True
        return False