Last active
February 13, 2025 12:22
-
-
Save ayr-ton/aff2bbacc465ef1ea833986bc2ff0d98 to your computer and use it in GitHub Desktop.
Sync workspaces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import subprocess | |
import argparse | |
SYNC_SERVER = "" | |
SYNC_DIRS = { | |
"~/workspace": "workspace", | |
} | |
def expand_paths(sync_dirs): | |
"""Expands paths in the SYNC_DIRS dictionary.""" | |
expanded_dirs = {} | |
for src, dest in sync_dirs.items(): | |
expanded_src = os.path.expanduser(src) | |
expanded_dirs[expanded_src] = dest | |
return expanded_dirs | |
def enter_directory(directory): | |
"""Changes to the specified directory. Handles errors.""" | |
try: | |
os.chdir(directory) | |
except FileNotFoundError: | |
print(f"Error: Directory '{directory}' not found.") | |
exit(1) | |
except OSError as e: | |
print(f"Error changing directory to '{directory}': {e}") | |
exit(1) | |
def leave_directory(previous_dir): | |
"""Returns to the previous directory.""" | |
os.chdir(previous_dir) | |
def sync_external(destination_server, local_dir, remote_dir): | |
"""Syncs a local directory to a remote server.""" | |
try: | |
command = [ | |
"rsync", "-zav", | |
"--filter=:- .gitignore", | |
"--progress", | |
"--delete", | |
f"{local_dir}/", | |
f"{destination_server}:{remote_dir}/" | |
] | |
subprocess.run(command, check=True) | |
except subprocess.CalledProcessError as e: | |
print(f"Error during sync_external (local: {local_dir}, remote: {remote_dir}): {e}") | |
exit(1) | |
except FileNotFoundError: | |
print("Error: rsync command not found. Please ensure it's installed.") | |
exit(1) | |
def sync_local(local_dir, remote_dir): | |
"""Syncs a remote directory to a local directory.""" | |
try: | |
command = [ | |
"rsync", "-zav", | |
"--filter=:- .gitignore", | |
"--progress", | |
"--delete", | |
f"{SYNC_SERVER}:{remote_dir}/", | |
f"{local_dir}/" | |
] | |
subprocess.run(command, check=True) | |
except subprocess.CalledProcessError as e: | |
print(f"Error during sync_local (local: {local_dir}, remote: {remote_dir}): {e}") | |
exit(1) | |
except FileNotFoundError: | |
print("Error: rsync command not found. Please ensure it's installed.") | |
exit(1) | |
def sync_directories(destination_server, sync_dirs): | |
previous_dir = os.getcwd() | |
for local_dir, remote_dir in sync_dirs.items(): | |
enter_directory(local_dir) | |
if destination_server is None: | |
sync_local(local_dir, remote_dir) | |
else: | |
sync_external(destination_server, local_dir, remote_dir) | |
leave_directory(previous_dir) | |
def main(): | |
parser = argparse.ArgumentParser(description="Sync local directories with a remote server.") | |
parser.add_argument("destination", nargs='?', default=None, help="Destination server for syncing. If omitted, syncs from the remote server.") | |
args = parser.parse_args() | |
expanded_sync_dirs = expand_paths(SYNC_DIRS) | |
sync_directories(args.destination, expanded_sync_dirs) | |
if __name__ == "__main__": | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SYNC_SERVER="" | |
function enter_workspace() { | |
cd ~/workspace | |
} | |
function leave_workspace() { | |
cd - | |
} | |
function sync_external() { | |
rsync -zav --filter=':- .gitignore' --progress --delete ./ $1:workspace/ | |
} | |
function sync_local() { | |
rsync -zav --filter=':- .gitignore' --progress --delete $SYNC_SERVER:workspace/ ./ | |
} | |
function main() { | |
enter_workspace | |
if [ "$#" -eq "0" ] | |
then | |
sync_local | |
else | |
sync_external $1 | |
fi | |
leave_workspace | |
} | |
main $1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment