解读:下载Android源码时为什么对Python版本有要求

2024-09-04 22:48

本文主要是介绍解读:下载Android源码时为什么对Python版本有要求,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在下载Android源码时,由于本机的Python版本和Android所要求的不一致,所以要重装Python。本文是在Python 3.x的情况下,对repo代码进行修改的一种尝试。通过这个尝试,结论是需要2.x版本的Python环境,而不能使用3.x。


The simplest method to download is the next blog, i.e. The steps for download android source code.


下载Android源码的方法:http://source.android.com/source/downloading.html#installing-repo


但在实际下载时,下载repo这一步可能会有问题。因此参考http://blog.csdn.net/zeroboundary/article/details/17555183的blog,使用了下面的地址:

curl http://git-repo.googlecode.com/files/repo-1.12 > ~/bin/repo


在下载了这个repo之后,init repo的时候会报错:

% repo init -u https://android.googlesource.com/platform/manifestFile "/home/test//bin/repo", line 174except OSError, e:^
SyntaxError: invalid syntax

查阅网上的分析,结论是Python版本的问题。本机的Python版本如下:

% python --version
Python 3.2.3

即版本太高了,导致repo文件中的Python语法在最新版本上通不过。为此,需要修改repo文件中捕获异常的代码。如:

  if not os.path.isdir(repodir):try:os.mkdir(repodir)except OSError, e:print >>sys.stderr, \

需要改成:

  if not os.path.isdir(repodir):try:os.mkdir(repodir)except OSError as e:print >>sys.stderr, \

repo脚本中有多处这种不兼容,需要一一修改。


以上错误修改之后,继续repo init,又出现下面的错误:

% repo init -u https://android.googlesource.com/platform/manifestFile "/home/test//bin/repo", line 259os.mkdir(gpg_dir, 0700)^
SyntaxError: invalid token

这同样是Python版本兼容问题,具体来讲,是Python对八进制数的表示方法有了变化。之前只需要一个0,现在则是数字0加上字母o。错误的表示方法:

