Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import collections
- import os
- import plistlib
- import shutil
- import io
- from .efun_logger import ef_logger
- import datetime
- import platform
- def recursive_update_dict(des_dic, update_dic):
- for k, v in update_dic.items():
- if isinstance(v, collections.Mapping):
- r = recursive_update_dict(des_dic.get(k, {}), v)
- des_dic[k] = r
- else:
- des_dic[k] = update_dic[k]
- return des_dic
- def derect_update_dict(des_dic, update_dic, ignore_empty=True):
- for k, v in update_dic.items():
- if ignore_empty:
- if v:
- des_dic[k] = v
- else:
- des_dic[k] = v
- return des_dic
- def str2bool(v):
- try:
- return v.lower() in ("yes", "true", "t", "1", "on")
- except Exception as e:
- ef_logger.exception(e)
- raise e
- def datetime_str_iso8601_to_datetime(datetime_str):
- if datetime_str:
- try:
- resultV = datetime.datetime.strptime(datetime_str, '%Y-%m-%dT%H:%M:%S.%f')
- except ValueError:
- resultV = datetime.datetime.strptime(datetime_str, '%Y-%m-%dT%H:%M:%S')
- return resultV
- def open_plist_to_dic(file_path):
- dic = {}
- with io.open(file_path, 'rb') as f: #binary mode doesn't take an encoding argument
- dic = plistlib.load(f)
- return dic
- def save_dic_to_plist(dic, file_path):
- with io.open(file_path, 'wb') as f: #binary mode doesn't take an encoding argument
- plistlib.dump(dic, f)
- def copy_file_dir(src_path, dst_path):#复制文件夹
- try:
- if os.path.exists(src_path):
- if os.path.exists(dst_path) and os.path.isdir(dst_path):
- shutil.rmtree(dst_path)
- shutil.copytree(src_path, dst_path, ignore=shutil.ignore_patterns('*.pyc', '.svn', '.DS_Store'))
- ef_logger.info("copy dir success: %s" %src_path + "\n")
- else:
- ef_logger.info("scrDir is not exists: %s" %src_path + "\n")
- except Exception as err:
- ef_logger.exception(err)
- def copy_file(src_file, dst_file):#复制文件
- try:
- if os.path.exists(src_file) and os.path.isfile(src_file):
- if os.path.exists(dst_file) and os.path.isfile(dst_file):
- os.remove(dst_file)
- os.makedirs(os.path.dirname(dst_file), exist_ok=True)
- shutil.copyfile(src_file, dst_file)
- ef_logger.info("copy file success: %s" %src_file + "\n")
- else:
- ef_logger.info("srcFile is not exists: %s" %src_file + "\n")
- except Exception as err:
- ef_logger.exception(err)
- # 从源目录复制文件(包括源目录的目录)到目标目录
- def copy_file_in_dir(src_dir, dst_dir):
- if os.path.exists(src_dir) and os.path.isdir(src_dir):
- for parent, dirnames, filenames in os.walk(src_dir): #三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字
- # for dirname in dirnames: #输出文件夹信息
- # print("parent is:" + parent)
- # print("dirname is:" + dirname)
- for filename in filenames: #输出文件信息
- # print(os.path.relpath(parent, src_dir)) #获取相对路径
- relative_path = os.path.relpath(parent, src_dir)
- _dst_path = os.path.join(dst_dir, relative_path, filename)
- _src_path = os.path.join(parent, filename)
- if os.path.exists(_dst_path) is False:
- copy_file(_src_path, _dst_path)
- else:
- ef_logger.warn("复制 %s 到 %s 时,目标文件已存在" %(_src_path, _dst_path))
- def copy_res(_src_path, _dst_path):#复制库工程res资源,参数为源res文件夹路径和目标res文件夹路径
- try:
- if os.path.isdir(_src_path):#路径为文件夹时
- for file in os.listdir(_src_path):#遍历
- ef_logger.info("资源文件名 %s" %file)
- _spath = os.path.join(_src_path, file)
- _dpath = os.path.join(_dst_path, file)
- copy_res(_spath, _dpath)#递归,遍历文件
- elif os.path.isfile(_src_path):#路径为文件时
- if(os.path.exists(_dst_path)):
- #raise Exception("复制 %s 到 %s 时,目标文件已存在" %(_src_path, _dst_path))
- ef_logger.warn("复制 %s 到 %s 时,目标文件已存在" %(_src_path, _dst_path))
- else:
- ef_logger.info("开始复制资源,从 %s 到 %s" %(_src_path, _dst_path))
- copy_file(_src_path, _dst_path)
- except Exception as err:
- ef_logger.exception(err)
- def copy_assets(scr_assets_dir, dst_assets_dir):
- copy_file_in_dir(scr_assets_dir, dst_assets_dir)
- def getCurrentSystem():
- system = platform.system()
- ef_logger.info("当前操作系统为%s" %system)
- return system
- # copy_res("C:\\Users\\Efun\\Desktop\\res2\\res", "C:\\Users\\Efun\\Desktop\\res1\\res")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement