setup.py 10.7 KB
Newer Older
Michael Büsch's avatar
Michael Büsch committed
1 2
#!/usr/bin/env python3

Michael Büsch's avatar
Michael Büsch committed
3 4
from __future__ import print_function

5 6
import sys
import os
7 8
import platform
import errno
9 10 11
import re
import shutil
import hashlib
Michael Büsch's avatar
Michael Büsch committed
12
from distutils.core import setup
13
from distutils.extension import Extension
Michael Büsch's avatar
Michael Büsch committed
14
from awlsim.common.version import VERSION_STRING
Michael Büsch's avatar
Michael Büsch committed
15 16 17 18
try:
	import py2exe
except ImportError as e:
	py2exe = None
19 20 21 22 23 24 25
try:
	if py2exe and "py2exe" in sys.argv:
		raise ImportError
	from cx_Freeze import setup, Executable
	cx_Freeze = True
except ImportError as e:
	cx_Freeze = False
Michael Büsch's avatar
Michael Büsch committed
26

27

28
def makedirs(path, mode=0o755):
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
	try:
		os.makedirs(path, mode)
	except OSError as e:
		if e.errno == errno.EEXIST:
			return
		raise

def hashFile(path):
	if sys.version_info[0] < 3:
		ExpectedException = IOError
	else:
		ExpectedException = FileNotFoundError
	try:
		return hashlib.sha1(open(path, "rb").read()).hexdigest()
	except ExpectedException as e:
		return None

Michael Büsch's avatar
Michael Büsch committed
46 47 48 49 50 51
def __fileopIfChanged(fromFile, toFile, fileop):
	toFileHash = hashFile(toFile)
	if toFileHash is not None:
		fromFileHash = hashFile(fromFile)
		if toFileHash == fromFileHash:
			return False
52
	makedirs(os.path.dirname(toFile))
Michael Büsch's avatar
Michael Büsch committed
53 54 55 56 57 58 59 60 61
	fileop(fromFile, toFile)
	return True

def copyIfChanged(fromFile, toFile):
	return __fileopIfChanged(fromFile, toFile, shutil.copy2)

def moveIfChanged(fromFile, toFile):
	return __fileopIfChanged(fromFile, toFile, os.rename)

62 63 64 65 66 67 68 69 70
def makeDummyFile(path):
	if os.path.isfile(path):
		return
	print("creating dummy file '%s'" % path)
	makedirs(os.path.dirname(path))
	fd = open(path, "w")
	fd.write("\n")
	fd.close()

71
def pyCythonPatch(fromFile, toFile, basicOnly=False):
72 73 74
	print("cython-patch: patching file '%s' to '%s'" %\
	      (fromFile, toFile))
	tmpFile = toFile + ".TMP"
75
	makedirs(os.path.dirname(tmpFile))
76 77 78
	infd = open(fromFile, "r")
	outfd = open(tmpFile, "w")
	for line in infd.readlines():
Michael Büsch's avatar
Michael Büsch committed
79 80
		stripLine = line.strip()

81 82 83 84
		if stripLine.endswith("#<no-cython-patch"):
			outfd.write(line)
			continue

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
		# Uncomment all lines containing #@cy
		if "#@cy" in stripLine:
			line = line.replace("#@cy", "")
			if line.startswith("#"):
				line = line[1:]
			if not line.endswith("\n"):
				line += "\n"

		# Sprinkle magic cdef, as requested by #+cdef
		if "#+cdef" in stripLine:
			if stripLine.startswith("class"):
				line = line.replace("class", "cdef class")
			else:
				line = line.replace("def", "cdef")

		# Comment all lines containing #@nocy
		if "#@nocy" in stripLine:
			line = "#" + line

		if not basicOnly:
105 106 107 108 109 110 111 112 113 114 115 116 117 118
			# Automagic types
			line = re.sub(r'\b_Bool\b', "unsigned char", line)
			line = re.sub(r'\bint8_t\b', "signed char", line)
			line = re.sub(r'\buint8_t\b', "unsigned char", line)
			line = re.sub(r'\bint16_t\b', "signed short", line)
			line = re.sub(r'\buint16_t\b', "unsigned short", line)
			line = re.sub(r'\bint32_t\b', "signed int", line)
			line = re.sub(r'\buint32_t\b', "unsigned int", line)
			line = re.sub(r'\bint64_t\b', "signed long long", line)
			line = re.sub(r'\buint64_t\b', "unsigned long long", line)

			# Remove compat stuff
			line = line.replace("absolute_import,", "")

119
		# Patch the import statements
120 121
		line = re.sub(r'^(\s*from awlsim[0-9a-zA-Z_]*)\.([0-9a-zA-Z_\.]+) import', r'\1_cython.\2 import', line)
		line = re.sub(r'^(\s*from awlsim[0-9a-zA-Z_]*)\.([0-9a-zA-Z_\.]+) cimport', r'\1_cython.\2 cimport', line)
122
		line = re.sub(r'^(\s*import awlsim[0-9a-zA-Z_]*)\.', r'\1_cython.', line)
123
		line = re.sub(r'^(\s*cimport awlsim[0-9a-zA-Z_]*)\.', r'\1_cython.', line)
Michael Büsch's avatar
Michael Büsch committed
124

125 126 127 128
		outfd.write(line)
	infd.close()
	outfd.flush()
	outfd.close()
Michael Büsch's avatar
Michael Büsch committed
129
	if not moveIfChanged(tmpFile, toFile):
130 131
		print("(already up to date)")
		os.unlink(tmpFile)
132

Michael Büsch's avatar
Michael Büsch committed
133
class CythonBuildUnit(object):
134 135
	def __init__(self, cyModName, baseName, fromPy, fromPxd, toDir, toPyx, toPxd):
		self.cyModName = cyModName
Michael Büsch's avatar
Michael Büsch committed
136 137 138 139 140 141
		self.baseName = baseName
		self.fromPy = fromPy
		self.fromPxd = fromPxd
		self.toDir = toDir
		self.toPyx = toPyx
		self.toPxd = toPxd
142

Michael Büsch's avatar
Michael Büsch committed
143
cythonBuildUnits = []
144

145
def patchCythonModules(buildDir):
Michael Büsch's avatar
Michael Büsch committed
146
	for unit in cythonBuildUnits:
147
		makedirs(unit.toDir)
148
		makeDummyFile(os.path.join(unit.toDir, "__init__.py"))
Michael Büsch's avatar
Michael Büsch committed
149
		if unit.baseName == "__init__":
150 151 152
			# Copy and patch the package __init__.py
			toPy = os.path.join(buildDir, *unit.cyModName.split(".")) + ".py"
			pyCythonPatch(unit.fromPy, toPy,
153
				      basicOnly=True)
Michael Büsch's avatar
Michael Büsch committed
154 155
		else:
			# Generate the .pyx
156
			pyCythonPatch(unit.fromPy, unit.toPyx)
157
		# Copy and patch the .pxd, if any
Michael Büsch's avatar
Michael Büsch committed
158
		if os.path.isfile(unit.fromPxd):
159
			pyCythonPatch(unit.fromPxd, unit.toPxd)
160

161
def registerCythonModule(baseDir, sourceModName):
162
	global ext_modules
Michael Büsch's avatar
Michael Büsch committed
163
	global cythonBuildUnits
164

165
	modDir = os.path.join(baseDir, sourceModName)
166
	# Make path to the cython patch-build-dir
167 168 169
	patchDir = os.path.join(baseDir, "build",
		"cython_patched.%s-%s-%d.%d" %\
		(platform.system().lower(),
170
		 platform.machine().lower(),
171 172
		 sys.version_info[0], sys.version_info[1]),
		"%s_cython" % sourceModName
173
	)
174

175
	if not os.path.exists(os.path.join(baseDir, "setup.py")) or\
176 177 178 179 180
	   not os.path.exists(modDir) or\
	   not os.path.isdir(modDir):
		raise Exception("Wrong directory. "
			"Execute setup.py from within the awlsim directory.")

181
	# Walk the "awlsim" module
182
	for dirpath, dirnames, filenames in os.walk(modDir):
183
		subpath = os.path.relpath(dirpath, modDir)
184
		if subpath == baseDir:
185
			subpath = ""
Michael Büsch's avatar
Michael Büsch committed
186 187 188

		if subpath.startswith("gui"):
			continue
189
		for filename in filenames:
