|
| 1 | +# Copyright (c) 2012-2014 The CEF Python authors. All rights reserved. |
| 2 | +# License: New BSD License. |
| 3 | +# Website: http://code.google.com/p/cefpython/ |
| 4 | + |
| 5 | +# Create a setup package. |
| 6 | + |
| 7 | +import sys |
| 8 | +import os |
| 9 | +import platform |
| 10 | +import argparse |
| 11 | +import re |
| 12 | +import shutil |
| 13 | +import glob |
| 14 | +import sysconfig |
| 15 | +import subprocess |
| 16 | +import struct |
| 17 | + |
| 18 | +BITS = "%sbit" % (8 * struct.calcsize("P")) |
| 19 | + |
| 20 | +PACKAGE_NAME = "cefpython3" |
| 21 | + |
| 22 | +INIT_TEMPLATE = os.getcwd()+r"/__init__.py.template" |
| 23 | +SETUP_TEMPLATE = os.getcwd()+r"/setup.py.template" |
| 24 | +PY_VERSION_DIGITS_ONLY = (str(sys.version_info.major) + "" |
| 25 | + + str(sys.version_info.minor)) # "27" or "34" |
| 26 | + |
| 27 | +def str_format(string, dictionary): |
| 28 | + orig_string = string |
| 29 | + for key, value in dictionary.iteritems(): |
| 30 | + string = string.replace("%("+key+")s", value) |
| 31 | + if string == orig_string: |
| 32 | + raise Exception("Nothing to format") |
| 33 | + if re.search(r"%\([a-zA-Z0-9_]+\)s", string): |
| 34 | + raise Exception("Not all strings formatted") |
| 35 | + return string |
| 36 | + |
| 37 | +def get_binaries_dir(b32, b64, bfat): |
| 38 | + pyver = PY_VERSION_DIGITS_ONLY |
| 39 | + if (os.path.exists(b32 + "/cefpython_py{}.so".format(pyver)) |
| 40 | + and os.path.exists(b64 + "/cefpython_py{}.so".format(pyver))): |
| 41 | + create_fat_binaries(b32, b64, bfat) |
| 42 | + return bfat |
| 43 | + return os.path.abspath(installer_dir+"/../binaries_"+BITS+"/") |
| 44 | + |
| 45 | +def create_fat_binaries(b32, b64, bfat): |
| 46 | + print("Creating fat binaries") |
| 47 | + if os.path.exists(bfat): |
| 48 | + shutil.rmtree(bfat) |
| 49 | + os.mkdir(bfat) |
| 50 | + res = os.system("cp -rf {}/* {}/".format(b32, bfat)) |
| 51 | + assert res == 0 |
| 52 | + files = os.listdir(b32) |
| 53 | + for fbase in files: |
| 54 | + if not (fbase.endswith(".dylib") or fbase.endswith(".so") |
| 55 | + or fbase.endswith("subprocess")): |
| 56 | + continue |
| 57 | + fname32 = os.path.join(b32, fbase) |
| 58 | + fname64 = os.path.join(b64, fbase) |
| 59 | + fnamefat = os.path.join(bfat, fbase) |
| 60 | + print("Creating fat binary: {}".format(fbase)) |
| 61 | + res = os.system("lipo {} {} -create -output {}" |
| 62 | + .format(fname32, fname64, fnamefat)) |
| 63 | + assert res == 0 |
| 64 | + |
| 65 | +def main(): |
| 66 | + parser = argparse.ArgumentParser(usage="%(prog)s [options]") |
| 67 | + parser.add_argument("-v", "--version", help="cefpython version", |
| 68 | + required=True) |
| 69 | + args = parser.parse_args() |
| 70 | + assert re.search(r"^\d+\.\d+$", args.version), ( |
| 71 | + "Invalid version string") |
| 72 | + |
| 73 | + vars = {} |
| 74 | + vars["APP_VERSION"] = args.version |
| 75 | + # vars["PLATFORM"] = sysconfig.get_platform() |
| 76 | + vars["PLATFORM"] = "macosx" |
| 77 | + vars["PY_VERSION_DIGITS_ONLY"] = PY_VERSION_DIGITS_ONLY |
| 78 | + |
| 79 | + print("Reading template: %s" % INIT_TEMPLATE) |
| 80 | + f = open(INIT_TEMPLATE) |
| 81 | + INIT_CONTENT = str_format(f.read(), vars) |
| 82 | + f.close() |
| 83 | + |
| 84 | + print("Reading template: %s" % SETUP_TEMPLATE) |
| 85 | + f = open(SETUP_TEMPLATE) |
| 86 | + SETUP_CONTENT = str_format(f.read(), vars) |
| 87 | + f.close() |
| 88 | + |
| 89 | + installer_dir = os.path.dirname(os.path.abspath(__file__)) |
| 90 | + |
| 91 | + setup_dir = installer_dir+"/"+PACKAGE_NAME+"-"+vars["APP_VERSION"]+"-"+vars["PLATFORM"]+"-setup" |
| 92 | + print("Creating setup dir: "+setup_dir) |
| 93 | + os.mkdir(setup_dir) |
| 94 | + |
| 95 | + package_dir = setup_dir+"/"+PACKAGE_NAME |
| 96 | + print("Creating package dir") |
| 97 | + os.mkdir(package_dir) |
| 98 | + |
| 99 | + print("Creating setup.py from template") |
| 100 | + with open(setup_dir+"/setup.py", "w") as f: |
| 101 | + f.write(SETUP_CONTENT) |
| 102 | + |
| 103 | + # Create fat binaries if both 32bit and 64bit are available |
| 104 | + b32 = os.path.abspath(installer_dir+"/../binaries_32bit/") |
| 105 | + b64 = os.path.abspath(installer_dir+"/../binaries_64bit/") |
| 106 | + bfat = os.path.abspath(installer_dir+"/binaries_fat/") |
| 107 | + binaries_dir = get_binaries_dir(b32, b64, bfat) |
| 108 | + print("Copying binaries to package dir") |
| 109 | + ret = os.system("cp -rf "+binaries_dir+"/* "+package_dir) |
| 110 | + assert ret == 0 |
| 111 | + |
| 112 | + os.chdir(package_dir) |
| 113 | + print("Removing .log files from the package dir") |
| 114 | + ret = os.system("rm *.log") |
| 115 | + # assert ret == 0 - if there are no .log files this assert would fail. |
| 116 | + os.chdir(installer_dir) |
| 117 | + |
| 118 | + print("Creating __init__.py from template") |
| 119 | + with open(package_dir+"/__init__.py", "w") as f: |
| 120 | + f.write(INIT_CONTENT) |
| 121 | + |
| 122 | + print("Creating examples dir in package dir") |
| 123 | + os.mkdir(package_dir+"/examples/") |
| 124 | + |
| 125 | + print("Creating wx dir in package dir") |
| 126 | + os.mkdir(package_dir+"/wx/") |
| 127 | + |
| 128 | + print("Moving example scripts from package dir to examples dir") |
| 129 | + examples = glob.glob(package_dir+"/*.py") |
| 130 | + for example in examples: |
| 131 | + # Ignore: cefpython_py27.py - dummy API script |
| 132 | + if os.path.basename(example).startswith("cefpython_"): |
| 133 | + continue |
| 134 | + # Ignore: __init__.py |
| 135 | + if os.path.basename(example).startswith("__"): |
| 136 | + continue |
| 137 | + os.rename(example, package_dir+"/examples/"+os.path.basename(example)) |
| 138 | + ret = os.system("mv "+package_dir+"/*.html "+package_dir+"/examples/") |
| 139 | + ret = os.system("mv "+package_dir+"/*.js "+package_dir+"/examples/") |
| 140 | + ret = os.system("mv "+package_dir+"/*.css "+package_dir+"/examples/") |
| 141 | + assert ret == 0 |
| 142 | + |
| 143 | + print("Copying wx-subpackage to wx dir in package dir") |
| 144 | + wx_subpackage_dir = os.path.abspath(installer_dir+"/../../wx-subpackage/") |
| 145 | + ret = os.system("cp -rf "+wx_subpackage_dir+"/* "+package_dir+"/wx/") |
| 146 | + assert ret == 0 |
| 147 | + |
| 148 | + print("Moving wx examples from wx/examples to examples/wx") |
| 149 | + shutil.move(package_dir+"/wx/examples", package_dir+"/wx/wx/") |
| 150 | + shutil.move(package_dir+"/wx/wx/", package_dir+"/examples/") |
| 151 | + |
| 152 | + print("Copying package dir examples to setup dir") |
| 153 | + ret = os.system("cp -rf "+package_dir+"/examples/ "+setup_dir+"/examples/") |
| 154 | + assert ret == 0 |
| 155 | + |
| 156 | + # Create empty debug.log files so that package uninstalls cleanly |
| 157 | + # in case examples were launched. Issue 149. |
| 158 | + debug_log_dirs = [package_dir, |
| 159 | + package_dir+"/examples/", |
| 160 | + package_dir+"/examples/wx/"] |
| 161 | + for dir in debug_log_dirs: |
| 162 | + print("Creating empty debug.log in %s" % dir) |
| 163 | + with open(dir+"/debug.log", "w") as f: |
| 164 | + f.write("") |
| 165 | + # Set write permissions so that Wheel package files have it |
| 166 | + # right. So that examples may be run from package directory. |
| 167 | + subprocess.call("chmod 666 %s/debug.log" % dir, shell=True) |
| 168 | + |
| 169 | + print("Setup Package created successfully.") |
| 170 | + |
| 171 | +if __name__ == "__main__": |
| 172 | + main() |
0 commit comments