All of lore.kernel.org
 help / color / mirror / Atom feed
From: "André Erdmann" <dywi@mailerd.de>
To: buildroot@busybox.net
Subject: [Buildroot] [PATCH 00/13] autobuild-run: python3 compat and misc improvements
Date: Sun, 01 Mar 2015 01:09:17 +0100	[thread overview]
Message-ID: <54F258AD.5040301@mailerd.de> (raw)
In-Reply-To: <EEF2BEA7-3CA2-4250-8E78-E2585246EE66@gmail.com>

2015/2/28 Thomas De Schampheleire <patrickdepinguin@gmail.com>:
> On February 26, 2015 11:08:58 AM CET, Thomas Petazzoni <thomas.petazzoni@free-electrons.com> wrote:
>> Dear Andr? Erdmann,
>>
>> On Wed, 25 Feb 2015 22:17:17 +0100, Andr? Erdmann wrote:
>>> Patches 1-4 make autobuild-run compatible with both python >=2.6 and
>> 3.
>>>
>>> Patches 5-9 are minor enhancements (mostly cleanup).
>>>
>>> Patches 10- are more invasive:
>>> * move the check_requirements() logic to the SystemInfo class
>>> * control environment(->locale), std{in,out,err} of all called
>> commands
>>
>> Thanks a lot all those patches. Overall they look very useful!
>>
>> I really need to spend some time to merge the patches proposed by
>> Thomas DS, and then your patches. I would still like to give the
>> priority to Thomas DS patches, since they have been in the queue for a
>> very long time.
>>
> 
> Andr?,
> 
> Could you give your personal feedback on the usage of docopt in my patches? Do
>  you think it is an improvement or rather an unnecessary dependency?
> 
> ThomasP has a different opinion than me, and there haven't been many other
>  opinions so far. Your input is this most definitely welcome, no matter which side
>  you choose.
> 


The advantage of docopt is that it's really simple and you always have the
usage message in the script, but I'd prefer argparse primarily because it's a built-in
module (in py >= 2.7). It also features arg validation (via type=<function|type>).
There's much more like subparsers, call a <function> each time an <arg> is encountered
and whatnot, but that's not of interest here.

The major improvement of the docopt approach is reading the config (argv/file)
into a few dicts that can be easily merged rather than doing a 3-way merge manually
(defaults as locals X ini file parser X argparser namedtuple-like object). That's a
good idea. It's doable with argparse as well, "vars(parser.parse_args())" returns
the parsed args as dict.

So, the ((untested)) prototype of a "dict-improved" config_get() argparse variant would be:


def config_get():
   def nonempty(val):
      if val:
         return val
      raise argparse.ArgumentTypeError("must be a non-empty value.")
   # --- end of nonempty (...) ---

   def positive_int(val):
      ival = int(val)
      if ival > 0:
         return ival
      raise argparse.ArgumentTypeError("must be > 0 : %s" % val)
   # --- end of positive_int (...) ---

   ## this is the dict that config_get() will return later on
   config = {
      "ninstances": 1, "njobs": 1, "http_login": None, "http_password": None, submitter: "N/A"
   } 

   ## build up the arg parser
   parser = argparse.ArgumentParser(...)

   ## note the use of SUPPRESS:
   ##  the option won't make it into the (namespace object =>) dict if it is not specified on the cmdline
   parser.add_argument("--ninstances", "-n", default=argparse.SUPPRESS, type=positive_int, help=...)
   ...more args...
   parser.add_argument("--submitter", "-s", default=argparse.SUPPRESS, type=nonempty, ...)
   parser.add_argument("--config", "-c", default=None, ...)

   ## parse args, store key/values in a dict
   argv_config = vars(parser.parse_args())

   ## load config file, if any
   if argv_config.get("config"):
      ## load_ini_config() must return a "usable" dict
      ##  (e.g. convert "a-b" keys to "a_b", drop empty values, use parser.getint() where appropriate)
      config_from_file = load_ini_config(argv_config["config"])
 
      ## transfer config from file to the result dict
      ##  (overrides default entries)
      config.update(config_from_file)
   # -- end if

   ## drop the "config" key from argv config, no longer needed
   argv_config.pop("config",None)

   ## transfer argv config to the result dict
   ##  (overrides entries from defaults, config_from_file)
   config.update(argv_config)

   return config
# --- end of config_get (...) ---


config_get() would then return a dict that can be passed around
(multiprocessing.Process(target=run_instance, args=(i, config, sysinfo)),
do_build(instance, log, config), send_results(instance, log, result, config) a.s.o.)

-- 
Andr?


 

  reply	other threads:[~2015-03-01  0:09 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-25 21:17 [Buildroot] [PATCH 00/13] autobuild-run: python3 compat and misc improvements André Erdmann
2015-02-25 21:17 ` [Buildroot] [PATCH 01/13] autobuild-run, python3: print is a function André Erdmann
2015-02-25 21:17 ` [Buildroot] [PATCH 02/13] autobuild-run, python3: urllib.request<>urllib2 André Erdmann
2015-02-25 21:17 ` [Buildroot] [PATCH 03/13] autobuild-run, python3: bytes<>str, decode() André Erdmann
2015-02-25 21:17 ` [Buildroot] [PATCH 04/13] autobuild-run, python3: configparser<>ConfigParser André Erdmann
2015-02-25 21:17 ` [Buildroot] [PATCH 05/13] autobuild-run: remove unneeded vars André Erdmann
2015-02-25 21:17 ` [Buildroot] [PATCH 06/13] autobuild-run: explicitly close web file handles André Erdmann
2015-02-25 21:17 ` [Buildroot] [PATCH 07/13] autobuild-run: get host arch once André Erdmann
2015-02-25 21:17 ` [Buildroot] [PATCH 08/13] autobuild-run: sort imports alphabetically André Erdmann
2015-02-25 21:17 ` [Buildroot] [PATCH 09/13] autobuild-run: unify "which <prog>" code André Erdmann
2015-02-25 21:17 ` [Buildroot] [PATCH 10/13] autobuild-run: use a built-in has_prog() implementation André Erdmann
2015-02-25 21:17 ` [Buildroot] [PATCH 11/13] autobuild-run: move check_requirements() to SystemInfo André Erdmann
2015-02-25 21:17 ` [Buildroot] [PATCH 12/13] autobuild-run: encapsulate subprocess calls André Erdmann
2015-02-25 21:17 ` [Buildroot] [PATCH 13/13] autobuild-run: set locale to en_US or C André Erdmann
2015-02-26 10:08 ` [Buildroot] [PATCH 00/13] autobuild-run: python3 compat and misc improvements Thomas Petazzoni
2015-02-26 19:25   ` Thomas De Schampheleire
2015-03-01  0:09     ` André Erdmann [this message]
2015-03-01 21:17       ` Thomas De Schampheleire
2015-03-01 21:37         ` André Erdmann
2015-03-01 21:52           ` Thomas De Schampheleire
2015-03-02  8:36             ` Thomas Petazzoni
2015-03-02 20:00               ` André Erdmann
2015-03-15 13:36 ` Thomas Petazzoni

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=54F258AD.5040301@mailerd.de \
    --to=dywi@mailerd.de \
    --cc=buildroot@busybox.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.