% python
Python 3.2.3 (default, Sep  7 2012, 03:04:57) 
[GCC 4.7.1 20120721 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.mkdir("aaa", 0700)File "<stdin>", line 1os.mkdir("aaa", 0700)^
SyntaxError: invalid token
>>> os.mkdir("aaa")
>>> exit()
% ll
总用量 4
drwxr-xr-x 2 test users 4096  3月  1 13:00 aaa/
%     

3.2版本所支持的语法:

% python
Python 3.2.3 (default, Sep  7 2012, 03:04:57) 
[GCC 4.7.1 20120721 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.mkdir("bbb", 0o700)
>>> exit()
% ll
总用量 8
drwxr-xr-x 2 test users 4096  3月  1 13:00 aaa/
drwx------ 2 test users 4096  3月  1 13:10 bbb/
%        


据此,再把repo中的八进制数字进行修改。


然后继续repo init,又有新的错误:

% repo init -u https://android.googlesource.com/platform/manifest
Traceback (most recent call last):File "/home/test//bin/repo", line 602, in <module>main(sys.argv[1:])File "/home/test//bin/repo", line 553, in maincmd, opt, args = _ParseArguments(orig_args)File "/home/test//bin/repo", line 458, in _ParseArgumentsfor i in xrange(0, len(args)):
NameError: global name 'xrange' is not defined


这是因为在Python 3.x中,xrange重命名成了range。


继续repo init,新的错误:

% repo init -u https://android.googlesource.com/platform/manifest
Traceback (most recent call last):File "/home/test//bin/repo", line 602, in <module>main(sys.argv[1:])File "/home/test//bin/repo", line 569, in main_Init(args)File "/home/test//bin/repo", line 183, in _Init_CheckGitVersion()File "/home/test//bin/repo", line 217, in _CheckGitVersionif not ver_str.startswith('git version '):
TypeError: startswith first arg must be bytes or a tuple of bytes, not str

相关的repo代码:

def _CheckGitVersion():cmd = [GIT, '--version']proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)ver_str = proc.stdout.read().strip()proc.stdout.close()proc.wait()if not ver_str.startswith('git version '):print >>sys.stderr, 'error: "%s" unsupported' % ver_strraise CloneFailure()


这是因为Python 3.x中字符串和二进制串有了区分,为此把bytes转化成string即可:

ver_str = ver_str.decode()

继续repo init,新的错误:

% repo init -u https://android.googlesource.com/platform/manifest
Traceback (most recent call last):File "/home/test//bin/repo", line 603, in <module>main(sys.argv[1:])File "/home/test//bin/repo", line 570, in main_Init(args)File "/home/test//bin/repo", line 186, in _Initcan_verify = _SetupGnuPG(opt.quiet)File "/home/test//bin/repo", line 282, in _SetupGnuPGproc.stdin.write(MAINTAINER_KEYS)
TypeError: 'str' does not support the buffer interface

编码问题,修改如下:

proc.stdin.write(MAINTAINER_KEYS.encode())

继续repo init:

% repo init -u https://android.googlesource.com/platform/manifest
gpg: /home/test/.repoconfig/gnupg/trustdb.gpg:建立了信任度数据库
gpg: 密钥 920F5C65:公钥“Repo Maintainer <repo@android.kernel.org>”已导入
gpg: 合计被处理的数量:1
gpg:           已导入:1
Traceback (most recent call last):File "/home/test//bin/repo", line 603, in <module>main(sys.argv[1:])File "/home/test//bin/repo", line 570, in main_Init(args)File "/home/test//bin/repo", line 191, in _Initprint >>sys.stderr, 'Getting repo ...'
TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and '_io.TextIOWrapper'

这是print语法变更,把:

    if not opt.quiet:print >>sys.stderr, 'Getting repo ...'print >>sys.stderr, '   from %s' % url

修改成:

    if not opt.quiet:print('Getting repo ...', file=sys.stderr)print('   from %s' % url, file=sys.stderr)

因为repo中有大量的print,因此需要逐一修改完成。继续:

 * [新tag]          v1.9.5     -> v1.9.5* [新tag]          v1.9.6     -> v1.9.6
Traceback (most recent call last):File "/home/test//bin/repo", line 594, in <module>main(sys.argv[1:])File "/home/test//bin/repo", line 561, in main_Init(args)File "/home/test//bin/repo", line 197, in _Initrev = _Verify(dst, branch, opt.quiet)File "/home/test//bin/repo", line 370, in _Verifym = re.compile(r'^(.*)-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur)
TypeError: can't use a string pattern on a bytes-like object

对应的代码:

  cur = proc.stdout.read().strip()proc.stdout.close()proc.stderr.read()proc.stderr.close()if proc.wait() != 0 or not cur:print("", file=sys.stderr)print("fatal: branch '%s' has not been signed" \% branch, file=sys.stderr)raise CloneFailure()m = re.compile(r'^(.*)-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur)

同样是前面提过的bytes和string的关系,修改如下:

  cur = proc.stdout.read().strip().decode()

同时走读所有代码,把所有这种出来都增加decode()。——一共3个地方,同时前面提及的那个修改也按照这里的方法进行修改,即:

def _CheckGitVersion():cmd = [GIT, '--version']proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)ver_str = proc.stdout.read().strip().decode()proc.stdout.close()proc.wait()if not ver_str.startswith('git version '):

继续repo init,错误:

  File "/home/test/Android/.repo/repo/main.py", line 285print msg^
SyntaxError: invalid syntax

现在的错误就不是repo文件了,而是已经下载的文件中的其他错误。然后在文件管理器中查看python文件,发现接下来需要修改的地方很多:


时已至此,只能选择安装一个老版本的Python,比如网上大家建议的2.7。另外下载的步骤还是官网的说明:http://source.android.com/source/downloading.html。


特别要注意这一部分:



这篇关于解读:下载Android源码时为什么对Python版本有要求的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1137249

相关文章

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Python虚拟环境与Conda使用指南分享

《Python虚拟环境与Conda使用指南分享》:本文主要介绍Python虚拟环境与Conda使用指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、python 虚拟环境概述1.1 什么是虚拟环境1.2 为什么需要虚拟环境二、Python 内置的虚拟环境工具

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Python pip下载包及所有依赖到指定文件夹的步骤说明

《Pythonpip下载包及所有依赖到指定文件夹的步骤说明》为了方便开发和部署,我们常常需要将Python项目所依赖的第三方包导出到本地文件夹中,:本文主要介绍Pythonpip下载包及所有依... 目录步骤说明命令格式示例参数说明离线安装方法注意事项总结要使用pip下载包及其所有依赖到指定文件夹,请按照以

Python实现精准提取 PDF中的文本,表格与图片

《Python实现精准提取PDF中的文本,表格与图片》在实际的系统开发中,处理PDF文件不仅限于读取整页文本,还有提取文档中的表格数据,图片或特定区域的内容,下面我们来看看如何使用Python实... 目录安装 python 库提取 PDF 文本内容:获取整页文本与指定区域内容获取页面上的所有文本内容获取

基于Python实现一个Windows Tree命令工具

《基于Python实现一个WindowsTree命令工具》今天想要在Windows平台的CMD命令终端窗口中使用像Linux下的tree命令,打印一下目录结构层级树,然而还真有tree命令,但是发现... 目录引言实现代码使用说明可用选项示例用法功能特点添加到环境变量方法一:创建批处理文件并添加到PATH1

Python包管理工具核心指令uvx举例详细解析

《Python包管理工具核心指令uvx举例详细解析》:本文主要介绍Python包管理工具核心指令uvx的相关资料,uvx是uv工具链中用于临时运行Python命令行工具的高效执行器,依托Rust实... 目录一、uvx 的定位与核心功能二、uvx 的典型应用场景三、uvx 与传统工具对比四、uvx 的技术实

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

python判断文件是否存在常用的几种方式

《python判断文件是否存在常用的几种方式》在Python中我们在读写文件之前,首先要做的事情就是判断文件是否存在,否则很容易发生错误的情况,:本文主要介绍python判断文件是否存在常用的几种... 目录1. 使用 os.path.exists()2. 使用 os.path.isfile()3. 使用