3636# under Contract DE-AC05-76RL01830
3737# }}}
3838
39- import sys
4039import os
41- import subprocess
42- import re
4340import platform
41+ import re
42+ import subprocess
43+ import sys
44+ from distutils .version import LooseVersion
45+ from pathlib import Path
4446
45- from setuptools import setup , Extension
47+ from setuptools import Extension , find_namespace_packages , find_packages , setup
4648from setuptools .command .build_ext import build_ext
47- from distutils .version import LooseVersion
4849
49- from setuptools import find_packages , find_namespace_packages
50- from pathlib import Path
50+ module_name = "dnp3_python"
51+
5152
52- __version__ = '0.3.0b1'
53+ # Function to extract version from the __init__.py file at /src
54+ def find_version ():
55+ here = os .path .abspath (os .path .dirname (__file__ ))
56+ with open (os .path .join (here , "src" , module_name , "__init__.py" ), "r" ) as f :
57+ contents = f .read ()
58+ version_match = re .search (r"^__version__ = ['\"]([^'\"]*)['\"]" , contents , re .M )
59+ if version_match :
60+ return version_match .group (1 )
61+ raise RuntimeError ("Unable to find version string." )
62+
63+
64+ __version__ = find_version ()
65+ print (f"{ __version__ = } " )
5366
5467
5568class CMakeExtension (Extension ):
56- def __init__ (self , name , sourcedir = '' ):
69+ def __init__ (self , name , sourcedir = "" ):
5770 Extension .__init__ (self , name , sources = [])
5871 self .sourcedir = os .path .abspath (sourcedir )
5972
6073
6174class CMakeBuild (build_ext ):
6275 def run (self ):
6376 try :
64- out = subprocess .check_output ([' cmake' , ' --version' ])
77+ out = subprocess .check_output ([" cmake" , " --version" ])
6578 except OSError :
66- raise RuntimeError ("CMake must be installed to build the following extensions: " +
67- ", " .join (e .name for e in self .extensions ))
79+ raise RuntimeError (
80+ "CMake must be installed to build the following extensions: "
81+ + ", " .join (e .name for e in self .extensions )
82+ )
6883
6984 if platform .system () == "Windows" :
70- cmake_version = LooseVersion (re .search (r'version\s*([\d.]+)' , out .decode ()).group (1 ))
71- if cmake_version < '3.1.0' :
85+ cmake_version = LooseVersion (
86+ re .search (r"version\s*([\d.]+)" , out .decode ()).group (1 )
87+ )
88+ if cmake_version < "3.1.0" :
7289 raise RuntimeError ("CMake >= 3.1.0 is required on Windows" )
7390
7491 for ext in self .extensions :
7592 self .build_extension (ext )
7693
7794 def build_extension (self , ext ):
7895 extdir = os .path .abspath (os .path .dirname (self .get_ext_fullpath (ext .name )))
79- cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir ,
80- '-DPYTHON_EXECUTABLE=' + sys .executable ]
96+ cmake_args = [
97+ "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=" + extdir ,
98+ "-DPYTHON_EXECUTABLE=" + sys .executable ,
99+ ]
81100
82- cfg = ' Debug' if self .debug else ' Release'
83- build_args = [' --config' , cfg ]
101+ cfg = " Debug" if self .debug else " Release"
102+ build_args = [" --config" , cfg ]
84103
85104 if platform .system () == "Windows" :
86- cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}' .format (cfg .upper (), extdir )]
105+ cmake_args += [
106+ "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}" .format (cfg .upper (), extdir )
107+ ]
87108 if sys .maxsize > 2 ** 32 :
88- cmake_args += ['-A' , ' x64' ]
89- build_args += ['--' , '/m' ]
109+ cmake_args += ["-A" , " x64" ]
110+ build_args += ["--" , "/m" ]
90111 else :
91- cmake_args += [' -DCMAKE_BUILD_TYPE=' + cfg ]
92- cmake_args += [' -DSTATICLIBS=ON' ]
93- build_args += ['--' , ' -j2' ]
112+ cmake_args += [" -DCMAKE_BUILD_TYPE=" + cfg ]
113+ cmake_args += [" -DSTATICLIBS=ON" ]
114+ build_args += ["--" , " -j2" ]
94115
95116 env = os .environ .copy ()
96- env ['CXXFLAGS' ] = '{} -DVERSION_INFO=\\ "{}\\ "' .format (env .get ('CXXFLAGS' , '' ),
97- self .distribution .get_version ())
117+ env ["CXXFLAGS" ] = '{} -DVERSION_INFO=\\ "{}\\ "' .format (
118+ env .get ("CXXFLAGS" , "" ), self .distribution .get_version ()
119+ )
98120 if not os .path .exists (self .build_temp ):
99121 os .makedirs (self .build_temp )
100122
@@ -106,37 +128,41 @@ def build_extension(self, ext):
106128 #
107129 # subprocess.check_call(['git', 'apply', patch_path], cwd=dnp3_path)
108130
109- subprocess .check_call (['cmake' , ext .sourcedir ] + cmake_args , cwd = self .build_temp , env = env )
110- subprocess .check_call (['cmake' , '--build' , '.' ] + build_args , cwd = self .build_temp )
131+ subprocess .check_call (
132+ ["cmake" , ext .sourcedir ] + cmake_args , cwd = self .build_temp , env = env
133+ )
134+ subprocess .check_call (
135+ ["cmake" , "--build" , "." ] + build_args , cwd = self .build_temp
136+ )
111137
112138
113139this_directory = Path (__file__ ).parent
114140long_description = (this_directory / "README.md" ).read_text ()
115141
116142setup (
117- name = ' dnp3-python' ,
143+ name = " dnp3-python" ,
118144 version = __version__ ,
119- author = ' Volttron Team' ,
120- 121- url = ' https://github.com/VOLTTRON/dnp3-python' ,
122- description = ' pydnp3 -- python binding for opendnp3' ,
145+ author = " Volttron Team" ,
146+ 147+ url = " https://github.com/VOLTTRON/dnp3-python" ,
148+ description = " pydnp3 -- python binding for opendnp3" ,
123149 long_description = long_description ,
124- long_description_content_type = ' text/markdown' ,
150+ long_description_content_type = " text/markdown" ,
125151 install_requires = [
126152 # 'pybind11>=2.2',
127- 'argcomplete' ],
128- ext_modules = [CMakeExtension ('pydnp3' )],
153+ "argcomplete"
154+ ],
155+ ext_modules = [CMakeExtension ("pydnp3" )],
129156 cmdclass = dict (build_ext = CMakeBuild ),
130157 zip_safe = False ,
131-
132158 packages = find_namespace_packages (
133- where = ' src' ,
134- include = ['dnp3_python*' , ' dnp3demo' ] # to include sub-packages as well.
159+ where = " src" ,
160+ include = [f" { module_name } *" , " dnp3demo" ], # to include sub-packages as well.
135161 ),
136162 package_dir = {"" : "src" },
137163 entry_points = {
138- ' console_scripts' : [
139- ' dnp3demo = dnp3demo.__main__:main' ,
140- ]
141- },
164+ " console_scripts" : [
165+ " dnp3demo = dnp3demo.__main__:main" ,
166+ ]
167+ },
142168)
0 commit comments