Example #1
0
def backup(
    context, with_files=False, backup_path_db=None, backup_path_files=None, backup_path_private_files=None, quiet=False
):
    "Backup"
    from frappe.utils.backups import scheduled_backup

    verbose = context.verbose
    for site in context.sites:
        frappe.init(site=site)
        frappe.connect()
        odb = scheduled_backup(
            ignore_files=not with_files,
            backup_path_db=backup_path_db,
            backup_path_files=backup_path_files,
            backup_path_private_files=backup_path_private_files,
            force=True,
        )
        if verbose:
            from frappe.utils import now

            print "database backup taken -", odb.backup_path_db, "- on", now()
            if with_files:
                print "files backup taken -", odb.backup_path_files, "- on", now()
                print "private files backup taken -", odb.backup_path_private_files, "- on", now()

        frappe.destroy()
Example #2
0
def _set_limits(context, site, limits):
	import datetime

	if not limits:
		return

	if not site:
		site = get_site(context)

	with frappe.init_site(site):
		frappe.connect()
		new_limits = {}
		for limit, value in limits:
			if limit not in ('daily_emails', 'emails', 'space', 'users', 'email_group',
				'expiry', 'support_email', 'support_chat', 'upgrade_url'):
				frappe.throw(_('Invalid limit {0}').format(limit))

			if limit=='expiry' and value:
				try:
					datetime.datetime.strptime(value, '%Y-%m-%d')
				except ValueError:
					raise ValueError("Incorrect data format, should be YYYY-MM-DD")

			elif limit=='space':
				value = float(value)

			elif limit in ('users', 'emails', 'email_group', 'daily_emails'):
				value = int(value)

			new_limits[limit] = value

		update_limits(new_limits)
Example #3
0
def scheduler_task(site, event, handler, now=False):
	from frappe.utils.scheduler import log
	traceback = ""
	task_logger.info('running {handler} for {site} for event: {event}'.format(handler=handler, site=site, event=event))
	try:
		frappe.init(site=site)
		if not create_lock(handler):
			return
		if not now:
			frappe.connect(site=site)
		frappe.get_attr(handler)()

	except Exception:
		frappe.db.rollback()
		traceback = log(handler, "Method: {event}, Handler: {handler}".format(event=event, handler=handler))
		task_logger.warn(traceback)
		raise

	else:
		frappe.db.commit()

	finally:
		delete_lock(handler)

		if not now:
			frappe.destroy()

	task_logger.info('ran {handler} for {site} for event: {event}'.format(handler=handler, site=site, event=event))
Example #4
0
def install_db(root_login="******", root_password=None, db_name=None, source_sql=None,
	admin_password=None, verbose=True, force=0, site_config=None, reinstall=False):
	make_conf(db_name, site_config=site_config)
	frappe.flags.in_install_db = True
	if reinstall:
		frappe.connect(db_name=db_name)
		dbman = DbManager(frappe.local.db)
		dbman.create_database(db_name)

	else:
		frappe.local.db = get_root_connection(root_login, root_password)
		frappe.local.session = frappe._dict({'user':'******'})
		create_database_and_user(force, verbose)

	frappe.conf.admin_password = frappe.conf.admin_password or admin_password

	frappe.connect(db_name=db_name)
	check_if_ready_for_barracuda()
	import_db_from_sql(source_sql, verbose)
	if not 'tabDefaultValue' in frappe.db.get_tables():
		print('''Database not installed, this can due to lack of permission, or that the database name exists.
Check your mysql root password, or use --force to reinstall''')
		sys.exit(1)

	remove_missing_apps()

	create_auth_table()
	setup_global_search_table()
	create_user_settings_table()

	frappe.flags.in_install_db = False
Example #5
0
def list_apps(context):
	"List apps in site"
	site = get_site(context)
	frappe.init(site=site)
	frappe.connect()
	print("\n".join(frappe.get_installed_apps()))
	frappe.destroy()
