# Buggy code
def main():
    """CLI Entrypoint"""
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "--downloads-cache",
        type=Path,
        metavar="PATH",
        default="../../downloads_cache",
        help="The path to the downloads cache",
    )
    parser.add_argument(
        "--disable-ssl-verification",
        action="store_true",
        help="Disables SSL verification for downloading",
    )
    parser.add_argument(
        "--7z-path",
        dest="sevenz_path",
        default=SEVENZIP_USE_REGISTRY,
        help=(
            'Command or path to 7-Zip\'s "7z" binary. If "_use_registry" is '
            "specified, determine the path from the registry. Default: %(default)s"
        ),
    )
    args = parser.parse_args()

    # Set common variables
    bundle_path = Path(__file__).parent / "config_bundles/windows"
    bundle = buildkit.config.ConfigBundle(bundle_path)
    source_tree = Path(__file__).resolve().parent.parent
    domsubcache = Path(__file__).parent / "domsubcache.tar.gz"

    # Test environment
    _test_python2(parser.error)

    # Setup environment
    if not args.downloads_cache.exists():
        args.downloads_cache.mkdir()
    _make_tmp_paths()

    # Retrieve downloads
    get_logger().info("Downloading required files...")
    buildkit.downloads.retrieve_downloads(
        bundle, args.downloads_cache, True, args.disable_ssl_verification
    )
    try:
        buildkit.downloads.check_downloads(bundle, args.downloads_cache)
    except buildkit.downloads.HashMismatchError as exc:
        get_logger().error("File checksum does not match: %s", exc)
        parser.exit(1)

    # Unpack downloads
    extractors = {
        ExtractorEnum.SEVENZIP: args.sevenz_path,
    }
    get_logger().info("Unpacking downloads...")
    buildkit.downloads.unpack_downloads(
        bundle, args.downloads_cache, source_tree, extractors
    )

    # Prune binaries
    unremovable_files = buildkit.extraction.prune_dir(source_tree, bundle.pruning)
    if unremovable_files:
        get_logger().error("Files could not be pruned: %s", unremovable_files)
        parser.exit(1)

    # Apply patches
    buildkit.patches.apply_patches(
        buildkit.patches.patch_paths_by_bundle(bundle), source_tree, patch_bin_path=None
    )

    # Substitute domains
    buildkit.domain_substitution.apply_substitution(bundle, source_tree, domsubcache)

    # Output args.gn
    (source_tree / "out/Default").mkdir(parents=True)
    (source_tree / "out/Default/args.gn").write_text(
        "\n".join(bundle.gn_flags), encoding=ENCODING
    )

    # Run GN bootstrap
    _run_build_process(
        shutil.which("python"),
        "tools\\gn\\bootstrap\\bootstrap.py",
        "-oout\\Default\\gn.exe",
    )

    # Run gn gen
    _run_build_process(
        "out\\Default\\gn.exe", "gen", "out\\Default", "--fail-on-unused-args"
    )

    # Run ninja
    _run_build_process(
        "third_party\\ninja\\ninja.exe", "-C", "out\\Default", "chrome", "chromedriver"
    )


