Skip to content

Commit

Permalink
Access private LZ data from GitLab now.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsjames committed Nov 1, 2023
1 parent 4ce1649 commit 9099884
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions flamedisx/lz/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,50 @@
import getpass
import os
import os.path
import random
import string

import flamedisx as fd
export, __all__ = fd.exporter()


# Path to the root folder of the private LZ data repository, if you have already cloned it
PATH = './lz_private_data'
PATH = 'lz_private_data'


def ensure_token(token=None):
"""Requests for token if token is not already available."""
if token is None:
print(" - Create a token with full 'repo' permissions at "
"https://github.com/settings/tokens\n"
print(" - Create a token with 'read repository' permissions at "
"https://gitlab.com/-/profile/personal_access_tokens\n"
" - Save or write it down somewhere \n"
" - Type it in the prompt above\n\n"
"'Repository not found' means you didn't give the token"
" full 'repo' permissions.\n"
"'Authentication failed' means you mistyped the token.\n")
" the correct permissions.\n"
"'Authentication failed' means you mistyped your username or the"
" token.\n")
# We could prompt for username/password instead, but then the password
# would be printed in plaintext if there is any problem during cloning.
token = getpass.getpass('Github OAuth token: ')
return token
user = input('Gitlab username:')
token = getpass.getpass('GitLab token: ')
return user, token


def ensure_repo(repo_name, repo_path, token=None):
def ensure_repo(repo_name, repo_path, user=None, token=None):
"""Clones private repository (prompting for credentials) if we do not have it"""
if not os.path.exists(repo_path):
print("Private data requested, we must clone repository folder.")
token = ensure_token()
fd.run_command(f'git clone https://{token}:x-oauth-basic'
f'@github.com/{repo_name}')
user, token = ensure_token()
temp_folder = ''.join(random.choices(string.ascii_lowercase, k=8))
fd.run_command(f'git clone https://{user}:{token}'
f'@gitlab.com/{repo_name} {temp_folder}')
fd.run_command(f'mv {temp_folder}/{repo_path} .')
fd.run_command(f'rm -r -f {temp_folder}')


@export
def get_lz_file(data_file_name):
"""Return information from file in lz_private_data/...
"""
ensure_repo('robertsjames/lz_private_data.git', PATH)
ensure_repo('luxzeplin/stats/LZFlameFit.git', PATH)
return fd.get_resource(f'{PATH}/{data_file_name}')

0 comments on commit 9099884

Please sign in to comment.