From ee33fa23dcdb7f9a0e89e2df3a50706b75678a36 Mon Sep 17 00:00:00 2001 From: Bibo Hao Date: Wed, 13 Sep 2017 11:47:12 +0800 Subject: python 3 bdist_egg --exclude-source-files __pycache__ issue --- setuptools/command/bdist_egg.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'setuptools/command/bdist_egg.py') diff --git a/setuptools/command/bdist_egg.py b/setuptools/command/bdist_egg.py index 51755d52..5bfc7ba2 100644 --- a/setuptools/command/bdist_egg.py +++ b/setuptools/command/bdist_egg.py @@ -8,6 +8,7 @@ from distutils import log from types import CodeType import sys import os +import re import textwrap import marshal @@ -238,6 +239,8 @@ class bdist_egg(Command): def zap_pyfiles(self): log.info("Removing .py files from temporary directory") + py3 = sys.version_info >= (3, 0) + re_pycache_file = re.compile('(.+)\.cpython-3\d(.pyc)$') for base, dirs, files in walk_egg(self.bdist_dir): for name in files: if name.endswith('.py'): @@ -245,6 +248,17 @@ class bdist_egg(Command): log.debug("Deleting %s", path) os.unlink(path) + if py3 and base.endswith('__pycache__'): + path_old = os.path.join(base, name) + + m = re_pycache_file.match(name) + path_new = os.path.join(base, os.pardir, m.group(1) + m.group(2)) + log.info("Renaming Python 3 .pyc file from [%s] to [%s]" % (path_old, path_new)) + if os.path.exists(path_new): + os.unlink(path_new) + os.rename(path_old, path_new) + + def zip_safe(self): safe = getattr(self.distribution, 'zip_safe', None) if safe is not None: -- cgit v1.2.1 From 0830232c2c700f09bbd39b8cddc2a0828399e7e8 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 9 Nov 2017 19:23:29 -0500 Subject: Unconditionally rename __pycache__ files if they exist. --- setuptools/command/bdist_egg.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'setuptools/command/bdist_egg.py') diff --git a/setuptools/command/bdist_egg.py b/setuptools/command/bdist_egg.py index 5bfc7ba2..f52c40d1 100644 --- a/setuptools/command/bdist_egg.py +++ b/setuptools/command/bdist_egg.py @@ -239,7 +239,6 @@ class bdist_egg(Command): def zap_pyfiles(self): log.info("Removing .py files from temporary directory") - py3 = sys.version_info >= (3, 0) re_pycache_file = re.compile('(.+)\.cpython-3\d(.pyc)$') for base, dirs, files in walk_egg(self.bdist_dir): for name in files: @@ -248,12 +247,12 @@ class bdist_egg(Command): log.debug("Deleting %s", path) os.unlink(path) - if py3 and base.endswith('__pycache__'): + if base.endswith('__pycache__'): path_old = os.path.join(base, name) - + m = re_pycache_file.match(name) path_new = os.path.join(base, os.pardir, m.group(1) + m.group(2)) - log.info("Renaming Python 3 .pyc file from [%s] to [%s]" % (path_old, path_new)) + log.info("Renaming file from [%s] to [%s]" % (path_old, path_new)) if os.path.exists(path_new): os.unlink(path_new) os.rename(path_old, path_new) -- cgit v1.2.1 From 9a79433af7e66ee5c1f721a1803dad4873666e62 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 9 Nov 2017 19:30:56 -0500 Subject: Use named groups in the pattern --- setuptools/command/bdist_egg.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'setuptools/command/bdist_egg.py') diff --git a/setuptools/command/bdist_egg.py b/setuptools/command/bdist_egg.py index f52c40d1..14dd97d3 100644 --- a/setuptools/command/bdist_egg.py +++ b/setuptools/command/bdist_egg.py @@ -239,7 +239,6 @@ class bdist_egg(Command): def zap_pyfiles(self): log.info("Removing .py files from temporary directory") - re_pycache_file = re.compile('(.+)\.cpython-3\d(.pyc)$') for base, dirs, files in walk_egg(self.bdist_dir): for name in files: if name.endswith('.py'): @@ -250,8 +249,9 @@ class bdist_egg(Command): if base.endswith('__pycache__'): path_old = os.path.join(base, name) - m = re_pycache_file.match(name) - path_new = os.path.join(base, os.pardir, m.group(1) + m.group(2)) + pattern = r'(?P.+)\.(?P[^.]+)\.pyc' + m = re.match(pattern, name) + path_new = os.path.join(base, os.pardir, m.group('name') + '.pyc') log.info("Renaming file from [%s] to [%s]" % (path_old, path_new)) if os.path.exists(path_new): os.unlink(path_new) -- cgit v1.2.1 From 2676c5a277c5e571cdaeed64e5494315d5ec4c1f Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 9 Nov 2017 19:34:54 -0500 Subject: Avoid check/act race condition --- setuptools/command/bdist_egg.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'setuptools/command/bdist_egg.py') diff --git a/setuptools/command/bdist_egg.py b/setuptools/command/bdist_egg.py index 14dd97d3..4e36c635 100644 --- a/setuptools/command/bdist_egg.py +++ b/setuptools/command/bdist_egg.py @@ -253,8 +253,10 @@ class bdist_egg(Command): m = re.match(pattern, name) path_new = os.path.join(base, os.pardir, m.group('name') + '.pyc') log.info("Renaming file from [%s] to [%s]" % (path_old, path_new)) - if os.path.exists(path_new): - os.unlink(path_new) + try: + os.remove(path_new) + except OSError: + pass os.rename(path_old, path_new) -- cgit v1.2.1 From 7874d4e0980265937b6957ba05805e00bacc0ed8 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 9 Nov 2017 19:36:07 -0500 Subject: Re-use path --- setuptools/command/bdist_egg.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'setuptools/command/bdist_egg.py') diff --git a/setuptools/command/bdist_egg.py b/setuptools/command/bdist_egg.py index 4e36c635..5fdb62d9 100644 --- a/setuptools/command/bdist_egg.py +++ b/setuptools/command/bdist_egg.py @@ -241,13 +241,14 @@ class bdist_egg(Command): log.info("Removing .py files from temporary directory") for base, dirs, files in walk_egg(self.bdist_dir): for name in files: + path = os.path.join(base, name) + if name.endswith('.py'): - path = os.path.join(base, name) log.debug("Deleting %s", path) os.unlink(path) if base.endswith('__pycache__'): - path_old = os.path.join(base, name) + path_old = path pattern = r'(?P.+)\.(?P[^.]+)\.pyc' m = re.match(pattern, name) -- cgit v1.2.1