Skip to content
Snippets Groups Projects
Commit 16784add authored by Sebastien Luttringer's avatar Sebastien Luttringer
Browse files

cachify doesn't create empty file when failing to get database

this commit also make cachify more exception safe
parent 0dbc483a
No related branches found
No related tags found
No related merge requests found
...@@ -84,6 +84,7 @@ class Repository(object): ...@@ -84,6 +84,7 @@ class Repository(object):
try: try:
self.db = Database(config.dbpath) self.db = Database(config.dbpath)
except: except:
debug("Unable to load database %s" % config.dbpath)
self.config.offline = True self.config.offline = True
if self.config.offline: if self.config.offline:
debug("Repository %s is offline" % config.name) debug("Repository %s is offline" % config.name)
...@@ -542,23 +543,23 @@ class RepositoryManager(object): ...@@ -542,23 +543,23 @@ class RepositoryManager(object):
:param temp: repository db should be stored in a temporary location :param temp: repository db should be stored in a temporary location
:param nosync: if a cache exists, don't try to update it :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: try:
original_dbpath = config.dbpath
if temp and nosync: if temp and nosync:
raise IOError("Unable to cache, sync is disabled") raise IOError("sync is disabled")
# Ensure destination file exists elif temp:
if temp is True or self.cache_path is None: # this is a temporary cached repository
# this is a forced temporary repository or without name repo tempfd, config.dbpath = tempfile.mkstemp()
tempfd, filedest = tempfile.mkstemp()
os.close(tempfd) os.close(tempfd)
self.tempfiles.append(filedest) self.tempfiles.append(config.dbpath)
else: else:
filedest = os.path.join(self.cache_path, config.name) config.dbpath = os.path.join(self.cache_path, config.name)
# create file if not exists
if not os.path.exists(filedest):
open(filedest, "wb")
if not nosync: if not nosync:
# Open remote database # Open remote database
rdb = PipeFile(config.dbpath, timeout=self.timeout) rdb = PipeFile(original_dbpath, timeout=self.timeout)
# get remote last modification # get remote last modification
if rdb.mtime is None: if rdb.mtime is None:
# We doesn't have modification time, we use the last file # We doesn't have modification time, we use the last file
...@@ -570,21 +571,28 @@ class RepositoryManager(object): ...@@ -570,21 +571,28 @@ class RepositoryManager(object):
else: else:
rlast = rdb.mtime rlast = rdb.mtime
# get local last value # get local last value
llast = int(os.stat(filedest).st_mtime) 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 repo is out of date, download it
if rlast != llast: if rlast != llast:
arrow("Downloading %s" % config.dbpath) try:
rdb.progressbar = True arrow("Downloading %s" % original_dbpath)
ldb = open(filedest, "wb") rdb.progressbar = True
rdb.consume(ldb) ldb = open(config.dbpath, "wb")
ldb.close() rdb.consume(ldb)
rdb.close() ldb.close()
istools.chrights(filedest, rdb.close()
uid=config.uid, istools.chrights(config.dbpath,
gid=config.gid, uid=config.uid,
mode=config.fmod, gid=config.gid,
mtime=rlast) mode=config.fmod,
config.dbpath = filedest mtime=rlast)
except:
if os.path.exists(config.dbpath):
os.unlink(config.dbpath)
raise
except IOError as e: except IOError as e:
# if something append bad during caching, we mark repo as offline # if something append bad during caching, we mark repo as offline
debug("Unable to cache repository %s: %s" % (config.name, e)) debug("Unable to cache repository %s: %s" % (config.name, e))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment