Exemplo n.º 1
0
 def test_semaphore_exception(self):
     """The semaphore should throw an exception when the wait time is over"""
     lockfile = join('/tmp', sha1(settings.SECRET_KEY).hexdigest() +'.semaphore')
     try:
         with mutex(max_wait=0, lockfile=lockfile):
             with mutex(max_wait=0, lockfile=lockfile):
                 pass
     except SemaphoreException:
         pass
     else:
         self.fail('Should have triggered SemaphoreException')
     self.assertFalse(os.path.exists(lockfile))
Exemplo n.º 2
0
 def test_semaphore(self):
     """the semaphore should bind a file to the context and hold on to it untill after the context is closed"""
     lockfile = join('/tmp', sha1(settings.SECRET_KEY).hexdigest() +'.semaphore')
     for a in range(1, 4):
         with mutex():
             assert(os.path.isfile(lockfile))
     
         assert(not os.path.exists(lockfile))
Exemplo n.º 3
0
    def __call__(self, instance, sender=None, **kwargs):
        """
        Handler for the post_save signal.
        Will create a django.po file in each language,
        in the directory specified by self.location.
        """

        # no need to create translation files if not in the master site
        if not getattr(settings, 'MASTER_SITE', False):
            return
            
        if hasattr(instance, 'language') and instance.language != settings.LANGUAGE_CODE:
            return
        
        if get_language() != settings.LANGUAGE_CODE:
            return

        po_form = self.poify(instance)
        if po_form is not None:
            # for each language create a po file
            for language in get_language_codes():
                
                locale_dir = os.path.join( self.location , 'locale',
                    to_locale(language), 'LC_MESSAGES')
                locale_file = os.path.join( locale_dir, 'django.po')

                # create locale dir if not available
                if not os.path.isdir(locale_dir):
                    os.makedirs(locale_dir)
                    
                # acquire exclusive lock this should only be run
                # by one process at a time                
                with mutex(max_wait=30):
                    # write proper header if the file is new
                    # this header contains the character set.
                    write_header = not os.path.exists(locale_file)
                    
                    if write_header: # write header and po stuffz0r
                        with codecs.open(locale_file, 'w', 'utf-8') as fp:
                            fp.write(polibext.po_to_unicode(po_form))
                            
                    else: 
                        #merge existing translation with the new one
                        msg = self.msgmerge(locale_file, polibext.po_to_unicode(po_form).encode('utf-8'))
                        if msg:
                            if len(po_form):
                                # add the new entries to the po file
                                with codecs.open(locale_file, 'a', 'utf-8') as fp:
                                    for entry in po_form: # this avoids printing the header
                                        fp.write(u"\n")
                                        fp.write(polibext.po_to_unicode(entry))
                                        
                                # filter away duplicates  
                                msg = self.msguniq(locale_file)
                                # save entire po file once more and overwrite with filtered shizzle
                                with open(locale_file, 'w') as fp:
                                    fp.write(msg)