Skip to content

Commit 928bad9

Browse files
author
Alexander Smishlajev
committed
If the target platform is win32, create .bat files...
...instead of *nix shell scripts. Target platform is set to "win32" if main command is 'bdist_wininst' or if the command is 'bdist' and it has the list of formats (from command line or config file) and the first item on that list is wininst. Otherwise target platform is set to current (build) platform.
1 parent 24cc8b4 commit 928bad9

File tree

1 file changed

+76
-17
lines changed

1 file changed

+76
-17
lines changed

setup.py

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1717
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1818
#
19-
# $Id: setup.py,v 1.68 2004-05-18 19:46:35 a1s Exp $
19+
# $Id: setup.py,v 1.69 2004-05-26 10:00:53 a1s Exp $
2020

2121
from distutils.core import setup, Extension
2222
from distutils.util import get_platform
@@ -52,9 +52,76 @@ class (due to the delayed instantiation of command classes
5252
5353
The mangling of script names replaces '-' and '/' characters
5454
with '-' and '.', so that they are valid module paths.
55+
56+
If the target platform is win32, create .bat files instead of
57+
*nix shell scripts. Target platform is set to "win32" if main
58+
command is 'bdist_wininst' or if the command is 'bdist' and
59+
it has the list of formats (from command line or config file)
60+
and the first item on that list is wininst. Otherwise
61+
target platform is set to current (build) platform.
5562
"""
5663
package_name = None
5764

65+
def initialize_options(self):
66+
build_scripts.initialize_options(self)
67+
self.script_preamble = None
68+
self.target_platform = None
69+
self.python_executable = None
70+
71+
def finalize_options(self):
72+
build_scripts.finalize_options(self)
73+
cmdopt=self.distribution.command_options
74+
75+
# find the target platform
76+
if self.target_platform:
77+
# TODO? allow explicit setting from command line
78+
target = self.target_platform
79+
if "bdist_wininst" in cmdopt:
80+
target = "win32"
81+
elif "formats" in cmdopt.get("bdist", {}):
82+
formats = cmdopt["bdist"]["formats"][1].split(",")
83+
if formats[0] == "wininst":
84+
target = "win32"
85+
else:
86+
target = sys.platform
87+
if len(formats) > 1:
88+
self.warn(
89+
"Scripts are built for %s only (requested formats: %s)"
90+
% (target, ",".join(formats)))
91+
else:
92+
# default to current platform
93+
target = sys.platform
94+
self.target_platfom = target
95+
96+
# for native builds, use current python executable path;
97+
# for cross-platform builds, use default executable name
98+
if self.python_executable:
99+
# TODO? allow command-line option
100+
pass
101+
if target == sys.platform:
102+
self.python_executable = os.path.normpath(sys.executable)
103+
else:
104+
self.python_executable = "python"
105+
106+
# for windows builds, add ".bat" extension
107+
if target == "win32":
108+
# *nix-like scripts may be useful also on win32 (cygwin)
109+
# to build both script versions, use:
110+
#self.scripts = list(self.scripts) + [script + ".bat"
111+
# for script in self.scripts]
112+
self.scripts = [script + ".bat" for script in self.scripts]
113+
114+
# tweak python path for installations outside main python library
115+
if "prefix" in cmdopt.get("install", {}):
116+
prefix = cmdopt['install']['prefix'][1]
117+
version = '%d.%d'%sys.version_info[:2]
118+
self.script_preamble = '''
119+
import sys
120+
sys.path.insert(1, "%s/lib/python%s/site-packages")
121+
'''%(prefix, version)
122+
else:
123+
self.script_preamble = ''
124+
58125
def copy_scripts(self):
59126
""" Create each script listed in 'self.scripts'
60127
"""
@@ -78,29 +145,23 @@ def copy_scripts(self):
78145

79146
module = os.path.splitext(os.path.basename(script))[0]
80147
module = string.translate(module, to_module)
81-
cmdopt=self.distribution.command_options
82-
if (cmdopt.has_key('install') and
83-
cmdopt['install'].has_key('prefix')):
84-
prefix = cmdopt['install']['prefix'][1]
85-
version = '%d.%d'%sys.version_info[:2]
86-
prefix = '''
87-
import sys
88-
sys.path.insert(1, "%s/lib/python%s/site-packages")
89-
'''%(prefix, version)
90-
else:
91-
prefix = ''
92148
script_vars = {
93-
'python': os.path.normpath(sys.executable),
149+
'python': self.python_executable,
94150
'package': self.package_name,
95151
'module': module,
96-
'prefix': prefix,
152+
'prefix': self.script_preamble,
97153
}
98154

99155
self.announce("creating %s" % outfile)
100156
file = open(outfile, 'w')
101157

102158
try:
103-
if sys.platform == "win32":
159+
# could just check self.target_platform,
160+
# but looking at the script extension
161+
# makes it possible to build both *nix-like
162+
# and windows-like scripts on win32.
163+
# may be useful for cygwin.
164+
if os.path.splitext(outfile)[1] == ".bat":
104165
file.write('@echo off\n'
105166
'if NOT "%%_4ver%%" == "" "%(python)s" -O -c "from %(package)s.scripts.%(module)s import run; run()" %%$\n'
106167
'if "%%_4ver%%" == "" "%(python)s" -O -c "from %(package)s.scripts.%(module)s import run; run()" %%*\n'
@@ -125,8 +186,6 @@ def scriptname(path):
125186
"""
126187
script = os.path.splitext(os.path.basename(path))[0]
127188
script = string.replace(script, '_', '-')
128-
if sys.platform == "win32":
129-
script = script + ".bat"
130189
return script
131190

132191
### Build Roundup

0 commit comments

Comments
 (0)