解读:下载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实现Microsoft Office自动化的几种方式及对比详解

《Python实现MicrosoftOffice自动化的几种方式及对比详解》办公自动化是指利用现代化设备和技术,代替办公人员的部分手动或重复性业务活动,优质而高效地处理办公事务,实现对信息的高效利用... 目录一、基于COM接口的自动化(pywin32)二、独立文件操作库1. Word处理(python-d

使用Python实现网络设备配置备份与恢复

《使用Python实现网络设备配置备份与恢复》网络设备配置备份与恢复在网络安全管理中起着至关重要的作用,本文为大家介绍了如何通过Python实现网络设备配置备份与恢复,需要的可以参考下... 目录一、网络设备配置备份与恢复的概念与重要性二、网络设备配置备份与恢复的分类三、python网络设备配置备份与恢复实

Python使用date模块进行日期处理的终极指南

《Python使用date模块进行日期处理的终极指南》在处理与时间相关的数据时,Python的date模块是开发者最趁手的工具之一,本文将用通俗的语言,结合真实案例,带您掌握date模块的六大核心功能... 目录引言一、date模块的核心功能1.1 日期表示1.2 日期计算1.3 日期比较二、六大常用方法详

Python使用DrissionPage中ChromiumPage进行自动化网页操作

《Python使用DrissionPage中ChromiumPage进行自动化网页操作》DrissionPage作为一款轻量级且功能强大的浏览器自动化库,为开发者提供了丰富的功能支持,本文将使用Dri... 目录前言一、ChromiumPage基础操作1.初始化Drission 和 ChromiumPage

Python中ModuleNotFoundError: No module named ‘timm’的错误解决

《Python中ModuleNotFoundError:Nomodulenamed‘timm’的错误解决》本文主要介绍了Python中ModuleNotFoundError:Nomodulen... 目录一、引言二、错误原因分析三、解决办法1.安装timm模块2. 检查python环境3. 解决安装路径问题

Python实现将MySQL中所有表的数据都导出为CSV文件并压缩

《Python实现将MySQL中所有表的数据都导出为CSV文件并压缩》这篇文章主要为大家详细介绍了如何使用Python将MySQL数据库中所有表的数据都导出为CSV文件到一个目录,并压缩为zip文件到... python将mysql数据库中所有表的数据都导出为CSV文件到一个目录,并压缩为zip文件到另一个

IDEA中Git版本回退的两种实现方案

《IDEA中Git版本回退的两种实现方案》作为开发者,代码版本回退是日常高频操作,IntelliJIDEA集成了强大的Git工具链,但面对reset和revert两种核心回退方案,许多开发者仍存在选择... 目录一、版本回退前置知识二、Reset方案:整体改写历史1、IDEA图形化操作(推荐)1.1、查看提

python dict转换成json格式的实现

《pythondict转换成json格式的实现》本文主要介绍了pythondict转换成json格式的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下... 一开始你变成字典格式data = [ { 'a' : 1, 'b' : 2, 'c编程' : 3,

Ollama Python 使用小结

《OllamaPython使用小结》Ollama提供了PythonSDK,使得开发者能够在Python环境中轻松集成和使用本地运行的模型进行自然语言处理任务,具有一定的参考价值,感兴趣的可以了解一... 目录安装 python SDK启动本地服务使用 Ollama 的 Python SDK 进行推理自定义客

使用Python实现文本转语音(TTS)并播放音频

《使用Python实现文本转语音(TTS)并播放音频》在开发涉及语音交互或需要语音提示的应用时,文本转语音(TTS)技术是一个非常实用的工具,下面我们来看看如何使用gTTS和playsound库将文本... 目录什么是 gTTS 和 playsound安装依赖库实现步骤 1. 导入库2. 定义文本和语言 3