-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtljh_repo2user_dir.py
43 lines (37 loc) · 1.27 KB
/
tljh_repo2user_dir.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import os
import shutil
from pwd import getpwnam
from git import Repo
from tljh.hooks import hookimpl
def clone_repo(user, git_url, repo_dir):
"""
A function to clone a github repo into a specific directory of a given user.
User receiver owner rights of all files in the repository
"""
Repo.clone_from(git_url, repo_dir)
uid = getpwnam(user).pw_uid
gid = getpwnam(user).pw_gid
for root, dirs, files in os.walk(repo_dir):
for d in dirs:
shutil.chown(os.path.join(root, d), user=uid, group=gid)
for f in files:
shutil.chown(os.path.join(root, f), user=uid, group=gid)
@hookimpl
def tljh_new_user_create(username):
"""
A function to clone a github repo into a 'repos' directory for every
JupyterHub user when the server spawns a new notebook instance.
"""
user_root_dir = os.path.join("/home", username)
# get repo url from environment variable
git_url = os.getenv("REPO_URL")
# nothing to do if no repo is specified
if git_url is None:
return
repo_dir = os.path.join(user_root_dir, 'repos')
if not os.path.isdir(repo_dir):
os.makedirs(repo_dir)
clone_repo(username, git_url, repo_dir)
else:
# user already has the repo downloaded
pass