diff -r 95eea8624d05 Doc/library/compileall.rst --- a/Doc/library/compileall.rst Thu Nov 21 11:30:06 2013 -0800 +++ b/Doc/library/compileall.rst Fri Nov 22 00:08:14 2013 +0200 @@ -65,9 +65,20 @@ is to write files to their :pep:`3147` locations and names, which allows byte-code files from multiple versions of Python to coexist. +.. cmdoption:: -r + + Control the maximum recursion level for subdirectories. + If this is given, then ``-l`` option will not be taken into account. + :program:`python -m compileall -r 0` is equivalent to + :program:`python -m compileall -l`. + + .. versionchanged:: 3.2 Added the ``-i``, ``-b`` and ``-h`` options. +.. versionchanged:: 3.4 + Added the ``-r`` option. + There is no command-line option to control the optimization level used by the :func:`compile` function, because the Python interpreter itself already provides the option: :program:`python -O -m compileall`. diff -r 95eea8624d05 Lib/compileall.py --- a/Lib/compileall.py Thu Nov 21 11:30:06 2013 -0800 +++ b/Lib/compileall.py Fri Nov 22 00:08:14 2013 +0200 @@ -170,6 +170,11 @@ parser.add_argument('-l', action='store_const', const=0, default=10, dest='maxlevels', help="don't recurse into subdirectories") + parser.add_argument('-r', action='store', + type=int, dest='recursion', + help=('control the maximum recursion level. ' + 'if `-l` and `-r` options are specified, ' + 'then `-r` takes precedence.')) parser.add_argument('-f', action='store_true', dest='force', help='force rebuild even if timestamps are up to date') parser.add_argument('-q', action='store_true', dest='quiet', @@ -204,6 +209,12 @@ import re args.rx = re.compile(args.rx) + + if args.recursion is not None: + maxlevels = args.recursion + else: + maxlevels = args.maxlevels + # if flist is provided then load it if args.flist: try: @@ -223,7 +234,7 @@ args.quiet, args.legacy): success = False else: - if not compile_dir(dest, args.maxlevels, args.ddir, + if not compile_dir(dest, maxlevels, args.ddir, args.force, args.rx, args.quiet, args.legacy): success = False diff -r 95eea8624d05 Lib/test/test_compileall.py --- a/Lib/test/test_compileall.py Thu Nov 21 11:30:06 2013 -0800 +++ b/Lib/test/test_compileall.py Fri Nov 22 00:08:14 2013 +0200 @@ -252,6 +252,39 @@ self.assertCompiled(subinitfn) self.assertCompiled(hamfn) + def test_recursion_limit(self): + subpackage = os.path.join(self.pkgdir, 'spam') + subpackage2 = os.path.join(subpackage, 'ham') + subpackage3 = os.path.join(subpackage2, 'eggs') + os.makedirs(subpackage3) + + subinitfn = script_helper.make_script(subpackage, '__init__', '') + hamfn = script_helper.make_script(subpackage, 'ham', '') + spamfn = script_helper.make_script(subpackage2, 'spam', '') + eggfn = script_helper.make_script(subpackage3, 'egg', '') + + self.assertRunOK('-q', '-r 0', self.pkgdir) + self.assertNotCompiled(subinitfn) + self.assertFalse( + os.path.exists(os.path.join(subpackage, '__pycache__'))) + + self.assertRunOK('-q', '-r 1', self.pkgdir) + self.assertCompiled(subinitfn) + self.assertCompiled(hamfn) + self.assertNotCompiled(spamfn) + + self.assertRunOK('-q', '-r 2', self.pkgdir) + self.assertCompiled(subinitfn) + self.assertCompiled(hamfn) + self.assertCompiled(spamfn) + self.assertNotCompiled(eggfn) + + self.assertRunOK('-q', '-r 5', self.pkgdir) + self.assertCompiled(subinitfn) + self.assertCompiled(hamfn) + self.assertCompiled(spamfn) + self.assertCompiled(eggfn) + def test_quiet(self): noisy = self.assertRunOK(self.pkgdir) quiet = self.assertRunOK('-q', self.pkgdir)