Example #6
0
def run_async_task(self, site=None, user=None, cmd=None, form_dict=None, hijack_std=False):
	ret = {}
	frappe.init(site)
	frappe.connect()

	frappe.local.task_id = self.request.id

	if hijack_std:
		original_stdout, original_stderr = sys.stdout, sys.stderr
		sys.stdout, sys.stderr = get_std_streams(self.request.id)
		frappe.local.stdout, frappe.local.stderr = sys.stdout, sys.stderr

	try:
		set_task_status(self.request.id, "Running")
		frappe.db.commit()
		frappe.set_user(user)
		# sleep(60)
		frappe.local.form_dict = frappe._dict(form_dict)
		execute_cmd(cmd, from_async=True)
		ret = frappe.local.response

	except Exception, e:
		frappe.db.rollback()
		ret = frappe.local.response
		http_status_code = getattr(e, "http_status_code", 500)
		ret['status_code'] = http_status_code
		frappe.errprint(frappe.get_traceback())
		frappe.utils.response.make_logs()
		set_task_status(self.request.id, "Error", response=ret)
		task_logger.error('Exception in running {}: {}'.format(cmd, ret['exc']))
Example #7
0
def sendmail(site, communication_name, print_html=None, print_format=None, attachments=None,
	recipients=None, cc=None):
	try:
		frappe.connect(site=site)

		# upto 3 retries
		for i in xrange(3):
			try:
				communication = frappe.get_doc("Communication", communication_name)
				communication._notify(print_html=print_html, print_format=print_format, attachments=attachments,
					recipients=recipients, cc=cc)
			except MySQLdb.OperationalError, e:
				# deadlock, try again
				if e.args[0]==1213:
					frappe.db.rollback()
					time.sleep(1)
					continue
				else:
					raise
			else:
				break

	except:
		frappe.db.rollback()

	else:
		frappe.db.commit()

	finally:
		frappe.destroy()
Example #8
0
def install_db(root_login="******", root_password=None, db_name=None, source_sql=None,
	admin_password=None, verbose=True, force=0, site_config=None, reinstall=False):
	make_conf(db_name, site_config=site_config)
	frappe.flags.in_install_db = True
	if reinstall:
		frappe.connect(db_name=db_name)
		dbman = DbManager(frappe.local.db)
		dbman.create_database(db_name)

	else:
		frappe.local.db = get_root_connection(root_login, root_password)
		frappe.local.session = frappe._dict({'user':'******'})
		create_database_and_user(force, verbose)

	frappe.conf.admin_password = frappe.conf.admin_password or admin_password

	frappe.connect(db_name=db_name)
	check_if_ready_for_barracuda()
	import_db_from_sql(source_sql, verbose)
	remove_missing_apps()

	create_auth_table()
	create_list_settings_table()

	frappe.flags.in_install_db = False
Example #9
0
def scheduler(context, state, site=None):
	from frappe.installer import update_site_config
	import frappe.utils.scheduler

	if not site:
		site = get_site(context)

	try:
		frappe.init(site=site)

		if state == 'pause':
			update_site_config('pause_scheduler', 1)
		elif state == 'resume':
			update_site_config('pause_scheduler', 0)
		elif state == 'disable':
			frappe.connect()
			frappe.utils.scheduler.disable_scheduler()
			frappe.db.commit()
		elif state == 'enable':
			frappe.connect()
			frappe.utils.scheduler.enable_scheduler()
			frappe.db.commit()

		print('Scheduler {0}d for site {1}'.format(state, site))

	finally:
		frappe.destroy()
def migrate(context, rebuild_website=False):
	"Run patches, sync schema and rebuild files/translations"
	import frappe.modules.patch_handler
	import frappe.model.sync
	from frappe.utils.fixtures import sync_fixtures
	import frappe.translate
	from frappe.desk.notifications import clear_notifications

	for site in context.sites:
		print 'Migrating', site
		frappe.init(site=site)
		frappe.connect()

		try:
			prepare_for_update()

			# run patches
			frappe.modules.patch_handler.run_all()
			# sync
			frappe.model.sync.sync_all(verbose=context.verbose)
			frappe.translate.clear_cache()
			sync_fixtures()

			clear_notifications()
		finally:
			frappe.destroy()

	if rebuild_website:
		call_command(build_website, context)
	else:
		call_command(sync_www, context)
Example #11
0
def backup_to_dropbox():
	if not frappe.db:
		frappe.connect()

	# upload database
	dropbox_settings = get_dropbox_settings()

	if not dropbox_settings['access_token']:
		access_token = generate_oauth2_access_token_from_oauth1_token(dropbox_settings)

		if not access_token.get('oauth2_token'):
			return 'Failed backup upload', 'No Access Token exists! Please generate the access token for Dropbox.'

		dropbox_settings['access_token'] = access_token['oauth2_token']
		set_dropbox_access_token(access_token['oauth2_token'])


	dropbox_client = dropbox.Dropbox(dropbox_settings['access_token'])
	backup = new_backup(ignore_files=True)
	filename = os.path.join(get_backups_path(), os.path.basename(backup.backup_path_db))
	upload_file_to_dropbox(filename, "/database", dropbox_client)

	frappe.db.close()
	
	# upload files to files folder
	did_not_upload = []
	error_log = []

	upload_from_folder(get_files_path(), "/files", dropbox_client, did_not_upload, error_log)
	upload_from_folder(get_files_path(is_private=1), "/private/files", dropbox_client, did_not_upload, error_log)

	frappe.connect()
	return did_not_upload, list(set(error_log))
Example #12
0
def execute(context, method, args=None, kwargs=None):
	"execute a function"
	for site in context.sites:
		try:
			frappe.init(site=site)
			frappe.connect()

			if args:
				args = eval(args)
			else:
				args = ()

			if kwargs:
				kwargs = eval(args)
			else:
				kwargs = {}

			ret = frappe.get_attr(method)(*args, **kwargs)

			if frappe.db:
				frappe.db.commit()
		finally:
			frappe.destroy()
		if ret:
			print json.dumps(ret)
def list_apps(context):
	"Reinstall site ie. wipe all data and start over"
	site = get_single_site(context)
	frappe.init(site=site)
	frappe.connect()
	print "\n".join(frappe.get_installed_apps())
	frappe.destroy()
Example #14
0
def execute(method):
	frappe.connect()
	ret = frappe.get_attr(method)()
	frappe.db.commit()
	frappe.destroy()
	if ret:
		print ret
Example #15
0
def disable_scheduler():
	import frappe.utils.scheduler
	frappe.connect()
	frappe.utils.scheduler.disable_scheduler()
	frappe.db.commit()
	print "Disabled"
	frappe.destroy()
Example #16
0
def build_website(verbose=False):
	from frappe.website import render, statics
	frappe.connect()
	render.clear_cache()
	statics.sync(verbose=verbose).start(True)
	frappe.db.commit()
	frappe.destroy()
Example #17
0
def reset_perms():
	frappe.connect()
	for d in frappe.db.sql_list("""select name from `tabDocType`
		where ifnull(istable, 0)=0 and ifnull(custom, 0)=0"""):
			frappe.clear_cache(doctype=d)
			frappe.reset_perms(d)
	frappe.destroy()
Example #18
0
def latest(rebuild_website=True, quiet=False):
	import frappe.modules.patch_handler
	import frappe.model.sync
	from frappe.utils.fixtures import sync_fixtures
	import frappe.translate
	from frappe.core.doctype.notification_count.notification_count import clear_notifications

	verbose = not quiet

	frappe.connect()

	try:
		# run patches
		frappe.modules.patch_handler.run_all()
		# sync
		frappe.model.sync.sync_all(verbose=verbose)
		frappe.translate.clear_cache()
		sync_fixtures()

		clear_notifications()

		if rebuild_website:
			build_website()
	finally:
		frappe.destroy()
Example #19
0
def clear_cache():
	import frappe.sessions
	from frappe.core.doctype.notification_count.notification_count import clear_notifications
	frappe.connect()
	frappe.clear_cache()
	clear_notifications()
	frappe.destroy()
Example #20
0
def import_csv(context, path, only_insert=False, submit_after_import=False, ignore_encoding_errors=False, no_email=True):
	"Import CSV using data import tool"
	from frappe.core.page.data_import_tool import importer
	from frappe.utils.csvutils import read_csv_content
	site = get_site(context)

	if not os.path.exists(path):
		path = os.path.join('..', path)
	if not os.path.exists(path):
		print('Invalid path {0}'.format(path))
		sys.exit(1)

	with open(path, 'r') as csvfile:
		content = read_csv_content(csvfile.read())

	frappe.init(site=site)
	frappe.connect()

	try:
		importer.upload(content, submit_after_import=submit_after_import, no_email=no_email,
			ignore_encoding_errors=ignore_encoding_errors, overwrite=not only_insert,
			via_console=True)
		frappe.db.commit()
	except Exception:
		print(frappe.get_traceback())

	frappe.destroy()
Example #21
0
def request(context, args=None, path=None):
	"Run a request as an admin"
	import frappe.handler
	import frappe.api
	for site in context.sites:
		try:
			frappe.init(site=site)
			frappe.connect()
			if args:
				if "?" in args:
					frappe.local.form_dict = frappe._dict([a.split("=") for a in args.split("?")[-1].split("&")])
				else:
					frappe.local.form_dict = frappe._dict()

				if args.startswith("/api/method"):
					frappe.local.form_dict.cmd = args.split("?")[0].split("/")[-1]
			elif path:
				with open(os.path.join('..', path), 'r') as f:
					args = json.loads(f.read())

				frappe.local.form_dict = frappe._dict(args)

			frappe.handler.execute_cmd(frappe.form_dict.cmd)

			print(frappe.response)
		finally:
			frappe.destroy()
Example #22
0
def install_db(root_login="******", root_password=None, db_name=None, source_sql=None,
	admin_password = '******', verbose=True, force=0, site_config=None, reinstall=False):
	frappe.flags.in_install_db = True
	db_name=db_name[:16]
	if "." in db_name: 
		dn=db_name.split('.')
		db_name=dn[0]
	frappe.errprint(db_name)
	make_conf(db_name, site_config=site_config)
	if reinstall:
		frappe.connect(db_name=db_name)
		dbman = DbManager(frappe.local.db)
		dbman.create_database(db_name)

	else:
		frappe.local.db = make_connection(root_login, root_password)
		frappe.local.session = frappe._dict({'user':'******'})
		create_database_and_user(force, verbose)

	frappe.conf.admin_password = admin_password

	frappe.connect(db_name=db_name)
	import_db_from_sql(source_sql, verbose)

	create_auth_table()
	frappe.flags.in_install_db = False
Example #23
0
def backup_to_dropbox():
	from dropbox import client, session
	from frappe.utils.backups import new_backup
	from frappe.utils import get_files_path, get_backups_path
	if not frappe.db:
		frappe.connect()

	sess = session.DropboxSession(frappe.conf.dropbox_access_key, frappe.conf.dropbox_secret_key, "app_folder")

	sess.set_token(frappe.db.get_value("Dropbox Backup", None, "dropbox_access_key"),
		frappe.db.get_value("Dropbox Backup", None, "dropbox_access_secret"))

	dropbox_client = client.DropboxClient(sess)

	# upload database
	backup = new_backup(ignore_files=True)
	filename = os.path.join(get_backups_path(), os.path.basename(backup.backup_path_db))
	upload_file_to_dropbox(filename, "/database", dropbox_client)

	frappe.db.close()

	# upload files to files folder
	did_not_upload = []
	error_log = []

	upload_from_folder(get_files_path(), "/files", dropbox_client, did_not_upload, error_log)
	upload_from_folder(get_files_path(is_private=1), "/private/files", dropbox_client, did_not_upload, error_log)

	frappe.connect()
	return did_not_upload, list(set(error_log))
Example #24
0
def import_csv(context, path, only_insert=False, submit_after_import=False, ignore_encoding_errors=False):
    "Import CSV using data import tool"
    from frappe.core.page.data_import_tool import importer
    from frappe.utils.csvutils import read_csv_content

    site = get_site(context)

    with open(path, "r") as csvfile:
        content = read_csv_content(csvfile.read())

    frappe.init(site=site)
    frappe.connect()

    try:
        importer.upload(
            content,
            submit_after_import=submit_after_import,
            ignore_encoding_errors=ignore_encoding_errors,
            overwrite=not only_insert,
            via_console=True,
        )
        frappe.db.commit()
    except Exception:
        print frappe.get_traceback()

    frappe.destroy()
Example #25
0
def send_newsletter(site, newsletter, event):
	# hack! pass event="bulk_long" to queue in longjob queue
	try:
		frappe.connect(site=site)
		doc = frappe.get_doc("Newsletter", newsletter)
		doc.send_bulk()

	except:
		frappe.db.rollback()

		task_logger.error(site)
		task_logger.error(frappe.get_traceback())

		# wasn't able to send emails :(
		doc.db_set("email_sent", 0)
		frappe.db.commit()

		log("send_newsletter")

		raise

	else:
		frappe.db.commit()

	finally:
		frappe.destroy()
Example #26
0
def patch(patch_module, force=False):
	import frappe.modules.patch_handler
	frappe.connect()
	frappe.local.patch_log_list = []
	frappe.modules.patch_handler.run_single(patch_module, force=force)
	print "\n".join(frappe.local.patch_log_list)
	frappe.destroy()
Example #27
0
def set_admin_password(admin_password):
	import frappe
	frappe.connect()
	frappe.db.sql("""update __Auth set `password`=password(%s)
		where user='******'""", (admin_password,))
	frappe.db.commit()
	frappe.destroy()
Example #28
0
def scheduler(context, state, site=None):
    from frappe.installer import update_site_config
    import frappe.utils.scheduler

    if not site:
        site = get_site(context)

    try:
        frappe.init(site=site)

        if state == "pause":
            update_site_config("pause_scheduler", 1)
        elif state == "resume":
            update_site_config("pause_scheduler", 0)
        elif state == "disable":
            frappe.connect()
            frappe.utils.scheduler.disable_scheduler()
            frappe.db.commit()
        elif state == "enable":
            frappe.connect()
            frappe.utils.scheduler.enable_scheduler()
            frappe.db.commit()

        print "Scheduler {0}d for site {1}".format(state, site)

    finally:
        frappe.destroy()
Example #29
0
def send_email(success, service_name, error_status=None):
	if success:
		if frappe.db.get_value("Dropbox Settings", None, "send_email_for_successful_backup") == '0':
			return

		subject = "Backup Upload Successful"
		message ="""<h3>Backup Uploaded Successfully</h3><p>Hi there, this is just to inform you
		that your backup was successfully uploaded to your %s account. So relax!</p>
		""" % service_name

	else:
		subject = "[Warning] Backup Upload Failed"
		message ="""<h3>Backup Upload Failed</h3><p>Oops, your automated backup to %s
		failed.</p>
		<p>Error message: <br>
		<pre><code>%s</code></pre>
		</p>
		<p>Please contact your system manager for more information.</p>
		""" % (service_name, error_status)

	if not frappe.db:
		frappe.connect()

	recipients = split_emails(frappe.db.get_value("Dropbox Settings", None, "send_notifications_to"))
	frappe.sendmail(recipients=recipients, subject=subject, message=message)
Example #30
0
def main(app=None, module=None, doctype=None, verbose=False, tests=(), force=False):
	frappe.flags.print_messages = verbose
	frappe.flags.in_test = True

	if not frappe.db:
		frappe.connect()

	# if not frappe.conf.get("db_name").startswith("test_"):
	# 	raise Exception, 'db_name must start with "test_"'

	# workaround! since there is no separate test db
	frappe.clear_cache()
	frappe.utils.scheduler.disable_scheduler()
	set_test_email_config()

	if verbose:
		print 'Running "before_tests" hooks'
	for fn in frappe.get_hooks("before_tests", app_name=app):
		frappe.get_attr(fn)()

	if doctype:
		ret = run_tests_for_doctype(doctype, verbose=verbose, tests=tests, force=force)
	elif module:
		ret = run_tests_for_module(module, verbose=verbose, tests=tests)
	else:
		ret = run_all_tests(app, verbose)

	frappe.db.commit()

	# workaround! since there is no separate test db
	frappe.clear_cache()

	return ret
Example #31
0
def build_sitemap():
	from frappe.website import rebuild_config
	frappe.connect()
	rebuild_config()
	frappe.destroy()
Example #32
0
def export_fixtures():
	from frappe.utils.fixtures import export_fixtures
	frappe.connect()
	export_fixtures()
	frappe.destroy()
Example #33
0
def export_csv(doctype, path):
	from frappe.core.page.data_import_tool import data_import_tool
	frappe.connect()
	data_import_tool.export_csv(doctype, path)
	frappe.destroy()
Example #34
0
def export_doclist(doctype, name, path):
	from frappe.core.page.data_import_tool import data_import_tool
	frappe.connect()
	data_import_tool.export_json(doctype, name, path)
	frappe.destroy()
Example #35
0
def export_doc(doctype, docname):
	import frappe.modules
	frappe.connect()
	frappe.modules.export_doc(doctype, docname)
	frappe.destroy()
Example #36
0
def run_scheduler_event(event, force=False):
	import frappe.utils.scheduler
	frappe.connect()
	frappe.utils.scheduler.trigger(frappe.local.site, event, now=force)
	frappe.destroy()
Example #37
0
def sync_statics():
	from frappe.website import statics
	frappe.connect()
	statics.sync_statics()
	frappe.db.commit()
	frappe.destroy()
Example #38
0
def clear_web():
	import frappe.website.render
	frappe.connect()
	frappe.website.render.clear_cache()
	frappe.destroy()
Example #39
0
def make_custom_server_script(doctype):
	from frappe.core.doctype.custom_script.custom_script import make_custom_server_script_file
	frappe.connect()
	make_custom_server_script_file(doctype)
	frappe.destroy()
Example #40
0
def clear_all_sessions():
	import frappe.sessions
	frappe.connect()
	frappe.sessions.clear_all_sessions()
	frappe.db.commit()
	frappe.destroy()
Example #41
0
def patch(patch_module, force=False):
	import frappe.modules.patch_handler
	frappe.connect()
	frappe.modules.patch_handler.run_single(patch_module, force=force)
	frappe.destroy()
Example #42
0
def clear_cache():
	import frappe.sessions
	frappe.connect()
	frappe.clear_cache()
	frappe.destroy()
Example #43
0
def add_system_manager(email, first_name=None, last_name=None):
	import frappe.utils.user
	frappe.connect()
	frappe.utils.user.add_system_manager(email, first_name, last_name)
	frappe.db.commit()
	frappe.destroy()
Example #44
0
def reload_doc(module, doctype, docname, force=False):
	frappe.connect()
	frappe.reload_doc(module, doctype, docname, force=force)
	frappe.db.commit()
	frappe.destroy()
                          send,
                          recipients=['*****@*****.**'] * 1000,
                          sender="*****@*****.**",
                          reference_doctype="User",
                          reference_name="Administrator",
                          subject='Testing Email Queue',
                          message='This email is queued!')

    def test_image_parsing(self):
        import re
        email_account = frappe.get_doc('Email Account',
                                       '_Test Email Account 1')

        with open(
                frappe.get_app_path('frappe', 'tests', 'data',
                                    'email_with_image.txt'), 'r') as raw:
            communication = email_account.insert_communication(raw.read())

        #print communication.content
        self.assertTrue(
            re.search('''<img[^>]*src=["']/private/files/rtco1.png[^>]*>''',
                      communication.content))
        self.assertTrue(
            re.search('''<img[^>]*src=["']/private/files/rtco2.png[^>]*>''',
                      communication.content))


if __name__ == '__main__':
    frappe.connect()
    unittest.main()
Example #46
0
def sync_all(force=False, quiet=False):
	import frappe.model.sync
	verbose = not quiet
	frappe.connect()
	frappe.model.sync.sync_all(force=force, verbose=verbose)
	frappe.destroy()
