zyjsuper 发表于 2021-1-28 20:34:19

MacOS签名脚本一枚

#! /usr/bin/env python3
#! coding = utf-8
import os
import subprocess as commands   # replace commands with subprocess in python3
import optparse

#use command "security find-identity -v" to list your developer ID.
DEVELOPER_ID = "Apple Development: Your Email (XXX......)"

def signWithPath(path):
    signCommand = "codesign --force --sign \"%s\" %s" % (DEVELOPER_ID, path)
    retCode, result = commands.getstatusoutput(signCommand)
    if retCode != 0:
      print(result)
      print("code sign failed")
    return retCode


def validateWithPath(path):
    signCommand = "codesign --verify --deep --verbose=3 %s" %path
    retCode, result = commands.getstatusoutput(signCommand)
    if retCode == 0:
      print(result)
      print("Accepted!")
      return 0
    else:
      print(result)
      print("Rejected!")
      return -2

def sign(APP_PATH):
    print("> signing frameworks & dylibs...")

    if not os.path.exists(APP_PATH):
      print("where's your app?!")
      return

    frameworkDir = os.path.join(APP_PATH, "Contents/")

   
    for root, dirs, files in os.walk(frameworkDir):
      for f in files:
            if f.endswith(".dylib"):
                print("signing", f)
                dylibPath = os.path.join(root, f)
                signWithPath(dylibPath)

    # sign frameworks
    for root, dirs, files in os.walk(frameworkDir):
      for d in dirs:
            if d.endswith(".framework"):
                print("signing", d)
                frameworkPath = os.path.join(root, d, "Versions/A")
                signWithPath(frameworkPath)


    print("> singing app...")
    print("singing", APP_PATH)
    signWithPath(APP_PATH)

    print("> validate code sign...")
    if validateWithPath(APP_PATH) == 0:
      print("Code sign completed!")
    else:
      print("Dohhh!")

def main():
    import sys
    parser = optparse.OptionParser(usage='"Codesign path"')
    parser.add_option('-s', '--sign',help='Sign and valid the app in the path.')
    parser.add_option('-v', '--valid',help='Only valid signature with app in the path.')

    options, args = parser.parse_args()

    if len(sys.argv) != 3:
      parser.print_help()
      sys.exit(1)

    if options.valid is not None:
      validateWithPath(options.valid)

    if options.sign is not None:
      sign(options.sign)


if __name__ == '__main__':
    main()

小七柒 发表于 2021-1-29 10:34:13

支持支持。

不破不立 发表于 2021-1-29 20:08:39

收藏备用,谢谢楼主提供

renyiup 发表于 2021-1-29 20:10:31

收藏备用,PYG有你更精彩!

dryzh 发表于 2021-2-1 11:34:27

666,学习python 执行命令检查命令回复状态源码

910265444 发表于 2021-10-26 10:15:09

收藏备用,谢谢楼主分享
页: [1]
查看完整版本: MacOS签名脚本一枚