Skip to content

Commit f5ebd24

Browse files
petrutlucian94alexpilotti
authored andcommitted
Refactor build process
At the moment, we're relying on a Visual Studio solution when building the extension. For this reason, simply doing a "pip install PyMI" will fail as setup.py doesn't build the extension, expecting it to be already built. We're now letting distutils build our extension (keeping the VS Solution as well for now). We're also switching to pbr, simplifying version management. The project structure will have to change a bit. Pbr seems to make some assumptions about the project layout, so we'll have to move setup.py in the top level dir, having wmi dir next to it.
1 parent 4e1f9ce commit f5ebd24

18 files changed

Lines changed: 176 additions & 15 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,5 @@ _Pvt_Extensions
233233
*.pyd
234234
*.egg-info
235235
dist
236+
237+
*.metaproj

PyMI/PyMI.vcxproj

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@
568568
<AdditionalDependencies>mi++.lib;mi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
569569
</Link>
570570
<PostBuildEvent>
571-
<Command>copy "$(TargetPath)" "$(ProjectDir)src\mi\" &amp;&amp; copy "$(TargetDir)\mi.pdb" "$(ProjectDir)src\mi\" &amp;&amp; cd "$(ProjectDir)" &amp;&amp; "$(PythonDir_27_x86)\python.exe" setup.py bdist_wheel --python-tag cp27
571+
<Command>copy "$(TargetPath)" "$(ProjectDir)src\mi\" &amp;&amp; copy "$(TargetDir)\mi.pdb" "$(ProjectDir)src\mi\" &amp;&amp; cd "$(ProjectDir)" &amp;&amp; "$(PythonDir_27_x86)\python.exe" setup_vs.py bdist_wheel --python-tag cp27
572572

573573
</Command>
574574
</PostBuildEvent>
@@ -592,7 +592,7 @@
592592
<AdditionalDependencies>mi++.lib;mi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
593593
</Link>
594594
<PostBuildEvent>
595-
<Command>copy "$(TargetPath)" "$(ProjectDir)src\mi\" &amp;&amp; copy "$(TargetDir)\mi.pdb" "$(ProjectDir)src\mi\" &amp;&amp; cd "$(ProjectDir)" &amp;&amp; "$(PythonDir_35_x86)\python.exe" setup.py bdist_wheel --python-tag cp35</Command>
595+
<Command>copy "$(TargetPath)" "$(ProjectDir)src\mi\" &amp;&amp; copy "$(TargetDir)\mi.pdb" "$(ProjectDir)src\mi\" &amp;&amp; cd "$(ProjectDir)" &amp;&amp; "$(PythonDir_35_x86)\python.exe" setup_vs.py bdist_wheel --python-tag cp35</Command>
596596
</PostBuildEvent>
597597
</ItemDefinitionGroup>
598598
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release (Python 3.6)|Win32'">
@@ -614,7 +614,7 @@
614614
<AdditionalDependencies>mi++.lib;mi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
615615
</Link>
616616
<PostBuildEvent>
617-
<Command>copy "$(TargetPath)" "$(ProjectDir)src\mi\" &amp;&amp; copy "$(TargetDir)\mi.pdb" "$(ProjectDir)src\mi\" &amp;&amp; cd "$(ProjectDir)" &amp;&amp; "$(PythonDir_36_x86)\python.exe" setup.py bdist_wheel --python-tag cp35</Command>
617+
<Command>copy "$(TargetPath)" "$(ProjectDir)src\mi\" &amp;&amp; copy "$(TargetDir)\mi.pdb" "$(ProjectDir)src\mi\" &amp;&amp; cd "$(ProjectDir)" &amp;&amp; "$(PythonDir_36_x86)\python.exe" setup_vs.py bdist_wheel --python-tag cp35</Command>
618618
</PostBuildEvent>
619619
</ItemDefinitionGroup>
620620
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release (Python 3.7)|Win32'">
@@ -636,7 +636,7 @@
636636
<AdditionalDependencies>mi++.lib;mi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
637637
</Link>
638638
<PostBuildEvent>
639-
<Command>copy "$(TargetPath)" "$(ProjectDir)src\mi\" &amp;&amp; copy "$(TargetDir)\mi.pdb" "$(ProjectDir)src\mi\" &amp;&amp; cd "$(ProjectDir)" &amp;&amp; "$(PythonDir_37_x86)\python.exe" setup.py bdist_wheel --python-tag cp37</Command>
639+
<Command>copy "$(TargetPath)" "$(ProjectDir)src\mi\" &amp;&amp; copy "$(TargetDir)\mi.pdb" "$(ProjectDir)src\mi\" &amp;&amp; cd "$(ProjectDir)" &amp;&amp; "$(PythonDir_37_x86)\python.exe" setup_vs.py bdist_wheel --python-tag cp37</Command>
640640
</PostBuildEvent>
641641
</ItemDefinitionGroup>
642642
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release (Python 3.4)|Win32'">
@@ -658,7 +658,7 @@
658658
<AdditionalDependencies>mi++.lib;mi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
659659
</Link>
660660
<PostBuildEvent>
661-
<Command>copy "$(TargetPath)" "$(ProjectDir)src\mi\" &amp;&amp; copy "$(TargetDir)\mi.pdb" "$(ProjectDir)src\mi\" &amp;&amp; cd "$(ProjectDir)" &amp;&amp; "$(PythonDir_34_x86)\python.exe" setup.py bdist_wheel --python-tag cp34</Command>
661+
<Command>copy "$(TargetPath)" "$(ProjectDir)src\mi\" &amp;&amp; copy "$(TargetDir)\mi.pdb" "$(ProjectDir)src\mi\" &amp;&amp; cd "$(ProjectDir)" &amp;&amp; "$(PythonDir_34_x86)\python.exe" setup_vs.py bdist_wheel --python-tag cp34</Command>
662662
</PostBuildEvent>
663663
</ItemDefinitionGroup>
664664
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@@ -680,7 +680,7 @@
680680
<AdditionalDependencies>mi++.lib;mi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
681681
</Link>
682682
<PostBuildEvent>
683-
<Command>copy "$(TargetPath)" "$(ProjectDir)src\mi\" &amp;&amp; copy "$(TargetDir)\mi.pdb" "$(ProjectDir)src\mi\" &amp;&amp; cd "$(ProjectDir)" &amp;&amp; "$(PythonDir_27_x64)\python.exe" setup.py bdist_wheel --python-tag cp27</Command>
683+
<Command>copy "$(TargetPath)" "$(ProjectDir)src\mi\" &amp;&amp; copy "$(TargetDir)\mi.pdb" "$(ProjectDir)src\mi\" &amp;&amp; cd "$(ProjectDir)" &amp;&amp; "$(PythonDir_27_x64)\python.exe" setup_vs.py bdist_wheel --python-tag cp27</Command>
684684
</PostBuildEvent>
685685
</ItemDefinitionGroup>
686686
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release (Python 3.5)|x64'">
@@ -702,7 +702,7 @@
702702
<AdditionalDependencies>mi++.lib;mi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
703703
</Link>
704704
<PostBuildEvent>
705-
<Command>copy "$(TargetPath)" "$(ProjectDir)src\mi\" &amp;&amp; copy "$(TargetDir)\mi.pdb" "$(ProjectDir)src\mi\" &amp;&amp; cd "$(ProjectDir)" &amp;&amp; "$(PythonDir_35_x64)\python.exe" setup.py bdist_wheel --python-tag cp35</Command>
705+
<Command>copy "$(TargetPath)" "$(ProjectDir)src\mi\" &amp;&amp; copy "$(TargetDir)\mi.pdb" "$(ProjectDir)src\mi\" &amp;&amp; cd "$(ProjectDir)" &amp;&amp; "$(PythonDir_35_x64)\python.exe" setup_vs.py bdist_wheel --python-tag cp35</Command>
706706
</PostBuildEvent>
707707
</ItemDefinitionGroup>
708708
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release (Python 3.6)|x64'">
@@ -724,7 +724,7 @@
724724
<AdditionalDependencies>mi++.lib;mi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
725725
</Link>
726726
<PostBuildEvent>
727-
<Command>copy "$(TargetPath)" "$(ProjectDir)src\mi\" &amp;&amp; copy "$(TargetDir)\mi.pdb" "$(ProjectDir)src\mi\" &amp;&amp; cd "$(ProjectDir)" &amp;&amp; "$(PythonDir_36_x64)\python.exe" setup.py bdist_wheel --python-tag cp36</Command>
727+
<Command>copy "$(TargetPath)" "$(ProjectDir)src\mi\" &amp;&amp; copy "$(TargetDir)\mi.pdb" "$(ProjectDir)src\mi\" &amp;&amp; cd "$(ProjectDir)" &amp;&amp; "$(PythonDir_36_x64)\python.exe" setup_vs.py bdist_wheel --python-tag cp36</Command>
728728
</PostBuildEvent>
729729
</ItemDefinitionGroup>
730730
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release (Python 3.7)|x64'">
@@ -746,7 +746,7 @@
746746
<AdditionalDependencies>mi++.lib;mi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
747747
</Link>
748748
<PostBuildEvent>
749-
<Command>copy "$(TargetPath)" "$(ProjectDir)src\mi\" &amp;&amp; copy "$(TargetDir)\mi.pdb" "$(ProjectDir)src\mi\" &amp;&amp; cd "$(ProjectDir)" &amp;&amp; "$(PythonDir_37_x64)\python.exe" setup.py bdist_wheel --python-tag cp37</Command>
749+
<Command>copy "$(TargetPath)" "$(ProjectDir)src\mi\" &amp;&amp; copy "$(TargetDir)\mi.pdb" "$(ProjectDir)src\mi\" &amp;&amp; cd "$(ProjectDir)" &amp;&amp; "$(PythonDir_37_x64)\python.exe" setup_vs.py bdist_wheel --python-tag cp37</Command>
750750
</PostBuildEvent>
751751
</ItemDefinitionGroup>
752752
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release (Python 3.4)|x64'">
@@ -768,7 +768,7 @@
768768
<AdditionalDependencies>mi++.lib;mi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
769769
</Link>
770770
<PostBuildEvent>
771-
<Command>copy "$(TargetPath)" "$(ProjectDir)src\mi\" &amp;&amp; copy "$(TargetDir)\mi.pdb" "$(ProjectDir)src\mi\" &amp;&amp; cd "$(ProjectDir)" &amp;&amp; "$(PythonDir_34_x64)\python.exe" setup.py bdist_wheel --python-tag cp34</Command>
771+
<Command>copy "$(TargetPath)" "$(ProjectDir)src\mi\" &amp;&amp; copy "$(TargetDir)\mi.pdb" "$(ProjectDir)src\mi\" &amp;&amp; cd "$(ProjectDir)" &amp;&amp; "$(PythonDir_34_x64)\python.exe" setup_vs.py bdist_wheel --python-tag cp34</Command>
772772
</PostBuildEvent>
773773
</ItemDefinitionGroup>
774774
<ItemGroup>
@@ -823,8 +823,8 @@
823823
</ItemGroup>
824824
<ItemGroup>
825825
<None Include="README.rst" />
826-
<None Include="setup.py" />
827-
<None Include="src\wmi\__init__.py" />
826+
<None Include="setup_vs.py" />
827+
<None Include="..\wmi\__init__.py" />
828828
</ItemGroup>
829829
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
830830
<ImportGroup Label="ExtensionTargets">

