Skip to content

Commit

Permalink
add OSF mirroring
Browse files Browse the repository at this point in the history
  • Loading branch information
zougloub committed Jul 1, 2020
1 parent 4fe3334 commit e40909d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
26 changes: 26 additions & 0 deletions distriploy/mirror_osf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env python
# -*- coding: utf-8 vi:et


from .osf import *


def mirror(repo_path, revision, config, release_meta):
"""
"""
ret = dict()

username = os.environ["OSF_USERNAME"]
password = os.environ["OSF_PASSWORD"]

url = upload_to_osf(
config["project"],
username, password,
release_meta["artifact_path"],
config["folder"],
config.get("filename"),
)

ret["urls"] = [url]

return ret
41 changes: 41 additions & 0 deletions distriploy/osf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env python
# -*- coding: utf-8 vi:et

import sys, io, os, logging
import urllib.request, urllib.error

import osfclient


logger = logging.getLogger(__name__)


def upload_to_osf(osf_project_id, osf_username, osf_password, asset_path, osf_folder, osf_location=None):
"""
Uploads new version of the data to the Open Science Framework.
:return: osf download url of the newly uploaded asset.
"""

upload_path = "/" + osf_folder
if osf_location is not None:
upload_path += "/" + osf_location
else:
upload_path += "/" + os.path.basename(asset_path)

osf = osfclient.OSF()

osf.login(username=osf_username, password=osf_password)

project_storage = osf.project(osf_project_id).storage()

logger.info("Uploading asset to OSF (project id:%s)", osf_project_id)

with io.open(asset_path, "rb") as fup:
project_storage.create_file(upload_path, fup)

for file in project_storage.files:
# compare strings instead of os.path.samefile() because objects are in different file systems.
if file.path == upload_path:
return file._download_url

0 comments on commit e40909d

Please sign in to comment.