为不同版本的 Python 维护不同版本的代码库的工作流程
我正在开发一个名为 GarlicSim 的开源应用程序。
到目前为止,我只为 Python 2.6 开发它。它似乎不适用于任何其他版本。
我认为生成支持其他 Python 版本的版本非常重要。我想我会制作一个 2.5、3.1 甚至 2.4 的版本。
所以我有几个问题:
- 组织我的存储库的文件夹结构以包含这些不同版本的好方法是什么?
- 将我在一个版本的代码中所做的更改“合并”到其他版本的好方法是什么?我知道如何在我的 SCM(即 git)中进行合并,但这些文件夹都位于同一个存储库中,我想在它们之间进行合并。当然可以选择为每个版本都有一个存储库,但我认为这不是一个好主意。
有人有什么建议吗?
I'm developing an open source application called GarlicSim.
Up to now I've been developing it only for Python 2.6. It seems not to work on any other version.
I decided it's important to produce versions of it that will support other versions of Python. I'm thinking I'll make a version for 2.5, 3.1 and maybe 2.4.
So I have several questions:
- What would be a good way to organize the folder structure of my repo to include these different versions?
- What would be a good way to 'merge' changes I do in one version of the code to other versions? I know how to do merges in my SCM (which is git), but these are folders that are all in the same repo, and I want to do a merge between them. There is of course the option of having a repo for each version, but I think it's not a good idea.
Does anyone have any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
仅在极少数情况下,您才需要为单独的版本提供单独的分支。你提到了上下文管理器,它们很棒,不使用它们会很糟糕,你是对的。但对于 Python 2.4 你将不得不不使用它们。所以那会很糟糕。因此,如果您想支持 Python 2.4,您必须编写一个没有上下文管理器的版本。但该版本也可以在 Python 2.6 下运行,因此没有必要在那里拥有单独的版本。
至于Python 3,拥有一个单独的分支是一种解决方案,但通常不是最好的解决方案。
对于 Python 3 支持,有一个名为 2to3 的工具,它会将您的 Python 2 代码转换为 Python 3 代码。它并不完美,因此通常您必须修改 Python 2 代码才能生成漂亮的 Python 3 代码,但无论如何,Python 2 代码有变得更好的趋势。
使用 Distribute(安装工具的一个维护分支),您可以在安装过程中自动进行此对话。这样,即使对于 Python 3,您也不必有单独的分支。请参阅 http://bitbucket.org/tarek/distribute/src/tip/docs/python3.txt 有关于此的文档。
正如 Paul McGuire 所写,甚至可以使用相同的代码支持 Python 3 和 Python 2,而不使用 2to3,但如果您想支持 2.6 和 3.x 以外的任何版本,我不会推荐它。你已经受够了这种丑陋的特殊黑客手段了。 2.6 与 Python 3 有足够的前向兼容性,可以编写美观的代码并支持 Python 2.6 和 3.x,但不支持 Python 2.5 和 3.x。
You need separate branches for separate versions only in the rarest of cases. You mention context managers, and they are great, and it would suck not to use them, and you are right. But for Python 2.4 you will have to not use them. So that will suck. So therefore, if you want to support Python 2.4 you'll have to write a version without context managers. But that one will work under Python 2.6 too, so there is no point in having separate versions there.
As for Python 3, having a separate branch there is a solution, but generally not the best one.
For Python 3 support there is something called 2to3 which will convert your Python 2 code to Python 3 code. It's not perfect, so quite often you will have to modify the Python 2 code to generate nice Python 3 code, but the Python 2 code has a tendency to become better as a result anyway.
With Distribute (a maintained fork of setuptools) you can make this conversation automatically during install. That way you don't have to have a separate branch even for Python 3. See http://bitbucket.org/tarek/distribute/src/tip/docs/python3.txt for the docs on that.
As Paul McGuire writes it is even possible to support Python 3 and Python 2 with the same code without using 2to3, but I would not recommend it if you want to support anything else than 2.6 and 3.x. You get too much of this ugly special hacks. With 2.6 there is enough forwards compatibility with Python 3 to make it possible to write decent looking code and support both Python 2.6 and 3.x, but not Python 2.5 and 3.x.
我会尝试维护一个分支来覆盖所有 python 2.4-2.6
差异不是很大,毕竟如果你必须为 2.4 编写一堆额外的代码来完成在 2.6 中很容易的事情,那么工作量会更少为了你长远使用2.4版本换2.5和2.6。
Python 3 应该有一个不同的分支,您仍然应该尝试尽可能多地保留共同的代码。
I would try to maintain one branch to cover all the python 2.4-2.6
The differences are not so great, after all if you have to write a bunch of extra code for 2.4 to do something that is easy in 2.6, it will be less work for you in the long run to use the 2.4 version for 2.5 and 2.6.
Python 3 should have a different branch, you should still try to keep as much code in common as you can.
如果您的代码不太依赖异常处理程序中的运行时性能,您甚至可能无需为 Py3 建立单独的分支即可逃脱。我设法为我的所有 Py2.x 版本保留一个版本的 pyparsing,尽管我必须坚持使用“最低公分母”方法,这意味着我必须放弃使用一些构造,例如生成器表达式,并且你的观点是,上下文管理者。我使用字典代替集合,并且所有生成器表达式都被包装为列表推导式,因此它们仍然可以追溯到 Python 2.3。我的代码顶部有一个块,负责处理许多 2vs3 问题(由 pyparsing 用户 Robert A Clark 贡献):
我遇到的最大困难是捕获异常的不兼容语法,这是在Py3,从 改为
当然
,如果你真的不需要异常变量,你可以只写:
这将在 Py2 或 Py3 上工作。但是如果你需要访问异常,你仍然可以想出一个跨版本兼容的语法,如下所示:
这有一个小的运行时惩罚,这使得它在 pyparsing 的某些地方不可用,所以我仍然需要维护单独的 Py2和 Py3 版本。对于源代码合并,我使用实用程序 WinMerge,我发现它非常适合保持源代码目录同步。
因此,即使我保留了代码的两个版本,其中一些统一技术也可以帮助我将差异降至绝对不兼容的最低限度。
If your code is not overly dependent on the run-time performance in exception handlers, you might even get away without having a separate branch for Py3. I've managed to keep one version of pyparsing for all of my Py2.x versions, although I've had to stick with a "lowest common denominator" approach, meaning that I have to forego using some constructs like generator expressions, and to your point, context managers. I use dicts in place of sets, and all my generator expressions get wrapped as list comprehensions, so they will still work going back to Python 2.3. I have a block at the top of my code that takes care of a number of 2vs3 issues (contributed by pyparsing user Robert A Clark):
The biggest difficulty I've had has been with the incompatible syntax for catching exceptions, that was introduced in Py3, changing from
to
Of course, if you don't really need the exception variable, you can just write:
and this will work on Py2 or Py3. But if you need to access the exception, you can still come up with a cross-version compatible syntax as:
This has a minor run-time penalty, which makes this unusable in some places in pyparsing, so I still have to maintain separate Py2 and Py3 versions. For source merging, I use the utility WinMerge, which I find very good for keeping directories of source code in synch.
So even though I keep two versions of my code, some of these unification techniques help me to keep the differences down to the absolute incompatible minimum.
我最终决定为我的项目使用 4 个不同的分支,即 2.4、2.5、2.6 和 3.1。我的主要优先事项是 2.6,我不想为了 2.4 而牺牲该代码的优雅性。因此,丑陋的兼容性黑客将出现在较低版本上,而不是较高版本上。
I eventually decided to have 4 different forks for my project, for 2.4, 2.5, 2.6 and 3.1. My main priority is 2.6 and I don't want to compromise the elegance of that code for the sake of 2.4. So the ugly compatibility hacks will be on the lower versions, not the higher versions.