-
-
Notifications
You must be signed in to change notification settings - Fork 32k
bpo-40495: compileall option to hardlink duplicate pyc files #19901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
005e6e0
bpo-40495: compileall option to hardlink duplicate pyc files
frenzymadness 7f8b63f
Update Misc/NEWS.d/next/Library/2020-05-04-11-20-49.bpo-40495.TyTc2O.rst
frenzymadness 6a9efa2
Update Doc/library/compileall.rst
frenzymadness e1ef909
Update Lib/compileall.py
frenzymadness b314c5f
remove debug code
frenzymadness e2f3a50
docs update
frenzymadness 4607d08
use is_hardlink to check inodes instead of repeating code
frenzymadness 4fb779a
use subTest to parametrize three tests with different combinations of…
frenzymadness 97b057e
fix tests
frenzymadness 9ca6eae
Refactor tests
vstinner b006361
Updated Whatsnew
frenzymadness 45259b2
Update Lib/compileall.py
vstinner 7e92096
Remove duplicated optimization levels
vstinner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
import importlib.util | ||
import py_compile | ||
import struct | ||
import filecmp | ||
|
||
from functools import partial | ||
from pathlib import Path | ||
|
@@ -47,7 +48,7 @@ def _walk_dir(dir, maxlevels, quiet=0): | |
def compile_dir(dir, maxlevels=None, ddir=None, force=False, | ||
rx=None, quiet=0, legacy=False, optimize=-1, workers=1, | ||
invalidation_mode=None, *, stripdir=None, | ||
prependdir=None, limit_sl_dest=None): | ||
prependdir=None, limit_sl_dest=None, hardlink_dupes=False): | ||
"""Byte-compile all modules in the given directory tree. | ||
|
||
Arguments (only dir is required): | ||
|
@@ -70,6 +71,7 @@ def compile_dir(dir, maxlevels=None, ddir=None, force=False, | |
after stripdir | ||
limit_sl_dest: ignore symlinks if they are pointing outside of | ||
the defined path | ||
hardlink_dupes: hardlink duplicated pyc files | ||
""" | ||
ProcessPoolExecutor = None | ||
if ddir is not None and (stripdir is not None or prependdir is not None): | ||
|
@@ -104,22 +106,24 @@ def compile_dir(dir, maxlevels=None, ddir=None, force=False, | |
invalidation_mode=invalidation_mode, | ||
stripdir=stripdir, | ||
prependdir=prependdir, | ||
limit_sl_dest=limit_sl_dest), | ||
limit_sl_dest=limit_sl_dest, | ||
hardlink_dupes=hardlink_dupes), | ||
files) | ||
success = min(results, default=True) | ||
else: | ||
for file in files: | ||
if not compile_file(file, ddir, force, rx, quiet, | ||
legacy, optimize, invalidation_mode, | ||
stripdir=stripdir, prependdir=prependdir, | ||
limit_sl_dest=limit_sl_dest): | ||
limit_sl_dest=limit_sl_dest, | ||
hardlink_dupes=hardlink_dupes): | ||
success = False | ||
return success | ||
|
||
def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, | ||
legacy=False, optimize=-1, | ||
invalidation_mode=None, *, stripdir=None, prependdir=None, | ||
limit_sl_dest=None): | ||
limit_sl_dest=None, hardlink_dupes=False): | ||
"""Byte-compile one file. | ||
|
||
Arguments (only fullname is required): | ||
|
@@ -140,6 +144,7 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, | |
after stripdir | ||
limit_sl_dest: ignore symlinks if they are pointing outside of | ||
the defined path. | ||
hardlink_dupes: hardlink duplicated pyc files | ||
""" | ||
|
||
if ddir is not None and (stripdir is not None or prependdir is not None): | ||
|
@@ -176,6 +181,10 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, | |
if isinstance(optimize, int): | ||
optimize = [optimize] | ||
|
||
if hardlink_dupes: | ||
raise ValueError(("Hardlinking of duplicated bytecode makes sense " | ||
"only for more than one optimization level.")) | ||
frenzymadness marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if rx is not None: | ||
mo = rx.search(fullname) | ||
if mo: | ||
|
@@ -220,10 +229,16 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, | |
if not quiet: | ||
print('Compiling {!r}...'.format(fullname)) | ||
try: | ||
for opt_level, cfile in opt_cfiles.items(): | ||
for index, opt_level in enumerate(sorted(optimize)): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By the way, sorting here is smart and efficient. That's a great idea :-) |
||
cfile = opt_cfiles[opt_level] | ||
ok = py_compile.compile(fullname, cfile, dfile, True, | ||
optimize=opt_level, | ||
invalidation_mode=invalidation_mode) | ||
if index > 0 and hardlink_dupes: | ||
previous_cfile = opt_cfiles[optimize[index - 1]] | ||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if filecmp.cmp(cfile, previous_cfile, shallow=False): | ||
os.unlink(cfile) | ||
os.link(previous_cfile, cfile) | ||
except py_compile.PyCompileError as err: | ||
success = False | ||
if quiet >= 2: | ||
|
@@ -352,6 +367,9 @@ def main(): | |
'Python interpreter itself (specified by -O).')) | ||
parser.add_argument('-e', metavar='DIR', dest='limit_sl_dest', | ||
help='Ignore symlinks pointing outsite of the DIR') | ||
parser.add_argument('--hardlink-dupes', action='store_true', | ||
dest='hardlink_dupes', | ||
help='Hardlink duplicated pyc files') | ||
|
||
args = parser.parse_args() | ||
compile_dests = args.compile_dest | ||
|
@@ -371,6 +389,10 @@ def main(): | |
if args.opt_levels is None: | ||
args.opt_levels = [-1] | ||
|
||
if len(args.opt_levels) == 1 and args.hardlink_dupes: | ||
parser.error(("Hardlinking of duplicated bytecode makes sense " | ||
"only for more than one optimization level.")) | ||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if args.ddir is not None and ( | ||
args.stripdir is not None or args.prependdir is not None | ||
): | ||
|
@@ -404,7 +426,8 @@ def main(): | |
stripdir=args.stripdir, | ||
prependdir=args.prependdir, | ||
optimize=args.opt_levels, | ||
limit_sl_dest=args.limit_sl_dest): | ||
limit_sl_dest=args.limit_sl_dest, | ||
hardlink_dupes=args.hardlink_dupes): | ||
success = False | ||
else: | ||
if not compile_dir(dest, maxlevels, args.ddir, | ||
|
@@ -414,7 +437,8 @@ def main(): | |
stripdir=args.stripdir, | ||
prependdir=args.prependdir, | ||
optimize=args.opt_levels, | ||
limit_sl_dest=args.limit_sl_dest): | ||
limit_sl_dest=args.limit_sl_dest, | ||
hardlink_dupes=args.hardlink_dupes): | ||
success = False | ||
return success | ||
else: | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.