Core Concept-Open Graph 01

本文介绍Facebook的OpenGraph如何让应用与其核心社交体验深度整合,包括Timeline、News Feed和Ticker等。通过定义自定义的动作和对象,开发者可以更好地模拟用户行为,增强用户体验。

FB的核心就是Social Graph,即社交图谱。用户和他们的联系人,以及所有他们关心的事物。最早,FB推出了这么一个图谱,并且随着时间的推移加入了新的产品,比如图片,地理位置。2010年的时候,FB通过使用Open Graph Protocol引入了一个早期版本的Open Graph,它是Social Graph的加强版,可以包含用户们喜欢的第三方网站和页面。现在的Open Graph可以包含第三方应用创建的任何的活动和对象,从而使得这些应用能够深度地被整合进入FB的社交体验。

当用户将你的app加入到他们的Timeline以后,和app相关的行为都会通过Open Graph被分享到FB上面。随着你的app逐渐成为用户表达自己的重要部分,这些行为会被更加突出地通过Timeline,News Feed,和Ticker来显示。如此一来,你的app就成为用户和他们好友的FB体验的重要组成部分。

Open Graph会允许apps通过用户的actions和那些objects来将用户的活动模型化。一个running app可能会定义run(action)和route(object)。一个reading app可能会定义read(action)和book(object)。Actions通常是指用户在apps里面做什么。而Objects定义那些actions适用的名词。FB创建了很多的actions和objects,用于通常使用,还有一个工具能够让你定制化你想要的加入到你的app里面的actions和objects。当用户绑定在你的app上的时候,社交活动会通过你的actions被发布到FB上面来连接你的app和用户的objects。

有了Open Graph,你的app就成为用户身份和Social Graph的一部分。通过一个API,就可以和FB进行深度的整合,从而挖掘出更多的用户,在用户和app之间创建一个深度持久的连接。


1. Key Concepts

构建一个Open Graph app让你有机会将你的app深度整合到FB的核心体验,包括Timeline,News,Feed和Ticker。

1). Actions and Objects

这个是构建Open Graph的基石。Actions是用户能在你的app里面进行的上层的交互。Objects表示用户能在你的app里面交互的内容。以前,用户可以通过使用Like Button来连接你的app里面的内容。现在,除了那个之外,还可以定制属于你的app的特定的actions和objects,模型化用户的交互,从而和用户建立更加深厚的连接。

首先你必须在App Dashboard里面定义你自己的action和object类型。比如,一个running app可以定义"Routes"和"Marathons"作为相关的object类型。"Run"作为action类型。一个cooking app可以定义"Recipe"和"Menu"作为object type,"Cook"作为action type。

一旦你的action types和object types定义好了以后,你需要创建和发布你的objects到Open Graph。Open Graph Objects其实就是包含了有Open Graph信息的<meta>标签的网页,然后嵌入到<head>里面。这些<meta>标签描述object的类型,object的名字,还有其它重要的信息。比如说,要为recipt app创建一个"Stuffed Cookie" object,那么就应该添加一些<meta>标签,这些标签会说明这个object的类型是一个recipe,它的title是"Stuffed Cookies",URL的它的展示图片。

2). Open Graph Mechanics

当用户在你的app里面干了一件事情,比如cook a "Stuffed Cookie",这个app就会调用Graph API来发布一个新的cook action type的实例,这个实例会将用户和一个"Stuffed Cookie"连起来,而"Stuffed Cookie"就是一个recipe的对象实例。这个过程是通过发布一个HTTP POST连带这个recipt的URL到用户的 /me/myapp:cook连接。FB随后就会进入该object页面,并且读取它的元数据,通过这个action将这个object连接到用户那里去。

下面的这个流程表展示了该过程:

a. 用户在该app中做一件事情,比如cook a recipe

b. App调用一个HTTP POST给Graph API终端  /me/appnamesapce:actiontype?objective=Object_URL

c. FB会进入该object页面,读取它的meta tags,然后通过这个action将用户和这个object连接起来。

3).Social Channels

使用一个API,Open Graph actions可以出现在任何Social Channel上面。

a. News Feed and Ticker

当Open Graph actions发布的时候,这个活动可以出现在用户的News Feeds或者他们好友的News Feeds和Ticker上面。

b. Timeline

Timeline可以很好的在FB上展现一个用户的身份。它强调那些用户生活中最重要的时刻,还有apps也是Timeline的一部分。和规定的FB活动显示在Timeline上面的方式一样,用户在第三方apps上面的活动会以Reports和Aggregations的方式显示在Timeline上面,帮助用户构建他们的身份。

c. Aggregations and Reports

Aggregations是一个summary新鲜事,都是你的App定义好的,以一种简单并且有组织的方式展示用户的Open Graph活动。你可以定制化你的app的Aggregations并且使用一些样板数据进行预览。

如果app定义了多种Aggregations,这些就会以Report的形式出现在Timeline上面。Report是一系列aggregations的集合。 Reports显示一段时间之内用户的活动以及和你的app的联系。这样就提供了一个联系的查看体验,也使得用户能够更加容易地消化来自你的app的数据。假如你定义了2个Aggregations,一个叫做我做了什么菜,一个叫做我做什么菜最多。那么这2个东西单独来看就叫Aggregation,放在一起就叫做Report。

d. Timeline Views

这个是app特定的在Timeline上面显示的views。当用户安装了你的Open Graph app并且发布了actions,一个app特定的View就会显示在Timeline是最顶端。


