|
| 1 | +# Copyright (c) 2012 CefPython Authors. All rights reserved. |
| 2 | +# License: New BSD License. |
| 3 | +# Website: http://code.google.com/p/cefpython/ |
| 4 | + |
| 5 | +# First, it copies all .pyx files from upper directory to setup/. |
| 6 | +# Then, fixes repeating of "include" statements in pyx files. |
| 7 | + |
| 8 | +# Only the mainfile needs to have "include" statements, |
| 9 | +# but we're using PyCharm and to get rid of "unresolved references" |
| 10 | +# and other errors displayed in pycharm we are adding "include" |
| 11 | +# statements in all of the pyx files. |
| 12 | + |
| 13 | +# I'm not 100% sure how includes work in Cython, but I suspect that |
| 14 | +# a few includes of the same file will include the same content more |
| 15 | +# than once, it should work, but function and variable definitions are |
| 16 | +# duplicated, it is some kind of overhead and it could lead to some |
| 17 | +# problems in the future, better to fix it now. |
| 18 | + |
| 19 | +# It also checks cdef & cpdef functions whether they are not missing "except *", |
| 20 | +# it is required to add it when returning non-python type. |
| 21 | + |
| 22 | +import glob |
| 23 | +import os |
| 24 | +import re |
| 25 | +import shutil |
| 26 | + |
| 27 | +def ExceptAllMissing(content): |
| 28 | + |
| 29 | + # This is not perfect, won't detect C++ custom types, but will find |
| 30 | + # the built-in types, templates and pointers. |
| 31 | + patterns = [] |
| 32 | + patterns.append( |
| 33 | + r"\bcp?def\s+" |
| 34 | + "((int|short|long|double|char|unsigned|float|double)\s+)+" |
| 35 | + "\w+\([^)]*\)\s*(with\s+(gil|nogil))?\s*:") |
| 36 | + patterns.append( |
| 37 | + r"\bcp?def\s+" |
| 38 | + # A template ends with bracket: CefRefPtr[CefBrowser] |
| 39 | + # or a pointer ends with asterisk: CefBrowser* |
| 40 | + "[^\s]+[\]*]\s+" |
| 41 | + "\w+\([^)]*\)\s*(with\s+(gil|nogil))?\s*:") |
| 42 | + patterns.append( |
| 43 | + r"\bcp?def\s+" |
| 44 | + # A reference, eg. CefString& |
| 45 | + "[^\s]+&\s+" |
| 46 | + "\w+\([^)]*\)\s*(with\s+(gil|nogil))?\s*:") |
| 47 | + |
| 48 | + for pattern in patterns: |
| 49 | + match = re.search(pattern, content) |
| 50 | + if match: break |
| 51 | + |
| 52 | + if match: |
| 53 | + lineNumber = (content.count("\n", 0, match.start()) + 1) |
| 54 | + return lineNumber |
| 55 | + |
| 56 | +print("\n") |
| 57 | +mainfile = "cefpython.pyx" |
| 58 | + |
| 59 | +pyxfiles = glob.glob("../../../*.pyx") |
| 60 | +if not len(pyxfiles): |
| 61 | + exit(1) |
| 62 | +pyxfiles = [file for file in pyxfiles if file.find(mainfile) == -1] |
| 63 | +# Now, pyxfiles contains all pyx files except the mainfile (cefpython.pyx), |
| 64 | +# we do not fix includes in mainfile. |
| 65 | + |
| 66 | +# So that this is the right directory we're in. |
| 67 | +if os.path.exists("setup"): |
| 68 | + print("Wrong directory, we should be inside setup!") |
| 69 | + exit() |
| 70 | + |
| 71 | +# Remove old pyx files in setup directory. |
| 72 | +oldpyxfiles = glob.glob("./*.pyx") |
| 73 | +print("Removing old pyx files in /setup/: %s" % oldpyxfiles) |
| 74 | +for pyxfile in oldpyxfiles: |
| 75 | + if os.path.exists(pyxfile): |
| 76 | + os.remove(pyxfile) |
| 77 | + |
| 78 | +# Copying pyxfiles and reading its contents. |
| 79 | + |
| 80 | +print("Copying .pyx files to /setup/: %s" % pyxfiles) |
| 81 | +shutil.copy("../../../%s" % mainfile, "./%s" % mainfile) |
| 82 | +# Rest of the files will be copied in for loop below. |
| 83 | + |
| 84 | +print("Fixing includes in .pyx files:") |
| 85 | +for pyxfile in pyxfiles: |
| 86 | + newfile = "./%s" % os.path.basename(pyxfile) |
| 87 | + shutil.copy(pyxfile, newfile) |
| 88 | + pyxfile = newfile |
| 89 | + with open(pyxfile, "r") as pyxfileopened: |
| 90 | + content = pyxfileopened.read() |
| 91 | + lineNumber = ExceptAllMissing(content) |
| 92 | + if lineNumber: |
| 93 | + print("WARNING: 'except *' missing in a cdef/cpdef function, " |
| 94 | + "in file %s on line %d" % (os.path.basename(pyxfile), lineNumber)) |
| 95 | + exit(1) |
| 96 | + # Do not remove the newline - so that line numbers are exact with originals. |
| 97 | + (content, subs) = re.subn(r"^include[\t ]+[\"'][^\"'\n\r]+[\"'][\t ]*", "", content, flags=re.MULTILINE) |
| 98 | + if subs: |
| 99 | + print("%s includes removed in: %s" % (subs, os.path.basename(pyxfile))) |
| 100 | + # Reading and writing with the same handle using "r+" mode doesn't work, |
| 101 | + # you need to seek(0) and write the same amount of bytes that was in the |
| 102 | + # file, otherwise old data from the end of file stays. |
| 103 | + with open(pyxfile, "w") as pyxfileopened: |
| 104 | + pyxfileopened.write(content) |
| 105 | + |
| 106 | +print("\n") |
0 commit comments