summaryrefslogtreecommitdiffstats
path: root/setup.py
blob: dea16acb4fd312c0771a6b3fb1389c20c2d7805c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3

import sys
import os
import re
import shutil
import hashlib
from distutils.core import setup
from distutils.extension import Extension
from awlsim.version import VERSION_MAJOR, VERSION_MINOR


def pyCythonPatch(toFile, fromFile):
	print("cython-patch: patching file '%s' to '%s'" %\
	      (fromFile, toFile))
	tmpFile = toFile + ".TMP"
	infd = open(fromFile, "r")
	outfd = open(tmpFile, "w")
	for line in infd.readlines():
		line = re.sub(r'^from awlsim\.', "from awlsim_cython.", line)
		line = re.sub(r'^import awlsim\.', "import awlsim_cython.", line)
		outfd.write(line)
	infd.close()
	outfd.flush()
	outfd.close()
	try:
		toFileHash = hashlib.sha1(open(toFile, "rb").read()).hexdigest()
	except FileNotFoundError:
		pass
	else:
		newFileHash = hashlib.sha1(open(tmpFile, "rb").read()).hexdigest()
		if toFileHash == newFileHash:
			print("(already up to date)")
			return
	shutil.move(tmpFile, toFile)

def addCythonModules():
	global cmdclass
	global ext_modules

	modDir = "./awlsim/"
	buildDir = "./build/awlsim_cython_patched/"

	if not os.path.exists("./setup.py") or\
	   not os.path.exists(modDir) or\
	   not os.path.isdir(modDir):
		raise Exception("Wrong directory. "
			"Execute setup.py from within the awlsim directory.")

	os.makedirs(buildDir, 0o755, True)

	for dirpath, dirnames, filenames in os.walk(modDir):
		for filename in filenames:
			if filename.endswith(".py"):
				pyCythonPatch(buildDir + filename,
					      modDir + filename)
				basename = filename[:-3]
				ext_modules.append(
					Extension("awlsim_cython.%s" % basename,
						  ["%s/%s" % (buildDir, filename)])
				)
	cmdclass["build_ext"] = Cython_build_ext

cmdclass = {}
ext_modules = []
if 0:
	try:
		from Cython.Distutils import build_ext as Cython_build_ext
		if sys.version_info[0] >= 3:
			addCythonModules()
	except ImportError:
		pass

setup(	name		= "awlsim",
	version		= "%d.%d" % (VERSION_MAJOR, VERSION_MINOR),
	description	= "Step 7 AWL/STL/PLC simulator",
	author		= "Michael Buesch",
	author_email	= "m@bues.ch",
	url		= "http://bues.ch/cms/hacking/awlsim.html",
	packages	= [ "awlsim",
			    "awlsimhw_dummy",
			    "awlsimhw_linuxcnc",
			    "awlsimhw_pyprofibus", ],
	scripts		= [ "awlsimcli",
			    "awlsimgui",
			    "awlsim-linuxcnc-hal", ],
	cmdclass	= cmdclass,
	ext_modules	= ext_modules
)
bues.ch cgit interface