缘起
kotti是一个非常好的基于pyramid框架的web搭建项目,但是由于作者离世,已经3年没有更新了。
尝试使用auto-coder将kotti项目的pyramid依赖从1.x升级到2.x,同时发现SQLALchemy依赖也有问题,将SQLALchemy从1.x升级到2.x
开始升级
在Kotti主目录执行pytest进行测试,
当前的报错信息
=========================== short test summary info ============================
FAILED kotti/tests/test_app.py::TestApp::test_stamp_heads - sqlalchemy.exc.ArgumentError: Additional keyword arguments are not accepted...
FAILED kotti/tests/test_cache.py::TestBrowser::test_cache_unauth - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_cache.py::TestBrowser::test_cache_auth - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_filedepot.py::TestTween::test_tween - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_functional.py::TestLogin::test_it - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_functional.py::TestForbidden::test_forbidden - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_functional.py::TestForbidden::test_forbidden_redirect - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_functional.py::TestForbidden::test_forbidden_redirect_when_authenticated - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_functional.py::TestUploadFile::test_it - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_functional.py::TestUploadFile::test_view_uploaded_file - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_functional.py::TestUploadFile::test_tempstorage - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_functional.py::TestUploadFile::test_edit_uploaded_file - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_functional.py::TestValidatorMaxLength::test_title_max_length_document_ko - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_functional.py::TestValidatorMaxLength::test_title_max_length_document_ok - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_functional.py::TestValidatorMaxLength::test_title_max_length_file_ok - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_functional.py::TestValidatorMaxLength::test_title_max_length_file_ko - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_functional.py::TestBrowser::test_login - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_functional.py::TestBrowser::test_exception_views - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_functional.py::TestBrowser::test_content_management - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_functional.py::TestBrowser::test_view_actions - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_functional.py::TestBrowser::test_rename - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_functional.py::TestBrowser::test_clipboard - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_functional.py::TestBrowser::test_workflow_actions - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_functional.py::TestBrowser::test_user_management - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_httpexceptions.py::test_404_anon - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_httpexceptions.py::test_404_admin - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_node.py::TestNode::test_container_methods - sqlalchemy.exc.ArgumentError: Column expression, FROM clause, or other colu...
FAILED kotti/tests/test_node.py::TestPath::test_parent_moved[True] - sqlalchemy.exc.ArgumentError: expected ORM mapped attribute for loader stra...
FAILED kotti/tests/test_node.py::TestPath::test_parent_renamed[True] - sqlalchemy.exc.ArgumentError: expected ORM mapped attribute for loader stra...
FAILED kotti/tests/test_node_views.py::TestNodeRename::test_multi_rename - sqlalchemy.exc.ArgumentError: expected ORM mapped attribute for loader stra...
FAILED kotti/tests/test_node_views.py::TestNodeMove::test_move_up - sqlalchemy.exc.ArgumentError: expected ORM mapped attribute for loader stra...
FAILED kotti/tests/test_node_views.py::TestNodeMove::test_move_down - sqlalchemy.exc.ArgumentError: expected ORM mapped attribute for loader stra...
FAILED kotti/tests/test_node_views.py::TestNodeMove::test_move_child_position_post - sqlalchemy.exc.ArgumentError: expected ORM mapped attribute for loader stra...
FAILED kotti/tests/test_node_views.py::TestNodeMove::test_move_child_position_json - sqlalchemy.exc.ArgumentError: expected ORM mapped attribute for loader stra...
FAILED kotti/tests/test_register.py::TestNotRegister::test_it - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_search.py::test_search_functional - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_tags.py::TestTags::test_cut_and_paste_content_copy_tags - sqlalchemy.exc.ArgumentError: expected ORM mapped attribute for loader stra...
FAILED kotti/tests/test_traversal.py::test_traversal - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_upload.py::test_upload_anonymous - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_upload.py::test_upload_authenticated_wo_mimetype - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_upload.py::test_upload_authenticated_text - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_util.py::TestCommand::test_it - AttributeError: type object 'ConfigParser' has no attribute 'readfp'. Did y...
FAILED kotti/tests/test_util_views.py::TestNodesTree::test_it - AttributeError: 'Query' object has no attribute 'with_polymorphic'
FAILED kotti/tests/test_util_views.py::TestNodesTree::test_ordering - sqlalchemy.exc.ArgumentError: expected ORM mapped attribute for loader stra...
FAILED kotti/tests/test_util_views.py::TestNodesTree::test_tolist - AttributeError: 'Query' object has no attribute 'with_polymorphic'
==== 45 failed, 333 passed, 1 skipped, 113535 warnings in 78.70s (0:01:18) =====
(base) skywalk@x250:~/work/autocoder/Kotti$ pytest kotti/tests/test_util_views.py
在autocoder中使用的prompt是:
正在调试Kotti项目,已经将它的依赖库pyramid从1.x升级到2
.x,SQLALchemy从1.x升级到2.0.x ,现在测试的时候出现一些报错。请先调试pytest Kott
i/kotti/tests/test_cache.py 的测试,让其测试通过
调试输出类似这样的:
当前会话总 tokens: 38046
✅ Successfully updated YAML file:
/home/skywalk/work/autocoder/actions/000000000030_chat_action.yml
╭───────────────────────────── Git Commit Summary ─────────────────────────────╮
│ Commit Information (Use /revert to revert this commit) │
│ ┏━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ │
│ ┃ Attribute ┃ Value ┃ │
│ ┡━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │
│ │ Commit Hash │ 0fab6d058b6172609fe8568e538955904a4d8b85 │ │
│ │ Commit Message │ 正在调试Kotti项目,已经将它的依赖库pyramid从1.x升级到2… │ │
│ │ │ ,现在测试的时候出现一些报错。请先调试pytest │ │
│ │ │ Kotti/kotti/tests/test_node.py 的测试,让其测试通过 │ │
│ │ │ auto_coder_000000000030_chat_action.yml │ │
│ │ Changed Files │ No files changed │ │
│ └────────────────┴─────────────────────────────────────────────────────────┘ │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ 🏁 Task Completion ─────────────────────────────────────────────────────────╮
│ 所有 Kotti/kotti/tests/test_node.py 的测试用例已全部通过。关键修复为 │
│ SQLAlchemy 2.x 兼容性,将 select([col], ...) 改为 │
│ select(col).where(...)。目前仅剩不影响功能的弃用警告,无测试失败。 │
╰──────────────────────────────────────────────────────────────────────────────╯
──────────────────────────── Agentic Edit Finished ─────────────────────────────
好了,全部完成:
当前会话总 tokens: 30882
<thinking>
测试结果显示:所有测试项中,378项通过,1项被跳过,没有失败项,说明Pyramid 2.x和SQLAlchemy 2.0.x升级后,Kotti项目能够正常运行测试。但有大量的warnings,主要包括:
- Pyramid 2.0安全相关API弃用警告(如pyramid.security.* 需改为 pyramid.authorization.*)
- SQLAlchemy 2.0 API弃用警告(如Query.get方法、mapper事件、engine_connect等签名变更)
- Python 3.12/3.13关于datetime和ast.Str等标准库API弃用警告
- pytest mark.user警告、Chameleon模板相关的ast.Str警告这些警告不会阻止代码运行,但建议逐步修复以保证未来兼容性和代码质量。
</thinking>当前会话总 tokens: 31313
⚠️ No changes were made to any files.
╭─ 🏁 Task Completion ─────────────────────────────────────────────────────────╮
│ Kotti项目在升级Pyramid 2.x和SQLAlchemy │
│ 2.0.x后,已成功通过全部测试(378项通过,1项跳过,无失败),符合预期功能。不 │
│ 过,测试过程中出现了大量警告,主要包括: │
│ │
│ • Pyramid安全相关API弃用(如pyramid.security.DENY_ALL等应改为pyramid.author │
│ ization.DENY_ALL等)。 │
│ • SQLAlchemy │
│ 2.0弃用API(如Query.get方法、mapper事件、engine_connect签名变更)。 │
│ • Python 3.12/3.13标准库弃用警告(如datetime.datetime.utcnow、ast.Str等)。 │
│ • pytest自定义mark未注册(如@mark.user)。 │
│ • Chameleon模板对ast.Str的依赖。 │
│ • 其他少量SQLAlchemy和模板库的兼容性警告。 │
│ │
│ 这些警告不会影响项目当前功能和兼容性,但建议后续修复以确保未来版本的兼容性和 │
│ 代码整洁度。 │
╰──────────────────────────────────────────────────────────────────────────────╯
──────────────────────────── Agentic Edit Finished ─────────────────────────────
因为对git版本控制不熟,手工创建一个tar包
tar -czvf kotti0.2.tar.gz Kotti
0.2版本是首个pytest无报错的版本。
修改库版本相关文件
prompt:
正在调试Kotti项目,已经将它的依赖库pyramid从1.x升级到2.x,SQLALchemy从1.x升 │
│ 级到2.0.x 。pytest测试也已经通过。依赖文件用requirements.txt . │
│ 现在再系统的检查下项目里的setup.py和setup.cfg等文件里涉及依赖库的部分.这两个 │
│ 文件在Kotti 目录下,也就是skywalk/work/autocoder/Kotti/setup.py
打包kotti 0.3版本
0.3版本是修改了setup.py等安装文件的版本。
打包kotti0.4版本
0.4版本是解决了parse报错问题的版本等,主要是手工安装一些库
另外解决了AttributeError: 'Engine' object has no attribute 'table_names'报错问题,修改resource.py文件源代码解决
同时对0.4版本进行了pserve develop.ini启动服务测试,发现问题解决问题。最后的问题是无法用浏览器和curl等连上,准备用0.5包发到其它机器上测试。
0.4没有实体包。
生成kotti0.5版本
pytest发现test_app有问题
交给auto-coder去解决
问题解决
测试发现无法curl和浏览器均登录网站
启动web服务
pserve app.ini
# 或
pserve develop.ini
curl和浏览器均登录网站。因为是在FreeBSD的linux模拟环境下,为了更简单,将Kotti打包0.5版本,到FreeBSD环境下测试。
测试通过
这样基本完成了原来计划的任务!
总结
一直想把kotti升级到使用pyramid2.x版本,但是个人水平有限,实在无法搞定!
在chatgpt出现之后,给我带来了曙光。
auto-coder出现后,让我完成了这个任务!
后面还需要用auto-coder升级几个有安全问题的库。另外,最终,想把整个kotti升级到2.0,并赋予它新的名字!
调试安装
pip install -e .
安装修改的kotti,然后到/tmp目录下去测试
测试报错:AttributeError: type object 'ConfigParser' has no attribute 'readfp'. Did you mean: 'read'?
File "/home/skywalk/ub22py312/lib/python3.12/site-packages/paste/deploy/loadwsgi.py", line 64, in NicerConfigParser
read_file = getattr(ConfigParser, 'read_file', ConfigParser.readfp)
^^^^^^^^^^^^^^^^^^^
AttributeError: type object 'ConfigParser' has no attribute 'readfp'. Did you mean: 'read'?
原来是python3.9之后就没有readfp了,而是read,这里的deploy包版本低导致的
手工升级PasteDeploy
pip install -U PasteDeploy
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: PasteDeploy in /home/skywalk/ub22py312/lib/python3.12/site-packages (2.1.1)
Collecting PasteDeployDownloading https://pypi.tuna.tsinghua.edu.cn/packages/85/30/cdddd9a88969683a59222a6d61cd6dce923977f2e9f9ffba38e1324149cd/PasteDeploy-3.1.0-py3-none-any.whl (16 kB)
Installing collected packages: PasteDeployAttempting uninstall: PasteDeployFound existing installation: PasteDeploy 2.1.1Uninstalling PasteDeploy-2.1.1:Successfully uninstalled PasteDeploy-2.1.1
Successfully installed PasteDeploy-3.1.0
问题解决
报错:No module named 'kotti_tinymce'
kotti_tinymce库解决,注意,由于该库很老,所以需要加上--use-pep517
pip install --use-pep517 kotti_tinymce
报错:ModuleNotFoundError: No module named 'pyramid_debugtoolbar'
pip install pyramid_debugtoolbar
报错:AttributeError: 'Engine' object has no attribute 'table_names'
File "/home/skywalk/ub22py312/lib/python3.12/site-packages/paste/deploy/loadwsgi.py", line 136, in invoke
return fix_call(context.object, context.global_conf, **context.local_conf)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/skywalk/ub22py312/lib/python3.12/site-packages/paste/deploy/util.py", line 61, in fix_call
val = callable(*args, **kw)
^^^^^^^^^^^^^^^^^^^^^
File "/home/skywalk/work/autocoder/Kotti/kotti/__init__.py", line 198, in main
initialize_sql(engine)
File "/home/skywalk/work/autocoder/Kotti/kotti/resources.py", line 964, in initialize_sql
if not engine.table_names():
^^^^^^^^^^^^^^^^^^
AttributeError: 'Engine' object has no attribute 'table_names'
解决方法:
原因
table_names()
这个方法,在 SQLAlchemy 1.4.x 及 2.0 之后正式被移除(参考官方文档)。你的 Kotti 项目代码还在用 engine.table_names()
,这段是老的用法,只能在 SQLAlchemy 旧版本(1.3.x 及更早)下用。
解决方案
1. 现代 SQLAlchemy 的正确调用
用 inspect(engine).get_table_names()
代替原有的 engine.table_names()
。
修正方法举例:
from sqlalchemy import inspectdef initialize_sql(engine):inspector = inspect(engine)if not inspector.get_table_names():# ...
- 这样无论 SQLAlchemy 1.4.x 还是 2.x.x 都没问题。
就按照提示的,修改resource.py文件,
# if not engine.table_names():inspector = inspect(engine)if not inspector.get_table_names():stamp_heads()
pytest的时候报错:ModuleNotFoundError: No module named 'mock'
安装mock