Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that command wrapping only runs on Windows #3056

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmake/macros/Public.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function(pxr_python_bin BIN_NAME)
COMMENT "Creating Python cmd wrapper"
COMMAND
${PYTHON_EXECUTABLE}
${PROJECT_SOURCE_DIR}/cmake/macros/shebang.py
${PROJECT_SOURCE_DIR}/cmake/macros/winpycmd.py
${BIN_NAME}
${outfile}.cmd
)
Expand Down
27 changes: 9 additions & 18 deletions cmake/macros/shebang.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,18 @@
#
# Usage:
# shebang shebang-str source.py dest.py
# shebang file output.cmd
#
# The former substitutes '/pxrpythonsubst' in <source.py> with <shebang-str>
# and writes the result to <dest.py>. The latter generates a file named
# <output.cmd> with the contents '@python "%~dp0<file>"'; this is a
# Windows command script to execute "python <file>" where <file> is in
# the same directory as the command script.
# This substitutes '/pxrpythonsubst' in <source.py> with <shebang-str>
# and writes the result to <dest.py>.

from __future__ import print_function
import sys

if len(sys.argv) < 3 or len(sys.argv) > 4:
print("Usage: %s {shebang-str source.py dest|file output.cmd}" % sys.argv[0])
if len(sys.argv) != 4:
print(f"Usage: {sys.argv[0]} {{shebang-str source.py dest}}",
file=sys.stderr)
sys.exit(1)

if len(sys.argv) == 3:
with open(sys.argv[2], 'w') as f:
print('@python "%%~dp0%s" %%*' % (sys.argv[1], ), file=f)

else:
with open(sys.argv[2], 'r') as s:
with open(sys.argv[3], 'w') as d:
for line in s:
d.write(line.replace('/pxrpythonsubst', sys.argv[1]))
with open(sys.argv[2], 'r') as s:
with open(sys.argv[3], 'w') as d:
for line in s:
d.write(line.replace('/pxrpythonsubst', sys.argv[1]))
44 changes: 44 additions & 0 deletions cmake/macros/winpycmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#
# Copyright 2017 Pixar
#
# Licensed under the Apache License, Version 2.0 (the "Apache License")
# with the following modification; you may not use this file except in
# compliance with the Apache License and the following modification to it:
# Section 6. Trademarks. is deleted and replaced with:
#
# 6. Trademarks. This License does not grant permission to use the trade
# names, trademarks, service marks, or product names of the Licensor
# and its affiliates, except as required to comply with Section 4(c) of
# the License and to reproduce the content of the NOTICE file.
#
# You may obtain a copy of the Apache License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the Apache License with the above modification is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the Apache License for the specific
# language governing permissions and limitations under the Apache License.
#
#
# Usage:
# wrappy file output.cmd
#
# This generates a file named <output.cmd> with the contents
# '@python "%~dp0<file>"'; this is a Windows command script to execute
# "python <file>" where <file> is in the same directory as the command script.

import sys
import platform
import warnings

if not platform.system() == 'Windows':
warnings.warn(f"{sys.argv[0]} should only be run on Windows")

if len(sys.argv) != 3:
print(f"Usage: {sys.argv[0]} {{file output.cmd}}", file=sys.stderr)
sys.exit(1)

with open(sys.argv[2], 'w') as f:
print(f'@python "%~dp0{sys.argv[1]}" %*', file=f)