pip3 install . Defaulting to user installation because normal site-packages is not writeable Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Processing /home/ubuntu/large_models/speech_pkg Preparing metadata (setup.py) ... done Building wheels for collected packages: speech DEPRECATION: Building 'speech' using the legacy setup.py bdist_wheel mechanism, which will be removed in a future version. pip 25.3 will enforce this behaviour change. A possible replacement is to use the standardized build interface by setting the `--use-pep517` option, (possibly combined with `--no-build-isolation`), or adding a `pyproject.toml` file to the source tree of 'speech'. Discussion can be found at https://github.com/pypa/pip/issues/6334 Building wheel for speech (setup.py) ... error error: subprocess-exited-with-error × python setup.py bdist_wheel did not run successfully. │ exit code: 1 ╰─> [322 lines of output] running bdist_wheel running build running build_py creating build creating build/lib creating build/lib/speech copying speech/awake.py -> build/lib/speech copying speech/__init__.py -> build/lib/speech running egg_info writing speech.egg-info/PKG-INFO writing dependency_links to speech.egg-info/dependency_links.txt writing top-level names to speech.egg-info/top_level.txt reading manifest file 'speech.egg-info/SOURCES.txt' reading manifest template 'MANIFEST.in' writing manifest file 'speech.egg-info/SOURCES.txt' /home/ubuntu/.local/lib/python3.10/site-packages/setuptools/command/build_py.py:207: _Warning: Package 'speech.jetson_nano' is absent from the `packages` configuration. !! ******************************************************************************** ############################ # Package would be ignored # ############################ Python recognizes 'speech.jetson_nano' as an importable package[^1], but it is absent from setuptools' `packages` configuration. This leads to an ambiguous overall configuration. If you want to distribute this package, please make sure that 'speech.jetson_nano' is explicitly added to the `packages` configuration field. Alternatively, you can also rely on setuptools' discovery methods (for example by using `find_namespace_packages(...)`/`find_namespace:` instead of `find_packages(...)`/`find:`). You can read more about "package discovery" on setuptools documentation page: - https://setuptools.pypa.io/en/latest/userguide/package_discovery.html If you don't want 'speech.jetson_nano' to be distributed and are already explicitly excluding 'speech.jetson_nano' via `find_namespace_packages(...)/find_namespace` or `find_packages(...)/find`, you can try to use `exclude_package_data`, or `include-package-data=False` in combination with a more fine grained `package-data` configuration. You can read more about "package data files" on setuptools documentation page: - https://setuptools.pypa.io/en/latest/userguide/datafiles.html [^1]: For Python, any directory (with suitable naming) can be imported, even if it does not contain any `.py` files. On the other hand, currently there is no concept of package data directory, all directories are treated like packages. ******************************************************************************** !! check.warn(importable) /home/ubuntu/.local/lib/python3.10/site-packages/setuptools/command/build_py.py:207: _Warning: Package 'speech.jetson_orin' is absent from the `packages` configuration. !! ******************************************************************************** ############################ # Package would be ignored # ############################ Python recognizes 'speech.jetson_orin' as an importable package[^1], but it is absent from setuptools' `packages` configuration. This leads to an ambiguous overall configuration. If you want to distribute this package, please make sure that 'speech.jetson_orin' is explicitly added to the `packages` configuration field. Alternatively, you can also rely on setuptools' discovery methods (for example by using `find_namespace_packages(...)`/`find_namespace:` instead of `find_packages(...)`/`find:`). You can read more about "package discovery" on setuptools documentation page: - https://setuptools.pypa.io/en/latest/userguide/package_discovery.html If you don't want 'speech.jetson_orin' to be distributed and are already explicitly excluding 'speech.jetson_orin' via `find_namespace_packages(...)/find_namespace` or `find_packages(...)/find`, you can try to use `exclude_package_data`, or `include-package-data=False` in combination with a more fine grained `package-data` configuration. You can read more about "package data files" on setuptools documentation page: - https://setuptools.pypa.io/en/latest/userguide/datafiles.html [^1]: For Python, any directory (with suitable naming) can be imported, even if it does not contain any `.py` files. On the other hand, currently there is no concept of package data directory, all directories are treated like packages. ******************************************************************************** !! check.warn(importable) /home/ubuntu/.local/lib/python3.10/site-packages/setuptools/command/build_py.py:207: _Warning: Package 'speech.resources' is absent from the `packages` configuration. !! ******************************************************************************** ############################ # Package would be ignored # ############################ Python recognizes 'speech.resources' as an importable package[^1], but it is absent from setuptools' `packages` configuration. This leads to an ambiguous overall configuration. If you want to distribute this package, please make sure that 'speech.resources' is explicitly added to the `packages` configuration field. Alternatively, you can also rely on setuptools' discovery methods (for example by using `find_namespace_packages(...)`/`find_namespace:` instead of `find_packages(...)`/`find:`). You can read more about "package discovery" on setuptools documentation page: - https://setuptools.pypa.io/en/latest/userguide/package_discovery.html If you don't want 'speech.resources' to be distributed and are already explicitly excluding 'speech.resources' via `find_namespace_packages(...)/find_namespace` or `find_packages(...)/find`, you can try to use `exclude_package_data`, or `include-package-data=False` in combination with a more fine grained `package-data` configuration. You can read more about "package data files" on setuptools documentation page: - https://setuptools.pypa.io/en/latest/userguide/datafiles.html [^1]: For Python, any directory (with suitable naming) can be imported, even if it does not contain any `.py` files. On the other hand, currently there is no concept of package data directory, all directories are treated like packages. ******************************************************************************** !! check.warn(importable) /home/ubuntu/.local/lib/python3.10/site-packages/setuptools/command/build_py.py:207: _Warning: Package 'speech.rpi5' is absent from the `packages` configuration. !! ******************************************************************************** ############################ # Package would be ignored # ############################ Python recognizes 'speech.rpi5' as an importable package[^1], but it is absent from setuptools' `packages` configuration. This leads to an ambiguous overall configuration. If you want to distribute this package, please make sure that 'speech.rpi5' is explicitly added to the `packages` configuration field. Alternatively, you can also rely on setuptools' discovery methods (for example by using `find_namespace_packages(...)`/`find_namespace:` instead of `find_packages(...)`/`find:`). You can read more about "package discovery" on setuptools documentation page: - https://setuptools.pypa.io/en/latest/userguide/package_discovery.html If you don't want 'speech.rpi5' to be distributed and are already explicitly excluding 'speech.rpi5' via `find_namespace_packages(...)/find_namespace` or `find_packages(...)/find`, you can try to use `exclude_package_data`, or `include-package-data=False` in combination with a more fine grained `package-data` configuration. You can read more about "package data files" on setuptools documentation page: - https://setuptools.pypa.io/en/latest/userguide/datafiles.html [^1]: For Python, any directory (with suitable naming) can be imported, even if it does not contain any `.py` files. On the other hand, currently there is no concept of package data directory, all directories are treated like packages. ******************************************************************************** !! check.warn(importable) /home/ubuntu/.local/lib/python3.10/site-packages/setuptools/command/build_py.py:207: _Warning: Package 'speech.rpi5_docker' is absent from the `packages` configuration. !! ******************************************************************************** ############################ # Package would be ignored # ############################ Python recognizes 'speech.rpi5_docker' as an importable package[^1], but it is absent from setuptools' `packages` configuration. This leads to an ambiguous overall configuration. If you want to distribute this package, please make sure that 'speech.rpi5_docker' is explicitly added to the `packages` configuration field. Alternatively, you can also rely on setuptools' discovery methods (for example by using `find_namespace_packages(...)`/`find_namespace:` instead of `find_packages(...)`/`find:`). You can read more about "package discovery" on setuptools documentation page: - https://setuptools.pypa.io/en/latest/userguide/package_discovery.html If you don't want 'speech.rpi5_docker' to be distributed and are already explicitly excluding 'speech.rpi5_docker' via `find_namespace_packages(...)/find_namespace` or `find_packages(...)/find`, you can try to use `exclude_package_data`, or `include-package-data=False` in combination with a more fine grained `package-data` configuration. You can read more about "package data files" on setuptools documentation page: - https://setuptools.pypa.io/en/latest/userguide/datafiles.html [^1]: For Python, any directory (with suitable naming) can be imported, even if it does not contain any `.py` files. On the other hand, currently there is no concept of package data directory, all directories are treated like packages. ******************************************************************************** !! check.warn(importable) copying speech/speech.so -> build/lib/speech creating build/lib/speech/jetson_nano copying speech/jetson_nano/py3.6 -> build/lib/speech/jetson_nano copying speech/jetson_nano/speech.so -> build/lib/speech/jetson_nano creating build/lib/speech/jetson_orin copying speech/jetson_orin/py3.10 -> build/lib/speech/jetson_orin copying speech/jetson_orin/speech.so -> build/lib/speech/jetson_orin creating build/lib/speech/resources copying speech/resources/_detect_jetson_nano.so -> build/lib/speech/resources copying speech/resources/_detect_jetson_orin.so -> build/lib/speech/resources copying speech/resources/_detect_rpi5.so -> build/lib/speech/resources copying speech/resources/_detect_rpi5_docker_ubuntu20_04.so -> build/lib/speech/resources copying speech/resources/common.res -> build/lib/speech/resources copying speech/resources/detect_jetson_nano.so -> build/lib/speech/resources copying speech/resources/detect_jetson_orin.so -> build/lib/speech/resources copying speech/resources/detect_rpi5.so -> build/lib/speech/resources copying speech/resources/detect_rpi5_docker_ubuntu20_04.so -> build/lib/speech/resources copying speech/resources/vad.jit -> build/lib/speech/resources creating build/lib/speech/rpi5 copying speech/rpi5/py3.11 -> build/lib/speech/rpi5 copying speech/rpi5/speech.so -> build/lib/speech/rpi5 creating build/lib/speech/rpi5_docker copying speech/rpi5_docker/py3.8 -> build/lib/speech/rpi5_docker copying speech/rpi5_docker/speech.so -> build/lib/speech/rpi5_docker copying speech/awake.py -> build/lib/speech copying speech/__init__.py -> build/lib/speech /home/ubuntu/.local/lib/python3.10/site-packages/setuptools/_distutils/cmd.py:66: SetuptoolsDeprecationWarning: setup.py install is deprecated. !! ******************************************************************************** Please avoid running ``setup.py`` directly. Instead, use pypa/build, pypa/installer or other standards-based tools. See https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html for details. ******************************************************************************** !! self.initialize_options() installing to build/bdist.linux-aarch64/wheel running install running install_lib creating build/bdist.linux-aarch64 creating build/bdist.linux-aarch64/wheel creating build/bdist.linux-aarch64/wheel/speech copying build/lib/speech/awake.py -> build/bdist.linux-aarch64/wheel/speech creating build/bdist.linux-aarch64/wheel/speech/jetson_orin copying build/lib/speech/jetson_orin/speech.so -> build/bdist.linux-aarch64/wheel/speech/jetson_orin copying build/lib/speech/jetson_orin/py3.10 -> build/bdist.linux-aarch64/wheel/speech/jetson_orin creating build/bdist.linux-aarch64/wheel/speech/rpi5_docker copying build/lib/speech/rpi5_docker/speech.so -> build/bdist.linux-aarch64/wheel/speech/rpi5_docker copying build/lib/speech/rpi5_docker/py3.8 -> build/bdist.linux-aarch64/wheel/speech/rpi5_docker creating build/bdist.linux-aarch64/wheel/speech/rpi5 copying build/lib/speech/rpi5/speech.so -> build/bdist.linux-aarch64/wheel/speech/rpi5 copying build/lib/speech/rpi5/py3.11 -> build/bdist.linux-aarch64/wheel/speech/rpi5 copying build/lib/speech/speech.so -> build/bdist.linux-aarch64/wheel/speech creating build/bdist.linux-aarch64/wheel/speech/jetson_nano copying build/lib/speech/jetson_nano/py3.6 -> build/bdist.linux-aarch64/wheel/speech/jetson_nano copying build/lib/speech/jetson_nano/speech.so -> build/bdist.linux-aarch64/wheel/speech/jetson_nano copying build/lib/speech/__init__.py -> build/bdist.linux-aarch64/wheel/speech creating build/bdist.linux-aarch64/wheel/speech/resources copying build/lib/speech/resources/_detect_rpi5.so -> build/bdist.linux-aarch64/wheel/speech/resources copying build/lib/speech/resources/common.res -> build/bdist.linux-aarch64/wheel/speech/resources copying build/lib/speech/resources/_detect_jetson_nano.so -> build/bdist.linux-aarch64/wheel/speech/resources copying build/lib/speech/resources/detect_jetson_orin.so -> build/bdist.linux-aarch64/wheel/speech/resources copying build/lib/speech/resources/detect_jetson_nano.so -> build/bdist.linux-aarch64/wheel/speech/resources copying build/lib/speech/resources/detect_rpi5_docker_ubuntu20_04.so -> build/bdist.linux-aarch64/wheel/speech/resources copying build/lib/speech/resources/detect_rpi5.so -> build/bdist.linux-aarch64/wheel/speech/resources copying build/lib/speech/resources/vad.jit -> build/bdist.linux-aarch64/wheel/speech/resources copying build/lib/speech/resources/_detect_jetson_orin.so -> build/bdist.linux-aarch64/wheel/speech/resources copying build/lib/speech/resources/_detect_rpi5_docker_ubuntu20_04.so -> build/bdist.linux-aarch64/wheel/speech/resources running install_egg_info Copying speech.egg-info to build/bdist.linux-aarch64/wheel/speech-1.0-py3.10.egg-info running install_scripts creating build/bdist.linux-aarch64/wheel/speech-1.0.dist-info/WHEEL creating '/tmp/pip-wheel-4gy84oln/speech-1.0-py3-none-any.whl' and adding 'build/bdist.linux-aarch64/wheel' to it Traceback (most recent call last): File "<string>", line 2, in <module> File "<pip-setuptools-caller>", line 35, in <module> File "/home/ubuntu/large_models/speech_pkg/setup.py", line 3, in <module> setup( File "/home/ubuntu/.local/lib/python3.10/site-packages/setuptools/__init__.py", line 103, in setup return distutils.core.setup(**attrs) File "/home/ubuntu/.local/lib/python3.10/site-packages/setuptools/_distutils/core.py", line 184, in setup return run_commands(dist) File "/home/ubuntu/.local/lib/python3.10/site-packages/setuptools/_distutils/core.py", line 200, in run_commands dist.run_commands() File "/home/ubuntu/.local/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 969, in run_commands self.run_command(cmd) File "/home/ubuntu/.local/lib/python3.10/site-packages/setuptools/dist.py", line 968, in run_command super().run_command(command) File "/home/ubuntu/.local/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 988, in run_command cmd_obj.run() File "/usr/lib/python3/dist-packages/wheel/bdist_wheel.py", line 361, in run wf.write_files(archive_root) File "/usr/lib/python3/dist-packages/wheel/wheelfile.py", line 136, in write_files self.write(path, arcname) File "/usr/lib/python3/dist-packages/wheel/wheelfile.py", line 147, in write zinfo = ZipInfo(arcname or filename, date_time=get_zipinfo_datetime(st.st_mtime)) File "/usr/lib/python3.10/zipfile.py", line 366, in __init__ raise ValueError('ZIP does not support timestamps before 1980') ValueError: ZIP does not support timestamps before 1980 [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for speech Running setup.py clean for speech Failed to build speech ERROR: Failed to build installable wheels for some pyproject.toml based projects (speech) > cat ~/large_models/speech_pkg/pyproject.toml cat: /home/ubuntu/large_models/speech_pkg/pyproject.toml: No such file or directory > pip3 install . Defaulting to user installation because normal site-packages is not writeable Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Processing /home/ubuntu/large_models/speech_pkg Preparing metadata (setup.py) ... done Building wheels for collected packages: speech DEPRECATION: Building 'speech' using the legacy setup.py bdist_wheel mechanism, which will be removed in a future version. pip 25.3 will enforce this behaviour change. A possible replacement is to use the standardized build interface by setting the `--use-pep517` option, (possibly combined with `--no-build-isolation`), or adding a `pyproject.toml` file to the source tree of 'speech'. Discussion can be found at https://github.com/pypa/pip/issues/6334 Building wheel for speech (setup.py) ... error error: subprocess-exited-with-error × python setup.py bdist_wheel did not run successfully. │ exit code: 1 ╰─> [322 lines of output] running bdist_wheel running build running build_py creating build creating build/lib creating build/lib/speech copying speech/awake.py -> build/lib/speech copying speech/__init__.py -> build/lib/speech running egg_info writing speech.egg-info/PKG-INFO writing dependency_links to speech.egg-info/dependency_links.txt writing top-level names to speech.egg-info/top_level.txt reading manifest file 'speech.egg-info/SOURCES.txt' reading manifest template 'MANIFEST.in' writing manifest file 'speech.egg-info/SOURCES.txt' /home/ubuntu/.local/lib/python3.10/site-packages/setuptools/command/build_py.py:207: _Warning: Package 'speech.jetson_nano' is absent from the `packages` configuration. !! ******************************************************************************** ############################ # Package would be ignored # ############################ Python recognizes 'speech.jetson_nano' as an importable package[^1], but it is absent from setuptools' `packages` configuration. This leads to an ambiguous overall configuration. If you want to distribute this package, please make sure that 'speech.jetson_nano' is explicitly added to the `packages` configuration field. Alternatively, you can also rely on setuptools' discovery methods (for example by using `find_namespace_packages(...)`/`find_namespace:` instead of `find_packages(...)`/`find:`). You can read more about "package discovery" on setuptools documentation page: - https://setuptools.pypa.io/en/latest/userguide/package_discovery.html If you don't want 'speech.jetson_nano' to be distributed and are already explicitly excluding 'speech.jetson_nano' via `find_namespace_packages(...)/find_namespace` or `find_packages(...)/find`, you can try to use `exclude_package_data`, or `include-package-data=False` in combination with a more fine grained `package-data` configuration. You can read more about "package data files" on setuptools documentation page: - https://setuptools.pypa.io/en/latest/userguide/datafiles.html [^1]: For Python, any directory (with suitable naming) can be imported, even if it does not contain any `.py` files. On the other hand, currently there is no concept of package data directory, all directories are treated like packages. ******************************************************************************** !! check.warn(importable) /home/ubuntu/.local/lib/python3.10/site-packages/setuptools/command/build_py.py:207: _Warning: Package 'speech.jetson_orin' is absent from the `packages` configuration. !! ******************************************************************************** ############################ # Package would be ignored # ############################ Python recognizes 'speech.jetson_orin' as an importable package[^1], but it is absent from setuptools' `packages` configuration. This leads to an ambiguous overall configuration. If you want to distribute this package, please make sure that 'speech.jetson_orin' is explicitly added to the `packages` configuration field. Alternatively, you can also rely on setuptools' discovery methods (for example by using `find_namespace_packages(...)`/`find_namespace:` instead of `find_packages(...)`/`find:`). You can read more about "package discovery" on setuptools documentation page: - https://setuptools.pypa.io/en/latest/userguide/package_discovery.html If you don't want 'speech.jetson_orin' to be distributed and are already explicitly excluding 'speech.jetson_orin' via `find_namespace_packages(...)/find_namespace` or `find_packages(...)/find`, you can try to use `exclude_package_data`, or `include-package-data=False` in combination with a more fine grained `package-data` configuration. You can read more about "package data files" on setuptools documentation page: - https://setuptools.pypa.io/en/latest/userguide/datafiles.html [^1]: For Python, any directory (with suitable naming) can be imported, even if it does not contain any `.py` files. On the other hand, currently there is no concept of package data directory, all directories are treated like packages. ******************************************************************************** !! check.warn(importable) /home/ubuntu/.local/lib/python3.10/site-packages/setuptools/command/build_py.py:207: _Warning: Package 'speech.resources' is absent from the `packages` configuration. !! ******************************************************************************** ############################ # Package would be ignored # ############################ Python recognizes 'speech.resources' as an importable package[^1], but it is absent from setuptools' `packages` configuration. This leads to an ambiguous overall configuration. If you want to distribute this package, please make sure that 'speech.resources' is explicitly added to the `packages` configuration field. Alternatively, you can also rely on setuptools' discovery methods (for example by using `find_namespace_packages(...)`/`find_namespace:` instead of `find_packages(...)`/`find:`). You can read more about "package discovery" on setuptools documentation page: - https://setuptools.pypa.io/en/latest/userguide/package_discovery.html If you don't want 'speech.resources' to be distributed and are already explicitly excluding 'speech.resources' via `find_namespace_packages(...)/find_namespace` or `find_packages(...)/find`, you can try to use `exclude_package_data`, or `include-package-data=False` in combination with a more fine grained `package-data` configuration. You can read more about "package data files" on setuptools documentation page: - https://setuptools.pypa.io/en/latest/userguide/datafiles.html [^1]: For Python, any directory (with suitable naming) can be imported, even if it does not contain any `.py` files. On the other hand, currently there is no concept of package data directory, all directories are treated like packages. ******************************************************************************** !! check.warn(importable) /home/ubuntu/.local/lib/python3.10/site-packages/setuptools/command/build_py.py:207: _Warning: Package 'speech.rpi5' is absent from the `packages` configuration. !! ******************************************************************************** ############################ # Package would be ignored # ############################ Python recognizes 'speech.rpi5' as an importable package[^1], but it is absent from setuptools' `packages` configuration. This leads to an ambiguous overall configuration. If you want to distribute this package, please make sure that 'speech.rpi5' is explicitly added to the `packages` configuration field. Alternatively, you can also rely on setuptools' discovery methods (for example by using `find_namespace_packages(...)`/`find_namespace:` instead of `find_packages(...)`/`find:`). You can read more about "package discovery" on setuptools documentation page: - https://setuptools.pypa.io/en/latest/userguide/package_discovery.html If you don't want 'speech.rpi5' to be distributed and are already explicitly excluding 'speech.rpi5' via `find_namespace_packages(...)/find_namespace` or `find_packages(...)/find`, you can try to use `exclude_package_data`, or `include-package-data=False` in combination with a more fine grained `package-data` configuration. You can read more about "package data files" on setuptools documentation page: - https://setuptools.pypa.io/en/latest/userguide/datafiles.html [^1]: For Python, any directory (with suitable naming) can be imported, even if it does not contain any `.py` files. On the other hand, currently there is no concept of package data directory, all directories are treated like packages. ******************************************************************************** !! check.warn(importable) /home/ubuntu/.local/lib/python3.10/site-packages/setuptools/command/build_py.py:207: _Warning: Package 'speech.rpi5_docker' is absent from the `packages` configuration. !! ******************************************************************************** ############################ # Package would be ignored # ############################ Python recognizes 'speech.rpi5_docker' as an importable package[^1], but it is absent from setuptools' `packages` configuration. This leads to an ambiguous overall configuration. If you want to distribute this package, please make sure that 'speech.rpi5_docker' is explicitly added to the `packages` configuration field. Alternatively, you can also rely on setuptools' discovery methods (for example by using `find_namespace_packages(...)`/`find_namespace:` instead of `find_packages(...)`/`find:`). You can read more about "package discovery" on setuptools documentation page: - https://setuptools.pypa.io/en/latest/userguide/package_discovery.html If you don't want 'speech.rpi5_docker' to be distributed and are already explicitly excluding 'speech.rpi5_docker' via `find_namespace_packages(...)/find_namespace` or `find_packages(...)/find`, you can try to use `exclude_package_data`, or `include-package-data=False` in combination with a more fine grained `package-data` configuration. You can read more about "package data files" on setuptools documentation page: - https://setuptools.pypa.io/en/latest/userguide/datafiles.html [^1]: For Python, any directory (with suitable naming) can be imported, even if it does not contain any `.py` files. On the other hand, currently there is no concept of package data directory, all directories are treated like packages. ******************************************************************************** !! check.warn(importable) copying speech/speech.so -> build/lib/speech creating build/lib/speech/jetson_nano copying speech/jetson_nano/py3.6 -> build/lib/speech/jetson_nano copying speech/jetson_nano/speech.so -> build/lib/speech/jetson_nano creating build/lib/speech/jetson_orin copying speech/jetson_orin/py3.10 -> build/lib/speech/jetson_orin copying speech/jetson_orin/speech.so -> build/lib/speech/jetson_orin creating build/lib/speech/resources copying speech/resources/_detect_jetson_nano.so -> build/lib/speech/resources copying speech/resources/_detect_jetson_orin.so -> build/lib/speech/resources copying speech/resources/_detect_rpi5.so -> build/lib/speech/resources copying speech/resources/_detect_rpi5_docker_ubuntu20_04.so -> build/lib/speech/resources copying speech/resources/common.res -> build/lib/speech/resources copying speech/resources/detect_jetson_nano.so -> build/lib/speech/resources copying speech/resources/detect_jetson_orin.so -> build/lib/speech/resources copying speech/resources/detect_rpi5.so -> build/lib/speech/resources copying speech/resources/detect_rpi5_docker_ubuntu20_04.so -> build/lib/speech/resources copying speech/resources/vad.jit -> build/lib/speech/resources creating build/lib/speech/rpi5 copying speech/rpi5/py3.11 -> build/lib/speech/rpi5 copying speech/rpi5/speech.so -> build/lib/speech/rpi5 creating build/lib/speech/rpi5_docker copying speech/rpi5_docker/py3.8 -> build/lib/speech/rpi5_docker copying speech/rpi5_docker/speech.so -> build/lib/speech/rpi5_docker copying speech/awake.py -> build/lib/speech copying speech/__init__.py -> build/lib/speech /home/ubuntu/.local/lib/python3.10/site-packages/setuptools/_distutils/cmd.py:66: SetuptoolsDeprecationWarning: setup.py install is deprecated. !! ******************************************************************************** Please avoid running ``setup.py`` directly. Instead, use pypa/build, pypa/installer or other standards-based tools. See https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html for details. ******************************************************************************** !! self.initialize_options() installing to build/bdist.linux-aarch64/wheel running install running install_lib creating build/bdist.linux-aarch64 creating build/bdist.linux-aarch64/wheel creating build/bdist.linux-aarch64/wheel/speech copying build/lib/speech/awake.py -> build/bdist.linux-aarch64/wheel/speech creating build/bdist.linux-aarch64/wheel/speech/jetson_orin copying build/lib/speech/jetson_orin/speech.so -> build/bdist.linux-aarch64/wheel/speech/jetson_orin copying build/lib/speech/jetson_orin/py3.10 -> build/bdist.linux-aarch64/wheel/speech/jetson_orin creating build/bdist.linux-aarch64/wheel/speech/rpi5_docker copying build/lib/speech/rpi5_docker/speech.so -> build/bdist.linux-aarch64/wheel/speech/rpi5_docker copying build/lib/speech/rpi5_docker/py3.8 -> build/bdist.linux-aarch64/wheel/speech/rpi5_docker creating build/bdist.linux-aarch64/wheel/speech/rpi5 copying build/lib/speech/rpi5/speech.so -> build/bdist.linux-aarch64/wheel/speech/rpi5 copying build/lib/speech/rpi5/py3.11 -> build/bdist.linux-aarch64/wheel/speech/rpi5 copying build/lib/speech/speech.so -> build/bdist.linux-aarch64/wheel/speech creating build/bdist.linux-aarch64/wheel/speech/jetson_nano copying build/lib/speech/jetson_nano/py3.6 -> build/bdist.linux-aarch64/wheel/speech/jetson_nano copying build/lib/speech/jetson_nano/speech.so -> build/bdist.linux-aarch64/wheel/speech/jetson_nano copying build/lib/speech/__init__.py -> build/bdist.linux-aarch64/wheel/speech creating build/bdist.linux-aarch64/wheel/speech/resources copying build/lib/speech/resources/_detect_rpi5.so -> build/bdist.linux-aarch64/wheel/speech/resources copying build/lib/speech/resources/common.res -> build/bdist.linux-aarch64/wheel/speech/resources copying build/lib/speech/resources/_detect_jetson_nano.so -> build/bdist.linux-aarch64/wheel/speech/resources copying build/lib/speech/resources/detect_jetson_orin.so -> build/bdist.linux-aarch64/wheel/speech/resources copying build/lib/speech/resources/detect_jetson_nano.so -> build/bdist.linux-aarch64/wheel/speech/resources copying build/lib/speech/resources/detect_rpi5_docker_ubuntu20_04.so -> build/bdist.linux-aarch64/wheel/speech/resources copying build/lib/speech/resources/detect_rpi5.so -> build/bdist.linux-aarch64/wheel/speech/resources copying build/lib/speech/resources/vad.jit -> build/bdist.linux-aarch64/wheel/speech/resources copying build/lib/speech/resources/_detect_jetson_orin.so -> build/bdist.linux-aarch64/wheel/speech/resources copying build/lib/speech/resources/_detect_rpi5_docker_ubuntu20_04.so -> build/bdist.linux-aarch64/wheel/speech/resources running install_egg_info Copying speech.egg-info to build/bdist.linux-aarch64/wheel/speech-1.0-py3.10.egg-info running install_scripts creating build/bdist.linux-aarch64/wheel/speech-1.0.dist-info/WHEEL creating '/tmp/pip-wheel-dpii6adm/speech-1.0-py3-none-any.whl' and adding 'build/bdist.linux-aarch64/wheel' to it Traceback (most recent call last): File "<string>", line 2, in <module> File "<pip-setuptools-caller>", line 35, in <module> File "/home/ubuntu/large_models/speech_pkg/setup.py", line 3, in <module> setup( File "/home/ubuntu/.local/lib/python3.10/site-packages/setuptools/__init__.py", line 103, in setup return distutils.core.setup(**attrs) File "/home/ubuntu/.local/lib/python3.10/site-packages/setuptools/_distutils/core.py", line 184, in setup return run_commands(dist) File "/home/ubuntu/.local/lib/python3.10/site-packages/setuptools/_distutils/core.py", line 200, in run_commands dist.run_commands() File "/home/ubuntu/.local/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 969, in run_commands self.run_command(cmd) File "/home/ubuntu/.local/lib/python3.10/site-packages/setuptools/dist.py", line 968, in run_command super().run_command(command) File "/home/ubuntu/.local/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 988, in run_command cmd_obj.run() File "/usr/lib/python3/dist-packages/wheel/bdist_wheel.py", line 361, in run wf.write_files(archive_root) File "/usr/lib/python3/dist-packages/wheel/wheelfile.py", line 136, in write_files self.write(path, arcname) File "/usr/lib/python3/dist-packages/wheel/wheelfile.py", line 147, in write zinfo = ZipInfo(arcname or filename, date_time=get_zipinfo_datetime(st.st_mtime)) File "/usr/lib/python3.10/zipfile.py", line 366, in __init__ raise ValueError('ZIP does not support timestamps before 1980') ValueError: ZIP does not support timestamps before 1980 [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for speech Running setup.py clean for speech Failed to build speech ERROR: Failed to build installable wheels for some pyproject.toml based projects (speech)
06-29
<think>我们面对的是一个构建自定义Python包(speech_pkg)时出现的两个主要问题:1.构建wheel时出现"ValueError: ZIPdoes notsupport timestamps before1980"错误。2.关于setup.py弃用和包发现配置不明确的警告。 ###问题分析1.**ZIP时间戳问题**:这是因为在构建过程中,某些文件的时间戳被设置为早于1980年(ZIP格式不支持这样的时间戳)。这通常发生在使用版本控制系统(如Git)时,文件的时间戳可能被设置为提交时间,而某些旧提交的时间戳早于1980年。 2. **setup.py弃用警告**:现代Python打包推荐使用`pyproject.toml`来替代`setup.py`,但为了兼容,我们可能需要同时使用两者,并正确配置。3.**包发现配置不明确**:在`setup.py`中,我们可能没有明确指定如何发现包(例如使用`find_packages()`)。 ###解决方案####解决时间戳问题我们可以通过修改构建过程,将文件的时间戳设置为一个合理的时间(例如1980年1月1日)来规避这个问题。有几种方法: **方法1:在构建前重置文件时间戳(推荐)**使用一个脚本将包中所有文件的时间戳设置为1980年1月1日之后的时间。例如,在Unix系统上可以使用`touch`命令,在Windows上可以使用PowerShell脚本。但为了跨平台,我们可以在Python构建过程中处理。 **方法2:在`setup.py`中设置`SOURCE_DATE_EPOCH`环境变量** 在构建之前设置环境变量`SOURCE_DATE_EPOCH`,将其设置为一个大于0的值(即1980年1月1日之后的时间戳)。例如: ```bashexport SOURCE_DATE_EPOCH=315532800#对应1980-01-0100:00:00 UTC```然后运行`pip install.`。但注意,这个环境变量可能不会被所有工具遵守。 **方法3:使用`wheel`包的修复功能** 我们可以尝试在构建wheel时使用`wheel`包的一个特性:它允许我们指定时间戳。但需要修改`setup.py`。 在`setup.py`中,我们可以覆盖`build_py`命令,将文件的时间戳设置为一个固定时间(如1980年1月2日): ```pythonimport osimportsysfromsetuptoolsimport setup, find_packagesfromsetuptools.command.build_py importbuild_pyclassBuildPy(build_py):def get_source_files(self):files =super().get_source_files()#将所有文件的时间戳设置为1980年1月2日forfile infiles:os.utime(file, (315532800,315532800))returnfilessetup(cmdclass={'build_py':BuildPy,},...#其他参数)``` **方法4:使用Git的配置** 如果问题文件在Git仓库中,我们可以配置Git在检出时不要修改时间戳:```bashgitconfig --global core.trustctime false```然后重新检出代码。但这种方法可能不总是有效。####解决setup.py弃用和包发现警告1. **创建`pyproject.toml`文件**:在项目根目录下创建`pyproject.toml`文件,指定构建系统要求。例如:```toml[build-system]requires =["setuptools>=61.0.0","wheel"]build-backend ="setuptools.build_meta"```2.**明确包发现**:在`setup.py`中使用`find_packages()`来明确指定包含哪些包。确保项目结构符合标准(包放在`src`目录下是推荐做法)。例如:```pythonfromsetuptools importsetup,find_packagessetup(name="speech_pkg",version="0.1.0",packages=find_packages(where="src"),package_dir={"": "src"},...#其他元数据)``` ###完整步骤示例假设项目结构如下:``` speech_pkg/├──src/│└── speech_pkg/│└──__init__.py├── setup.py└── pyproject.toml```1.**创建`pyproject.toml`**(如果不存在):```toml[build-system]requires =["setuptools>=61.0.0","wheel"]build-backend ="setuptools.build_meta"```2.**修改`setup.py`**,解决时间戳问题和包发现:```pythonimport osimport sysfrom setuptoolsimport setup, find_packagesfrom setuptools.command.build_pyimport build_py#解决时间戳问题:将文件时间戳设置为1980-01-02class BuildPy(build_py):defget_source_files(self):files =super().get_source_files()#315532800是1980-01-0100:00:00UTC的时间戳,我们加86400秒(一天)得到1980-01-02fixed_timestamp =315532800+86400for filein files:#设置访问时间和修改时间os.utime(file, (fixed_timestamp,fixed_timestamp))returnfilessetup(name="speech_pkg",version="0.1.0",author="YourName",author_email="your.email@example.com",description="Aspeech package",long_description=open('README.md').read(),long_description_content_type="text/markdown",packages=find_packages(where="src"),package_dir={"": "src"},install_requires=[#依赖列表],cmdclass={'build_py': BuildPy,},)```3.**构建并安装**:```bashpip install.``` ###注意事项-如果项目中没有早于1980年的文件,但构建过程中仍然出现此错误,可能是临时文件或生成文件的问题。上述方法同样适用。-如果使用`setuptools_scm`等工具,可能需要额外配置。###技术原理ZIP文件格式规定,文件时间戳不能早于1980年1月1日(DOS纪元)。当构建工具尝试将时间戳早于该时间的文件打包时,就会引发此错误。在版本控制系统中,文件的时间戳可能被设置为提交时间,而历史提交可能包含早于1980年的时间戳(尽管罕见,但可能发生)。通过强制修改文件时间戳,我们确保它们在ZIP支持范围内。 关于`setup.py`弃用,PEP517和PEP518引入了`pyproject.toml`作为新的打包标准,旨在提供更可靠的构建环境[^1]。 §§相关问题§§1.如何为Python包创建跨平台的构建配置? 2.在Python打包中,`pyproject.toml`和`setup.py`各自的作用是什么?3.除了时间戳问题,还有哪些常见的wheel构建错误?</think>### 解决Python包构建错误:时间戳问题与弃用警告 您遇到的构建错误主要由两个核心问题引起: 1. **ZIP时间戳限制**:`ValueError: ZIP does not support timestamps before 1980` 2. **打包工具弃用警告**:`setup.py`弃用和包发现配置不明确 以下是系统化的解决方案: --- ### 解决方案 #### 1. 修复ZIP时间戳问题(核心错误) 此错误源于构建过程中某些文件的时间戳早于1980年(ZIP格式限制)。通过强制重置时间戳解决: **方法1:修改构建命令(推荐)** ```bash # 设置固定时间戳(1980年1月1日之后) export SOURCE_DATE_EPOCH=315532800 # 对应1980-01-01 00:00:00 UTC # 安装时忽略缓存 pip install --no-cache-dir /path/to/speech_pkg ``` **方法2:修改`setup.py`(代码级修复)** ```python import os from setuptools import setup, find_packages # 强制设置文件时间戳 os.environ['SOURCE_DATE_EPOCH'] = '315532800' # 固定时间戳 setup( name="speech_pkg", version="0.1.0", packages=find_packages(), # 其他配置... ) ``` **方法3:使用构建钩子(高级方案)** 在`pyproject.toml`中添加: ```toml [build-system] requires = ["setuptools", "setuptools_scm"] build-backend = "setuptools.build_meta" [tool.setuptools_scm] # 强制时间戳规范化 version_scheme = "no-guess-dev" local_scheme = "no-local-version" ``` #### 2. 解决`setup.py`弃用警告 现代Python打包推荐迁移到`pyproject.toml`: **步骤1:创建`pyproject.toml`文件** ```toml [build-system] requires = ["setuptools>=61.0.0", "wheel"] build-backend = "setuptools.build_meta" [project] name = "speech_pkg" version = "0.1.0" description = "Your package description" [tool.setuptools.packages] find = {} # 启用自动包发现 ``` **步骤2:简化`setup.py`(过渡期兼容)** ```python # 仅保留必要内容 from setuptools import setup if __name__ == "__main__": setup() ``` #### 3. 明确包发现配置 在`pyproject.toml`中指定包结构: ```toml [tool.setuptools.packages.find] where = ["src"] # 包所在目录 include = ["speech_pkg*"] # 包含的包模式 exclude = ["tests*"] # 排除的目录 ``` --- ### 完整操作流程 ```bash # 在包根目录操作 cd /path/to/speech_pkg # 1. 创建配置文件 echo '[build-system] requires = ["setuptools>=61.0.0", "wheel"] build-backend = "setuptools.build_meta"' > pyproject.toml # 2. 设置环境变量 export SOURCE_DATE_EPOCH=315532800 # 3. 安装包 pip install --no-cache-dir . ``` --- ### 技术原理 1. **时间戳问题**: - ZIP格式使用DOS时间戳格式,时间范围限制在1980-2107年之间 - 数学表达:$$ t_{\text{valid}} \in [t_{1980}, t_{2107}] \quad \text{其中} \quad t_{1980} = 315,532,800 $$ - 版本控制系统可能引入早期时间戳[^1] 2. **打包演进**: ```mermaid graph LR A[setup.py] --> B[setup.cfg] B --> C[pyproject.toml] C --> D[PEP 517/518标准] ``` - `pyproject.toml`提供声明式配置,避免脚本执行问题[^2] ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值