From 63c7bc4577c1aa8a35e7af5677d275cf8a76637d Mon Sep 17 00:00:00 2001 From: Changaco Date: Thu, 3 Dec 2015 17:25:15 +0100 Subject: [PATCH 01/10] fix and simplify option 1 There is no need to decode the file in order to extract the version number from it. --- source/single_source_version.rst | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/source/single_source_version.rst b/source/single_source_version.rst index 54da6bd7b7..57a8983ed6 100644 --- a/source/single_source_version.rst +++ b/source/single_source_version.rst @@ -14,19 +14,11 @@ number of your project: #. Read the file in ``setup.py`` and parse the version with a regex. Example ( from `pip setup.py `_):: - def read(*names, **kwargs): - with io.open( - os.path.join(os.path.dirname(__file__), *names), - encoding=kwargs.get("encoding", "utf8") - ) as fp: - return fp.read() - def find_version(*file_paths): - version_file = read(*file_paths) - version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", - version_file, re.M) - if version_match: - return version_match.group(1) + with open(os.path.join(os.path.dirname(__file__), *file_paths), 'rb') as f: + match = re.search(br"^__version__ = ['\"]([^'\"]*)['\"]", f.read(), re.M) + if match: + return match.group(1) raise RuntimeError("Unable to find version string.") setup( From 0aaf755174f263a2f31d1ff326c29b2ae942662d Mon Sep 17 00:00:00 2001 From: Changaco Date: Thu, 3 Dec 2015 17:32:02 +0100 Subject: [PATCH 02/10] remove variant that only works with python 2 --- source/single_source_version.rst | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/source/single_source_version.rst b/source/single_source_version.rst index 57a8983ed6..3d577c20a3 100644 --- a/source/single_source_version.rst +++ b/source/single_source_version.rst @@ -41,19 +41,7 @@ number of your project: #. Set the value to a ``__version__`` global variable in a dedicated module in your project (e.g. ``version.py``), then have ``setup.py`` read and ``exec`` the - value into a variable. - - Using ``execfile``: - - :: - - execfile('...sample/version.py') - # now we have a `__version__` variable - # later on we use: __version__ - - Using ``exec``: - - :: + value into a variable:: version = {} with open("...sample/version.py") as fp: From 0349bde1305fb7f75184b5669f5700f240277cd5 Mon Sep 17 00:00:00 2001 From: Changaco Date: Thu, 3 Dec 2015 17:42:00 +0100 Subject: [PATCH 03/10] fix option 3: open the file in binary mode `exec()` can handle the decoding better --- source/single_source_version.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/single_source_version.rst b/source/single_source_version.rst index 3d577c20a3..dff4904e91 100644 --- a/source/single_source_version.rst +++ b/source/single_source_version.rst @@ -44,7 +44,7 @@ number of your project: value into a variable:: version = {} - with open("...sample/version.py") as fp: + with open("...sample/version.py", 'rb') as fp: exec(fp.read(), version) # later on we use: version['__version__'] From 676374e265b1fa305427121535390d1fe5ee6a10 Mon Sep 17 00:00:00 2001 From: Changaco Date: Thu, 3 Dec 2015 17:49:16 +0100 Subject: [PATCH 04/10] handle paths properly --- source/single_source_version.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/single_source_version.rst b/source/single_source_version.rst index dff4904e91..9e50c40b2e 100644 --- a/source/single_source_version.rst +++ b/source/single_source_version.rst @@ -43,8 +43,9 @@ number of your project: your project (e.g. ``version.py``), then have ``setup.py`` read and ``exec`` the value into a variable:: + base_dir = os.path.dirname(__file__) version = {} - with open("...sample/version.py", 'rb') as fp: + with open(os.path.join(base_dir, "module_name", "version.py"), 'rb') as fp: exec(fp.read(), version) # later on we use: version['__version__'] From 8bf114a466ea48e381d93f5ee976d8b844d263fd Mon Sep 17 00:00:00 2001 From: Changaco Date: Thu, 3 Dec 2015 17:52:23 +0100 Subject: [PATCH 05/10] explicitly show the `setup()` call like above --- source/single_source_version.rst | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/source/single_source_version.rst b/source/single_source_version.rst index 9e50c40b2e..f72dcaaf07 100644 --- a/source/single_source_version.rst +++ b/source/single_source_version.rst @@ -47,7 +47,12 @@ number of your project: version = {} with open(os.path.join(base_dir, "module_name", "version.py"), 'rb') as fp: exec(fp.read(), version) - # later on we use: version['__version__'] + + setup( + ... + version=version['__version__'] + ... + ) Example using this technique: `warehouse `_. From 23e18e02acf850887247f46cabc2c201e23a56c3 Mon Sep 17 00:00:00 2001 From: Changaco Date: Thu, 3 Dec 2015 17:53:20 +0100 Subject: [PATCH 06/10] add imports so the reader can simply copy-paste --- source/single_source_version.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/source/single_source_version.rst b/source/single_source_version.rst index f72dcaaf07..7fc6cfc3a0 100644 --- a/source/single_source_version.rst +++ b/source/single_source_version.rst @@ -14,6 +14,9 @@ number of your project: #. Read the file in ``setup.py`` and parse the version with a regex. Example ( from `pip setup.py `_):: + import os.path + import re + def find_version(*file_paths): with open(os.path.join(os.path.dirname(__file__), *file_paths), 'rb') as f: match = re.search(br"^__version__ = ['\"]([^'\"]*)['\"]", f.read(), re.M) @@ -43,6 +46,8 @@ number of your project: your project (e.g. ``version.py``), then have ``setup.py`` read and ``exec`` the value into a variable:: + import os.path + base_dir = os.path.dirname(__file__) version = {} with open(os.path.join(base_dir, "module_name", "version.py"), 'rb') as fp: From 5b23f1a39ae30af1b36fde5beedf79131b7fdf61 Mon Sep 17 00:00:00 2001 From: Changaco Date: Thu, 3 Dec 2015 17:56:08 +0100 Subject: [PATCH 07/10] use `__about__.py` instead of `version.py` to be coherent with the example linked to below --- source/single_source_version.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/source/single_source_version.rst b/source/single_source_version.rst index 7fc6cfc3a0..942cd6355d 100644 --- a/source/single_source_version.rst +++ b/source/single_source_version.rst @@ -43,19 +43,19 @@ number of your project: #. Set the value to a ``__version__`` global variable in a dedicated module in - your project (e.g. ``version.py``), then have ``setup.py`` read and ``exec`` the + your project (e.g. ``__about__.py``), then have ``setup.py`` read and ``exec`` the value into a variable:: import os.path base_dir = os.path.dirname(__file__) - version = {} - with open(os.path.join(base_dir, "module_name", "version.py"), 'rb') as fp: - exec(fp.read(), version) + about = {} + with open(os.path.join(base_dir, "module_name", "__about__.py"), 'rb') as fp: + exec(fp.read(), about) setup( ... - version=version['__version__'] + version=about['__version__'] ... ) From 09472c855feb41c979098e919b79aacf3d0b656f Mon Sep 17 00:00:00 2001 From: Changaco Date: Thu, 3 Dec 2015 17:57:41 +0100 Subject: [PATCH 08/10] add comments to explain why we do it that way --- source/single_source_version.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/source/single_source_version.rst b/source/single_source_version.rst index 942cd6355d..21f86b33d9 100644 --- a/source/single_source_version.rst +++ b/source/single_source_version.rst @@ -18,6 +18,10 @@ number of your project: import re def find_version(*file_paths): + # We read __version__ instead of importing it because that would fail if + # the module it's in tries to import things from dependencies that aren't + # installed yet when setup.py is being run. + # Moreover, imports can fail if sys.path isn't properly set. with open(os.path.join(os.path.dirname(__file__), *file_paths), 'rb') as f: match = re.search(br"^__version__ = ['\"]([^'\"]*)['\"]", f.read(), re.M) if match: @@ -48,6 +52,8 @@ number of your project: import os.path + # We exec the module instead of importing it because imports can fail if + # sys.path isn't properly set. base_dir = os.path.dirname(__file__) about = {} with open(os.path.join(base_dir, "module_name", "__about__.py"), 'rb') as fp: From ad5eff8287ed50c08ee030cd53ea45f38dcc7f2e Mon Sep 17 00:00:00 2001 From: Changaco Date: Sat, 12 Dec 2015 10:53:25 +0100 Subject: [PATCH 09/10] add comment explaining why we manipulate bytes --- source/single_source_version.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/single_source_version.rst b/source/single_source_version.rst index 21f86b33d9..f25b6dce26 100644 --- a/source/single_source_version.rst +++ b/source/single_source_version.rst @@ -22,6 +22,8 @@ number of your project: # the module it's in tries to import things from dependencies that aren't # installed yet when setup.py is being run. # Moreover, imports can fail if sys.path isn't properly set. + # Since version numbers only use a subset of ascii, we can read the file + # as bytes, thus avoiding unicode decode errors. with open(os.path.join(os.path.dirname(__file__), *file_paths), 'rb') as f: match = re.search(br"^__version__ = ['\"]([^'\"]*)['\"]", f.read(), re.M) if match: From 90197a64318711d1c4c6dbd9ff9d54bff50e376f Mon Sep 17 00:00:00 2001 From: Changaco Date: Tue, 19 Jan 2016 13:30:39 +0100 Subject: [PATCH 10/10] don't clutter the `about` dict with globals --- source/single_source_version.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/single_source_version.rst b/source/single_source_version.rst index f25b6dce26..c4c2c6b72a 100644 --- a/source/single_source_version.rst +++ b/source/single_source_version.rst @@ -59,7 +59,7 @@ number of your project: base_dir = os.path.dirname(__file__) about = {} with open(os.path.join(base_dir, "module_name", "__about__.py"), 'rb') as fp: - exec(fp.read(), about) + exec(fp.read(), {}, about) setup( ...