Example #47
0
def execute_job(site,
                method,
                event,
                job_name,
                kwargs,
                user=None,
                is_async=True,
                retry=0):
    """Executes job in a worker, performs commit/rollback and logs if there is any error"""
    if is_async:
        frappe.connect(site)
        if os.environ.get("CI"):
            frappe.flags.in_test = True

        if user:
            frappe.set_user(user)

    if isinstance(method, string_types):
        method_name = method
        method = frappe.get_attr(method)
    else:
        method_name = cstr(method.__name__)

    frappe.monitor.start("job", method_name, kwargs)
    try:
        method(**kwargs)

    except (frappe.db.InternalError, frappe.RetryBackgroundJobError) as e:
        frappe.db.rollback()

        if retry < 5 and (isinstance(e, frappe.RetryBackgroundJobError) or
                          (frappe.db.is_deadlocked(e)
                           or frappe.db.is_timedout(e))):
            # retry the job if
            # 1213 = deadlock
            # 1205 = lock wait timeout
            # or RetryBackgroundJobError is explicitly raised
            frappe.destroy()
            time.sleep(retry + 1)

            return execute_job(site,
                               method,
                               event,
                               job_name,
                               kwargs,
                               is_async=is_async,
                               retry=retry + 1)

        else:
            frappe.log_error(title=method_name)
            raise

    except:
        frappe.db.rollback()
        frappe.log_error(title=method_name)
        frappe.db.commit()
        print(frappe.get_traceback())
        raise

    else:
        frappe.db.commit()

    finally:
        frappe.monitor.stop()
        if is_async:
            frappe.destroy()
Example #48
0
def install_app(app_name, quiet=False):
	verbose = not quiet
	from frappe.installer import install_app
	frappe.connect()
	install_app(app_name, verbose=verbose)
	frappe.destroy()
 def __enter__(self):
     frappe.init(site=self.site)
     frappe.connect()
     print frappe.local.site
     frappe.set_user(self.user)
Example #50
0
def trim_database(context, dry_run, format, no_backup):
    if not context.sites:
        raise SiteNotSpecifiedError

    from frappe.utils.backups import scheduled_backup

    ALL_DATA = {}

    for site in context.sites:
        frappe.init(site=site)
        frappe.connect()

        TABLES_TO_DROP = []
        STANDARD_TABLES = get_standard_tables()
        information_schema = frappe.qb.Schema("information_schema")
        table_name = frappe.qb.Field("table_name").as_("name")

        queried_result = frappe.qb.from_(
            information_schema.tables).select(table_name).where(
                information_schema.tables.table_schema ==
                frappe.conf.db_name).run()

        database_tables = [x[0] for x in queried_result]
        doctype_tables = frappe.get_all("DocType", pluck="name")

        for x in database_tables:
            doctype = x.lstrip("tab")
            if not (doctype in doctype_tables or x.startswith("__")
                    or x in STANDARD_TABLES):
                TABLES_TO_DROP.append(x)

        if not TABLES_TO_DROP:
            if format == "text":
                click.secho(
                    f"No ghost tables found in {frappe.local.site}...Great!",
                    fg="green")
        else:
            if not (no_backup or dry_run):
                if format == "text":
                    print(f"Backing Up Tables: {', '.join(TABLES_TO_DROP)}")

                odb = scheduled_backup(
                    ignore_conf=False,
                    include_doctypes=",".join(
                        x.lstrip("tab") for x in TABLES_TO_DROP),
                    ignore_files=True,
                    force=True,
                )
                if format == "text":
                    odb.print_summary()
                    print("\nTrimming Database")

            for table in TABLES_TO_DROP:
                if format == "text":
                    print(f"* Dropping Table '{table}'...")
                if not dry_run:
                    frappe.db.sql_ddl(f"drop table `{table}`")

            ALL_DATA[frappe.local.site] = TABLES_TO_DROP
        frappe.destroy()

    if format == "json":
        import json
        print(json.dumps(ALL_DATA, indent=1))
Example #51
0
                          })


