解读:下载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 Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

python实现svg图片转换为png和gif

《python实现svg图片转换为png和gif》这篇文章主要为大家详细介绍了python如何实现将svg图片格式转换为png和gif,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录python实现svg图片转换为png和gifpython实现图片格式之间的相互转换延展:基于Py

Python中的getopt模块用法小结

《Python中的getopt模块用法小结》getopt.getopt()函数是Python中用于解析命令行参数的标准库函数,该函数可以从命令行中提取选项和参数,并对它们进行处理,本文详细介绍了Pyt... 目录getopt模块介绍getopt.getopt函数的介绍getopt模块的常用用法getopt模

Python利用ElementTree实现快速解析XML文件

《Python利用ElementTree实现快速解析XML文件》ElementTree是Python标准库的一部分,而且是Python标准库中用于解析和操作XML数据的模块,下面小编就来和大家详细讲讲... 目录一、XML文件解析到底有多重要二、ElementTree快速入门1. 加载XML的两种方式2.

Python如何精准判断某个进程是否在运行

《Python如何精准判断某个进程是否在运行》这篇文章主要为大家详细介绍了Python如何精准判断某个进程是否在运行,本文为大家整理了3种方法并进行了对比,有需要的小伙伴可以跟随小编一起学习一下... 目录一、为什么需要判断进程是否存在二、方法1:用psutil库(推荐)三、方法2:用os.system调用

使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

《使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)》PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了... 目录一、引言二、环境与工具三、python 提取PPT背景图片3.1 提取幻灯片背景图片3.2 提取

Python实现图片分割的多种方法总结

《Python实现图片分割的多种方法总结》图片分割是图像处理中的一个重要任务,它的目标是将图像划分为多个区域或者对象,本文为大家整理了一些常用的分割方法,大家可以根据需求自行选择... 目录1. 基于传统图像处理的分割方法(1) 使用固定阈值分割图片(2) 自适应阈值分割(3) 使用图像边缘检测分割(4)

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl