Skip to content

Instantly share code, notes, and snippets.

@Cellebyte
Created March 28, 2018 18:05
Show Gist options
  • Save Cellebyte/c3cd95ae5fed27a5075981d6658ba7eb to your computer and use it in GitHub Desktop.
Save Cellebyte/c3cd95ae5fed27a5075981d6658ba7eb to your computer and use it in GitHub Desktop.
Simple sharelatex to git repo dumper in python ugly but usefull
#!/usr/bin/env python3
'''
Simple Python Module to dump sharelatex onto gitlab
apt install git
apt install python3
apt install python3-requests
'''
import sys
import os
import shutil
from subprocess import check_output, CalledProcessError
import re
import requests
if len(sys.argv) != 7:
print('python3 dump.py EMAIL_SHARELATEX PASSWORD_SHARELATEX '
+ 'PROJECT_ID GIT_PROJECT GIT_ORIGIN BRANCH')
sys.exit(1)
EMAIL_SHARELATEX = sys.argv[1] # user@example.com
PASSWORD_SHARELATEX = sys.argv[2] # 'secret_password' (clear text)
URL_SHARELATEX = 'https://share.cellebyte.de' # sharelatex url
PROJECT_ID = sys.argv[3] # '59ff3bbe1d9f9300718ea4ff' # https://share.cellebyte.de/project/PROJECT_ID
GIT_PROJECT = sys.argv[4] # 'tischkicker_doku'
GIT_ORIGIN = sys.argv[5] # 'git@github.com:$NAMESPACE/{}.git'.format(GIT_PROJECT)
BRANCH = sys.argv[6] # 'dumper'
def main():
'''
Main Routine for dumping it to git
'''
session = requests.Session()
response = session.get(URL_SHARELATEX + '/login')
login_site = response.text
token = re.findall(
r'<input name="_csrf" type="hidden" value="(?P<token>.*?)">', login_site)
print(token)
response = session.post(
'{}/login'.format(URL_SHARELATEX), data={
'_csrf': token,
'email': EMAIL_SHARELATEX,
'password': PASSWORD_SHARELATEX})
print(response.status_code)
git_path = os.path.join('/tmp', GIT_PROJECT)
if not os.path.exists(git_path):
try:
print(check_output(['git', 'clone', GIT_ORIGIN, git_path]))
except CalledProcessError as error:
print(error.returncode, error.output)
try:
print(check_output(['git', '-C', git_path, 'fetch', '--all']))
print(check_output(['git', '-C', git_path, 'checkout', BRANCH]))
print(check_output(['git', '-C', git_path, 'pull']))
print(check_output(['git', '-C', git_path,
'config', 'user.name', 'sharelatex_' + BRANCH]))
print(check_output(['git', '-C', git_path,
'config', 'push.default', 'simple']))
print(check_output(['git', '-C', git_path,
'config', 'user.email', BRANCH + '@example.com']))
except CalledProcessError as error:
print(error.returncode, error.output)
for thing in os.listdir(git_path):
if not re.findall(r'^\.\w+$', thing, flags=re.MULTILINE):
try:
shutil.rmtree(os.path.join(git_path, thing))
except NotADirectoryError:
try:
os.remove(os.path.join(git_path, thing))
except Exception:
pass
response = session.get('{}/project/{}/download/zip'.format(URL_SHARELATEX,PROJECT_ID))
dump = os.path.join(git_path, '{}.zip'.format(GIT_PROJECT)
with open(dump, 'wb') as file_dump:
for chunk in response.iter_content(chunk_size=128):
file_dump.write(chunk)
try:
check_output(['unzip', dump, '-d', git_path])
check_output(['rm', dump])
check_output(['git', '-C', git_path, 'add', '.'])
output = check_output(['git', '-C', git_path, 'status'])
output = output.decode(sys.stdout.encoding)
print(output)
matches = re.findall(
r'^\t(?P<statement>.*?):\s+(?P<file>.*?)$', output, re.MULTILINE)
commit_message = 'Dumper recognizes these files:\n'
for statement, file_name in matches:
formatted = ' {}: {}\n'.format(statement, file_name)
commit_message = commit_message + formatted
commit_message = commit_message + '\n\nPlease revert if changes are not right'
print(check_output(
['git', '-C', git_path, 'commit', '-m', commit_message]))
print(check_output(['git', '-C', git_path, 'push']))
except CalledProcessError as error:
print(error.returncode, error.output)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment