Skip to content

Instantly share code, notes, and snippets.

@wez

wez/migrate.py Secret

Last active June 11, 2017 16:16
Show Gist options
  • Save wez/81fcfdd90e5ae9cbc6fd614beacdf1fe to your computer and use it in GitHub Desktop.
Save wez/81fcfdd90e5ae9cbc6fd614beacdf1fe to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import subprocess
import os
import re
Kaleidoscope = '/Users/wez/src/WezsKeyboards/externals/Kaleidoscope'
def passthru(*args):
subprocess.check_call(args, cwd=Kaleidoscope)
def run(*args):
return subprocess.check_output(args, cwd=Kaleidoscope)
def add_remote(name, url):
try:
passthru('git', 'remote', 'add', '-f', name, url)
except Exception:
# Probably added it previously, so allow errors here
pass
class Submodules(object):
skip = ('https://github.com/keyboardio/lufa',
'https://github.com/keyboardio/Kaleidoscope')
def __init__(self):
self.modules = []
def add_module(self, path, url):
if 'https://github.com/keyboardio' not in url:
return
if url in Submodules.skip:
print('*** Not adding %s' % url)
return
self.modules.append({
'path': path,
'url': url})
self.add_remote(path, url)
def add_remote(self, path, url):
add_remote(path, url)
self.load_modules('%s/master' % path)
def load_modules(self, branch):
try:
out = run('git', 'show', '%s:.gitmodules' % branch)
except Exception as e:
print('Failed to show .gitmodules in %s: %s' % (branch, str(e)))
return
path = None
url = None
for line in out.split('\n'):
m = re.match('\s+path = (.*)$', line)
if m:
path = m.group(1)
m = re.match('\s+url = (.*)$', line)
if m:
url = m.group(1)
if url and path:
self.add_module(path, url)
url = None
path = None
submodules = Submodules()
submodules.add_module('boards', 'https://github.com/keyboardio/Arduino-Boards.git')
submodules.add_module('Kaleidoscope-Plugin', 'https://github.com/keyboardio/Kaleidoscope-Plugin.git')
# Clean up a previous iteration
passthru('git', 'reset', 'HEAD')
passthru('git', 'checkout', 'master')
def delete_branch(name):
try:
passthru('git', 'branch', '-D', name)
except:
pass
delete_branch('unify')
# Move the subrepo contents around to fit the new structure
for module in submodules.modules:
branch = 'b-%s' % module['path']
# Remove possible prior iteration
delete_branch(branch)
passthru('git', 'checkout', '-b', branch, '%s/master' % module['path'])
to_move = list(os.listdir(Kaleidoscope))
dir = os.path.join(Kaleidoscope, module['path'])
if not os.path.exists(dir):
os.makedirs(dir)
for ent in to_move:
if ent == '.git':
continue
passthru('git', 'mv', ent, module['path'])
passthru('git', 'commit', '-m', 'Move contents for unification')
passthru('git', 'checkout', '-b', 'unify', 'master')
for module in submodules.modules:
passthru('git', 'merge', '--allow-unrelated-histories',
'-s', 'recursive',
'b-%s' % module['path'],
'-m', 'Merge in %s' % module['url'])
# Move the main parts of myself into a library dir
os.mkdir(os.path.join(Kaleidoscope, 'libraries/Kaleidoscope'))
for f in ('library.properties', 'src', 'examples'):
passthru('git', 'mv', f, 'libraries/Kaleidoscope/%s' % f)
passthru('git', 'mv', 'Kaleidoscope-Plugin/build', 'build')
passthru('git', 'mv', 'Kaleidoscope-Plugin/tools', 'tools')
passthru('git', 'rm', '-rf', 'Kaleidoscope-Plugin')
passthru('git', 'rm', '-rf', 'boards/libraries')
passthru('git', 'commit', '-m', 'Restructure for unification')
print('Done!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment