From e123eeff89ecb41de2d41b854f016845ab90e6a1 Mon Sep 17 00:00:00 2001 From: Lubomír Sedlář Date: Oct 11 2017 18:14:59 +0000 Subject: Add upload command Fixes: https://pagure.io/pag/issue/9 --- diff --git a/pag/app.py b/pag/app.py index 2134a4b..ce3207a 100644 --- a/pag/app.py +++ b/pag/app.py @@ -40,6 +40,7 @@ from .commands import remote from .commands import pullrequest from .commands import gitaliasing from .commands import review +from .commands import upload if __name__ == '__main__': app() diff --git a/pag/client.py b/pag/client.py index 6d989b5..9079801 100644 --- a/pag/client.py +++ b/pag/client.py @@ -88,6 +88,34 @@ class Pagure(fedora.client.OpenIdBaseClient): issue_url = self.base_url + '/' + repo + '/issue/' + issue_id return issue_url + def upload(self, repo, filepath): + if not self.is_logged_in: + raise PagureException('Not logged in.') + + url = self.base_url + '/' + repo + '/upload' + response = self._session.get(url) + if not bool(response): + raise PagureException("Couldn't get form to get " + "csrf token %r" % response) + + soup = bs4.BeautifulSoup(response.text, "html.parser") + data = { + 'csrf_token': soup.find(id='csrf_token').attrs['value'], + } + files = { + 'filestream': open(filepath, 'rb') + } + response = self._session.post(url, data=data, files=files) + soup = bs4.BeautifulSoup(response.text, "html.parser") + alert = soup.find(class_="alert") + if 'alert-info' in alert.attrs['class']: + # Not an error -> the upload was successful. + return None + # Filter out only text elements from the alert (throwing away the close + # button). + text = ''.join(str(c) for c in alert.children + if isinstance(c, bs4.element.NavigableString)) + return text.strip() def fork(self, name): if not self.is_logged_in: diff --git a/pag/commands/upload.py b/pag/commands/upload.py new file mode 100644 index 0000000..e12b3a0 --- /dev/null +++ b/pag/commands/upload.py @@ -0,0 +1,32 @@ +import getpass +import sys + +import click + +from pag.app import app +from pag.utils import ( + assert_local_repo, + configured, + in_git_repo, +) +from pag.client import client + + +@app.command('upload') +@click.argument('tarball', + type=click.Path(exists=True, dir_okay=False)) +@configured +@assert_local_repo +def upload(conf, tarball): + name = in_git_repo() + click.echo('Trying to upload %r' % tarball) + if not client.is_logged_in: + password = getpass.getpass("Fas password for %r" % conf['username']) + client.login(username=conf['username'], password=password) + + error = client.upload(name, tarball) + if error: + click.echo("Upload failed: %s" % error, sys.stderr) + sys.exit(1) + else: + click.echo('Upload successful.')