Пример #1
0
 def _cachify(self, config, temp=False, nosync=False):
     '''
     Return a config of a cached repository from an orignal config file
     :param config: repository configuration
     :param temp: repository db should be stored in a temporary location
     :param nosync: if a cache exists, don't try to update it
     '''
     # if cache is disable => temp =True
     if self.cache_path is None:
         temp = True
     try:
         original_dbpath = config.dbpath
         if temp and nosync:
             raise ISError("sync is disabled")
         elif temp:
             # this is a temporary cached repository
             tempfd, config.dbpath = tempfile.mkstemp()
             os.close(tempfd)
             self.tempfiles.append(config.dbpath)
         else:
             config.dbpath = os.path.join(self.cache_path, config.name)
         if not nosync:
             # Open remote database
             rdb = PipeFile(original_dbpath, timeout=self.timeout)
             # get remote last modification
             if rdb.mtime is None:
                 # We doesn't have modification time, we use the last file
                 try:
                     rlast = int(PipeFile(config.lastpath, mode='r',
                                          timeout=self.timeout).read().strip())
                 except ISError:
                     rlast = -1
             else:
                 rlast = rdb.mtime
             # get local last value
             if os.path.exists(config.dbpath):
                 llast = int(os.stat(config.dbpath).st_mtime)
             else:
                 llast = -2
             # if repo is out of date, download it
             if rlast != llast:
                 try:
                     arrow(u"Downloading %s" % original_dbpath)
                     rdb.progressbar = True
                     ldb = open(config.dbpath, "wb")
                     rdb.consume(ldb)
                     ldb.close()
                     rdb.close()
                     istools.chrights(config.dbpath,
                                      uid=config.uid,
                                      gid=config.gid,
                                      mode=config.fmod,
                                      mtime=rlast)
                 except:
                     if os.path.exists(config.dbpath):
                         os.unlink(config.dbpath)
                     raise
     except ISError as e :
         # if something append bad during caching, we mark repo as offline
         debug(u"Unable to cache repository %s: %s" % (config.name, e))
         config.offline = True
     return self.factory.create(config)