From patchwork Tue Jul 1 18:14:24 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Yann E. MORIN" X-Patchwork-Id: 366172 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [103.22.144.68]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id A3D9114009E for ; Wed, 2 Jul 2014 04:14:43 +1000 (EST) Received: from ozlabs.org (ozlabs.org [103.22.144.67]) by lists.ozlabs.org (Postfix) with ESMTP id 924181A017C for ; Wed, 2 Jul 2014 04:14:43 +1000 (EST) X-Original-To: patchwork@lists.ozlabs.org Delivered-To: patchwork@lists.ozlabs.org Received: from mail-wg0-x22c.google.com (mail-wg0-x22c.google.com [IPv6:2a00:1450:400c:c00::22c]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id DAD701A0148 for ; Wed, 2 Jul 2014 04:14:33 +1000 (EST) Received: by mail-wg0-f44.google.com with SMTP id x13so10120826wgg.27 for ; Tue, 01 Jul 2014 11:14:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=vrHGoR8ibjl1dx+KmM8hRl1i4j68kmX7uta8JyRVXgc=; b=cw2OF2KFdZAJDpeUePde/H3w48T5/a70uwizJv9/AZp697oj8mrJlVMYT2asZVYekU 5yoHTPy0Ud6l3EKZpT+mN4qaVquuw0/aj4Bb0oZ4kMkezEcYduccW43SNsCKuQfHN6xP 1hbWwosIAAp0VyT1pN2UGbkiPKvtIAk3xR9EvIlqdB5EvJMteucxircSo/caZtn49AlS +8WOFGY23n88Ly2j8alSk1aGJ6H33WC33mwSuqCaeD/t3hxgU0DVEv+D4/1JdiCNBhVe whaWgrKZ/VKjD5lF9M4uu9WLkgH/cdgxh98MmRz7CnixoaCXIhgueOGotMH40KfO9HWb Ai2Q== X-Received: by 10.194.238.6 with SMTP id vg6mr50886760wjc.24.1404238469221; Tue, 01 Jul 2014 11:14:29 -0700 (PDT) Received: from gourin.bzh.lan (ks3095497.kimsufi.com. [94.23.60.27]) by mx.google.com with ESMTPSA id p3sm49650327wjw.13.2014.07.01.11.14.28 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 01 Jul 2014 11:14:28 -0700 (PDT) From: "Yann E. MORIN" To: patchwork@lists.ozlabs.org Subject: [PATCH 2/3] pwclient: accept more than one project in ~/.pwclientrc Date: Tue, 1 Jul 2014 20:14:24 +0200 Message-Id: <3db2b6eeaefaa8bd4d77e8028772aefede913a6f.1404238250.git.yann.morin.1998@free.fr> X-Mailer: git-send-email 1.9.1 In-Reply-To: References: X-BeenThere: patchwork@lists.ozlabs.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: Patchwork development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Patchwork" Currently, given the format of ~/.pwclientrc, pwclient can only really act on a single project, as ~/pwclientrc can only contain the configuration for a single project. Although the -p options comes in handy to specify a project different from the one configured in ~/.pwclientrc, this only works if it is hosted on the same server. As soon as one needs to switch server, it is necessary to edit ~/pwclientrc. This can be quite inefficient when dealing with many projects, hosted on different servers. Change the format of ~/.pwclientrc so it is possible to define more than one project, and for each project, specify an URL and credentials. The new format is like: [options] default = project-A [project-A] url = http://my.patchwork.server/path/to/xmlrpc username = that-is-me password = secret [other-project] url = http://you.get/the/idea username = someone password = 1234 This has the advantage of not changing the options to pwclient, so the user experience is unmodified. If a ~/.pwclentrc exists in the old format, it is automatically converted over to the new format, and the previous one is saved as ~/.pwclientrc.orig. Upon conversion, no action is made, pwclient just exits (with return-code 1) to inform the user to review the conversion. Signed-off-by: "Yann E. MORIN" --- apps/patchwork/bin/pwclient | 69 +++++++++++++++++++++++++++++++++--------- templates/patchwork/pwclientrc | 16 +++++----- 2 files changed, 63 insertions(+), 22 deletions(-) diff --git a/apps/patchwork/bin/pwclient b/apps/patchwork/bin/pwclient index 2104f59..dfbea30 100755 --- a/apps/patchwork/bin/pwclient +++ b/apps/patchwork/bin/pwclient @@ -28,6 +28,7 @@ import tempfile import subprocess import base64 import ConfigParser +import shutil # Default Patchwork remote XML-RPC server URL # This script will check the PW_XMLRPC_URL environment variable @@ -350,16 +351,6 @@ def main(): msgid_str = "" url = DEFAULT_URL - config = ConfigParser.ConfigParser() - config.read([CONFIG_FILE]) - - # grab settings from config files - if config.has_option('base', 'url'): - url = config.get('base', 'url') - - if config.has_option('base', 'project'): - project_str = config.get('base', 'project') - for name, value in opts: if name == '-s': state_str = value @@ -389,17 +380,67 @@ def main(): sys.stderr.write("Too many arguments specified\n") usage() + # grab settings from config files + config = ConfigParser.ConfigParser() + config.read([CONFIG_FILE]) + + if not config.has_section('options'): + sys.stderr.write('~/.pwclientrc is in the old format. Migrating it...') + + old_project = config.get('base','project') + + new_config = ConfigParser.ConfigParser() + new_config.add_section('options') + + new_config.set('options','default',old_project) + new_config.add_section(old_project) + + new_config.set(old_project,'url',config.get('base','url')) + if config.has_option('auth', 'username'): + new_config.set(old_project,'username',config.get('auth','username')) + if config.has_option('auth', 'password'): + new_config.set(old_project,'password',config.get('auth','password')) + + old_config_file = CONFIG_FILE + '.orig' + shutil.copy2(CONFIG_FILE,old_config_file) + + with open(CONFIG_FILE, 'wb') as fd: + new_config.write(fd) + + sys.stderr.write(' Done.\n') + sys.stderr.write('Your old ~/.pwclientrc was saved to %s\n' % old_config_file) + sys.stderr.write('and was converted to the new format. You may want to\n') + sys.stderr.write('inspect it before continuing.\n') + sys.exit(1) + + if not project_str: + try: + project_str = config.get('options', 'default') + except: + sys.stderr.write("No default project configured in ~/.pwclientrc\n") + usage() + + if not config.has_section(project_str): + sys.stderr.write("No section for project %s\n" % project_str) + sys.exit(1) + + if not config.has_option(project_str, 'url'): + sys.stderr.write("No URL for project %s\n" % project_str) + sys.exit(1) + + url = config.get(project_str, 'url') + (username, password) = (None, None) transport = None if action in auth_actions: - if config.has_option('auth', 'username') and \ - config.has_option('auth', 'password'): + if config.has_option(project_str, 'username') and \ + config.has_option(project_str, 'password'): use_https = url.startswith('https') transport = BasicHTTPAuthTransport( \ - config.get('auth', 'username'), - config.get('auth', 'password'), + config.get(project_str, 'username'), + config.get(project_str, 'password'), use_https) else: diff --git a/templates/patchwork/pwclientrc b/templates/patchwork/pwclientrc index 436a28b..d331003 100644 --- a/templates/patchwork/pwclientrc +++ b/templates/patchwork/pwclientrc @@ -1,15 +1,15 @@ # Sample .pwclientrc file for the {{ project.linkname }} project, # running on {{ site.domain }}. # -# Save this file to ~/.pwclientrc -# -[base] -url: {{scheme}}://{{site.domain}}{% url 'patchwork.views.xmlrpc.xmlrpc' %} -project: {{ project.linkname }} +# Just append this file to your existing ~/.pwclientrc +# If you do not already have a ~/.pwclientrc, then copy this file to +# ~/.pwclientrc, and uncomment the following two lines: +# [options] +# default={{ project.linkname }} + +[{{ project.linkname }}] +url= {{scheme}}://{{site.domain}}{% url 'patchwork.views.xmlrpc.xmlrpc' %} {% if user.is_authenticated %} -# Adding authentication parameters will allow you to use the 'update' -# command on patches that you are allowed to edit. -[auth] username: {{ user.username }} password: {% endif %}