Exemple #1
0
def archive_add_dir(archive_path,
                    dir_name,
                    base_dir,
                    remove_src_dir=False,
                    zf=None):

    close_file = False

    if zf == None:
        close_file = True
        zf = zipfile.ZipFile(archive_path, 'a', zipfile.ZIP_DEFLATED)

    for root, dirs, files in os.walk(dir_name):
        for name in files:
            file_name = os.path.join(root, name)

            if os.path.isfile(file_name):
                f = open(file_name, mode='rb')
                lines = f.read()
                f.close()

                name_of_file_in_archive = subtract_paths(base_dir, file_name)
                zf.writestr(name_of_file_in_archive, lines)

    if close_file == True:
        zf.close()

    if remove_src_dir == True:
        if base_dir == "" or base_dir == dir_name or dir_name == "/" or dir_name == "/home/rod" or dir_name == "/home/rod/" or dir_name == "c:\\":  #paranoia
            return

        shutil.rmtree(dir_name)
Exemple #2
0
def cp_spectra(dest,src):
	src=os.path.join(src,"spectra")
	dest=os.path.join(dest,"spectra")

	for dirpath, dirnames, filenames in os.walk(src):
		for filename in [f for f in filenames if f=="mat.inp"]:
			mat_f_name=os.path.join(dirpath, filename)
			status=inp_get_token_value(mat_f_name, "#status")

			if status=="public_all" or status=="public":
				print("copy spectra",mat_f_name,status)
				src_mat_path=os.path.dirname(mat_f_name)

				delta_path=subtract_paths(src,src_mat_path)
				dst_mat_path=os.path.join(dest,delta_path)

				if not os.path.exists(dst_mat_path):
					os.makedirs(dst_mat_path)
				
				safe_cpy(dst_mat_path,src_mat_path,"spectra_gen.inp")
				
				safe_cpy(dst_mat_path,src_mat_path,"spectra.inp")
				
				safe_cpy(dst_mat_path,src_mat_path,"mat.inp")

				safe_cpy(dst_mat_path,src_mat_path,"spectra_eq.inp")
Exemple #3
0
def archive_zip_dir(path, extentions=[]):
    print("zipping: ", path)
    all_files = []
    for root, dirs, files in os.walk(path):
        for name in files:
            file_name = os.path.join(root, name)
            if len(extentions) == 0:
                all_files.append(file_name)
            else:
                ext = os.path.splitext(file_name)
                if len(ext) > 1:
                    if ext[1] in extentions:
                        all_files.append(file_name)

    archive_path = path + ".zip"
    if os.path.isfile(archive_path) == True:
        os.remove(archive_path)

    zf = zipfile.ZipFile(archive_path, 'a', zipfile.ZIP_DEFLATED)
    for file_name in all_files:
        f = open(file_name, mode='rb')
        lines = f.read()
        f.close()

        name_of_file_in_archive = subtract_paths(path, file_name)

        zf.writestr(name_of_file_in_archive, lines)

    zf.close()
    def find_files(self, path):
        self.sims = []
        self.path = os.path.abspath(path)
        sims = scan_list_simulations(path)
        #print(path,sims)
        for sim_path in sims:
            sim_data = sim_dir()
            sim_data.path = sim_path
            #print(sim_data.path)
            for root, dirs, files in os.walk(sim_data.path):
                for name in files:
                    full_name = os.path.join(root, name)
                    rel_path = subtract_paths(sim_data.path, full_name)
                    if full_name.endswith(".dat"):

                        add = False
                        if name == rel_path:  #find files which are only on the first level directory
                            add = True
                        if rel_path.startswith(
                                "dynamic"):  #Is the dynamic directory
                            add = True

                        if add == True:
                            text = peek_data(full_name)
                            if text.startswith(b"#gpvdm"):
                                sim_data.files.append(rel_path)

            self.sims.append(sim_data)
    def gen_gnu_plot_files(self):
        path = self.path
        if len(self.sims) > 0:
            for cur_file in self.sims[0].files:
                dat = dat_file()
                dat.load(os.path.join(self.sims[0].path, cur_file))
                found_files = []
                found_files.extend(dat_file_to_gnuplot_header(dat))
                found_files.append("plot \\")
                for s in self.sims:
                    if cur_file in s.files:
                        full_path_name = os.path.join(s.path, cur_file)
                        title = os.path.dirname(
                            subtract_paths(self.path, full_path_name))
                        found_files.append(
                            "\'" + full_path_name +
                            "' using ($1):($2) with l lw 3 title '" + title +
                            "',\\")

                if self.exp_data == "":
                    found_files[-1] = found_files[-1][:-1]
                else:
                    found_files.append("\'" + self.exp_data +
                                       "' using ($1):($2) with p title '" +
                                       "Experimental" + "'")

                out_file = os.path.join(path, cur_file)
                out_file = os.path.splitext(out_file)[0] + ".plot"

                self.make_dirs(out_file)
                f = inp()
                f.lines = found_files
                #print("saving as:",out_file)
                f.save_as(out_file)
 def ls_dir(self, path):
     for h in self.list:
         print(
             subtract_paths(
                 os.path.normpath(path),
                 os.path.normpath(os.path.dirname("/" + h.human_label))),
             h.human_label)
         if os.path.normpath(path) == os.path.normpath(
                 os.path.dirname(h.human_label)):
             print(h.human_label)
	def callback_button_click(self):
		from gpvdm_open import gpvdm_open
		dialog=gpvdm_open(get_materials_path(),act_as_browser=False)

		ret=dialog.exec_()
		if ret==QDialog.Accepted:
			file_name=dialog.get_filename()
			rel_path=subtract_paths(get_materials_path(),file_name)
			rel_path=rel_path.replace("\\", "/")
			self.setText(rel_path)
			self.changed.emit()