190 191 192 193 194
			if not filename.endswith(".py"):
				continue
			baseName = filename[:-3] # Strip .py

			fromPy = os.path.join(dirpath, baseName + ".py")
195 196
			fromPxd = os.path.join(dirpath, baseName + ".pxd.in")
			toDir = os.path.join(patchDir, subpath)
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
			toPyx = os.path.join(toDir, baseName + ".pyx")
			toPxd = os.path.join(toDir, baseName + ".pxd")

			# Construct the new cython module name
			cyModName = [ "%s_cython" % sourceModName ]
			if subpath:
				cyModName.extend(subpath.split(os.sep))
			cyModName.append(baseName)
			cyModName = ".".join(cyModName)

			# Remember the filenames for the build
			unit = CythonBuildUnit(cyModName, baseName, fromPy, fromPxd,
					       toDir, toPyx, toPxd)
			cythonBuildUnits.append(unit)

			if baseName != "__init__":
213
				# Create a distutils Extension for the module
214
				ext_modules.append(
215
					Extension(cyModName, [toPyx])
216 217
				)

218 219 220 221 222 223 224 225
def registerCythonModules():
	baseDir = os.curdir # Base directory, where setup.py lives.

	for filename in os.listdir(baseDir):
		if filename == "awlsim" or\
		   filename.startswith("awlsimhw_"):
			registerCythonModule(baseDir, filename)

Michael Büsch's avatar
Michael Büsch committed
226
def tryBuildCythonModules():
227
	try:
Michael Büsch's avatar
Michael Büsch committed
228 229 230 231 232
		if int(os.getenv("NOCYTHON", "0")):
			print("Skipping build of CYTHON modules due to "
			      "NOCYTHON environment variable setting.")
			return
	except ValueError:
233
		pass
Michael Büsch's avatar
Michael Büsch committed
234 235 236 237
	if os.name != "posix":
		print("WARNING: Not building CYTHON modules on '%s' platform." %\
		      os.name)
		return
238 239 240 241
	if "bdist_wininst" in sys.argv:
		print("WARNING: Omitting CYTHON modules while building "
		      "Windows installer.")
		return
Michael Büsch's avatar
Michael Büsch committed
242 243 244 245 246 247
	try:
		from Cython.Distutils import build_ext as Cython_build_ext
	except ImportError as e:
		print("WARNING: Could not build the CYTHON modules: "
		      "%s" % str(e))
		print("--> Is Cython installed?")
248 249 250
		return

	class MyCythonBuildExt(Cython_build_ext):
251
		def build_extension(self, ext):
252 253
			assert(not ext.name.endswith("__init__"))
			Cython_build_ext.build_extension(self, ext)
254

255 256
		def build_extensions(self):
			# First patch the files, the run the normal build
257
			patchCythonModules(self.build_lib)
258
			Cython_build_ext.build_extensions(self)
259

260 261
	cmdclass["build_ext"] = MyCythonBuildExt
	registerCythonModules()
Michael Büsch's avatar
Michael Büsch committed
262 263 264

cmdclass = {}
ext_modules = []
265
extraScripts = []
Michael Büsch's avatar
Michael Büsch committed
266
extraKeywords = {}
Michael Büsch's avatar
Michael Büsch committed
267 268
# Try to build the Cython modules. This might fail.
tryBuildCythonModules()
269

270 271 272 273 274 275 276 277
# Workaround for mbcs codec bug in distutils
# http://bugs.python.org/issue10945
import codecs
try:
	codecs.lookup("mbcs")
except LookupError:
	codecs.register(lambda name: codecs.lookup("ascii") if name == "mbcs" else None)

278 279 280 281 282 283 284 285 286 287
# Add win postinstall script
try:
	idx = sys.argv.index("bdist_wininst")
	if idx > 0:
		sys.argv.insert(idx + 1, "--install-script")
		sys.argv.insert(idx + 2, "awlsim-wininst-postinstall.py")
		extraScripts.append("awlsim-wininst-postinstall.py")
except ValueError:
	pass

288 289 290 291 292
freezeExecutables = [ "awlsim-cli",
		      "awlsim-gui",
		      "awlsim-server",
		      "awlsim-symtab",
		      "awlsim/coreserver/server.py", ]