PyMI/PyMI.vcxproj.filters

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
</ClCompile>
105105
</ItemGroup>
106106
<ItemGroup>
107-
<None Include="src\wmi\__init__.py">
107+
<None Include="..\wmi\__init__.py">
108108
<Filter>Python</Filter>
109109
</None>
110110
<None Include="setup.py">

PyMI/README.rst

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,69 @@ which provides a simpler and higher level interface over the *mi* API: ::
6868
Build
6969
-----
7070

71+
Use the following to build Python 3 wheels. Those will be copied to the build
72+
dir.
73+
74+
python setup.py bdist_wheel
75+
76+
The best way to build PyMI for Python 2.7 or 3.4 and below is to use the
77+
Visual Studio solution (described below). This will statically link the
78+
vc140 runtime, which is required by PyMI.
79+
80+
Custom VS env vars
81+
^^^^^^^^^^^^^^^^^^
82+
83+
distutils will automatically locate your Visual Studio and Windows SDK
84+
installation. If you'd like to call vcvarsall.bat yourself and use a specific
85+
version, use the following:
86+
87+
function SetVCVars($vcvarsdir, $platform="amd64")
88+
{
89+
pushd $vcvarsdir
90+
try
91+
{
92+
cmd /c "vcvarsall.bat $platform & set" |
93+
foreach {
94+
if ($_ -match "=") {
95+
$v = $_.split("="); set-item -force -path "ENV:\$($v[0])" -value "$($v[1])"
96+
}
97+
}
98+
}
99+
finally
100+
{
101+
popd
102+
}
103+
}
104+
105+
# Replace this folder with the one in which the vcvarsall.bat script is
106+
# located (the exact location depends on the Visual Studio version).
107+
# SetVCVars "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build"
108+
SetVCVars "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC"
109+
110+
$env:DISTUTILS_USE_SDK=1
111+
$env:MSSdk=1
112+
113+
python setup.py bdist_wheel
114+
115+
Debug builds
116+
^^^^^^^^^^^^
117+
118+
The easiest way to do a debug build is to set the following in setup.cfg:
119+
120+
[build]
121+
debug = 1
122+
123+
This will be honored regardless of the build type (e.g. stdist, wheel, etc).
124+
125+
To enable distutils debug logging, you may set the following:
126+
127+
$env:DISTUTILS_DEBUG = 1
128+
129+
Before doing a debug build, you may wish to clean the build dir.
130+
131+
Using the Visual Studio Solution
132+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
133+
71134
Open the provided *PyMI.sln* solution in Visual Studio 2015 [#VS2015]_, choose
72135
your target Python version / platform and build. Wheel packages are
73136
automatically generated in the *dist* folder for release builds.
@@ -79,6 +142,12 @@ e.g. *PythonDir_34_x64*. The *wheel* and *GitPython* packages are required durin
79142
pip install wheel
80143
pip install GitPython
81144

145+
As an alternative, you can use the MSBuild CLI tool:
146+
147+
$env:MSBuildEmitSolution="TRUE"
148+
MSBuild.exe .\PyMI.sln /p:Configuration="Release (Python 3.7)"
149+
150+
82151
References
83152
----------
84153

PyMI/setup.py renamed to PyMI/setup_vs.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
# This module is used by the Visual Studio solution.
2+
13
import git
24
import os
35
from setuptools import setup, find_packages
6+
from setuptools.command import build_clib
47

58

69
def get_git_version():
@@ -21,8 +24,8 @@ def read(fname):
2124
license="Apache 2.0",
2225
keywords="wmi mi windows",
2326
url="https://github.com/cloudbase/PyMI",
24-
packages=find_packages("src", exclude="samples"),
25-
package_dir={'': 'src'},
27+
packages=find_packages("src") + find_packages(".."),
28+
package_dir={'mi': 'src/mi', 'wmi': '../wmi'},
2629
# Note: this extension is compiled in Visual Studio
2730
package_data={'mi': ['mi.pyd', 'mi.pdb']},
2831
long_description=read('README.rst'),

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pbr!=2.1.0,>=2.0.0 # Apache-2.0
2+
six>=1.10.0 # MIT

setup.cfg

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[metadata]
2+
name = PyMI
3+
summary = Windows Management Infrastructure API for Python.
4+
author = Alessandro Pilotti
5+
author-email = info@cloudbasesolutions.com
6+
home-page = https://github.com/cloudbase/PyMI
7+
description-file = PyMI/README.rst
8+
classifier =
9+
Development Status :: 5 - Production/Stable
10+
Programming Language :: Python :: 2
11+
Programming Language :: Python :: 2.7
12+
Programming Language :: Python :: 3
13+
Programming Language :: Python :: 3.4
14+
Programming Language :: Python :: 3.5
15+
Programming Language :: Python :: 3.6
16+
Programming Language :: Python :: 3.7
17+
Environment :: Win32 (MS Windows)
18+
Intended Audience :: Developers
19+
Intended Audience :: System Administrators
20+
License :: OSI Approved :: Apache Software License
21+
Natural Language :: English
22+
Operating System :: Microsoft :: Windows
23+
Topic :: System :: Systems Administration
24+
keywords = wmi mi windows
25+
26+
[files]
27+
# mi.pyd is declared separately as an extension.
28+
packages = wmi

setup.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import os
2+
import setuptools
3+
4+
try:
5+
import multiprocessing # noqa
6+
except ImportError:
7+
pass
8+
9+
# Setuptools requires relative paths
10+
mi_dir = 'MI'
11+
pymi_dir = 'PyMI'
12+
13+
libmipp = (
14+
'mi++',
15+
{'sources': [os.path.join(mi_dir, src) for src in
16+
['MI++.cpp', 'MIExceptions.cpp', 'MIValue.cpp']],
17+
'macros': [('UNICODE', 1), ('_UNICODE', 1)]}
18+
)
19+
pymi_ext = setuptools.Extension(
20+
"mi",
21+
sources=[os.path.join(pymi_dir, src) for src in
22+
['Application.cpp',
23+
'Callbacks.cpp',
24+
'Class.cpp',
25+
'DestinationOptions.cpp',
26+
'Instance.cpp',
27+
'MiError.cpp',
28+
'Operation.cpp',
29+
'OperationOptions.cpp',
30+
'PyMI.cpp',
31+
'Serializer.cpp',
32+
'Session.cpp',
33+
'stdafx.cpp',
34+
'Utils.cpp']],
35+
libraries=['mi++', 'mi', 'kernel32', 'user32', 'gdi32',
36+
'winspool', 'comdlg32', 'advapi32', 'shell32',
37+
'ole32', 'oleaut32', 'uuid', 'odbc32',
38+
'odbccp32'],
39+
include_dirs=[mi_dir],
40+
define_macros=[('UNICODE', 1), ('_UNICODE', 1)],
41+
)
42+
43+
setuptools.setup(
44+
libraries=[libmipp],
45+
ext_modules=[pymi_ext],
46+
setup_requires=['pbr>=2.0.0'],
47+
pbr=True
48+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import mi
2626
from mi import mi_error
27+
import pbr.version
2728

2829
try:
2930
import eventlet
@@ -38,6 +39,14 @@
3839
eventlet = None
3940
EVENTLET_NONBLOCKING_MODE_ENABLED = False
4041

42+
__all__ = ['__version__']
43+
44+
version_info = pbr.version.VersionInfo('PyMI')
45+
try:
46+
__version__ = version_info.version_string()
47+
except AttributeError:
48+
__version__ = None
49+
4150

4251
def avoid_blocking_call(f):
4352
# Performs blocking calls in a different thread using tpool.execute

0 commit comments

Comments
 (0)