def run(self): psync_settings = tools.get_psync_settings() if psync_settings["prompt_for_location"] == False: zip_backup_path = psync_settings["zip_backup_path"] backup_path = None try: if zip_backup_path == "": backup_path = tools.default_zip_backup_path elif os.path.isfile(zip_backup_path): backup_path = zip_backup_path else: sublime.error_message( "Invalid path provided in user-settings. Please correct & then retry." ) backup_path = None except Exception as e: tools.log("PackageSync: Error while fetching backup path.", force=True) tools.log("PackageSync: Error message: %s" % str(e), force=True) self.restore_zip(backup_path) else: offline.prompt_parameters = { "mode": "restore", "type": "file", "window_context": self.window, "initial_text": tools.default_zip_backup_path, "operation_to_perform": self.restore_zip, "on_change": None, "on_cancel": tools.packagesync_cancelled } offline.prompt_for_location()
def find_files(self, path): tools.log("PackageSync: find_files started for %s" % path) include_files = self.psync_settings["include_files"] ignore_files = self.psync_settings["ignore_files"] ignore_dirs = self.psync_settings["ignore_dirs"] # tools.log("PackageSync: path %s" % path) # tools.log("PackageSync: include_files %s" % include_files) # tools.log("PackageSync: ignore_files %s" % ignore_files) # tools.log("PackageSync: ignore_dirs %s" % ignore_dirs) resources = {} for root, dirs, files in os.walk(path): [dirs.remove(dir) for dir in dirs if dir in ignore_dirs] for file in files: absolute_path = os.path.join(root, file) relative_path = os.path.relpath(absolute_path, path) include_matches = [ fnmatch.fnmatch(relative_path, p) for p in include_files] ignore_matches = [ fnmatch.fnmatch(relative_path, p) for p in ignore_files] if any(ignore_matches) or not any(include_matches): continue resources[relative_path] = {"version": os.path.getmtime( absolute_path), "path": absolute_path, "dir": os.path.dirname(relative_path)} return resources
def run(self): sync_interval = self.psync_settings.get("online_sync_interval", 1) # Stop watcher and wait for the poll tools.pause_watcher(local="pull" in self.mode, remote="push" in self.mode) # If no item pull and push all if not self.item: tools.log("PackageSync: Complete sync started.", force=True) # Fetch all items from the remote location if "pull" in self.mode: self.pull_all() # Push all items to the remote location if "push" in self.mode: self.push_all() tools.log("PackageSync: Complete sync done.", force=True) else: # Pull the selected item if "pull" in self.mode: self.pull(self.item) # Push the selected item if "push" in self.mode: self.push(self.item) # Restart watcher again tools.pause_watcher(False, local="pull" in self.mode, remote="push" in self.mode)
def run(self): sync_interval = self.psync_settings.get("online_sync_interval", 1) # Stop watcher and wait for the poll tools.pause_watcher( local="pull" in self.mode, remote="push" in self.mode) # If no item pull and push all if not self.item: tools.log("PackageSync: Complete sync started.", force=True) # Fetch all items from the remote location if "pull" in self.mode: self.pull_all() # Push all items to the remote location if "push" in self.mode: self.push_all() tools.log("PackageSync: Complete sync done.", force=True) else: # Pull the selected item if "pull" in self.mode: self.pull(self.item) # Push the selected item if "push" in self.mode: self.push(self.item) # Restart watcher again tools.pause_watcher( False, local="pull" in self.mode, remote="push" in self.mode)
def create_temp_backup(): psync_settings = tools.get_psync_settings() try: if os.path.exists(tools.temp_backup_folder): shutil.rmtree(tools.temp_backup_folder, True) shutil.copytree(tools.user_settings_folder, tools.temp_backup_folder) for root, dirs, files in os.walk(tools.temp_backup_folder): for dir in dirs: if dir in psync_settings["ignore_dirs"]: shutil.rmtree(os.path.join(root, dir), True) for file in files: absolute_path = os.path.join(root, file) relative_path = os.path.relpath( absolute_path, tools.temp_backup_folder) include_matches = [ fnmatch.fnmatch(relative_path, p) for p in psync_settings["include_files"]] ignore_matches = [ fnmatch.fnmatch(relative_path, p) for p in psync_settings["ignore_files"]] if any(ignore_matches) or not any(include_matches): os.remove(absolute_path) except Exception as e: tools.log("PackageSync: Error while creating temp backup.", force=True) tools.log("PackageSync: Error message: %s" % str(e), force=True)
def create_temp_backup(): psync_settings = tools.get_psync_settings() try: if os.path.exists(tools.temp_backup_folder): shutil.rmtree(tools.temp_backup_folder, True) shutil.copytree(tools.user_settings_folder, tools.temp_backup_folder) for root, dirs, files in os.walk(tools.temp_backup_folder): for dir in dirs: if dir in psync_settings["ignore_dirs"]: shutil.rmtree(os.path.join(root, dir), True) for file in files: absolute_path = os.path.join(root, file) relative_path = os.path.relpath(absolute_path, tools.temp_backup_folder) include_matches = [ fnmatch.fnmatch(relative_path, p) for p in psync_settings["include_files"] ] ignore_matches = [ fnmatch.fnmatch(relative_path, p) for p in psync_settings["ignore_files"] ] if any(ignore_matches) or not any(include_matches): os.remove(absolute_path) except Exception as e: tools.log("PackageSync: Error while creating temp backup.", force=True) tools.log("PackageSync: Error message: %s" % str(e), force=True)
def restore_zip(self, backup_path): if backup_path is not None: try: tools.log( "PackageSync: Restoring package list & user settings from %s" % backup_path, force=True) # Backup PackageSync user settings before restore operation packagesync_settings_backup = os.path.join( tempfile.gettempdir(), str(time.time())) packagesync_settings_original = os.path.join( tools.user_settings_folder, "PackageSync.sublime-settings") # Verify that user setting are present before backing up if os.path.exists(packagesync_settings_original): shutil.copy2(packagesync_settings_original, packagesync_settings_backup) tools.log( "PackageSync: PackageSync.sublime-settings backed up to %s" % packagesync_settings_backup, force=True) if os.path.exists(tools.temp_restore_folder): shutil.rmtree(tools.temp_restore_folder, True) # Extract to temp restore folder & then perform restore operation # as per the preserve setting # # Sublime 2 has Python 2.6.9 as interpreter. So using with zipfile.ZipFile would not work # Therefore explicitly open & close the zip file for operation z = zipfile.ZipFile(backup_path, "r") z.extractall(tools.temp_restore_folder) z.close() offline.restore_from_temp() # Restore PackageSync user settings if they were backed up if os.path.exists( packagesync_settings_backup ) and not os.path.exists(packagesync_settings_original): shutil.copy2(packagesync_settings_backup, packagesync_settings_original) tools.log( "PackageSync: PackageSync.sublime-settings restored from %s" % packagesync_settings_backup, force=True) tools.install_new_packages() except Exception as e: tools.log( "PackageSync: Error while restoring packages from zip file", force=True) tools.log("PackageSync: Error message: %s" % str(e), force=True) else: tools.packagesync_cancelled()
def restore_folder(self, backup_path): if backup_path is not None: try: tools.log( "PackageSync: Restoring package list & user settings from %s" % backup_path, force=True) # Backup PackageSync user settings before restore operation packagesync_settings_backup = os.path.join( tempfile.gettempdir(), str(time.time())) packagesync_settings_original = os.path.join( tools.user_settings_folder, "PackageSync.sublime-settings") # Verify that user setting are present before backing up if os.path.exists(packagesync_settings_original): shutil.copy2(packagesync_settings_original, packagesync_settings_backup) tools.log( "PackageSync: PackageSync.sublime-settings backed up to %s" % packagesync_settings_backup, force=True) if os.path.exists(tools.temp_restore_folder): shutil.rmtree(tools.temp_restore_folder, True) # Copy to temp restore folder & restore as per the preserve # setting shutil.copytree(backup_path, tools.temp_restore_folder) offline.restore_from_temp() # Restore PackageSync user settings if they were backed up if os.path.exists( packagesync_settings_backup ) and not os.path.exists(packagesync_settings_original): shutil.copy2(packagesync_settings_backup, packagesync_settings_original) tools.log( "PackageSync: PackageSync.sublime-settings restored from %s" % packagesync_settings_backup, force=True) tools.install_new_packages() except Exception as e: tools.log( "PackageSync: Error while restoring packages from folder", force=True) tools.log("PackageSync: Error message: %s" % str(e), force=True) else: tools.packagesync_cancelled()
def restore_from_temp(): try: if tools.get_psync_settings()["preserve_packages"] == False: # Delete all existing user settings & restore from temp backup shutil.rmtree(tools.user_settings_folder, True) shutil.copytree(tools.temp_restore_folder, tools.user_settings_folder) else: for src_root, dirs, files in os.walk(tools.temp_restore_folder): dst_root = src_root.replace(tools.temp_restore_folder, tools.user_settings_folder) if not os.path.exists(dst_root): os.mkdir(dst_root) for file in files: src_file = os.path.join(src_root, file) dst_file = os.path.join(dst_root, file) if file == "Package Control.sublime-settings": new_installed_packages = [] with open(src_file, "r") as f: new_installed_packages = json.load( f)["installed_packages"] package_control_settings = sublime.load_settings( "Package Control.sublime-settings") current_installed_packages = package_control_settings.get( "installed_packages") for package_name in new_installed_packages: if package_name not in current_installed_packages: current_installed_packages.append(package_name) package_control_settings.set( "installed_packages", current_installed_packages) sublime.save_settings( "Package Control.sublime-settings") else: if os.path.exists(dst_file): os.remove(dst_file) shutil.move(src_file, dst_root) except Exception as e: tools.log("PackageSync: Error while restoring from backup.", force=True) tools.log("PackageSync: Error message: %s" % str(e), force=True)
def push_all(self): tools.log("PackageSync: push_all started with override = %s" % self.override) local_dir = os.path.join(sublime.packages_path(), "User") remote_dir = self.psync_settings.get("online_sync_folder") local_data = self.find_files(local_dir) remote_data = self.find_files(remote_dir) # Get data of last sync last_run_data = tools.load_last_run_data() last_run_data_local = last_run_data.get("last_run_data_local", {}) last_run_data_remote = last_run_data.get("last_run_data_remote", {}) deleted_local_data = [ key for key in last_run_data_local if key not in local_data ] deleted_remote_data = [ key for key in last_run_data_remote if key not in remote_data ] # tools.log("PackageSync: local_data: %s" % local_data) # tools.log("PackageSync: remote_data: %s" % remote_data) # tools.log("PackageSync: deleted_local_data: %s" % deleted_local_data) # tools.log("PackageSync: deleted_remote_data: %s" % deleted_remote_data) diff = [{ "type": "d", "key": key } for key in last_run_data_local if key not in local_data] for key, value in local_data.items(): if key in deleted_remote_data: pass elif key not in remote_data: diff += [dict({"type": "c", "key": key}, **value)] elif int(value["version"]) > int( remote_data[key]["version"]) or self.override: diff += [dict({"type": "m", "key": key}, **value)] for item in diff: self.push(item) # Set data for next last sync tools.save_last_run_data( last_run_data_local=self.find_files(local_dir), last_run_data_remote=self.find_files(remote_dir))
def restore_from_temp(): try: if tools.get_psync_settings()["preserve_packages"] == False: # Delete all existing user settings & restore from temp backup shutil.rmtree(tools.user_settings_folder, True) shutil.copytree( tools.temp_restore_folder, tools.user_settings_folder) else: for src_root, dirs, files in os.walk(tools.temp_restore_folder): dst_root = src_root.replace( tools.temp_restore_folder, tools.user_settings_folder) if not os.path.exists(dst_root): os.mkdir(dst_root) for file in files: src_file = os.path.join(src_root, file) dst_file = os.path.join(dst_root, file) if file == "Package Control.sublime-settings": new_installed_packages = [] with open(src_file, "r") as f: new_installed_packages = json.load( f)["installed_packages"] package_control_settings = sublime.load_settings( "Package Control.sublime-settings") current_installed_packages = package_control_settings.get( "installed_packages") for package_name in new_installed_packages: if package_name not in current_installed_packages: current_installed_packages.append(package_name) package_control_settings.set( "installed_packages", current_installed_packages) sublime.save_settings( "Package Control.sublime-settings") else: if os.path.exists(dst_file): os.remove(dst_file) shutil.move(src_file, dst_root) except Exception as e: tools.log("PackageSync: Error while restoring from backup.", force=True) tools.log("PackageSync: Error message: %s" % str(e), force=True)
def run(self): psync_settings = tools.get_psync_settings() if psync_settings["prompt_for_location"] == False: list_backup_path = psync_settings["list_backup_path"] backup_path = None try: if list_backup_path == "": backup_path = tools.default_list_backup_path elif os.path.exists(list_backup_path): if sublime.ok_cancel_dialog( "Backup already exists @ %s \nReplace it?" % list_backup_path) == True: os.remove(list_backup_path) backup_path = list_backup_path else: backup_path = None elif os.path.isabs(os.path.dirname(list_backup_path)): backup_path = list_backup_path else: sublime.error_message( "Invalid path provided in user-settings. Please correct & then retry." ) backup_path = None except Exception as e: tools.log("PackageSync: Error while fetching backup path.", force=True) tools.log("PackageSync: Error message: %s" % str(e), force=True) self.backup_pkg_list(backup_path) else: offline.prompt_parameters = { "mode": "backup", "type": "file", "window_context": self.window, "initial_text": tools.default_list_backup_path, "operation_to_perform": self.backup_pkg_list, "on_change": None, "on_cancel": tools.packagesync_cancelled } offline.prompt_for_location()
def push_all(self): tools.log("PackageSync: push_all started with override = %s" % self.override) local_dir = os.path.join(sublime.packages_path(), "User") remote_dir = self.psync_settings.get("online_sync_folder") local_data = self.find_files(local_dir) remote_data = self.find_files(remote_dir) # Get data of last sync last_run_data = tools.load_last_run_data() last_run_data_local = last_run_data.get("last_run_data_local", {}) last_run_data_remote = last_run_data.get("last_run_data_remote", {}) deleted_local_data = [ key for key in last_run_data_local if key not in local_data] deleted_remote_data = [ key for key in last_run_data_remote if key not in remote_data] # tools.log("PackageSync: local_data: %s" % local_data) # tools.log("PackageSync: remote_data: %s" % remote_data) # tools.log("PackageSync: deleted_local_data: %s" % deleted_local_data) # tools.log("PackageSync: deleted_remote_data: %s" % deleted_remote_data) diff = [{"type": "d", "key": key} for key in last_run_data_local if key not in local_data] for key, value in local_data.items(): if key in deleted_remote_data: pass elif key not in remote_data: diff += [dict({"type": "c", "key": key}, **value)] elif int(value["version"]) > int(remote_data[key]["version"]) or self.override: diff += [dict({"type": "m", "key": key}, **value)] for item in diff: self.push(item) # Set data for next last sync tools.save_last_run_data( last_run_data_local=self.find_files(local_dir), last_run_data_remote=self.find_files(remote_dir))
def run(self, mode=["pull", "push"], override=False): # Load settings settings = sublime.load_settings("PackageSync.sublime-settings") # Check for valid online_sync_folder if not os.path.isdir(settings.get("online_sync_folder")): sublime.error_message( "Invalid path provided for online_sync_folder in PackageSync settings. Online syncing has been turned off. Please correct and retry.\n %s" % settings.get("online_sync_folder")) settings.set("online_sync_enabled", False) sublime.save_settings("PackageSync.sublime-settings") return # Check if sync is already running if not sync_queue.has("sync_online"): t = online.Sync(mode, override) sync_queue.add(t, "sync_online") else: tools.log("PackageSync: Sync operation already running.", force=True)
def pull_package_control(self, last_run_data, previous_installed_packages, installed_packages): # Save items to remove to_install = [ item for item in installed_packages if item not in previous_installed_packages] to_remove = [ item for item in previous_installed_packages if item not in installed_packages] tools.log("PackageSync: install: %s", to_install) tools.log("PackageSync: remove: %s", to_remove) # Check for old remove_packages packages_to_remove = last_run_data.get("packages_to_remove", []) packages_to_remove += [item for item in to_remove if item != "Package Control" and item not in packages_to_remove] tools.log("PackageSync: packages_to_remove %s", packages_to_remove) if packages_to_remove: removed_packages = tools.remove_packages(packages_to_remove) else: removed_packages = [] # Check if new packages are available and run package cleanup to # install missing packages if to_install: sublime.set_timeout(tools.install_new_packages(), 1000) tools.save_last_run_data( packages_to_remove=[item for item in packages_to_remove if item not in removed_packages])
def backup_pkg_list(self, backup_path): if backup_path is not None: try: package_control_settings = sublime.load_settings( "Package Control.sublime-settings") installed_packages = package_control_settings.get( "installed_packages") or [] if os.path.isfile(backup_path): os.remove(backup_path) elif not os.path.exists(os.path.dirname(backup_path)): os.makedirs(os.path.dirname(backup_path)) with open(backup_path, "w") as _backupFile: json.dump({"installed_packages": installed_packages}, _backupFile, indent=4) tools.log( "PackageSync: Backup of installed packages list created at %s" % backup_path) except Exception as e: tools.log( "PackageSync: Error while backing up installed packages list", force=True) tools.log("PackageSync: Error message: %s" % str(e), force=True) else: tools.packagesync_cancelled()
def find_files(self, path): tools.log("PackageSync: find_files started for %s" % path) include_files = self.psync_settings["include_files"] ignore_files = self.psync_settings["ignore_files"] ignore_dirs = self.psync_settings["ignore_dirs"] # tools.log("PackageSync: path %s" % path) # tools.log("PackageSync: include_files %s" % include_files) # tools.log("PackageSync: ignore_files %s" % ignore_files) # tools.log("PackageSync: ignore_dirs %s" % ignore_dirs) resources = {} for root, dirs, files in os.walk(path): [dirs.remove(dir) for dir in dirs if dir in ignore_dirs] for file in files: absolute_path = os.path.join(root, file) relative_path = os.path.relpath(absolute_path, path) include_matches = [ fnmatch.fnmatch(relative_path, p) for p in include_files ] ignore_matches = [ fnmatch.fnmatch(relative_path, p) for p in ignore_files ] if any(ignore_matches) or not any(include_matches): continue resources[relative_path] = { "version": os.path.getmtime(absolute_path), "path": absolute_path, "dir": os.path.dirname(relative_path) } return resources
def backup_folder(self, backup_path): if backup_path is not None: try: offline.create_temp_backup() if os.path.isdir(backup_path): shutil.rmtree(backup_path, True) shutil.copytree(tools.temp_backup_folder, backup_path) tools.log( "PackageSync: Backup of packages & settings created at %s" % backup_path) except Exception as e: tools.log( "PackageSync: Error while backing up packages to folder", force=True) tools.log("PackageSync: Error message: %s" % str(e), force=True) else: tools.packagesync_cancelled()
def backup_zip(self, backup_path): if backup_path is not None: try: offline.create_temp_backup() temp_zip_file_path = os.path.join(tempfile.gettempdir(), str(time.time())) + ".zip" # create temp backup zip from the temp backup z = zipfile.ZipFile(temp_zip_file_path, 'w') basePath = tools.temp_backup_folder.rstrip("\\/") + "" basePath = basePath.rstrip("\\/") for root, dirs, files in os.walk(tools.temp_backup_folder): for item in (files + dirs): itemPath = os.path.join(root, item) inZipPath = itemPath.replace(basePath, "", 1).lstrip("\\/") z.write(itemPath, inZipPath) # close the temp backup file z.close() if os.path.isfile(backup_path): os.remove(backup_path) elif not os.path.exists(os.path.dirname(backup_path)): os.makedirs(os.path.dirname(backup_path)) shutil.move(temp_zip_file_path, backup_path) tools.log( "PackageSync: Zip backup of packages & settings created at %s" % backup_path) except Exception as e: tools.log( "PackageSync: Error while backing up packages to zip file", force=True) tools.log("PackageSync: Error message: %s" % str(e), force=True) else: tools.packagesync_cancelled()
def pull_package_control(self, last_run_data, previous_installed_packages, installed_packages): # Save items to remove to_install = [ item for item in installed_packages if item not in previous_installed_packages ] to_remove = [ item for item in previous_installed_packages if item not in installed_packages ] tools.log("PackageSync: install: %s", to_install) tools.log("PackageSync: remove: %s", to_remove) # Check for old remove_packages packages_to_remove = last_run_data.get("packages_to_remove", []) packages_to_remove += [ item for item in to_remove if item != "Package Control" and item not in packages_to_remove ] tools.log("PackageSync: packages_to_remove %s", packages_to_remove) if packages_to_remove: removed_packages = tools.remove_packages(packages_to_remove) else: removed_packages = [] # Check if new packages are available and run package cleanup to # install missing packages if to_install: sublime.set_timeout(tools.install_new_packages(), 1000) tools.save_last_run_data(packages_to_remove=[ item for item in packages_to_remove if item not in removed_packages ])
def restore_pkg_list(self, backup_path): if backup_path is not None: try: tools.log("PackageSync: Restoring package list from %s" % backup_path) with open(backup_path, "r") as f: _installed_packages = json.load(f)["installed_packages"] _package_control_settings = sublime.load_settings( "Package Control.sublime-settings") _package_control_settings.set("installed_packages", _installed_packages) sublime.save_settings("Package Control.sublime-settings") tools.install_new_packages() except Exception as e: tools.log( "PackageSync: Error while restoring packages from package list", force=True) tools.log("PackageSync: Error message: %s" % str(e), force=True) else: tools.packagesync_cancelled()
def pull(self, item): tools.log("PackageSync: pull started for %s" % item) local_dir = os.path.join(sublime.packages_path(), "User") remote_dir = self.psync_settings.get("sync_folder") # Get data of last sync last_run_data = tools.load_last_run_data() last_run_data_local = last_run_data.get("last_run_data_local", {}) last_run_data_remote = last_run_data.get("last_run_data_remote", {}) # Make target file path and directory target = os.path.join(local_dir, item["key"]) target_dir = os.path.dirname(target) # TODO -- Added for error mitigation but theoretically this was not needed # Verify why the error is occuring for these variables try: previous_installed_packages installed_packages except NameError: previous_installed_packages = [] installed_packages = [] # Skip if file was just pushed try: if item["type"] == "c" or item["type"] == "m": # Check for an updated Package Control setting file and backup # old file if item["key"] == "Package Control.sublime-settings": previous_installed_packages = tools.load_installed_packages( target) installed_packages = tools.load_installed_packages( item["path"]) # Check if the watcher detects a file again if last_run_data_local[item["key"]]["version"] == item["version"]: # tools.log("PackageSync: Already pulled") return except: pass # If a file was created if item["type"] == "c": if not os.path.isdir(target_dir): os.makedirs(target_dir) shutil.copy2(item["path"], target) tools.log("PackageSync: Created %s" % target) # last_run_data_local[item["key"]] = { "path": target, "dir": item["dir"], "version": item["version"]} last_run_data_remote[item["key"]] = { "path": item["path"], "dir": item["dir"], "version": item["version"]} # If a file was delated elif item["type"] == "d": if os.path.isfile(target): os.remove(target) tools.log("PackageSync: Deleted %s" % target) try: del last_run_data_local[item["key"]] del last_run_data_remote[item["key"]] except: pass # Check if directory is empty and remove it if, just cosmetic issue if os.path.isdir(target_dir) and not os.listdir(target_dir): os.rmdir(target_dir) # If a file was modified elif item["type"] == "m": if not os.path.isdir(target_dir): os.mkdir(target_dir) shutil.copy2(item["path"], target) tools.log("PackageSync: Updated %s" % target) # last_run_data_local[item["key"]] = { "path": target, "dir": item["dir"], "version": item["version"]} last_run_data_remote[item["key"]] = { "path": item["path"], "dir": item["dir"], "version": item["version"]} # Set data for next last sync tools.save_last_run_data( last_run_data_local=last_run_data_local, last_run_data_remote=last_run_data_remote) if item["type"] != "d" and item["key"] == "Package Control.sublime-settings": # Handle Package Control self.pull_package_control( last_run_data, previous_installed_packages, installed_packages)
def push(self, item): tools.log("PackageSync: push started for %s" % item) local_dir = os.path.join(sublime.packages_path(), "User") remote_dir = self.psync_settings.get("online_sync_folder") # Get data of last sync last_run_data = tools.load_last_run_data() last_run_data_local = last_run_data.get("last_run_data_local", {}) last_run_data_remote = last_run_data.get("last_run_data_remote", {}) # Skip if file was just copied try: if item["type"] == "c" or item["type"] == "m": if last_run_data_remote[item["key"]]["version"] == item["version"]: tools.log("PackageSync: Already pushed") return except: pass # Make target file path and dir target = os.path.join(remote_dir, item["key"]) target_dir = os.path.dirname(target) if item["type"] == "c": if not os.path.isdir(target_dir): os.makedirs(target_dir) shutil.copy2(item["path"], target) tools.log("PackageSync: Created %s" % target) # last_run_data_local[item["key"]] = { "path": item["path"], "dir": item["dir"], "version": item["version"]} last_run_data_remote[item["key"]] = { "path": target, "dir": item["dir"], "version": item["version"]} elif item["type"] == "d": if os.path.isfile(target): os.remove(target) tools.log("PackageSync: Deleted %s" % target) try: del last_run_data_local[item["key"]] del last_run_data_remote[item["key"]] except: pass # Check if dir is empty and remove it if if os.path.isdir(target_dir) and not os.listdir(target_dir): os.rmdir(target_dir) elif item["type"] == "m": if not os.path.isdir(target_dir): os.mkdir(target_dir) shutil.copy2(item["path"], target) tools.log("PackageSync: Updated %s" % target) # last_run_data_local[item["key"]] = { "path": item["path"], "dir": item["dir"], "version": item["version"]} last_run_data_remote[item["key"]] = { "path": target, "dir": item["dir"], "version": item["version"]} # Set data for next last sync tools.save_last_run_data( last_run_data_local=last_run_data_local, last_run_data_remote=last_run_data_remote)
def push(self, item): tools.log("PackageSync: push started for %s" % item) local_dir = os.path.join(sublime.packages_path(), "User") remote_dir = self.psync_settings.get("online_sync_folder") # Get data of last sync last_run_data = tools.load_last_run_data() last_run_data_local = last_run_data.get("last_run_data_local", {}) last_run_data_remote = last_run_data.get("last_run_data_remote", {}) # Skip if file was just copied try: if item["type"] == "c" or item["type"] == "m": if last_run_data_remote[ item["key"]]["version"] == item["version"]: tools.log("PackageSync: Already pushed") return except: pass # Make target file path and dir target = os.path.join(remote_dir, item["key"]) target_dir = os.path.dirname(target) if item["type"] == "c": if not os.path.isdir(target_dir): os.makedirs(target_dir) shutil.copy2(item["path"], target) tools.log("PackageSync: Created %s" % target) # last_run_data_local[item["key"]] = { "path": item["path"], "dir": item["dir"], "version": item["version"] } last_run_data_remote[item["key"]] = { "path": target, "dir": item["dir"], "version": item["version"] } elif item["type"] == "d": if os.path.isfile(target): os.remove(target) tools.log("PackageSync: Deleted %s" % target) try: del last_run_data_local[item["key"]] del last_run_data_remote[item["key"]] except: pass # Check if dir is empty and remove it if if os.path.isdir(target_dir) and not os.listdir(target_dir): os.rmdir(target_dir) elif item["type"] == "m": if not os.path.isdir(target_dir): os.mkdir(target_dir) shutil.copy2(item["path"], target) tools.log("PackageSync: Updated %s" % target) # last_run_data_local[item["key"]] = { "path": item["path"], "dir": item["dir"], "version": item["version"] } last_run_data_remote[item["key"]] = { "path": target, "dir": item["dir"], "version": item["version"] } # Set data for next last sync tools.save_last_run_data(last_run_data_local=last_run_data_local, last_run_data_remote=last_run_data_remote)
def pull(self, item): tools.log("PackageSync: pull started for %s" % item) local_dir = os.path.join(sublime.packages_path(), "User") remote_dir = self.psync_settings.get("sync_folder") # Get data of last sync last_run_data = tools.load_last_run_data() last_run_data_local = last_run_data.get("last_run_data_local", {}) last_run_data_remote = last_run_data.get("last_run_data_remote", {}) # Make target file path and directory target = os.path.join(local_dir, item["key"]) target_dir = os.path.dirname(target) # TODO -- Added for error mitigation but theoretically this was not needed # Verify why the error is occuring for these variables try: previous_installed_packages installed_packages except NameError: previous_installed_packages = [] installed_packages = [] # Skip if file was just pushed try: if item["type"] == "c" or item["type"] == "m": # Check for an updated Package Control setting file and backup # old file if item["key"] == "Package Control.sublime-settings": previous_installed_packages = tools.load_installed_packages( target) installed_packages = tools.load_installed_packages( item["path"]) # Check if the watcher detects a file again if last_run_data_local[ item["key"]]["version"] == item["version"]: # tools.log("PackageSync: Already pulled") return except: pass # If a file was created if item["type"] == "c": if not os.path.isdir(target_dir): os.makedirs(target_dir) shutil.copy2(item["path"], target) tools.log("PackageSync: Created %s" % target) # last_run_data_local[item["key"]] = { "path": target, "dir": item["dir"], "version": item["version"] } last_run_data_remote[item["key"]] = { "path": item["path"], "dir": item["dir"], "version": item["version"] } # If a file was delated elif item["type"] == "d": if os.path.isfile(target): os.remove(target) tools.log("PackageSync: Deleted %s" % target) try: del last_run_data_local[item["key"]] del last_run_data_remote[item["key"]] except: pass # Check if directory is empty and remove it if, just cosmetic issue if os.path.isdir(target_dir) and not os.listdir(target_dir): os.rmdir(target_dir) # If a file was modified elif item["type"] == "m": if not os.path.isdir(target_dir): os.mkdir(target_dir) shutil.copy2(item["path"], target) tools.log("PackageSync: Updated %s" % target) # last_run_data_local[item["key"]] = { "path": target, "dir": item["dir"], "version": item["version"] } last_run_data_remote[item["key"]] = { "path": item["path"], "dir": item["dir"], "version": item["version"] } # Set data for next last sync tools.save_last_run_data(last_run_data_local=last_run_data_local, last_run_data_remote=last_run_data_remote) if item["type"] != "d" and item[ "key"] == "Package Control.sublime-settings": # Handle Package Control self.pull_package_control(last_run_data, previous_installed_packages, installed_packages)
def run(self, item): tools.log("PsyncOnlinePushItemCommand %s", item) # Start a thread to push the current item t = online.Sync(mode=["push"], item=item) sync_queue.add(t)