Michael Büsch's avatar
Michael Büsch committed
293
if py2exe:
294 295 296 297 298 299 300 301 302 303 304
	extraKeywords["console"] = freezeExecutables
if cx_Freeze:
	extraKeywords["executables"] = [ Executable(e)
					 for e in freezeExecutables ]
	extraKeywords["options"] = {
			"build_exe"     : {
				"packages"      : [ "awlsimhw_debug",
						    "awlsimhw_dummy",
						    "awlsim.library.iec", ],
			}
		}
Michael Büsch's avatar
Michael Büsch committed
305

Michael Büsch's avatar
Michael Büsch committed
306
setup(	name		= "awlsim",
Michael Büsch's avatar
Michael Büsch committed
307
	version		= VERSION_STRING,
308
	description	= "S7 AWL/STL Soft-PLC",
309
	license		= "GNU General Public License v2 or later",
Michael Büsch's avatar
Michael Büsch committed
310 311
	author		= "Michael Buesch",
	author_email	= "m@bues.ch",
312
	url		= "http://bues.ch/h/awlsim",
313
	packages	= [ "awlsim",
314
			    "awlsim/common",
315 316
			    "awlsim/core",
			    "awlsim/core/instructions",
317
			    "awlsim/core/systemblocks",
318
			    "awlsim/coreclient",
319
			    "awlsim/coreserver",
320
			    "awlsim/gui",
Michael Büsch's avatar
Michael Büsch committed
321
			    "awlsim/gui/icons",
Michael Büsch's avatar
Michael Büsch committed
322 323
			    "awlsim/library",
			    "awlsim/library/iec",
Michael Büsch's avatar
Michael Büsch committed
324
			    "awlsimhw_debug",
325 326 327
			    "awlsimhw_dummy",
			    "awlsimhw_linuxcnc",
			    "awlsimhw_pyprofibus", ],
Michael Büsch's avatar
Michael Büsch committed
328 329
	scripts		= [ "awlsim-cli",
			    "awlsim-gui",
330
			    "awlsim-server",
331
			    "awlsim-symtab",
332
			    "awlsim-linuxcnc-hal",
333
			    "awlsim-win.bat", ] + extraScripts,
334
	cmdclass	= cmdclass,
335
	ext_modules	= ext_modules,
336 337 338
	keywords	= [ "AWL", "STL", "SPS", "PLC", "Step 7",
			    "Siemens", "emulator", "simulator",
			    "PROFIBUS", "LinuxCNC", ],
339 340 341 342 343 344 345 346 347 348 349 350 351
	classifiers	= [
		"Development Status :: 4 - Beta",
		"Environment :: Console",
		"Environment :: Win32 (MS Windows)",
		"Environment :: X11 Applications",
		"Intended Audience :: Developers",
		"Intended Audience :: Education",
		"Intended Audience :: Information Technology",
		"Intended Audience :: Manufacturing",
		"Intended Audience :: Science/Research",
		"License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)",
		"Operating System :: Microsoft :: Windows",
		"Operating System :: POSIX",
352
		"Operating System :: POSIX :: Linux",
353
		"Programming Language :: Cython",
354
		"Programming Language :: Python",
355 356 357 358
		"Programming Language :: Python :: 2.7",
		"Programming Language :: Python :: 3",
		"Programming Language :: Python :: Implementation :: CPython",
		"Programming Language :: Python :: Implementation :: PyPy",
Michael Büsch's avatar
Michael Büsch committed
359 360
		"Programming Language :: Python :: Implementation :: Jython",
		"Programming Language :: Python :: Implementation :: IronPython",
361
		"Topic :: Education",
Michael Büsch's avatar
Michael Büsch committed
362
		"Topic :: Home Automation",
363
		"Topic :: Scientific/Engineering",
364
		"Topic :: Software Development",
365
		"Topic :: Software Development :: Interpreters",
Michael Büsch's avatar
Michael Büsch committed
366
		"Topic :: Software Development :: Embedded Systems",
367 368
		"Topic :: Software Development :: Testing",
		"Topic :: System :: Emulators",
369
	],
Michael Büsch's avatar
Michael Büsch committed
370 371
	long_description = open("README.txt").read(),
	**extraKeywords
Michael Büsch's avatar
Michael Büsch committed
372
)