Python 零基础学习记录
先说说学习python的目的,本人是IOS攻城狮。在做项目的时候要做组件管理,组件写多了维护起来,各种命令让人头疼,看着python 能写脚本管理就学一下,并且以此为自己完成python 的第一个Dome。当然写脚本管理有多种比如shell、ruby等但是我想学一下python。
1.编译工具 pyCharm https://www.jetbrains.com/pycharm/(Python官网下载了开发工具觉得很不好用就换这个了)
2.激活码 http://idea.lanyus.com 地址获取激活码
一、输出hello word
1.创建一个工程。
2.新建一个文件创建文件形式以python 类型并且后缀输入.py 。
3.用Terminal 直接跑.py 文件的时候注意输入版本号,例如:Python3.7 test.py
Hello word 跑起来了!!!!!
二、Python 基础语句
基础教程地址 http://www.runoob.com/python/python-tutorial.html
有编程基础的同学看一些基本就会了,然后开始快乐编码了!!!
然后就是我花了1天写的Python,为了完成IOS组件代码的发布。
import subprocess
import os
# 输出高亮
def JLBPrint(message):
print('\033[1;31;40m' + message + '\033[0m')
# 输入高亮
def JLBInput(message):
return input('\033[1;31;40m' + message + '\033[0m')
# 终端跑命令 返回结果code
def JLBTerminalRun(terminalString):
return subprocess.run(terminalString, shell=True).returncode == 0
# archive结果
def JLBArchiveResult(result):
if result:
JLBPrint('archive success')
else:
JLBPrint('archive fail')
# git命令
class JLBGit:
def add(self):
return JLBTerminalRun('git add .')
def commit(self, commitMessage):
return JLBTerminalRun('git commit -m \"' + commitMessage + '\"')
def push(self): # 提交先用master 分支
return JLBTerminalRun('git push origin master')
def pull(self): # 从主分支拉取代码
return JLBTerminalRun('git pull origin master')
def tag(self, version):
return JLBTerminalRun('git tag ' + version)
def deleteTag( self , version):
return JLBTerminalRun('git tag -d '+ version)
def pushTags(self):
return JLBTerminalRun('git push origin --tags')
class JLBModuleArchive:
# 组件
specsJLBRepo = 'http://10.10.10.60/Ios/JLBModule.git'
specsCocoapods = 'https://github.com/CocoaPods/Specs.git'
localRepoName = 'JLBModule'
def __init__(self, commitMessage):
self.commitMessage = commitMessage
def getPodSpecFilePath(self):
filePaths = os.listdir(os.getcwd())
file: str
for file in filePaths:
if os.path.isfile(file):
(shotname, extension) = os.path.splitext(file)
if extension.strip() == '.podspec':
return file
return ''
# 默认修改PodspecFile 中版本号自动加一 (暂时没有用)
def changePodspecFileVersionPlus(self):
podSpecFile = self.getPodSpecFilePath()
codeVersion = ''
if podSpecFile.strip() != '':
fileRead = open(podSpecFile, "r")
for line in fileRead.readlines():
string = line.replace(' ', '')
if string.find("s.version=") >= 0:
# 更换版本号
version = string[len("s.version="):]
version = version.replace('\'', '')
version = version.replace('\n', '')
versionChars = version.split(".", 2)
lastVersion = versionChars[1]
newLastVersion = str(int(lastVersion) + 1)
versionChars[1] = newLastVersion
newVersion = '.'.join(versionChars)
codeVersion = newVersion
newLine = line.replace(version, newVersion)
# 更换行内容
fileWrite = open(podSpecFile, "r+")
content = fileWrite.read()
content = content.replace(line, newLine)
fileWrite.seek(0)
fileWrite.write(content)
fileWrite.close()
break
fileRead.close()
return codeVersion
# 修改PodspecFile 版本号自动加一
def changePodspecFileVersion(self):
podSpecFile = self.getPodSpecFilePath()
codeVersion = ''
if podSpecFile.strip() != '':
fileRead = open(podSpecFile, "r")
for line in fileRead.readlines():
string = line.replace(' ', '')
if string.find("s.version=") >= 0:
# 更换版本号
version = string[len("s.version="):]
version = version.replace('\'', '')
version = version.replace('\n', '')
versionChars = version.split(".", 2)
lastVersion = versionChars[2]
newLastVersion = str(int(lastVersion) + 1)
versionChars[2] = newLastVersion
newVersion = '.'.join(versionChars)
codeVersion = newVersion
newLine = line.replace(version, newVersion)
# 更换行内容
fileWrite = open(podSpecFile, "r+")
content = fileWrite.read()
content = content.replace(line, newLine)
fileWrite.seek(0)
fileWrite.write(content)
fileWrite.close()
break
fileRead.close()
return codeVersion
#修改PodspecFile 版本号自动减一
def changePodspecFileVersionBack ( self ):
podSpecFile = self.getPodSpecFilePath()
codeVersion = ''
if podSpecFile.strip() != '':
fileRead = open(podSpecFile, "r")
for line in fileRead.readlines():
string = line.replace(' ', '')
if string.find("s.version=") >= 0:
# 更换版本号
version = string[len("s.version="):]
version = version.replace('\'', '')
version = version.replace('\n', '')
codeVersion = version
versionChars = version.split(".", 2)
lastVersion = versionChars[2]
newLastVersion = str(int(lastVersion) - 1)
versionChars[2] = newLastVersion
newVersion = '.'.join(versionChars)
newLine = line.replace(version, newVersion)
# 更换行内容
fileWrite = open(podSpecFile, "r+")
content = fileWrite.read()
content = content.replace(line, newLine)
fileWrite.seek(0)
fileWrite.write(content)
fileWrite.close()
break
fileRead.close()
return codeVersion
#pod 发布失败后 git tag 提交的版本删除
def podReleaseFail( self ):
codeVersion = self.changePodspecFileVersionBack()
if codeVersion != '':
git = JLBGit ()
if git.deleteTag(codeVersion):
if git.pushTags():
JLBPrint('git tag back success')
else:
JLBPrint('git push tags fail')
else:
JLBPrint('git delete tag fail')
else:
JLBPrint('version back fail')
def gitPull(self): # git拉取代码
git = JLBGit()
return git.pull()
def gitCommit(self):
git = JLBGit()
if git.add():
if git.commit(self.commitMessage):
if git.push() == False:
JLBPrint('git push fail')
return False
else:
JLBPrint('git commit fail')
return False
else:
JLBPrint('git add fail')
return False
return True
def gitCommitAndNewTag(self):
if self.gitCommit():
return True
else:
return False
# 提交代码并且修改本地podspecfile Version
def gitCommitAndNewTagAutoAddVersion(self):
if self.gitCommit():
version = self.changePodspecFileVersion()
if version.strip() != '':
git = JLBGit()
if git.tag(version):
if git.pushTags():
return True
else:
JLBPrint('push tags fail')
self.changePodspecFileVersionBack ()
return False
else:
JLBPrint(version + 'tag fail')
self.changePodspecFileVersionBack ()
return False
else:
JLBPrint('change Version fail')
return False
else:
return False
# pod 组件版本发布
def podRelease(self):
# 先本地验证代码
if self.podLibVerify():
if self.gitCommitAndNewTagAutoAddVersion():
if self.podRepoPush():
JLBPrint('pod repo push success')
else:
JLBPrint ( 'pod repo push fail' )
self.podReleaseFail()
return False
else:
JLBPrint('gitCommitAndNewTagAutoAddVersion fail')
return False
else:
JLBPrint('pod lib verify fail')
return False
return True
def podLibVerify(self): # 组件本地代码验证
archiveCMD = 'pod lib lint --sources=\'' + self.specsJLBRepo + ',' + self.specsCocoapods + '\' --use-libraries --fail-fast --allow-warnings --verbose'
return JLBTerminalRun ( archiveCMD )
def podSpecVerify(self): # 组件远程代码验证
archiveCMD = 'pod spec lint --sources=\'' + self.specsJLBRepo + ',' + self.specsCocoapods + '\' --use-libraries --fail-fast --allow-warnings --verbose'
return JLBTerminalRun ( archiveCMD )
def podRepoPush(self):
podSpecFile = self.getPodSpecFilePath ()
archiveCMD = 'pod repo push ' + self.localRepoName + ' ' + podSpecFile + ' --sources=\'' + self.specsJLBRepo + ',' + self.specsCocoapods + '\' --use-libraries --allow-warnings'
return JLBTerminalRun ( archiveCMD )
def main():
string = JLBInput('please input number \n1.git pull \n2.git commit \n3.pod release\n')
if string is '0':#退出操作
JLBPrint('exit')
elif string is '1':#git 拉代码
moduleArchive = JLBModuleArchive('')
JLBArchiveResult(moduleArchive.gitPull())
elif string is '2':#git 提交代码
string = JLBInput('please input commit message!\n')
moduleArchive = JLBModuleArchive(string)
JLBArchiveResult(moduleArchive.gitCommit())
elif string is '3':# pod 发布新代码
string = JLBInput('please input commit message!\n')
moduleArchive = JLBModuleArchive(string)
JLBArchiveResult(moduleArchive.podRelease())
else:
JLBPrint('input error')
main()
if __name__ == '__main__':
main()
运行结果如下


本文分享了使用Python脚本来管理iOS组件的经验,包括搭建开发环境、实现基础语句及功能,以及自动化发布流程,旨在简化组件管理和提高开发效率。
1万+

被折叠的 条评论
为什么被折叠?



