Skip to content

Instantly share code, notes, and snippets.

@frantzmiccoli
Last active May 30, 2019 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frantzmiccoli/316465ca03003ed09d95611f8e0f41d1 to your computer and use it in GitHub Desktop.
Save frantzmiccoli/316465ca03003ed09d95611f8e0f41d1 to your computer and use it in GitHub Desktop.
Dirty little script
#!/usr/local/bin/python --
# -*- coding: utf-8 -*-
from paver.easy import sh
import os
import sys
clear_flag = '--clear'
def local_sync():
_check_dependencies()
associations = _get_sources_targets_associations()
should_clear = _should_clear()
_confirm_execution(associations, should_clear)
if should_clear:
_run_clear(associations)
_run_associations(associations)
def _check_dependencies():
programs = ['unison']
missing_programs = []
for program in programs:
command = 'command -v ' + program
output = sh(command, capture=True, ignore_error=True)
if program not in output:
missing_programs.append(program)
if missing_programs:
error = 'Missing programs : ' + ', '.join(missing_programs)
if 'unison' in missing_programs:
error += "\n" + \
'if brew install unison raises problems on ' \
'execution, you should check ' \
'https://github.com/bcpierce00/unison/issues/79'
raise Exception(error)
def _get_sources_targets_associations():
targets_env_variable_prefix = 'LOCAL_SYNC_'
associations = [
_get_association_from_env_variable(value)
for env_variable, value in os.environ.iteritems()
if env_variable.find(targets_env_variable_prefix) == 0
]
if not associations:
error = 'We are unable to find any variable prefixed ' + \
targets_env_variable_prefix + '. Syntax: ' + \
'"export ' + targets_env_variable_prefix + 'S0M3N4ME="path1:path2"'
raise Exception(error)
filters = sys.argv[1:]
if clear_flag in filters:
del filters[filters.index(clear_flag)]
if filters:
pattern = filters.pop()
associations = [
association for association in associations
if pattern in '|'.join(association)
]
if not associations:
raise Exception('No more association after filter: ' + pattern)
return associations
def _get_association_from_env_variable(env_variable):
association_items = env_variable.split(':')
if len(association_items) != 2:
raise Exception('Unable to parse ' + env_variable)
for association_item in association_items:
if not os.path.exists(association_item):
raise Exception(association_item + ' does not seem to exist')
return association_items
def _should_clear():
return clear_flag in sys.argv
def _confirm_execution(associations, clear=None):
if clear is None:
clear = False
message = ''
clear_lines = []
if clear:
clear_lines = [' ==> '.join(association)
for association in associations]
clear_lines_as_string = "\n".join(clear_lines)
message = ('=' * 40) + "\n" + \
'Beware you are about to force a clean up: ' + \
"\n" + ('=' * 40) + "\n" + "\n" + \
clear_lines_as_string + "\n\n"
associations_lines = [' <--> '.join(association)
for association in associations]
associations_as_string = "\n".join(associations_lines)
message += associations_as_string
print(message)
user_instruction = raw_input('Shall we proceed? 🤔 [y/n]')
if user_instruction != 'y':
sys.exit(0)
def _run_clear(associations):
command = ''
for association in associations:
from_path = association[0]
to_path = association[1]
#command += 'rm -rf ' + to_path + ' && ' +\
command += '' +\
'rsync -av --exclude=".git" --exclude="node_modules" ' +\
'--exclude=".idea" --exclude="vendor" '+ \
'--exclude=".paver_targets" ' +\
from_path + '/ ' + to_path + ' && '
command += ' echo "copy done"'
sh(command)
def _run_associations(associations):
command = ''
for association in associations:
command += 'unison ' + '"' + '" "'.join(association) + '" ' + \
'-ignore "Name .git" -ignore "Name node_modules" ' + \
'-ignore "Name vendor" -ignore "Name .idea" ' + \
'-ignore "Name .paver_targets" -repeat watch & '
if not command: #empty string
raise Exception('No unison sync to run')
command += 'wait'
sh(command)
if __name__ == "__main__":
local_sync()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment