Skip to content

Instantly share code, notes, and snippets.

@openp2pdesign
Last active August 29, 2015 14:14
Show Gist options
  • Save openp2pdesign/f77052c891afd4a3e87b to your computer and use it in GitHub Desktop.
Save openp2pdesign/f77052c891afd4a3e87b to your computer and use it in GitHub Desktop.
Fab Academy Git + Hg sync
# -*- encoding: utf-8 -*-
#
# Author: Massimo Menichinelli
# Homepage: http://www.openp2pdesign.org
# License: MIT
#
import subprocess
import os
import string
from time import sleep
# Adapted from: http://www.andrew-seaford.co.uk/generate-safe-filenames-using-python/
## Make a file name that only contains safe charaters
# @param inputFilename A filename containing illegal characters
# @return A filename containing only safe characters
def makeSafeFilename(inputFilename):
# Set here the valid chars
safechars = string.letters + string.digits + "~ -_."
try:
return filter(lambda c: c in safechars, inputFilename)
except:
return ""
pass
# Clear the screen
os.system('cls' if os.name=='nt' else 'clear')
# Variables
mercurial_local_repo = "/Users/massimo/Documents/FabAcademy2015/europe"
git_local_repo = [
"/Users/massimo/Documents/FabAcademy2015/FabAcademy2015-Opendot",
"/Users/massimo/Documents/FabAcademy2015/FabAcademy2015-WeMake"
]
users_directories = []
user_names = []
users_main_directories = []
# Welcome
print
print "Fab Academy 2015 Git + Hg sync"
# Git pull to update the local copy from GitHub
for i in git_local_repo:
print
print "Git pull for",i
try:
print subprocess.check_output(["git","pull","origin"], cwd=i)
except subprocess.CalledProcessError as e:
print "There was an error... please retry. Error code:",e.returncode
exit()
# Check each filename in subfolders for illegal name and rename it
for i in git_local_repo:
for root, dirs, files in os.walk(i):
for name in files:
checkname = makeSafeFilename(name)
if name != checkname:
print "In:",root
print "There was an error with",name
if os.name == "nt":
os.rename(root+"\\"+name, root+"\\"+checkname)
else:
os.rename(root+"/"+name, root+"/"+checkname)
print name,"has been renamed to", checkname
# Load the main subdirectories of the users
for i in git_local_repo:
for k in os.walk(i).next()[1]:
if ".git" in k:
# We are not interested in the .git folder of the git project
pass
else:
users_main_directories.append(i+"/"+k)
user_names.append(k.replace(i,""))
# Load all the subdirectories of the users and their sub
for i in git_local_repo:
sub = [x[0] for x in os.walk(i)]
for k in sub:
if k == i:
# We are not interested in the root folder of the git project
pass
elif ".git" in k:
# We are not interested in the .git folder of the git project
pass
else:
# We are interested in this
users_directories.append(k)
# Mercurial Pull + Update
print
print "Hg pull+update for",mercurial_local_repo
try:
print subprocess.check_output(["hg","pull"], cwd=mercurial_local_repo)
except subprocess.CalledProcessError as e:
print "There was an error... please retry. Error code:",e.returncode
exit()
try:
print subprocess.check_output(["hg","update"], cwd=mercurial_local_repo)
except subprocess.CalledProcessError as e:
print "There was an error... please retry. Error code:",e.returncode
exit()
# For each user, check the file sizes and ask for confirmation for sync
print
print "Checking file sizes..."
for k,i in enumerate(users_main_directories):
print "Checking",i
try:
print subprocess.check_output(["du","-s","*","|","sort","-rn","|","head"],cwd=i, shell=True)
except subprocess.CalledProcessError as e:
print "There was an error... please retry. Error code:",e.returncode
exit()
# Give time for checking the output
print
progress = raw_input("Would you like to sync this user? y/n: ")
if progress == "n" or progress == "N":
print "User not updated."
else:
print "Syncing",user_names[k], i
destfolder = mercurial_local_repo+"/students/"+user_names[k]
try:
print subprocess.check_output(["rsync","-rtv","--delete",i+"/",destfolder])
except subprocess.CalledProcessError as e:
print "There was an error... please retry. Error code:",e.returncode
exit()
# Hg addremove
print
print "Hg addremove for",mercurial_local_repo
try:
print subprocess.check_output(["hg","addremove"], cwd=mercurial_local_repo)
except subprocess.CalledProcessError as e:
print "There was an error... please retry. Error code:",e.returncode
exit()
# Hg status with full path
print
print "Hg check status",mercurial_local_repo
try:
print subprocess.check_output(["hg","status"], cwd=mercurial_local_repo)
except subprocess.CalledProcessError as e:
print "There was an error... please retry. Error code:",e.returncode
exit()
# Give time for checking the output
print
progress = raw_input("Would you like to continue? y/n: ")
if progress == "n" or progress == "N":
exit()
else:
pass
# Commit in the Mercurial repo
print
print "Hg commit for",mercurial_local_repo
message = raw_input("Which message would you like to leave with this commit? ")
try:
print subprocess.check_output(["hg","commit","-m",message], cwd=mercurial_local_repo)
except subprocess.CalledProcessError as e:
print "There was an error... please retry. Error code:",e.returncode
exit()
# Push from the Mercurial repo
print
print "Hg push for",mercurial_local_repo
try:
print subprocess.check_output(["hg","push"], cwd=mercurial_local_repo)
except subprocess.CalledProcessError as e:
print "There was an error... please retry. Error code:",e.returncode
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment