Skip to content

Commit b2170a1

Browse files
committed
MSVS: revise the vcxproj embedded python script contents
Changes: * Revert setting SCONS_HOME in the testenv in runtest.py. * Simplify the generated embedded python script. * Simplified evaluation order: 1. If scons_home is defined: * If SCons is not found using scons_home, display an error message and exit the embedded python script 2. If scons_abspath is defined: * If SCons is not found using scons_abspath, display an error message and exit the embedded python script 3. Evaluate local library locations plus the python system path: * If SCons is not found using the extended python system path, display an error message and exit the embedded python script 4. Add the found SCons path to the front of the python system path 5. Display the SCons path 6. Import and run SCons/Tool/msvs.py
1 parent 0a5ba77 commit b2170a1

File tree

3 files changed

+30
-96
lines changed

3 files changed

+30
-96
lines changed

SCons/Tool/msvs.py

Lines changed: 13 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,6 @@ def getExecScriptMain(env, xml=None):
177177
if not scons_home and 'SCONS_LIB_DIR' in os.environ:
178178
scons_home = os.environ['SCONS_LIB_DIR']
179179

180-
if scons_home is None:
181-
scons_home = ''
182-
183180
scons_abspath = os.path.abspath(os.path.dirname(os.path.dirname(SCons.__file__)))
184181

185182
def _in_pytree(scons_abspath):
@@ -198,79 +195,29 @@ def _in_pytree(scons_abspath):
198195
in_pytree = _in_pytree(scons_abspath)
199196
# print(f"in_pytree={in_pytree}, scons_abspath=`{scons_abspath}', sys.prefix='{sys.prefix}', sys.exec_prefix='{sys.exec_prefix}'")
200197

201-
if in_pytree:
202-
scons_abspath = ''
203-
204198
if _exec_script_main_template is None:
205-
_exec_script_main_template = "; ".join([line for block in [textwrap.dedent(s).splitlines() for s in [
206-
# Import libraries and functions.
199+
_exec_script_main_template = "; ".join(textwrap.dedent(
207200
"""\
208201
import importlib.util
209202
import sys
210-
from os.path import abspath, dirname, isdir, isfile, join, normcase, realpath
211-
""",
212-
# Initialize the generated paths:
213-
# * convert scons_home to an absolute path,
214-
# * clear scons_abspath when equal to the scons_home absolute path.
215-
"""\
216-
usrinit = lambda p: abspath(p) if p else p
217-
geninit = lambda p, u: '' if (p and u and normcase(p) == normcase(u)) else p
218-
usr_path = usrinit(r'{scons_home}')
219-
gen_path = geninit(r'{scons_abspath}', usr_path)
220-
""",
221-
# Evaluate candidate lists:
222-
# 1. If scons_home is defined:
223-
# * Record scons_home as found iff the path contains SCons.
224-
# * Stop evaluating remaining alternatives.
225-
# 2. If scons_abspath is defined:
226-
# * Record scons_abspath as found iff the path contains SCons.
227-
# * Stop evaluating remaining alternatives.
228-
# 3. Evaluate known library locations:
229-
# * Record the first library path that contains SCons as found.
230-
"""\
231-
state = {{}}
232-
isvalid = lambda p: p and isdir(p) and isfile(join(p, 'SCons', '__init__.py'))
233-
store = lambda k, l, s: {{k: l[0], 'Found': l[0], 'Stop': True}} if l else {{k: '', 'Stop': s}}
234-
check = lambda k, l, s: state.update(store(k, [p for p in l if isvalid(p)], s)) if not state.get('Stop') else None
235-
_ = [check(k, l, s) for k, l, s in [('usr', [usr_path], bool(usr_path)), ('gen', [gen_path], bool(gen_path)), ('lib', [join(sys.prefix, *t) for t in [('Lib', 'site-packages', 'scons-{scons_version}'), ('scons-{scons_version}',), ('Lib', 'site-packages', 'scons'), ('scons',), ('Lib', 'site-packages')]], False)]]
236-
""",
237-
# If an SCons module path was found, add the path to the front of
238-
# the sys.path list.
239-
"""\
240-
path = state.get('Found', '')
241-
_ = sys.path.insert(0, path) if path else None
242-
""",
243-
# Use importlib to find the SCons module path prior to import.
244-
# Add a valid module spec origin path to the front of the sys.path list if:
245-
# * a module path was not found earlier, or
246-
# * the module spec origin path is different than the module
247-
# path found earlier.
248-
"""\
203+
from os.path import abspath, dirname, isdir, isfile, join, realpath
204+
usr_path = r'{scons_home}'
205+
gen_path = r'{scons_abspath}'
206+
syspath = sys.path
207+
search, path = ([usr_path], usr_path) if usr_path else ([gen_path], gen_path) if gen_path else ([join(sys.prefix, *t) for t in [('Lib', 'site-packages', 'scons-{scons_version}'), ('scons-{scons_version}',), ('Lib', 'site-packages', 'scons'), ('scons',), ('Lib', 'site-packages')]] + sys.path, None)
208+
sys.path = search
249209
spec = importlib.util.find_spec('SCons')
250210
orig = dirname(dirname(abspath(spec.origin))) if (spec and spec.origin) else ''
251-
syspath = orig and (not path or normcase(abspath(path)) != normcase(orig))
252-
_ = sys.path.insert(0, orig) if syspath else None
253-
""",
254-
# Display diagnostic messages.
255-
"""\
256-
_ = print(f'proj: *** SCons not found at user path \\\'{{usr_path}}\\\'. ***') if (usr_path and state.get('usr') == '') else None
257-
_ = print(f'proj: *** SCons not found at generated path \\\'{{gen_path}}\\\' ***.') if (gen_path and state.get('gen') == '') else None
258-
_ = print( 'proj: *** SCons not found. ***') if (not orig) else None
259-
""",
260-
# Display SCons module path (if found).
261-
"""\
262-
_ = print(f'proj: Using SCons path \\\'{{orig}}\\\' (realpath=\\\'{{realpath(orig)}}\\\', syspath={{syspath}}).') if (orig) else None
263-
""",
264-
# Import SCons and run main script.
265-
"""\
211+
sys.path = [orig] + syspath if orig else syspath
212+
_ = print(f'proj: Using SCons path \\\'{{orig}}\\\' (realpath=\\\'{{realpath(orig)}}\\\').') if orig else (print(f'proj: Error: SCons not found (path=\\\'{{path if path else search}}\\\').'), sys.exit(1))
266213
import SCons.Script
267214
SCons.Script.main()
268-
""",
269-
]] for line in block])
215+
"""
216+
).splitlines())
270217

271218
exec_script_main = _exec_script_main_template.format(
272-
scons_home=scons_home,
273-
scons_abspath=scons_abspath,
219+
scons_home=scons_home if scons_home else '',
220+
scons_abspath=scons_abspath if not in_pytree else '',
274221
scons_version=SCons.__version__,
275222
)
276223
# print("exec_script_main:\n", ' ' + '\n '.join(exec_script_main.split("; ")))

runtest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,6 @@ def footer(self, f):
540540
# Because SCons is really aggressive about finding its modules,
541541
# it sometimes finds SCons modules elsewhere on the system.
542542
# This forces SCons to use the modules that are being tested.
543-
testenv['SCONS_HOME'] = scons_lib_dir
544543
testenv['SCONS_LIB_DIR'] = scons_lib_dir
545544

546545
if args.scons_exec:

testing/framework/TestSConsMSVS.py

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -842,14 +842,15 @@ def get_exec_script_main(scons_home=None):
842842
global _exec_script_main_template
843843

844844
scons_home = scons_home
845-
if not scons_home and 'SCONS_HOME' in os.environ:
846-
scons_home = os.environ['SCONS_HOME']
845+
# Some tests may fail if SCONS_HOME is defined in the os environment when
846+
# the tests are run. The SCons location set in the user's os environment
847+
# may point to a different SCons than the SCons currently being tested.
848+
# This behavior is consistent with the main branch code at the time.
849+
# if not scons_home and 'SCONS_HOME' in os.environ:
850+
# scons_home = os.environ['SCONS_HOME']
847851
if not scons_home and 'SCONS_LIB_DIR' in os.environ:
848852
scons_home = os.environ['SCONS_LIB_DIR']
849853

850-
if scons_home is None:
851-
scons_home = ''
852-
853854
scons_abspath = os.path.abspath(os.path.dirname(os.path.dirname(SCons.__file__)))
854855

855856
def _in_pytree(scons_abspath):
@@ -868,42 +869,29 @@ def _in_pytree(scons_abspath):
868869
in_pytree = _in_pytree(scons_abspath)
869870
# print(f"in_pytree={in_pytree}, scons_abspath=`{scons_abspath}', sys.prefix='{sys.prefix}', sys.exec_prefix='{sys.exec_prefix}'")
870871

871-
if in_pytree:
872-
scons_abspath = ''
873-
874872
if _exec_script_main_template is None:
875873
_exec_script_main_template = "; ".join(textwrap.dedent(
876874
"""\
877875
import importlib.util
878876
import sys
879-
from os.path import abspath, dirname, isdir, isfile, join, normcase, realpath
880-
usrinit = lambda p: abspath(p) if p else p
881-
geninit = lambda p, u: '' if (p and u and normcase(p) == normcase(u)) else p
882-
usr_path = usrinit(r'{scons_home}')
883-
gen_path = geninit(r'{scons_abspath}', usr_path)
884-
state = {{}}
885-
isvalid = lambda p: p and isdir(p) and isfile(join(p, 'SCons', '__init__.py'))
886-
store = lambda k, l, s: {{k: l[0], 'Found': l[0], 'Stop': True}} if l else {{k: '', 'Stop': s}}
887-
check = lambda k, l, s: state.update(store(k, [p for p in l if isvalid(p)], s)) if not state.get('Stop') else None
888-
_ = [check(k, l, s) for k, l, s in [('usr', [usr_path], bool(usr_path)), ('gen', [gen_path], bool(gen_path)), ('lib', [join(sys.prefix, *t) for t in [('Lib', 'site-packages', 'scons-{scons_version}'), ('scons-{scons_version}',), ('Lib', 'site-packages', 'scons'), ('scons',), ('Lib', 'site-packages')]], False)]]
889-
path = state.get('Found', '')
890-
_ = sys.path.insert(0, path) if path else None
877+
from os.path import abspath, dirname, isdir, isfile, join, realpath
878+
usr_path = r'{scons_home}'
879+
gen_path = r'{scons_abspath}'
880+
syspath = sys.path
881+
search, path = ([usr_path], usr_path) if usr_path else ([gen_path], gen_path) if gen_path else ([join(sys.prefix, *t) for t in [('Lib', 'site-packages', 'scons-{scons_version}'), ('scons-{scons_version}',), ('Lib', 'site-packages', 'scons'), ('scons',), ('Lib', 'site-packages')]] + sys.path, None)
882+
sys.path = search
891883
spec = importlib.util.find_spec('SCons')
892884
orig = dirname(dirname(abspath(spec.origin))) if (spec and spec.origin) else ''
893-
syspath = orig and (not path or normcase(abspath(path)) != normcase(orig))
894-
_ = sys.path.insert(0, orig) if syspath else None
895-
_ = print(f'proj: *** SCons not found at user path \\\'{{usr_path}}\\\'. ***') if (usr_path and state.get('usr') == '') else None
896-
_ = print(f'proj: *** SCons not found at generated path \\\'{{gen_path}}\\\' ***.') if (gen_path and state.get('gen') == '') else None
897-
_ = print( 'proj: *** SCons not found. ***') if (not orig) else None
898-
_ = print(f'proj: Using SCons path \\\'{{orig}}\\\' (realpath=\\\'{{realpath(orig)}}\\\', syspath={{syspath}}).') if (orig) else None
885+
sys.path = [orig] + syspath if orig else syspath
886+
_ = print(f'proj: Using SCons path \\\'{{orig}}\\\' (realpath=\\\'{{realpath(orig)}}\\\').') if orig else (print(f'proj: Error: SCons not found (path=\\\'{{path if path else search}}\\\').'), sys.exit(1))
899887
import SCons.Script
900888
SCons.Script.main()
901-
""",
889+
"""
902890
).splitlines())
903891

904892
exec_script_main = _exec_script_main_template.format(
905-
scons_home=scons_home,
906-
scons_abspath=scons_abspath,
893+
scons_home=scons_home if scons_home else '',
894+
scons_abspath=scons_abspath if not in_pytree else '',
907895
scons_version=SCons.__version__,
908896
)
909897
# print("exec_script_main:\n", ' ' + '\n '.join(exec_script_main.split("; ")))

0 commit comments

Comments
 (0)