Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import collections
  3. import os
  4. import plistlib
  5. import shutil
  6. import io
  7. from .efun_logger import ef_logger
  8. import datetime
  9. import platform
  10.  
  11. def recursive_update_dict(des_dic, update_dic):
  12. for k, v in update_dic.items():
  13. if isinstance(v, collections.Mapping):
  14. r = recursive_update_dict(des_dic.get(k, {}), v)
  15. des_dic[k] = r
  16. else:
  17. des_dic[k] = update_dic[k]
  18. return des_dic
  19.  
  20. def derect_update_dict(des_dic, update_dic, ignore_empty=True):
  21. for k, v in update_dic.items():
  22. if ignore_empty:
  23. if v:
  24. des_dic[k] = v
  25. else:
  26. des_dic[k] = v
  27. return des_dic
  28.  
  29. def str2bool(v):
  30. try:
  31. return v.lower() in ("yes", "true", "t", "1", "on")
  32. except Exception as e:
  33. ef_logger.exception(e)
  34. raise e
  35.  
  36.  
  37. def datetime_str_iso8601_to_datetime(datetime_str):
  38. if datetime_str:
  39. try:
  40. resultV = datetime.datetime.strptime(datetime_str, '%Y-%m-%dT%H:%M:%S.%f')
  41. except ValueError:
  42. resultV = datetime.datetime.strptime(datetime_str, '%Y-%m-%dT%H:%M:%S')
  43. return resultV
  44.  
  45.  
  46. def open_plist_to_dic(file_path):
  47. dic = {}
  48. with io.open(file_path, 'rb') as f: #binary mode doesn't take an encoding argument
  49. dic = plistlib.load(f)
  50. return dic
  51.  
  52.  
  53. def save_dic_to_plist(dic, file_path):
  54. with io.open(file_path, 'wb') as f: #binary mode doesn't take an encoding argument
  55. plistlib.dump(dic, f)
  56.  
  57.  
  58. def copy_file_dir(src_path, dst_path):#复制文件夹
  59. try:
  60. if os.path.exists(src_path):
  61. if os.path.exists(dst_path) and os.path.isdir(dst_path):
  62. shutil.rmtree(dst_path)
  63. shutil.copytree(src_path, dst_path, ignore=shutil.ignore_patterns('*.pyc', '.svn', '.DS_Store'))
  64. ef_logger.info("copy dir success: %s" %src_path + "\n")
  65. else:
  66. ef_logger.info("scrDir is not exists: %s" %src_path + "\n")
  67. except Exception as err:
  68. ef_logger.exception(err)
  69.  
  70.  
  71. def copy_file(src_file, dst_file):#复制文件
  72. try:
  73. if os.path.exists(src_file) and os.path.isfile(src_file):
  74. if os.path.exists(dst_file) and os.path.isfile(dst_file):
  75. os.remove(dst_file)
  76.  
  77. os.makedirs(os.path.dirname(dst_file), exist_ok=True)
  78. shutil.copyfile(src_file, dst_file)
  79. ef_logger.info("copy file success: %s" %src_file + "\n")
  80. else:
  81. ef_logger.info("srcFile is not exists: %s" %src_file + "\n")
  82. except Exception as err:
  83. ef_logger.exception(err)
  84.  
  85.  
  86. # 从源目录复制文件(包括源目录的目录)到目标目录
  87. def copy_file_in_dir(src_dir, dst_dir):
  88. if os.path.exists(src_dir) and os.path.isdir(src_dir):
  89. for parent, dirnames, filenames in os.walk(src_dir): #三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字
  90. # for dirname in dirnames: #输出文件夹信息
  91. # print("parent is:" + parent)
  92. # print("dirname is:" + dirname)
  93. for filename in filenames: #输出文件信息
  94. # print(os.path.relpath(parent, src_dir)) #获取相对路径
  95. relative_path = os.path.relpath(parent, src_dir)
  96. _dst_path = os.path.join(dst_dir, relative_path, filename)
  97. _src_path = os.path.join(parent, filename)
  98. if os.path.exists(_dst_path) is False:
  99. copy_file(_src_path, _dst_path)
  100. else:
  101. ef_logger.warn("复制 %s 到 %s 时,目标文件已存在" %(_src_path, _dst_path))
  102.  
  103.  
  104. def copy_res(_src_path, _dst_path):#复制库工程res资源,参数为源res文件夹路径和目标res文件夹路径
  105. try:
  106. if os.path.isdir(_src_path):#路径为文件夹时
  107. for file in os.listdir(_src_path):#遍历
  108. ef_logger.info("资源文件名 %s" %file)
  109. _spath = os.path.join(_src_path, file)
  110. _dpath = os.path.join(_dst_path, file)
  111. copy_res(_spath, _dpath)#递归,遍历文件
  112.  
  113. elif os.path.isfile(_src_path):#路径为文件时
  114. if(os.path.exists(_dst_path)):
  115. #raise Exception("复制 %s 到 %s 时,目标文件已存在" %(_src_path, _dst_path))
  116. ef_logger.warn("复制 %s 到 %s 时,目标文件已存在" %(_src_path, _dst_path))
  117. else:
  118. ef_logger.info("开始复制资源,从 %s 到 %s" %(_src_path, _dst_path))
  119. copy_file(_src_path, _dst_path)
  120.  
  121. except Exception as err:
  122. ef_logger.exception(err)
  123.  
  124.  
  125. def copy_assets(scr_assets_dir, dst_assets_dir):
  126. copy_file_in_dir(scr_assets_dir, dst_assets_dir)
  127.  
  128. def getCurrentSystem():
  129. system = platform.system()
  130. ef_logger.info("当前操作系统为%s" %system)
  131. return system
  132.  
  133. # copy_res("C:\\Users\\Efun\\Desktop\\res2\\res", "C:\\Users\\Efun\\Desktop\\res1\\res")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement