Browse Source

add github transport (#3)

pull/4/head
l-n-s 8 years ago
parent
commit
e333cc5b56
  1. 14
      pyseeder.py
  2. 6
      pyseeder/transport.py
  3. 49
      pyseeder/transports/github.py
  4. 1
      requirements.txt
  5. 14
      transports.ini.example

14
pyseeder.py

@ -2,13 +2,20 @@ @@ -2,13 +2,20 @@
import os
import sys
import argparse
import logging
import pyseeder.transport
import pyseeder.actions
from pyseeder.utils import PyseederException
log = logging.getLogger(__name__)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--loglevel', default=logging.INFO, help="Log level",
choices=[logging.CRITICAL, logging.ERROR, logging.WARNING,
logging.INFO, logging.DEBUG])
subparsers = parser.add_subparsers(title="actions",
help="Command to execute")
@ -101,12 +108,17 @@ echo $YOUR_PASSWORD | %(prog)s --netdb /path/to/netDb \\ @@ -101,12 +108,17 @@ echo $YOUR_PASSWORD | %(prog)s --netdb /path/to/netDb \\
help=".su3 file (default: output/i2pseeds.su3)")
serve_parser.set_defaults(func=pyseeder.actions.serve)
args = parser.parse_args()
logging.basicConfig(level=args.loglevel,
format='%(levelname)-8s %(message)s')
if hasattr(args, "func"):
try:
args.func(args)
except PyseederException as pe:
print("Pyseeder error: {}".format(pe))
log.critical("Pyseeder error: {}".format(pe))
sys.exit(1)
else:
parser.print_help()

6
pyseeder/transport.py

@ -3,9 +3,11 @@ import urllib.request @@ -3,9 +3,11 @@ import urllib.request
from urllib.error import URLError
import os
import importlib
import logging
from pyseeder.utils import PyseederException
from pyseeder.utils import PyseederException, TransportException
log = logging.getLogger(__name__)
RESEED_URLS = [
"https://reseed.i2p-projekt.de/",
@ -55,4 +57,6 @@ def upload(filename, config): @@ -55,4 +57,6 @@ def upload(filename, config):
except ImportError:
raise PyseederException(
"{} transport can't be loaded".format(t))
except TransportException as e:
log.error("Transport error: {}".format(e))

49
pyseeder/transports/github.py

@ -0,0 +1,49 @@ @@ -0,0 +1,49 @@
#!/usr/bin/env python3
# (re)uploads reseed file to github repository releases as an asset
import requests
from urllib.parse import urljoin
from mimetypes import guess_type
import sys
import os
from pyseeder.utils import TransportException
TRANSPORT_NAME = "github"
def run(filename, config):
API_URL = "https://api.github.com/"
asset_name = os.path.split(filename)[-1]
content_type = guess_type(asset_name)[0] or "application/zip"
creds = (config["username"], config["token"])
release_info_url = urljoin(API_URL, "/repos/{}/releases/tags/{}".format(
config["repo"], config["release_tag"]))
# get release info
try:
resp = requests.get(release_info_url, auth=creds)
except:
raise TransportException("Failed to connect to GitHub API")
if resp.status_code is not 200:
raise TransportException("Check your GitHub API auth settings")
# delete old asset
for x in resp.json()["assets"]:
if x["name"] == asset_name:
r = requests.delete(x["url"], auth=creds)
if r.status_code is not 204:
raise TransportException("Failed to delete asset from GitHub")
# upload new asset
upload_url = resp.json()["upload_url"].split("{")[0] # wat
headers = {'Content-Type': content_type}
params = {'name': asset_name}
data = open(filename, 'rb').read()
r = requests.post(upload_url, headers=headers, params=params, auth=creds,
data=data)
if r.status_code is not 201:
raise TransportException("Failed to upload asset to GitHub API")

1
requirements.txt

@ -1 +1,2 @@ @@ -1 +1,2 @@
cryptography>=1.4
requests

14
transports.ini.example

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
[transports]
; enabled transports separated by space
enabled=git
enabled=github
[git]
; Folder with git repository to use
@ -8,3 +8,15 @@ folder=/home/user/reseed-data-repo @@ -8,3 +8,15 @@ folder=/home/user/reseed-data-repo
[dropbox]
; todo
[github]
; GitHub username
username=username
; GitHub API token
; Generate token for this script here --> https://github.com/settings/tokens
; Check scope: public_repo (shall be enough)
token=token
; Repository
repo=username/repo-name
; Repository tag to which asset is being uploaded
release_tag=v1.0

Loading…
Cancel
Save