Skip to content

Use shutil.copyfile instead of os.system("cp") in packing (Windows portability)#1343

Open
rayair250-droid wants to merge 1 commit into
mosdef-hub:mainfrom
rayair250-droid:fix/packing-portable-copyfile
Open

Use shutil.copyfile instead of os.system("cp") in packing (Windows portability)#1343
rayair250-droid wants to merge 1 commit into
mosdef-hub:mainfrom
rayair250-droid:fix/packing-portable-copyfile

Conversation

@rayair250-droid

Copy link
Copy Markdown

In _run_packmol (mbuild/packing.py), two copies shell out to cp via os.system. cp is a Unix-only command, but mBuild is distributed on conda-forge for win-64, and _check_packmol right below already branches on sys.platform.startswith("win"). On Windows both calls fail, which breaks:

  • the "imperfect packing" fallback that copies <name>_FORCED over the result, and
  • the copy of the packed file to the user-provided temp_file.

They also invoke a shell with interpolated filenames unnecessarily (breaks on paths containing spaces).

Change

Replace both os.system("cp ...") calls with shutil.copyfile(...) — portable, shell-free, and already used elsewhere in this module (shutil is imported at the top). Also drops a no-op single-argument os.path.join(temp_file).

No behavior change on Unix; fixes the code path on Windows. Happy to add a regression test if desired.

The two os.system("cp ...") calls in packing.py fail on Windows (cp is
Unix-only) even though mBuild ships on conda-forge win-64. They also shell
out unnecessarily. shutil is already imported and used in this module, so
replace them with shutil.copyfile for a portable, shell-free copy.
@codecov

codecov Bot commented Jul 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.07%. Comparing base (293124c) to head (9471c0a).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1343   +/-   ##
=======================================
  Coverage   86.07%   86.07%           
=======================================
  Files          52       52           
  Lines        4897     4897           
=======================================
  Hits         4215     4215           
  Misses        682      682           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@chrisjonesBSU

Copy link
Copy Markdown
Contributor

Thanks for the pull request! I agree, using shutil instead of os is better here in general.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants