<commit_before>import os
import subprocess

import sublime
import sublime_plugin


class SublimePackageSyncAllCommand(sublime_plugin.ApplicationCommand):

    def __init__(self):
        self.settings = None

    def run(self, autorun=False):
        print("[SublimePackageSync] Syncing all packages.")
        packages_to_sync = self.get_setting("sync_repos")
        installed_packages = os.listdir(sublime.packages_path())
        for package_name in packages_to_sync:
            try:
                if autorun and package_name in self.get_setting("auto_sync_ignore"):
                    continue
                package = packages_to_sync.get(package_name)
                remotes = package.get("remotes")
                package_path = os.path.join(
                    sublime.packages_path(), package_name)
                if package_name not in installed_packages:
                    print("[SublimePackageSync] Cloning " + package_name + ".")
                    remotes = self.git_clone(remotes, package_path)
                if self.is_git_repo(package_path):
                    print("[SublimePackageSync] Updating " +
                          package_name + ".")
                    existing_remotes = self.git_remote_show(package_path)
                    if not self.git_remotes_check(remotes, existing_remotes, package_path):
                        print("[SublimePackageSync] ERROR - Mismatched remote." +
                              " Check your remote settings.")
                        return
                    self.git_remotes_add(
                        remotes, existing_remotes, package_path)
                    self.git_fetch(package_path)
                    self.git_checkout(
                        package.get("object_to_checkout"), package_path)
                    self.git_submodule_update(package_path)
            except SublimePackageSyncGitError:
                print(
                    "[SublimePackageSync] ERROR - Git command failed. See output above.")
        print("[SublimePackageSync] Done!")

    def description(self):
        # TODO: Add a description here.
        return None

    def get_setting(self, key):
        if self.settings is None:
            self.settings = sublime.load_settings(
                "SublimePackageSync.sublime-settings")
        return self.settings.get(key)

    def is_git_repo(self, cwd):
        process = subprocess.Popen(["git", "rev-parse", "--git-dir"],
                                   cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        if process.returncode != 0:
            print(stdoutdata.decode("utf-8"), end="")
            print(stderrdata.decode("utf-8"), end="")
            raise SublimePackageSyncGitError
        return cwd == os.path.dirname(os.path.join(cwd, stdoutdata.decode("utf-8").strip().split("\n")[0]))

    def git_clone(self, remotes, path):
        if "origin" in remotes:
            remote = remotes.pop("origin")
        else:<commit_msg>Stop the command from blocking the main thread.<commit_after>import os
import subprocess

import sublime
import sublime_plugin


class SublimePackageSyncAllCommand(sublime_plugin.ApplicationCommand):

    def __init__(self):
        self.settings = None

    def run(self, autorun=False):
        sublime.set_timeout_async(lambda: self.sync(autorun), 0)

    def description(self):
        # TODO: Add a description here.
        return None

    def sync(self, autorun):
        print("[SublimePackageSync] Syncing all packages.")
        packages_to_sync = self.get_setting("sync_repos")
        installed_packages = os.listdir(sublime.packages_path())
        for package_name in packages_to_sync:
            try:
                if autorun and package_name in self.get_setting("auto_sync_ignore"):
                    continue
                package = packages_to_sync.get(package_name)
                remotes = package.get("remotes")
                package_path = os.path.join(
                    sublime.packages_path(), package_name)
                if package_name not in installed_packages:
                    print("[SublimePackageSync] Cloning " + package_name + ".")
                    remotes = self.git_clone(remotes, package_path)
                if self.is_git_repo(package_path):
                    print("[SublimePackageSync] Updating " +
                          package_name + ".")
                    existing_remotes = self.git_remote_show(package_path)
                    if not self.git_remotes_check(remotes, existing_remotes, package_path):
                        print("[SublimePackageSync] ERROR - Mismatched remote." +
                              " Check your remote settings.")
                        return
                    self.git_remotes_add(
                        remotes, existing_remotes, package_path)
                    self.git_fetch(package_path)
                    self.git_checkout(
                        package.get("object_to_checkout"), package_path)
                    self.git_submodule_update(package_path)
            except SublimePackageSyncGitError:
                print(
                    "[SublimePackageSync] ERROR - Git command failed. See output above.")
        print("[SublimePackageSync] Done!")

    def get_setting(self, key):
        if self.settings is None:
            self.settings = sublime.load_settings(
                "SublimePackageSync.sublime-settings")
        return self.settings.get(key)

    def is_git_repo(self, cwd):
        process = subprocess.Popen(["git", "rev-parse", "--git-dir"],
                                   cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        if process.returncode != 0:
            print(stdoutdata.decode("utf-8"), end="")
            print(stderrdata.decode("utf-8"), end="")
            raise SublimePackageSyncGitError
        return cwd == os.path.dirname(os.path.join(cwd, stdoutdata.decode("utf-8").strip().split("\n")[0]))

    def git_clone(self, remotes, path):
        if "origin" in remotes:
            remote = remotes.pop("origin")
        else:
{'fraction_non_alphanumeric': 0.09415741187831965, 'fraction_numerical': 0.0017704812489940447, 'mean_word_length': 1.936672967863894, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2809154', 'n_tokens_mistral': 1719, 'n_tokens_neox': 1645, 'n_words': 326}