设为首页 收藏本站
查看: 947|回复: 0

[经验分享] 增加了自动创建插件工程的Python脚本

[复制链接]

尚未签到

发表于 2015-4-26 08:13:00 | 显示全部楼层 |阅读模式
  为了避免手工创建新的插件工程,同时也是学习python,今天用了6个小时边学边试,终于编写完了。
  学习到的新知识有:
  1、正则表达式一次替换多种子串   
2、遍历创建目录     
3、回调函数     
4、路径模块     
5、字典的一些用法     
6、主函数、编写习惯、递进开发方式
#!/usr/bin/env python
"""
Function:   Create a plugin project based on Win32DllTempl or MFCExtTempl.
This script has been tested with ActivePython 2.7.
Usage:      makeplugin.py projname pkgname basetype
projname: name of the new project.
pkgname: package name of the new project, the default value
is 'pkg_Example'.
basetype: 'mfc', 'win32', 'view' or project name in pkg_Example,
the default value is 'win32'.
Creator:    ooyg
Date:       2011.10.9
ChangeList:
1. Implemented the function: multireplace, copyfiles, makeproj
2. Add 'view' for the param of 'basetype'.
Skip project files while copying files in pkg_Example. [2011/10/10]
"""
import os, sys, re
def multireplace(text, adict):
rx = re.compile('|'.join(map(re.escape, adict)))
def xlat(match):
return adict[match.group(0)]
return rx.sub(xlat, text)
def copyfiles(srcdir, destdir, pairs, callback=None):
if srcdir.find(".svn") > 0:
return
if not os.path.exists(destdir):
os.makedirs(destdir)
for fn in os.listdir(srcdir):
srcfile = os.path.join(srcdir, fn)
destfile = os.path.join(destdir, multireplace(fn, pairs))
if os.path.isdir(srcfile):
copyfiles(srcfile, destfile, pairs)
if os.path.isfile(srcfile) and not os.path.exists(destfile) \
and (not callback or callback(fn, pairs)):
open(destfile, "wb").write(open(srcfile, "rb").read())
text = open(destfile).read()
newtext = multireplace(text, pairs)
if newtext != text:
open(destfile, 'w').write(newtext)
print('%s [replaced]' % destfile)
else:
print(destfile)
def makeproj(projname, pkgname, baseproj):
codepath = os.path.abspath('../code')
basepath = os.path.join(codepath, 'pkg_Example', 'Modules', baseproj)
if not os.path.exists(basepath):
raise OSError, basepath
if not os.path.exists(os.path.join(codepath, pkgname)):
raise OSError, os.path.join(codepath, pkgname)
if projname == '':
raise AttributeError, projname
destdir = os.path.join(codepath, pkgname, 'Modules', projname)
pairs = {baseproj:projname, 'pkg_Example':pkgname}
def matchfile(filename, pairs):
if filename.find(".dsp") > 0 or filename.find(".vc") > 0:
return False
return True
copyfiles(basepath, destdir, pairs, matchfile)
def matchproj(filename, pairs):
for key in pairs.keys():
if filename.startswith(key):
return True
return False
projects = os.path.abspath('../projects/msvc/vcproj')
copyfiles(projects, projects, pairs, matchproj)
if __name__=="__main__":
def inputparam(index, prompt, default=''):
if len(sys.argv) > index:
ret = sys.argv[index]
else:
ret = raw_input(prompt)
if ret == '': ret = default
return ret
projname = inputparam(1, 'Project name: ')
pkgname  = inputparam(2, 'Package name (pkg_Example): ', 'pkg_Example')
baseproj = inputparam(3, 'Template project (win32, mfc, view, '   \
'or project name in pkg_Example): ', 'win32')
if baseproj == 'win32': baseproj = 'Win32DllTempl'
if baseproj == 'mfc':   baseproj = 'MFCExtTempl'
if baseproj == 'view':  baseproj = 'HelloView'
makeproj(projname, pkgname, baseproj)

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-60713-1-1.html 上篇帖子: Python天天美味(17) 下篇帖子: Python天天美味(10)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表