def execute_job(site,
                method,
                event,
                job_name,
                kwargs,
                user=None,
                async=True,
                retry=0):
    '''Executes job in a worker, performs commit/rollback and logs if there is any error'''
    from frappe.utils.scheduler import log

    if async:
        frappe.connect(site)
        if user:
            frappe.set_user(user)

    if isinstance(method, basestring):
        method_name = method
        method = frappe.get_attr(method)
    else:
        method_name = cstr(method.__name__)

    try:
        method(**kwargs)

    except (MySQLdb.OperationalError, frappe.RetryBackgroundJobError), e:
        frappe.db.rollback()
Example #52
0
 def _get():
     if not frappe.db:
         frappe.connect()
     return frappe.db.sql_list('select name from tabLanguage')
Example #53
0
def ipython(site):
	import frappe
	frappe.connect(site=site)
	import IPython
	IPython.embed()
Example #54
0
def main(app=None,
         module=None,
         doctype=None,
         verbose=False,
         tests=(),
         force=False,
         profile=False,
         junit_xml_output=None,
         ui_tests=False,
         doctype_list_path=None,
         skip_test_records=False,
         failfast=False):
    global unittest_runner

    if doctype_list_path:
        app, doctype_list_path = doctype_list_path.split(os.path.sep, 1)
        with open(frappe.get_app_path(app, doctype_list_path), 'r') as f:
            doctype = f.read().strip().splitlines()

    xmloutput_fh = None
    if junit_xml_output:
        xmloutput_fh = open(junit_xml_output, 'wb')
        unittest_runner = xmlrunner_wrapper(xmloutput_fh)
    else:
        unittest_runner = unittest.TextTestRunner

    try:
        frappe.flags.print_messages = verbose
        frappe.flags.in_test = True

        if not frappe.db:
            frappe.connect()

        # if not frappe.conf.get("db_name").startswith("test_"):
        # 	raise Exception, 'db_name must start with "test_"'

        # workaround! since there is no separate test db
        frappe.clear_cache()
        frappe.utils.scheduler.disable_scheduler()
        set_test_email_config()

        if not frappe.flags.skip_before_tests:
            if verbose:
                print('Running "before_tests" hooks')
            for fn in frappe.get_hooks("before_tests", app_name=app):
                frappe.get_attr(fn)()

        if doctype:
            ret = run_tests_for_doctype(doctype, verbose, tests, force,
                                        profile)
        elif module:
            ret = run_tests_for_module(module, verbose, tests, profile)
        else:
            ret = run_all_tests(app,
                                verbose,
                                profile,
                                ui_tests,
                                failfast=failfast)

        frappe.db.commit()

        # workaround! since there is no separate test db
        frappe.clear_cache()
        return ret

    finally:
        if xmloutput_fh:
            xmloutput_fh.flush()
            xmloutput_fh.close()
Example #55
0
def get_untranslated(lang, untranslated_file, all=None):
	import frappe.translate
	frappe.connect()
	frappe.translate.get_untranslated(lang, untranslated_file, get_all=all)
	frappe.destroy()
Example #56
0
def validate(site):
    frappe.init(site=site, sites_path=sites_path)
    frappe.connect()
    if not executed(last_3_patch):
        raise Exception, "site not ready to migrate to version 4"
    frappe.destroy()
Example #57
0
def import_doc(path, force=False):
	from frappe.core.page.data_import_tool import data_import_tool
	frappe.connect()
	data_import_tool.import_doc(path, overwrite=force)
	frappe.destroy()
Example #58
0
def update_translations(lang, untranslated_file, translated_file):
	import frappe.translate
	frappe.connect()
	frappe.translate.update_translations(lang, untranslated_file, translated_file)
	frappe.destroy()
Example #59
0
def sync_statics(force=False):
	from frappe.website import statics
	frappe.connect()
	statics.sync_statics(rebuild = force)
	frappe.db.commit()
	frappe.destroy()
Example #60
0
def build_message_files():
	import frappe.translate
	frappe.connect()
	frappe.translate.rebuild_all_translation_files()
	frappe.destroy()