<commit_before>from __future__ import unicode_literals
import os
import frappe
from frappe import _
from frappe.utils import get_site_path, cint, get_url
from frappe.utils.data import convert_utc_to_user_timezone
import datetime

def get_context(context):
	def get_time(path):
		dt = os.path.getmtime(path)
		return convert_utc_to_user_timezone(datetime.datetime.utcfromtimestamp(dt)).strftime('%Y-%m-%d %H:%M')

	def get_size(path):
		size = os.path.getsize(path)
		if size > 1048576:
			return "{0:.1f}M".format(float(size) / 1048576)
		else:
			return "{0:.1f}K".format(float(size) / 1024)

	path = get_site_path('private', 'backups')
	files = [x for x in os.listdir(path) if os.path.isfile(os.path.join(path, x))]
	backup_limit = get_scheduled_backup_limit()

	if len(files) > backup_limit:
		cleanup_old_backups(path, files, backup_limit)

	files = [('/backups/' + _file,
		get_time(os.path.join(path, _file)),
		get_size(os.path.join(path, _file))) for _file in files if _file.endswith('sql.gz')]
	files.sort(key=lambda x: x[1], reverse=True)

	return {"files": files}

def get_scheduled_backup_limit():
	backup_limit = frappe.db.get_singles_value('System Settings', 'backup_limit')
	return cint(backup_limit)

def cleanup_old_backups(site_path, files, limit):
	backup_paths = []
	minimum_site_backups = frappe.conf.minimum_site_backups or 3
	for f in files:
		if f.endswith('sql.gz'):
			_path = os.path.abspath(os.path.join(site_path, f))
			backup_paths.append(_path)

	backup_paths = sorted(backup_paths, key=os.path.getctime)
	files_to_delete = len(backup_paths) - limit
	remaining_backups = len(backup_paths) - files_to_delete

	# keep atleast specified number of backups
	if remaining_backups <= minimum_site_backups:
		files_to_delete = len(backup_paths) - minimum_site_backups

	for idx in range(0, files_to_delete):
		f = os.path.basename(backup_paths[idx])
		files.remove(f)

		os.remove(backup_paths[idx])

def delete_downloadable_backups():
	path = get_site_path('private', 'backups')
	files = [x for x in os.listdir(path) if (os.path.isfile(os.path.join(path, x)) and x.endswith('sql.gz'))]
	backup_limit = get_scheduled_backup_limit()

	if len(files) > backup_limit:
		cleanup_old_backups(path, files, backup_limit)

@frappe.whitelist()
def schedule_files_backup(user_email):<commit_msg>fix(backup): use system setting's limit instead of 3<commit_after>from __future__ import unicode_literals
import os
import frappe
from frappe import _
from frappe.utils import get_site_path, cint, get_url
from frappe.utils.data import convert_utc_to_user_timezone
import datetime

def get_context(context):
	def get_time(path):
		dt = os.path.getmtime(path)
		return convert_utc_to_user_timezone(datetime.datetime.utcfromtimestamp(dt)).strftime('%Y-%m-%d %H:%M')

	def get_size(path):
		size = os.path.getsize(path)
		if size > 1048576:
			return "{0:.1f}M".format(float(size) / 1048576)
		else:
			return "{0:.1f}K".format(float(size) / 1024)

	path = get_site_path('private', 'backups')
	files = [x for x in os.listdir(path) if os.path.isfile(os.path.join(path, x))]
	backup_limit = get_scheduled_backup_limit()

	if len(files) > backup_limit:
		cleanup_old_backups(path, files, backup_limit)

	files = [('/backups/' + _file,
		get_time(os.path.join(path, _file)),
		get_size(os.path.join(path, _file))) for _file in files if _file.endswith('sql.gz')]
	files.sort(key=lambda x: x[1], reverse=True)

	return {"files": files}

def get_scheduled_backup_limit():
	backup_limit = frappe.db.get_singles_value('System Settings', 'backup_limit')
	return cint(backup_limit)

def cleanup_old_backups(site_path, files, limit):
	backup_paths = []
	minimum_site_backups = frappe.conf.minimum_site_backups or limit
	for f in files:
		if f.endswith('sql.gz'):
			_path = os.path.abspath(os.path.join(site_path, f))
			backup_paths.append(_path)

	backup_paths = sorted(backup_paths, key=os.path.getctime)
	files_to_delete = len(backup_paths) - limit
	remaining_backups = len(backup_paths) - files_to_delete

	# keep atleast specified number of backups
	if remaining_backups <= minimum_site_backups:
		files_to_delete = len(backup_paths) - minimum_site_backups

	for idx in range(0, files_to_delete):
		f = os.path.basename(backup_paths[idx])
		files.remove(f)

		os.remove(backup_paths[idx])

def delete_downloadable_backups():
	path = get_site_path('private', 'backups')
	files = [x for x in os.listdir(path) if (os.path.isfile(os.path.join(path, x)) and x.endswith('sql.gz'))]
	backup_limit = get_scheduled_backup_limit()

	if len(files) > backup_limit:
		cleanup_old_backups(path, files, backup_limit)

@frappe.whitelist()
def schedule_files_backup(user_email):
{'fraction_non_alphanumeric': 0.1270658939686628, 'fraction_numerical': 0.010731916720326251, 'mean_word_length': 5.793002915451895, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6066366', 'n_tokens_mistral': 1831, 'n_tokens_neox': 1729, 'n_words': 346}