Skip to content

Commit

Permalink
Issue webcompat#1161 - Print message if there is nothing to backup
Browse files Browse the repository at this point in the history
Follow-up of issue webcompat#1157, print message if there is nothing to backup
to BACKUP_DEFAULT_DEST

Issue webcompat#1294 - Check BAKCUP_DEFAULT_DEST before backup

Also fix the syntax, no warning using PEP8 check.
  • Loading branch information
MDTsai committed Jan 25, 2017
1 parent 0544562 commit 476c317
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,35 @@ def config_validator():
print("Starting server in ~*TEST MODE*~")
app.run(host='localhost')
elif args.backup:
issue_engine = create_engine('sqlite:///' + os.path.join(app.config['BASE_DIR'], 'issues.db'))
# Check 'BACKUP_DEFAULT_DEST' before backup
if 'BACKUP_DEFAULT_DEST' not in app.config.keys():
sys.exit('Please define BAKCUP_DEFAULT_DEST in secrets.py.')

issue_engine = create_engine(
'sqlite:///' + os.path.join(app.config['BASE_DIR'], 'issues.db'))
issue_session_maker = sessionmaker(autocommit=False,
autoflush=False,
bind=issue_engine)
issue_db = scoped_session(issue_session_maker)
# Take a backup if issues.db has data dump.
if issue_db().execute('select count(*) from webcompat_issues').fetchall()[0][0] > 0:
if (issue_db().execute('select count(*) from webcompat_issues')
.fetchall()[0][0] > 0):
if not os.path.exists(app.config['BACKUP_DEFAULT_DEST']):
print ('Creating backup directory at {}').format(os.path.join(app.config['BASE_DIR'], 'issues.db'))
print ('Creating backup directory at {}').format(
os.path.join(app.config['BASE_DIR'], 'issues.db'))
os.makedirs(app.config['BACKUP_DEFAULT_DEST'])
time_stamp = time.strftime('%Y-%m-%dT%H-%M-%S', time.localtime())
issue_backup_db = 'issues_' + time_stamp + '.db'
os.rename(os.getcwd() + '/issues.db', app.config['BACKUP_DEFAULT_DEST'] + issue_backup_db)
os.rename(os.getcwd() + '/issues.db',
app.config['BACKUP_DEFAULT_DEST'] + issue_backup_db)
# Retain last 3 recent backup files
backup_files = os.listdir(app.config['BACKUP_DEFAULT_DEST'])
backup_files.sort()
for old_file in backup_files[:-3]:
os.remove(app.config['BACKUP_DEFAULT_DEST'] + old_file)
else:
print('There is nothing to backup to ' +
app.config['BACKUP_DEFAULT_DEST'])
else:
if check_pip_deps():
app.run(host='localhost')

0 comments on commit 476c317

Please sign in to comment.