Exemplo n.º 1
0
def remove_init_files():
	import os
	from webnotes.utils import get_site_path, cstr
	for path, folders, files in os.walk(get_site_path(webnotes.conf.get("plugins_path"))):
		for f in files:
			# cstr(f) is required when filenames are non-ascii
			if cstr(f) in ("__init__.py", "__init__.pyc"):
				os.remove(os.path.join(path, f))
Exemplo n.º 2
0
def get_file(fname):
	f = webnotes.conn.sql("""select file_name from `tabFile Data` 
		where name=%s or file_name=%s""", (fname, fname))
	if f:
		file_name = f[0][0]
	else:
		file_name = fname
		
	if not "/" in file_name:
		file_name = "files/" + file_name
	
	# read the file	
	with open(get_site_path("public", file_name), 'r') as f:
		content = f.read()

	return [file_name, content]
Exemplo n.º 3
0
def get_file(fname):
	f = webnotes.conn.sql("""select file_name from `tabFile Data` 
		where name=%s or file_name=%s""", (fname, fname))
	if f:
		file_name = f[0][0]
	else:
		file_name = fname
		
	if not "/" in file_name:
		file_name = "files/" + file_name
	
	# read the file	
	with open(get_site_path("public", file_name), 'r') as f:
		content = f.read()

	return [file_name, content]
Exemplo n.º 4
0
def clear_cache(doctype=None, docname=None):
	import os
	from webnotes.utils import get_site_path
	
	def clear_single(dt, dn):
		webnotes.cache().delete_value(get_cache_key(dt, dn, "py"))
		webnotes.cache().delete_value(get_cache_key(dt, dn, "js"))
		
	if not (doctype and docname):
		for path, folders, files in os.walk(get_site_path(webnotes.conf.get("plugins_path"))):
			if files:
				dt = os.path.basename(os.path.dirname(path))
				dn = os.path.basename(path)
				clear_single(dt, dn)
	else:
		clear_single(doctype, docname)
		
Exemplo n.º 5
0
def save_file(fname, content, dt, dn, decode=False):
    if decode:
        if isinstance(content, unicode):
            content = content.encode("utf-8")
        content = base64.b64decode(content)

    fname = slugify(unicode(
        os.path.splitext(fname)[0])) + os.path.splitext(fname)[1]

    import filecmp
    from webnotes.model.code import load_doctype_module
    files_path = get_site_path(conf.files_path)
    module = load_doctype_module(
        dt, webnotes.conn.get_value("DocType", dt, "module"))

    if hasattr(module, "attachments_folder"):
        files_path = os.path.join(files_path, module.attachments_folder)

    file_size = check_max_file_size(content)
    temp_fname = write_file(content, files_path)
    fname = scrub_file_name(fname)

    fname_parts = fname.split(".", -1)
    main = ".".join(fname_parts[:-1])
    extn = fname_parts[-1]
    versions = get_file_versions(files_path, main, extn)

    if versions:
        found_match = False
        for version in versions:
            if filecmp.cmp(os.path.join(files_path, version), temp_fname):
                # remove new file, already exists!
                os.remove(temp_fname)
                fname = version
                fpath = os.path.join(files_path, fname)
                found_match = True
                break

        if not found_match:
            # get_new_version name
            fname = get_new_fname_based_on_version(files_path, main, extn,
                                                   versions)
            fpath = os.path.join(files_path, fname)

            # rename
            if os.path.exists(fpath.encode("utf-8")):
                webnotes.throw("File already exists: " + fname)

            os.rename(temp_fname, fpath.encode("utf-8"))
    else:
        fpath = os.path.join(files_path, fname)

        # rename new file
        if os.path.exists(fpath.encode("utf-8")):
            webnotes.throw("File already exists: " + fname)

        os.rename(temp_fname, fpath.encode("utf-8"))

    f = webnotes.bean({
        "doctype":
        "File Data",
        "file_name":
        os.path.relpath(os.path.join(files_path, fname),
                        get_site_path(conf.get("public_path", "public"))),
        "attached_to_doctype":
        dt,
        "attached_to_name":
        dn,
        "file_size":
        file_size
    })
    f.ignore_permissions = True
    try:
        f.insert()
    except webnotes.DuplicateEntryError:
        return {"file_name": f.doc.file_name}

    return f.doc
Exemplo n.º 6
0
def get_lock_path(name):
    name = name.lower()
    lock_path = get_site_path(name + '.lock')
    return lock_path
Exemplo n.º 7
0
def get_lock_path(name):
	name = name.lower()
	lock_path = get_site_path(name + '.lock')
	return lock_path
Exemplo n.º 8
0
def save_file(fname, content, dt, dn, decode=False):
	if decode:
		if isinstance(content, unicode):
			content = content.encode("utf-8")
		content = base64.b64decode(content)
	
	import filecmp
	from webnotes.model.code import load_doctype_module
	files_path = get_site_path(conf.files_path)
	module = load_doctype_module(dt, webnotes.conn.get_value("DocType", dt, "module"))
	
	if hasattr(module, "attachments_folder"):
		files_path = os.path.join(files_path, module.attachments_folder)

	file_size = check_max_file_size(content)
	temp_fname = write_file(content, files_path)
	fname = scrub_file_name(fname)

	fname_parts = fname.split(".", -1)
	main = ".".join(fname_parts[:-1])
	extn = fname_parts[-1]
	versions = get_file_versions(files_path, main, extn)
	
	if versions:
		found_match = False
		for version in versions:
			if filecmp.cmp(os.path.join(files_path, version), temp_fname):
				# remove new file, already exists!
				os.remove(temp_fname)
				fname = version
				fpath = os.path.join(files_path, fname)
				found_match = True
				break
				
		if not found_match:
			# get_new_version name
			fname = get_new_fname_based_on_version(files_path, main, extn, versions)
			fpath = os.path.join(files_path, fname)
			
			# rename
			if os.path.exists(fpath.encode("utf-8")):
				webnotes.throw("File already exists: " + fname)
				
			os.rename(temp_fname, fpath.encode("utf-8"))
	else:
		fpath = os.path.join(files_path, fname)
		
		# rename new file
		if os.path.exists(fpath.encode("utf-8")):
			webnotes.throw("File already exists: " + fname)
		
		os.rename(temp_fname, fpath.encode("utf-8"))

	f = webnotes.bean({
		"doctype": "File Data",
		"file_name": os.path.relpath(os.path.join(files_path, fname), 
			get_site_path(conf.get("public_path", "public"))),
		"attached_to_doctype": dt,
		"attached_to_name": dn,
		"file_size": file_size
	})
	f.ignore_permissions = True
	try:
		f.insert();
	except webnotes.DuplicateEntryError:
		return {"file_name": f.doc.file_name}

	return f.doc
Exemplo n.º 9
0
def get_plugin_path(plugin):
    from webnotes.utils import get_site_path
    return get_site_path(webnotes.conf.get("plugins_path"), scrub(plugin))
Exemplo n.º 10
0
def get_plugin_path(plugin):
	from webnotes.utils import get_site_path
	return get_site_path(webnotes.conf.get("plugins_path"), scrub(plugin))
Exemplo n.º 11
0
def get_plugin_path(plugin=None):
	from webnotes.modules import scrub
	from webnotes.utils import get_site_path
	if not plugin: plugin = get_plugin_name(None, None)
	
	return get_site_path(webnotes.conf.get("plugins_path"), scrub(plugin))