Skip to content

Fix CLI mode crash when -o/--output or -t/--target is missing - #1964

Open
mayuriphad wants to merge 4 commits into
hacksider:mainfrom
mayuriphad:cli-default-output
Open

mayuriphad wants to merge 4 commits into
hacksider:mainfrom
mayuriphad:cli-default-output

Conversation

@mayuriphad

@mayuriphad mayuriphad commented Oct 25, 2026 •

Copy link
Copy Markdown

Fixes #1962

Running in CLI mode without -o/--output crashes during argument parsing:

$ python run.py -s source.jpg -t target.mp4
  File "modules/utilities.py", line 240, in normalize_output_path
    if os.path.isdir(output_path):
TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType

Changes:

  • normalize_output_path: when -s and -t are given but -o is not, the output now defaults to <source>-<target><ext> in the target's directory. That's the same name the code already builds when -o is a directory. An explicit -o file or directory works as before.
  • parse_args: passing only some of -s/-t/-o (for example only -o) also switches to CLI mode, and start() then failed on has_image_extension(None). It now exits with an argparse usage error: CLI mode needs both -s/--source and -t/--target. Starting with no arguments still opens the GUI.

Tests:

  • tests/test_utilities.py: normalize_output_path with no output, an output directory and an output file.
  • tests/test_core_parse_args.py: the usage error for -o only and -s only, plus the valid CLI and GUI cases. It reuses the import stubs from test_core_map_faces_fallback.py.

The new tests fail on main and pass with the change. pytest tests/ passes, except test_ui_live_preview.py, which needs cv2 and isn't installed on my machine. ruff check is clean.

Summary by Sourcery

Harden CLI argument validation and output-path handling for source, target, and enhancer workflows.

Bug Fixes:

  • Prevent CLI argument parsing from crashing when output is omitted or required source/target arguments are missing.
  • Default omitted output paths to a safe generated filename beside the target, including enhancer-only runs.

Enhancements:

  • Allow enhancer-only CLI runs without a source path while enforcing source requirements for face swapping.
  • Preserve explicit output files and generate collision-free names when the default output already exists.

Tests:

  • Add coverage for CLI validation, GUI fallback, enhancer-only execution, and output-path generation and preservation.

normalize_output_path called os.path.isdir(None) when -s and -t were
given without -o, so 'python run.py -s face.jpg -t clip.mp4' died with
a TypeError. Default the output to <source>-<target><ext> next to the
target, the same name the directory case already uses.

Passing only some of -s/-t/-o also switches to CLI mode, where start()
then failed on a None target. Report a usage error instead.

Fixes hacksider#1962

Signed-off-by: mayuriphad <mayuriphad656@gmail.com>
Copilot AI balanced review requested due to automatic review settings October 25, 2026 05:35
@sourcery-ai

sourcery-ai Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Implementation

Update output path normalization to support a missing output, enhancer-only naming, and unused suffixed names while preserving explicit output paths.

  • modules/utilities.py L236-259

Validate CLI target and face-swapper source requirements during argument parsing while allowing enhancer-only runs without a source.

  • modules/core.py L126-132

Tests

Add tests for CLI argument validation and GUI fallback, plus default, collision-safe, directory, and explicit-file output path behavior.

  • tests/test_core_parse_args.py L1-59
  • tests/test_utilities.py L5, L78-132

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="modules/utilities.py" line_range="240-244" />
<code_context>
     if source_path and target_path:
         source_name, _ = os.path.splitext(os.path.basename(source_path))
         target_name, target_extension = os.path.splitext(os.path.basename(target_path))
+        if not output_path:
+            # No -o/--output: write next to the target, named like the directory case.
+            return os.path.join(
+                os.path.dirname(target_path), source_name + "-" + target_name + target_extension
+            )
         if os.path.isdir(output_path):
             return os.path.join(
</code_context>
<issue_to_address>
**Concurrent outputs are corrupted**

When two CLI processes run concurrently with different sources and the same target, omitting `-o/--output`, `get_temp_output_path(target_path)` returns the same target-keyed temporary path for both processes, so their frame extraction or `temp.mp4` encoding overwrites or consumes the other process’s intermediates before each moves to its distinct default output, leaving one or both outputs corrupted or missing.

Use unique temporary paths for each process so their frame extraction and encoding intermediates cannot collide.
</issue_to_address>

### Comment 2
<location path="tests/test_utilities.py" line_range="100-101" />
<code_context>
+        utilities = _load_utilities_module()
+
+        with mock.patch.object(utilities.os.path, "isdir", return_value=True):
+            self.assertEqual(
+                utilities.normalize_output_path("source.jpg", "target.mp4", "out"),
+                utilities.os.path.join("out", "source-target.mp4"),
+            )
+
+    def test_output_file_is_kept(self) -> None:
+        utilities = _load_utilities_module()
+
+        self.assertEqual(
+            utilities.normalize_output_path("source.jpg", "target.mp4", "result.mp4"),
+            "result.mp4",
+        )
</code_context>
<issue_to_address>
**Explicit-file test depends on cwd**

When a directory named `result.mp4` exists in the test's current working directory, in `test_output_file_is_kept`, the call at `tests/test_utilities.py:101` treats `result.mp4` as a directory when one exists in the working directory, so the equality at line 102 fails even though the function correctly handles directory outputs.

Run this check in a temporary directory where `result.mp4` is known not to be a directory, or mock `os.path.isdir` to return false.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 2 findings to address first, and when -o is omitted, the generated output is placed beside the target using a predictable source-target filename; if a file with that name already exists, processing may overwrite it. Reverting restores the old path behavior but cannot recover the overwritten file.

Blocking findings: modules/utilities.py:244, tests/test_utilities.py:101


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread modules/utilities.py Resolved
Comment thread tests/test_utilities.py Resolved
Signed-off-by: mayuriphad <mayuriphad656@gmail.com>
@mayuriphad

Copy link
Copy Markdown
Author

Hi, just checking whether anything else is needed from my side on this PR. Happy to make changes if so.

@noahximus

Copy link
Copy Markdown
Collaborator

This looks good overall, but the new check blocks enhancer-only runs like:

python run.py --frame-processor face_enhancer -t target.jpg -o enhanced.jpg

Enhancers don’t need a source image. Could you require -s only when face_swapper is selected and add a test for this case? The GPEN enhancers are affected too.

Enhancer-only CLI runs (face_enhancer, face_enhancer_gpen256,
face_enhancer_gpen512) do not use a source image. Require -t/--target
in CLI mode and -s/--source only for face_swapper. Without a source
the default output name is <target>-enhanced, so the target is never
overwritten.

Signed-off-by: mayuriphad <mayuriphad656@gmail.com>
@mayuriphad

Copy link
Copy Markdown
Author

Thanks, good catch. Changed in 6e19293:

  • CLI mode now requires -t/--target, and -s/--source only when face_swapper is in --frame-processor. python run.py --frame-processor face_enhancer -t target.jpg -o enhanced.jpg runs again, and so do the two GPEN enhancers.
  • Without a source, the default output (no -o, or -o set to a directory) is <target>-enhanced<ext>, so an enhancer-only run never overwrites the target.
  • Added tests for enhancer-only runs (all three enhancers), for face_swapper without -s, and for the enhancer default output name.

@sourcery-ai

sourcery-ai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Linked issues

Checked at commit 70f8258

#1962 CLI mode without -o/--output crashes with TypeError in normalize_output_path · 2 of 2 met

# Requirement Status Notes
1 Handle CLI runs with -s and -t but no -o without crashing. ✅ Met When output is omitted, normalize_output_path generates the source-target name next to the target.
2 Report a clean error when -o is passed without a target. ✅ Met parse_args calls argparse's usage error when CLI mode has no target.

@kier007

kier007 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

the cli crash is fixed, but omitting -o still silently overwrites a file if the generated output name is already taken, could you pick an unused name or exit with a clear error and add a test that the existing file stays intact?

Without -o/--output the generated output name could already exist next
to the target, and the run silently replaced it. Add a -1, -2, ...
suffix until the name is unused.

Signed-off-by: mayuriphad <mayuriphad656@gmail.com>
@mayuriphad

Copy link
Copy Markdown
Author

Good point, changed in 70f8258: without -o, if <source>-<target><ext> (or <target>-enhanced<ext>) already exists next to the target, the output gets a -1, -2, ... suffix until the name is unused. An explicit -o file still writes where it is told. Added a test that creates the taken file, checks the new name, and checks the existing file's content is unchanged.

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.

CLI mode without -o/--output crashes with TypeError in normalize_output_path

4 participants