From d85aa8088a5cd4bc27b7d2d568242e5c84a758a0 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Tue, 29 Jul 2008 12:05:43 +0000 Subject: [PATCH] Add support for the --jobs option to be specified without an argument, and also support -j as a short option. Since optparse doesn't natively support options with non-required args, create an insert_optional_args() function that inserts the required argument into the args so that optparse is happy. The function inserts the string True as a substitute for the argument that is required. This string is later converted to the True constant when stored in the emerge opts dict (similar to how normal boolean options are stored). The PollScheduler and SequentialTaskQueue classes recognize the meaning of the True constant to mean unlimited concurrent jobs. svn path=/main/trunk/; revision=11261 --- man/emerge.1 | 7 ++-- pym/_emerge/__init__.py | 84 ++++++++++++++++++++++++++++++++++++----- pym/_emerge/help.py | 9 +++-- 3 files changed, 85 insertions(+), 15 deletions(-) diff --git a/man/emerge.1 b/man/emerge.1 index 59d335f62..e252c19b1 100644 --- a/man/emerge.1 +++ b/man/emerge.1 @@ -323,9 +323,10 @@ directory. .BR "\-\-ignore-default-opts" Causes \fIEMERGE_DEFAULT_OPTS\fR (see \fBmake.conf\fR(5)) to be ignored. .TP -.BR \-\-jobs=JOBS -Specifies the number of packages to build simultaneously. Also see -the related \fB\-\-load\-average\fR option. +.BR "-j [JOBS], \-\-jobs[=JOBS]" +Specifies the number of packages to build simultaneously. If this option is +given without an argument, emerge will not limit the number of jobs that can +run simultaneously. Also see the related \fB\-\-load\-average\fR option. .TP .BR "\-\-keep\-going" Continue as much as possible after an error. When an error occurs, diff --git a/pym/_emerge/__init__.py b/pym/_emerge/__init__.py index a833ad5c4..205111999 100644 --- a/pym/_emerge/__init__.py +++ b/pym/_emerge/__init__.py @@ -8153,7 +8153,8 @@ class SequentialTaskQueue(SlotObject): if task.poll() is not None: state_changed = True - while task_queue and (len(running_tasks) < max_jobs): + while task_queue and \ + (max_jobs is True or len(running_tasks) < max_jobs): task = task_queue.popleft() cancelled = getattr(task, "cancelled", None) if not cancelled: @@ -8271,10 +8272,12 @@ class PollScheduler(object): max_jobs = self._max_jobs max_load = self._max_load - if self._running_job_count() >= self._max_jobs: + if self._max_jobs is not True and \ + self._running_job_count() >= self._max_jobs: return False - if max_load is not None and max_jobs > 1 and \ + if max_load is not None and \ + (max_jobs is True or max_jobs > 1) and \ self._running_job_count() > 1: try: avg1, avg5, avg15 = os.getloadavg() @@ -8911,7 +8914,8 @@ class Scheduler(PollScheduler): @rtype: bool @returns: True if background mode is enabled, False otherwise. """ - background = (self._max_jobs > 1 or "--quiet" in self.myopts) and \ + background = (self._max_jobs is True or \ + self._max_jobs > 1 or "--quiet" in self.myopts) and \ not bool(self._opts_no_background.intersection(self.myopts)) self._status_display.quiet = \ @@ -12723,6 +12727,61 @@ def multiple_actions(action1, action2): sys.stderr.write("!!! '%s' or '%s'\n\n" % (action1, action2)) sys.exit(1) +def insert_optional_args(args): + """ + Parse optional arguments and insert a value if one has + not been provided. This is done before feeding the args + to the optparse parser since that parser does not support + this feature natively. + """ + + new_args = [] + jobs_opts = ("-j", "--jobs") + for i, arg in enumerate(args): + + short_job_opt = bool("j" in arg and arg[:1] == "-" and arg[:2] != "--") + if not (short_job_opt or arg in jobs_opts): + new_args.append(arg) + continue + + # Insert an empty placeholder in order to + # satisfy the requirements of optparse. + + new_args.append("--jobs") + job_count = None + saved_opts = None + if short_job_opt and len(arg) > 2: + if arg[:2] == "-j": + try: + job_count = int(arg[2:]) + except ValueError: + saved_opts = arg[2:] + else: + job_count = "True" + saved_opts = arg[1:].replace("j", "") + + if job_count is None and \ + i < len(args) - 1: + try: + job_count = int(args[i+1]) + except ValueError: + pass + else: + # The next loop iteration will append + # the validated job count to new_args. + continue + + if job_count is None: + # unlimited number of jobs + new_args.append("True") + else: + new_args.append(str(job_count)) + + if saved_opts is not None: + new_args.append("-" + saved_opts) + + return new_args + def parse_opts(tmpcmdline, silent=False): myaction=None myopts = {} @@ -12793,15 +12852,22 @@ def parse_opts(tmpcmdline, silent=False): parser.add_option(myopt, dest=myopt.lstrip("--").replace("-", "_"), **kwargs) + tmpcmdline = insert_optional_args(tmpcmdline) + myoptions, myargs = parser.parse_args(args=tmpcmdline) if myoptions.jobs: - try: - jobs = int(myoptions.jobs) - except ValueError: - jobs = 0 + jobs = None + if myoptions.jobs == "True": + jobs = True + else: + try: + jobs = int(myoptions.jobs) + except ValueError: + jobs = -1 - if jobs < 1: + if jobs is not True and \ + jobs < 1: jobs = None if not silent: writemsg("!!! Invalid --jobs parameter: '%s'\n" % \ diff --git a/pym/_emerge/help.py b/pym/_emerge/help.py index b2c032b21..bb4077aa5 100644 --- a/pym/_emerge/help.py +++ b/pym/_emerge/help.py @@ -14,7 +14,7 @@ def shorthelp(): print " "+turquoise("emerge")+" < "+turquoise("--sync")+" | "+turquoise("--metadata")+" | "+turquoise("--info")+" >" print " "+turquoise("emerge")+" "+turquoise("--resume")+" [ "+green("--pretend")+" | "+green("--ask")+" | "+green("--skipfirst")+" ]" print " "+turquoise("emerge")+" "+turquoise("--help")+" [ "+green("system")+" | "+green("world")+" | "+green("--sync")+" ] " - print bold("Options:")+" "+green("-")+"["+green("abBcCdDefgGhkKlnNoOpqPsStuvV")+"]" + print bold("Options:")+" "+green("-")+"["+green("abBcCdDefgGhjkKlnNoOpqPsStuvV")+"]" print " [ " + green("--color")+" < " + turquoise("y") + " | "+ turquoise("n")+" > ] [ "+green("--columns")+" ]" print " [ "+green("--complete-graph")+" ] [ "+green("--deep")+" ]" print " [ "+green("--jobs") + " " + turquoise("JOBS")+" ] [ "+green("--keep-going")+" ] [ " + green("--load-average")+" " + turquoise("LOAD") + " ]" @@ -305,9 +305,12 @@ def help(myaction,myopts,havecolor=1): print " downloaded from the remote server without consulting packages" print " existing in the packages directory." print - print " " + green("--jobs") + " " + turquoise("JOBS") + print " " + green("--jobs") + " " + turquoise("[JOBS]") + " ("+green("-j")+" short option)" desc = "Specifies the number of packages " + \ - "to build simultaneously. Also see " + \ + "to build simultaneously. If this option is " + \ + "given without an argument, emerge will not " + \ + "limit the number of jobs that " + \ + "can run simultaneously. Also see " + \ "the related --load-average option." for line in wrap(desc, desc_width): print desc_indent + line -- 2.26.2