def extract_files(site_name, file_path): import shutil import subprocess from frappe.utils import get_bench_relative_path file_path = get_bench_relative_path(file_path) # Need to do frappe.init to maintain the site locals frappe.init(site=site_name) abs_site_path = os.path.abspath(frappe.get_site_path()) # Copy the files to the parent directory and extract shutil.copy2(os.path.abspath(file_path), abs_site_path) # Get the file name splitting the file path on tar_name = os.path.split(file_path)[1] tar_path = os.path.join(abs_site_path, tar_name) try: if file_path.endswith(".tar"): subprocess.check_output(["tar", "xvf", tar_path, "--strip", "2"], cwd=abs_site_path) elif file_path.endswith(".tgz"): subprocess.check_output(["tar", "zxvf", tar_path, "--strip", "2"], cwd=abs_site_path) except: raise finally: frappe.destroy() return tar_path
def test_get_bench_relative_path(self): bench_path = frappe.utils.get_bench_path() test1_path = os.path.join(bench_path, "test1.txt") test2_path = os.path.join(bench_path, "sites", "test2.txt") with open(test1_path, "w+") as test1: test1.write("asdf") with open(test2_path, "w+") as test2: test2.write("asdf") self.assertTrue("test1.txt" in get_bench_relative_path("test1.txt")) self.assertTrue("sites/test2.txt" in get_bench_relative_path("test2.txt")) with self.assertRaises(SystemExit): get_bench_relative_path("test3.txt") os.remove(test1_path) os.remove(test2_path)
def extract_sql_from_archive(sql_file_path): """Return the path of an SQL file if the passed argument is the path of a gzipped SQL file or an SQL file path. The path may be absolute or relative from the bench root directory or the sites sub-directory. Args: sql_file_path (str): Path of the SQL file Returns: str: Path of the decompressed SQL file """ from frappe.utils import get_bench_relative_path sql_file_path = get_bench_relative_path(sql_file_path) # Extract the gzip file if user has passed *.sql.gz file instead of *.sql file if sql_file_path.endswith('sql.gz'): decompressed_file_name = extract_sql_gzip(sql_file_path) else: decompressed_file_name = sql_file_path return decompressed_file_name