-
Notifications
You must be signed in to change notification settings - Fork 433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Viewmanager.py: add check disk space #1212
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,7 +80,7 @@ | |
from gramps.gen.const import (HOME_DIR, ICON, URL_BUGTRACKER, URL_HOMEPAGE, | ||
URL_MAILINGLIST, URL_MANUAL_PAGE, URL_WIKISTRING, | ||
WIKI_EXTRAPLUGINS, URL_BUGHOME) | ||
from gramps.gen.constfunc import is_quartz | ||
from gramps.gen.constfunc import is_quartz, win | ||
from gramps.gen.config import config | ||
from gramps.gen.errors import WindowActiveError | ||
from .dialog import ErrorDialog, WarningDialog, QuestionDialog2, InfoDialog | ||
|
@@ -580,10 +580,47 @@ def no_del_event(self, *obj): | |
hits 'x' multiple times. """ | ||
return True | ||
|
||
def checkdisk(self, dbfolder): # TODO check this function | ||
""" | ||
Check disk space | ||
""" | ||
if win(): # for windows: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What should we do for Windows? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gramps work in Windows too. it's draft for Windows. I check this code in my Linux system, but I have not Windows. |
||
# import psutil | ||
# DISK = "C:" | ||
# free = psutil.disk_usage(DISK).free/(1024*1024) | ||
# print(f"{free:.4} Mb free on disk {DISK}") | ||
pass | ||
else: # for linux: | ||
fd = os.open(dbfolder, os.O_RDONLY) | ||
stats = os.fstatvfs(fd) | ||
os.close(fd) | ||
freedisk = round(stats[1] * stats[4]/(1024*1024)) | ||
return freedisk | ||
|
||
def quit(self, *obj): | ||
""" | ||
Closes out the program, backing up data | ||
""" | ||
#**************** | ||
# We must check disk space before: | ||
# - run (Gramps need space for load config file) | ||
# - quit (Gramps need some space for: | ||
# - db.commit and close database, | ||
# - save config file) | ||
# - every backup (we need space 'last_backup_file_size'+ 5%) | ||
diskspace = 20 # Disk space for Linux - 20 MB, for Gramps - 5 MB, for backup 'last_backup_file_size' + 5% | ||
if (self.checkdisk(config.get('database.path')) < diskspace): | ||
self.uistate.push_message(self.dbstate, _("Disk space < " + str(diskspace) + " MB. Quit impossible. Clear your disk and repeat this operation.")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use format strings instead of concatenation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not Python man. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I rewrite this code. New PR have normal string concatenation. |
||
WarningDialog( | ||
_("Message for user:"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps "Low disk space" instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, of course. |
||
_('You have less ' + str(diskspace) + ' MB on system disk.\n' | ||
'Gramps need more space for save database and config file before quit.\n' | ||
'Clear your disk and repeat this operation again.'), parent=self.uistate.window) | ||
return | ||
else: | ||
pass | ||
# *********** | ||
|
||
# mark interface insenstitive to prevent unexpected events | ||
self.uistate.set_sensitive(False) | ||
# the following prevents reentering quit if user hits 'x' again | ||
|
@@ -1188,6 +1225,35 @@ def autobackup(self): | |
""" | ||
Backup the current family tree. | ||
""" | ||
#************ | ||
# Before every backup Gramps must check disk space. | ||
# Needed space = last_backup_file_size + 5% | ||
#************ | ||
|
||
backup_name = self.dbstate.db.get_dbname() | ||
backup_ext = ".gramps" | ||
path = config.get('database.backup-path') | ||
bpath = sorted(os.listdir(path)) | ||
for n, bfile in enumerate(bpath): | ||
if os.path.isfile(os.path.join(path, bfile)) and bfile.startswith(backup_name) and bfile.endswith(backup_ext): | ||
blastfile = bfile | ||
|
||
bfile_size = round(os.path.getsize(os.path.join(path, blastfile))/(1024)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A utility function to return the largest backup size would be neater. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think about this - we must use biggest or last backup file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. It just needs to be put into a utility function in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I test this code two day.
|
||
bfile_new_size = round(bfile_size/100*105) | ||
|
||
diskspace = bfile_new_size # Disk space for Linux - 20 MB, for Gramps - 5 MB, for backup 'last_backup_file_size' + 5% | ||
if (self.checkdisk(config.get('database.backup-path')) < diskspace): | ||
self.uistate.push_message(self.dbstate, _("Disk space < " + str(diskspace) + " MB. Backup impossible. Clear your disk and Gramps create backup next time.")) | ||
print(_("Disk space < " + str(diskspace) + " kB. Backup impossible. Clear your disk and Gramps create backup next time.")) | ||
WarningDialog( | ||
_("Message for user:"), | ||
_('You have less ' + str(diskspace) + ' kB on backup disk.\n' | ||
'Gramps need more space for save backup.\n' | ||
'Clear your disk and Gramps create backup next time.'), parent=self.uistate.window) | ||
return | ||
else: | ||
pass | ||
|
||
if self.delay_timer is not None: | ||
GLib.source_remove(self.delay_timer) | ||
self.delay_timer = None | ||
|
@@ -1848,3 +1914,4 @@ def media_toggle(self, widget, file_entry): | |
file_entry.set_text("%s.%s" % (base, extension)) | ||
else: | ||
file_entry.set_text("%s.%s" % (filename, extension)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This belongs in
gramps/gen/utils/file.py
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, in new PR this function move to file.py.