Exemple #8
0
def tree_gen_flat_list(dir_to_search,level=0):
	found_dirs=[]
	for root, dirs, files in os.walk(dir_to_search):
		for name in files:

			if name=="sim.gpvdm":
				full_name=os.path.join(root, name)
				f=subtract_paths(dir_to_search,full_name)
				f=os.path.dirname(f)
				if len(f.split("/"))>level:
					found_dirs.append(f)
	return found_dirs
    def plot(self, file_name):
        f = inp()
        f.load(file_name)
        files = f.lines[1:]

        window = plot_window()
        labels = []
        for f in files:
            rel_path = subtract_paths(get_sim_path(), f)
            rel_path = rel_path.replace("\\", "/")
            labels.append(rel_path)
        window.init(files, labels, "")
Exemple #10
0
def tree_save_flat_list(sim_dir,flat_list):
	config=[]
	file_name=os.path.join(sim_dir,'flat_list.inp')

	a = open(file_name, "w")
	a.write(str(len(flat_list))+"\n")
	for i in range(0,len(flat_list)):
		rel_dir=subtract_paths(sim_dir,flat_list[i])
		a.write(rel_dir+"\n")

	a.close()

	return config
Exemple #11
0
def extract_dir_from_archive(dest, zip_file_path, dir_name, zf=None):
    items = zf.namelist()
    for i in range(0, len(items)):
        if items[i].startswith(dir_name) == True:
            read_lines = zf.read(items[i])
            output_file = os.path.join(dest,
                                       subtract_paths(dir_name, items[i]))
            output_dir = os.path.dirname(output_file)

            if os.path.isdir(output_dir) == False:
                os.makedirs(output_dir)

            f = open(output_file, mode='wb')
            lines = f.write(read_lines)
            f.close()
Exemple #12
0
def cp_materials(dest,src):
	
	dest=os.path.join(dest,"materials")
	src=os.path.join(src,"materials")

	print(dest,src)
	for dirpath, dirnames, filenames in os.walk(src):
		for filename in [f for f in filenames if f=="mat.inp"]:
			mat_f_name=os.path.join(dirpath, filename)
			status=inp_get_token_value(mat_f_name, "#status")

			if status=="public_all" or status=="public":
				print("copy materials",mat_f_name,status)
				src_mat_path=os.path.dirname(mat_f_name)

				delta_path=subtract_paths(src,src_mat_path)
				dst_mat_path=os.path.join(dest,delta_path)
				if not os.path.exists(dst_mat_path):
					os.makedirs(dst_mat_path)
				
				safe_cpy(dst_mat_path,src_mat_path,"alpha_gen.omat")
				
				safe_cpy(dst_mat_path,src_mat_path,"n_gen.omat")
				
				safe_cpy(dst_mat_path,src_mat_path,"n_eq.inp")
				
				safe_cpy(dst_mat_path,src_mat_path,"alpha_eq.inp")
				
				safe_cpy(dst_mat_path,src_mat_path,"dos.inp")

				safe_cpy(dst_mat_path,src_mat_path,"pl.inp")

				safe_cpy(dst_mat_path,src_mat_path,"mat.inp")
				safe_cpy(dst_mat_path,src_mat_path,"fit.inp")

				safe_cpy(dst_mat_path,src_mat_path,"cost.xlsx")


				safe_cpy(dst_mat_path,src_mat_path,"alpha.omat")
				safe_cpy(dst_mat_path,src_mat_path,"n.omat")
				safe_cpy(dst_mat_path,src_mat_path,"alpha.ref")
				safe_cpy(dst_mat_path,src_mat_path,"n.ref")


				files=os.listdir(src_mat_path)
				for i in range(0,len(files)):
					if files[i].endswith(".ref")==True:
						safe_cpy(dst_mat_path,src_mat_path,files[i])
Exemple #13
0
def archive_add_file(archive_path, file_name, base_dir, dont_delete=False):
    lines = []
    name_of_file_in_archive = subtract_paths(base_dir, file_name)

    if dont_delete == False:
        zip_remove_file(archive_path, name_of_file_in_archive)

    if os.path.isfile(file_name):
        f = open(file_name, mode='rb')
        lines = f.read()
        f.close()
    else:
        return False

    zf = zipfile.ZipFile(archive_path, 'a', zipfile.ZIP_DEFLATED)

    zf.writestr(name_of_file_in_archive, lines)
    zf.close()
    return True
Exemple #14
0
def cp_devices(dest,src):
	src=os.path.join(src,"device_lib")
	dest=os.path.join(dest,"device_lib")

	if not os.path.exists(dest):
		os.makedirs(dest)

	for dirpath, dirnames, files in os.walk(src):
		for name in files:
			if name.endswith(".gpvdm")==True:
				src_file=os.path.join(dirpath, name)
				dst_file=os.path.join(dest,subtract_paths(src,src_file))
				dst_dir=os.path.dirname(dst_file)

				private=inp_get_token_value(os.path.join(os.path.dirname(src_file),"info.inp"), "#private",archive=os.path.basename(src_file))

				if private!=None:
					if str2bool(private)==False:
						if os.path.isdir(dst_dir)==False:
							os.makedirs(dst_dir)
						shutil.copyfile(src_file,dst_file)