Skip to content

Commit

Permalink
Fixed #134: Allow specifying a different target Python version.
Browse files Browse the repository at this point in the history
  • Loading branch information
vsajip committed Jun 12, 2020
1 parent c9c986b commit 78231f0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 33 deletions.
7 changes: 4 additions & 3 deletions distlib/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def __init__(self, source_dir, target_dir, add_launchers=True,

self._is_nt = os.name == 'nt' or (
os.name == 'java' and os._name == 'nt')
self.version_info = sys.version_info

def _get_alternate_executable(self, executable, options):
if options.get('gui', False) and self._is_nt: # pragma: no cover
Expand Down Expand Up @@ -293,10 +294,10 @@ def _make_script(self, entry, filenames, options=None):
if '' in self.variants:
scriptnames.add(name)
if 'X' in self.variants:
scriptnames.add('%s%s' % (name, sys.version_info[0]))
scriptnames.add('%s%s' % (name, self.version_info[0]))
if 'X.Y' in self.variants:
scriptnames.add('%s-%s.%s' % (name, sys.version_info[0],
sys.version_info[1]))
scriptnames.add('%s-%s.%s' % (name, self.version_info[0],
self.version_info[1]))
if options and options.get('gui', False):
ext = 'pyw'
else:
Expand Down
5 changes: 5 additions & 0 deletions docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,11 @@ installed as ``foo`` and ``foo-3.2`` under Python 3.2. If the value of the
would be installed as ``foo``, ``foo3`` and ``foo-3.2`` when run under Python
3.2.

.. note:: If you need to generate variants for a different version of Python than the
one running the script, set the ``version_info`` attribute of the
:class:`ScriptMaker` instance to a 2-tuple holding the major and minor version
numbers of the target Python version.

Avoiding overwriting existing scripts
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
67 changes: 37 additions & 30 deletions tests/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,37 +136,44 @@ def test_multiple(self):

def test_generation(self):
self.maker.clobber = True
for name in ('main', 'other_main'):
for options in (None, {}, {'gui': False}, {'gui': True}):
gui = options and options.get('gui', False)
spec = 'foo = foo:' + name
files = self.maker.make(spec, options)
self.assertEqual(len(files), 2)
actual = set()
for f in files:
d, f = os.path.split(f)
actual.add(f)
if os.name == 'nt': # pragma: no cover
if gui:
ext = 'pyw'

def do_test(maker, version_info):
for name in ('main', 'other_main'):
for options in (None, {}, {'gui': False}, {'gui': True}):
gui = options and options.get('gui', False)
spec = 'foo = foo:' + name
files = maker.make(spec, options)
self.assertEqual(len(files), 2)
actual = set()
for f in files:
d, f = os.path.split(f)
actual.add(f)
if os.name == 'nt': # pragma: no cover
if gui:
ext = 'pyw'
else:
ext = 'py'
expected = set(['foo.%s' % ext,
'foo-%s.%s.%s' % (version_info[0],
version_info[1],
ext)])
else:
ext = 'py'
expected = set(['foo.%s' % ext,
'foo-%s.%s.%s' % (sys.version_info[0],
sys.version_info[1],
ext)])
else:
expected = set(['foo', 'foo-%s.%s' % (sys.version_info[0],
sys.version_info[1])])
self.assertEqual(actual, expected)
self.assertEqual(d, self.maker.target_dir)
for fn in files:
with open(fn, 'r') as f:
text = f.read()
# self.assertIn("_resolve('foo', '%s')" % name, text)
if options and options['gui'] and os.name == 'nt': # pragma: no cover
first_line, rest = text.split('\n', 1)
self.assertIn('pythonw', first_line)
expected = set(['foo', 'foo-%s.%s' % (version_info[0],
version_info[1])])
self.assertEqual(actual, expected)
self.assertEqual(d, maker.target_dir)
for fn in files:
with open(fn, 'r') as f:
text = f.read()
# self.assertIn("_resolve('foo', '%s')" % name, text)
if options and options['gui'] and os.name == 'nt': # pragma: no cover
first_line, rest = text.split('\n', 1)
self.assertIn('pythonw', first_line)

do_test(self.maker, sys.version_info)
# See issue 134. Test for specifying the target Python version
self.maker.version_info = version_info = (4, 4) # just any other Python version
do_test(self.maker, version_info)

def test_clobber(self):
files = self.maker.make('foo = foo:main')
Expand Down

0 comments on commit 78231f0

Please sign in to comment.