Skip to content

Commit

Permalink
Bugfix: support the case where the user-site dir doesn't exist becaus…
Browse files Browse the repository at this point in the history
…e 'pip install --user' wasn't run on the installment. Also extend startup timeout due to Win11 slower startup times. Also supporting python3.exe use-case for firewall disablement inside target sandbox
  • Loading branch information
karkason authored and yiftachkarkason committed Jun 26, 2024
1 parent 85d82fd commit 24a6dfe
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
long_description = f.read()

setup(name='pywinsandbox',
version='1.3.0',
version='1.4.0',
description=u"Python Utilities for Windows Sandbox",
long_description=long_description,
long_description_content_type='text/markdown',
Expand Down
2 changes: 2 additions & 0 deletions winsandbox/config/config_genereator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def _format_folder_mappers(folder_mappers, tag, text):


def generate_config_file(config):
config.folder_mappers = list(filter(lambda m: m.path().exists(), config.folder_mappers))

document, tag, text = yattag.Doc().tagtext()

with tag('Configuration'):
Expand Down
8 changes: 6 additions & 2 deletions winsandbox/session/online_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ def _get_logon_script(self, server_address_path):
remote_python_path = sys.executable.replace(str(PythonMapper().path()), str(
shared_folder_path_in_sandbox(PythonMapper().path())))

remote_user_site_packages_path = shared_folder_path_in_sandbox(PythonUserSitePackagesMapper().path())
user_site_path = PythonUserSitePackagesMapper().path()
if user_site_path.exists():
remote_user_site_packages_path = shared_folder_path_in_sandbox(user_site_path)
else:
remote_user_site_packages_path = ''

# Launch the target script.
commands.append(r'{} {} --disable-firewall {} {}'.format(
Expand Down Expand Up @@ -119,7 +123,7 @@ def configure_sandbox(self):
self.sandbox.config.logon_script = self._get_logon_script(self.server_address_path_in_sandbox)
self.sandbox.config.folder_mappers.extend(extra_folder_mappers)

def connect_to_sandbox(self, timeout=30):
def connect_to_sandbox(self, timeout=60):
"""
Connect to the sandbox sever.
Waits for the creation of the shared server address file, and checks that it responds.
Expand Down
22 changes: 13 additions & 9 deletions winsandbox/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@

def enable_python_incoming_firewall():
# Using `{sys.base_exec_prefix}\python.exe` instead of `sys.executable` to support venvs.
subprocess.run(
'netsh advfirewall firewall add rule name=AllowPythonServer '
'dir=in action=allow enable=yes program="{}\\python.exe"'.format(Path(sys.base_exec_prefix).resolve()),
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
for python_executable in ['python.exe', 'python3.exe']:
subprocess.run(
'netsh advfirewall firewall add rule name=AllowPythonServer '
'dir=in action=allow enable=yes program="{}\\{}"'.format(Path(sys.base_exec_prefix).resolve(), python_executable),
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)


def get_ip_address():
Expand All @@ -35,14 +36,17 @@ def main():
parser.add_argument("address_path",
type=lambda p: Path(p))
parser.add_argument("custom_user_site_packages",
type=str)
type=str,
default='',
nargs='?')
parser.add_argument("--disable-firewall", '-f', default=False, action='store_true')
args = parser.parse_args()

if args.disable_firewall:
enable_python_incoming_firewall()

site.addsitedir(args.custom_user_site_packages)
if args.custom_user_site_packages:
site.addsitedir(args.custom_user_site_packages)

import rpyc.utils.server

Expand Down

0 comments on commit 24a6dfe

Please sign in to comment.