# Traceback (subprocess.CalledProcessError)
osuse-leap-42-3-2018:/home/intika/chromlast/ungoogled-chromium/build/src # ./tools/gn/bootstrap/bootstrap.py -o out/Default/gn
ninja: Entering directory `/home/intika/chromlast/ungoogled-chromium/build/src/out/Release/gn_build'
[171/171] LINK gn
ERROR at //build/config/sysroot.gni:57:5: Assertion failed.
assert(
^-----
Missing sysroot (//build/linux/debian_sid_amd64-sysroot). To fix, run: build/linux/sysroot_scripts/install-sysroot.py --arch=amd64
See //build/config/sysroot.gni:58:9:
exec_script("//build/dir_exists.py",
^-----------------------------------
This is where it was set.
See //chrome/installer/BUILD.gn:7:1: whence it was imported.
import("//build/config/sysroot.gni")
^----------------------------------
See //BUILD.gn:67:5: which caused the file to be included.
"//chrome/installer",
^-------------------
Traceback (most recent call last):
File "./tools/gn/bootstrap/bootstrap.py", line 101, in <module>
sys.exit(main(sys.argv[1:]))
File "./tools/gn/bootstrap/bootstrap.py", line 96, in main
'--args=%s' % gn_gen_args, "--root=" + SRC_ROOT
File "/usr/lib64/python2.7/subprocess.py", line 190, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['out/Default/gn', 'gen', '/home/intika/chromlast/ungoogled-chromium/build/src/out/Release', '--args= is_debug=false', '--root=/home/intika/chromlast/ungoogled-chromium/build/src']' returned non-zero exit status 1
osuse-leap-42-3-2018:/home/intika/chromlast/ungoogled-chromium/build/src # ./tools/gn/bootstrap/bootstrap.py -o out/Default/gn
ninja: Entering directory `/home/intika/chromlast/ungoogled-chromium/build/src/out/Release/gn_build'
ninja: no work to do.
ERROR at //build/config/sysroot.gni:57:5: Assertion failed.
assert(
^-----
Missing sysroot (//build/linux/debian_sid_amd64-sysroot). To fix, run: build/linux/sysroot_scripts/install-sysroot.py --arch=amd64
See //build/config/sysroot.gni:58:9:
exec_script("//build/dir_exists.py",
^-----------------------------------
This is where it was set.
See //chrome/installer/BUILD.gn:7:1: whence it was imported.
import("//build/config/sysroot.gni")
^----------------------------------
See //BUILD.gn:67:5: which caused the file to be included.
"//chrome/installer",
^-------------------
Traceback (most recent call last):
File "./tools/gn/bootstrap/bootstrap.py", line 101, in <module>
sys.exit(main(sys.argv[1:]))
File "./tools/gn/bootstrap/bootstrap.py", line 96, in main
'--args=%s' % gn_gen_args, "--root=" + SRC_ROOT
File "/usr/lib64/python2.7/subprocess.py", line 190, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['out/Default/gn', 'gen', '/home/intika/chromlast/ungoogled-chromium/build/src/out/Release', '--args= is_debug=false', '--root=/

# Fixed code
def main():
    """CLI Entrypoint"""
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "--downloads-cache",
        type=Path,
        metavar="PATH",
        default="../../downloads_cache",
        help="The path to the downloads cache",
    )
    parser.add_argument(
        "--disable-ssl-verification",
        action="store_true",
        help="Disables SSL verification for downloading",
    )
    parser.add_argument(
        "--7z-path",
        dest="sevenz_path",
        default=SEVENZIP_USE_REGISTRY,
        help=(
            'Command or path to 7-Zip\'s "7z" binary. If "_use_registry" is '
            "specified, determine the path from the registry. Default: %(default)s"
        ),
    )
    args = parser.parse_args()

    # Set common variables
    bundle_path = Path(__file__).parent / "config_bundles/windows"
    bundle = buildkit.config.ConfigBundle(bundle_path)
    source_tree = Path(__file__).resolve().parent.parent
    domsubcache = Path(__file__).parent / "domsubcache.tar.gz"

    # Test environment
    _test_python2(parser.error)

    # Setup environment
    if not args.downloads_cache.exists():
        args.downloads_cache.mkdir()
    _make_tmp_paths()

    # Retrieve downloads
    get_logger().info("Downloading required files...")
    buildkit.downloads.retrieve_downloads(
        bundle, args.downloads_cache, True, args.disable_ssl_verification
    )
    try:
        buildkit.downloads.check_downloads(bundle, args.downloads_cache)
    except buildkit.downloads.HashMismatchError as exc:
        get_logger().error("File checksum does not match: %s", exc)
        parser.exit(1)

    # Unpack downloads
    extractors = {
        ExtractorEnum.SEVENZIP: args.sevenz_path,
    }
    get_logger().info("Unpacking downloads...")
    buildkit.downloads.unpack_downloads(
        bundle, args.downloads_cache, source_tree, extractors
    )

    # Prune binaries
    unremovable_files = buildkit.extraction.prune_dir(source_tree, bundle.pruning)
    if unremovable_files:
        get_logger().error("Files could not be pruned: %s", unremovable_files)
        parser.exit(1)

    # Apply patches
    buildkit.patches.apply_patches(
        buildkit.patches.patch_paths_by_bundle(bundle), source_tree, patch_bin_path=None
    )

    # Substitute domains
    buildkit.domain_substitution.apply_substitution(bundle, source_tree, domsubcache)

    # Output args.gn
    (source_tree / "out/Default").mkdir(parents=True)
    (source_tree / "out/Default/args.gn").write_text(
        "\n".join(bundle.gn_flags), encoding=ENCODING
    )
    (source_tree / "out/gn_build").mkdir(parents=True)
    (source_tree / "out/gn_build/args.gn").write_text(
        "\n".join(bundle.gn_flags), encoding=ENCODING
    )

    # Run GN bootstrap
    _run_build_process(
        shutil.which("python"),
        "tools\\gn\\bootstrap\\bootstrap.py",
        "-oout\\Default\\gn.exe",
        "--build-path",
        "out\\gn_build",
    )
    shutil.rmtree("out\\gn_build")

    # Run gn gen
    _run_build_process(
        "out\\Default\\gn.exe", "gen", "out\\Default", "--fail-on-unused-args"
    )

    # Run ninja
    _run_build_process(
        "third_party\\ninja\\ninja.exe", "-C", "out\\Default", "chrome", "chromedriver"
    )