|
import zipfile
class CompressWidgetCommand(sublime_plugin.WindowCommand):
def run(self, dirs):
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(message)s',
datefmt='%Y %m %d %H:%M:%S',
filename=os.path.join(curDir,'apicloud.log'),
filemode='a')
dirname=dirs[0]
filelist=[]
fulldirname=os.path.abspath(dirname)
zipfilename=os.path.basename(fulldirname)+'.zip'
fullzipfilename=os.path.join(os.path.dirname(fulldirname),zipfilename)
logging.info('*'*30+'begin CompressWidgetCommand'+'*'*30)
logging.info("CompressWidgetCommand: Begin to zip %s to %s ..." % (fulldirname, fullzipfilename) )
if not os.path.exists(fulldirname):
logging.info( "CompressWidgetCommand: Folder %s is not exist" % fulldirname )
sublime.error_message(u"文件夹 %s 不存在!" % fulldirname)
return
if os.path.exists(fullzipfilename):
flag=sublime.ok_cancel_dialog(u"文件%s 已存在,确定覆盖该文件 ? [Y/N]" % fullzipfilename)
logging.info("CompressWidgetCommand: %s has already exist" % fullzipfilename )
if not flag:
logging.info('CompressWidgetCommand: cancel zip the folder')
return
for root, dirlist, files in os.walk(dirname):
for filename in files:
filelist.append(os.path.join(root,filename))
destZip=zipfile.ZipFile(fullzipfilename, "w")
for eachfile in filelist:
destfile=eachfile[len(dirname):]
sublime.status_message(u"正在压缩文件 file %s." % destfile )
logging.info("CompressWidgetCommand: Zip file %s." % destfile )
destZip.write(eachfile, 'widget'+destfile)
destZip.close()
sublime.status_message(u'压缩完成')
logging.info("CompressWidgetCommand: Zip folder succeed!")
logging.info('*'*30+'CompressWidgetCommand complete'+'*'*30)
def is_visible(self, dirs):
return len(dirs) == 1
def is_enabled(self, dirs):
if 0==len(dirs):
return False
appFileList=os.listdir(dirs[0])
if 'config.xml' in appFileList:
return True
return False
.......哪有你说的那功能....... |
|