Created
August 4, 2017 15:44
-
-
Save dennissergeev/fc731ce27bfc0ea13c9b60edbb52543a to your computer and use it in GitHub Desktop.
This file contains 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 python | |
""" | |
Synchronize an Overleaf LaTeX paper with a Github repository | |
""" | |
import argparse | |
import sys | |
import subprocess as sb | |
from path import Path # non-standard path.py package | |
SCRIPT = Path(__file__).basename().splitext()[0] | |
OVERLEAF_BASE = Path('https://git.overleaf.com') | |
def parse_args(args=None): | |
ap = argparse.ArgumentParser(SCRIPT, | |
description=__doc__, | |
formatter_class=argparse. | |
ArgumentDefaultsHelpFormatter, | |
epilog='Example of use: coming soon') | |
ap.add_argument('-o', '--overleaf', type=str, required=True, | |
help='Overleaf repository address') | |
ap.add_argument('-n', '--name', type=str, default='.', | |
help='Name of the cloned repo') | |
ap_new = ap.add_mutually_exclusive_group(required=True) | |
ap_new.add_argument('-g', '--github', type=str, | |
help='GitHub repository address') | |
ap_new.add_argument('-b', '--bitbucket', type=str, | |
help=('BitBucket repository address')) | |
# ap.add_argument('-v', '--verbose', action='count', | |
# default=0, help='Verbosity (-v, -vv, etc)') | |
return ap.parse_args(args) | |
def main(args=None): | |
args = parse_args(args) | |
if args.overleaf.startswith(str(OVERLEAF_BASE)): | |
addr = args.overleaf | |
else: | |
# assume only the repo id is given | |
addr = OVERLEAF_BASE / args.overleaf | |
wdir = Path(args.name) | |
cmd = f'git clone {addr} {wdir}' | |
# Clone the overleaf repository | |
sb.call(cmd, shell=True) | |
# Change working directory | |
wdir.chdir() | |
# Rename origin | |
cmd = 'git remote rename origin overleaf' | |
sb.call(cmd, shell=True) | |
# Add remote | |
if args.github: | |
target = args.github | |
remote = 'github' | |
elif args.bitbucket: | |
target = args.bitbucket | |
remote = 'bitbucket' | |
cmd = f'git remote add {remote} {target}' | |
sb.call(cmd, shell=True) | |
cmd = f'git push {remote}' | |
sb.call(cmd, shell=True) | |
# Pushing / pulling to/from both | |
cmd = f'git remote add both {addr}' | |
sb.call(cmd, shell=True) | |
cmd = f'git remote set-url --add --push both {addr}' | |
sb.call(cmd, shell=True) | |
cmd = f'git remote set-url --add --push both {target}' | |
sb.call(cmd, shell=True) | |
# Print results | |
sb.call('git remote -v', shell=True) | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment