Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 18 additions & 30 deletions colcon_python_setup_py/package_identification/python_setup_py.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Copyright 2016-2018 Dirk Thomas
# Licensed under the Apache License, Version 2.0

import ast
import distutils.core
import os
import pickle
from pathlib import Path
import runpy
try:
Expand Down Expand Up @@ -134,11 +134,11 @@ def get_setup_arguments(setup_py):
setuptools.setup = setuptools_setup
except NameError:
pass
# filter out any data which doesn't work with ast.literal_eval
# filter out any data which doesn't serialize
for key, value in list(data.items()):
try:
ast.literal_eval(repr(value))
except SyntaxError:
pickle.dumps(value)
except pickle.PicklingError:
del data[key]
return data

Expand Down Expand Up @@ -226,20 +226,22 @@ def get_setup_arguments_with_context(setup_py, env):
pkg_path = str(pkg_path).replace(os.sep, os.altsep)
setup_py = str(setup_py).replace(os.sep, os.altsep)
code_lines = [
'import pickle',
'import sys',
"sys.path.insert(0, '%s')" % pkg_path,
'from colcon_python_setup_py.package_identification.python_setup_py'
' import get_setup_arguments',
"output = repr(get_setup_arguments('%s'))" % setup_py,
"sys.stdout.buffer.write(output.encode('utf-8'))"]
"output = get_setup_arguments('%s')" % setup_py,
'pickle.dump(output, sys.stdout.buffer)'
]

# invoke get_setup_arguments() in a separate interpreter
cmd = [sys.executable, '-c', ';'.join(code_lines)]
result = subprocess.run(
cmd, stdout=subprocess.PIPE, env=env, check=True)
output = result.stdout.decode('utf-8')
output = result.stdout

return ast.literal_eval(output)
return pickle.loads(output)


_setup_information_cache = {}
Expand Down Expand Up @@ -269,38 +271,24 @@ def get_setup_information(setup_py, *, env=None):
def _get_setup_information(setup_py, *, env=None):
code_lines = [
'import sys',
'import pickle',
'from distutils.core import run_setup',

'dist = run_setup('
" 'setup.py', script_args=('--dry-run',), stop_after='config')",

"skip_keys = ('cmdclass', 'distclass', 'ext_modules', 'metadata')",
'data = {'
' key: value for key, value in dist.__dict__.items() '
' if ('
# skip private properties
" not key.startswith('_') and "
# skip methods
' not callable(value) and '
# skip objects whose representation can't be evaluated
' key not in skip_keys and '
# skip display options since they have no value, using metadata instead
' key not in dist.display_option_names'
' )'
'}',
"data['metadata'] = {"
' k: v for k, v in dist.metadata.__dict__.items() '
# skip values with custom type OrderedSet
" if k not in ('license_files', 'provides_extras')}",

"sys.stdout.buffer.write(repr(data).encode('utf-8'))"]
'pickle.dump(dist, sys.stdout.buffer)']

# invoke distutils.core.run_setup() in a separate interpreter
cmd = [
sys.executable, '-c', ';'.join(line.lstrip() for line in code_lines)]
result = subprocess.run(
cmd, stdout=subprocess.PIPE,
cwd=os.path.abspath(str(setup_py.parent)), check=True, env=env)
output = result.stdout.decode('utf-8')
output = result.stdout
dist = pickle.loads(output)

return ast.literal_eval(output)
# turn into a dict for backwards compatibility
dist_dict = dist.__dict__.copy()
dist_dict['metadata'] = dist.metadata.__dict__
return dist_dict
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ keywords = colcon
[options]
install_requires =
colcon-core>=0.3.10
setuptools
# DistributionMetadata not picklable in these versions https://github.com/pypa/setuptools/issues/1888
setuptools !=42.*,!=43.*,!=44.*,!=45.*
packages = find:
tests_require =
flake8>=3.6.0
Expand Down
1 change: 0 additions & 1 deletion test/spell_check.words
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
apache
chdir
colcon
distclass
hashable
iterdir
lstrip
Expand Down