解读:下载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中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

利用Python打造一个Excel记账模板

《利用Python打造一个Excel记账模板》这篇文章主要为大家详细介绍了如何使用Python打造一个超实用的Excel记账模板,可以帮助大家高效管理财务,迈向财富自由之路,感兴趣的小伙伴快跟随小编一... 目录设置预算百分比超支标红预警记账模板功能介绍基础记账预算管理可视化分析摸鱼时间理财法碎片时间利用财

Python中的Walrus运算符分析示例详解

《Python中的Walrus运算符分析示例详解》Python中的Walrus运算符(:=)是Python3.8引入的一个新特性,允许在表达式中同时赋值和返回值,它的核心作用是减少重复计算,提升代码简... 目录1. 在循环中避免重复计算2. 在条件判断中同时赋值变量3. 在列表推导式或字典推导式中简化逻辑

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

使用Python和Pyecharts创建交互式地图

《使用Python和Pyecharts创建交互式地图》在数据可视化领域,创建交互式地图是一种强大的方式,可以使受众能够以引人入胜且信息丰富的方式探索地理数据,下面我们看看如何使用Python和Pyec... 目录简介Pyecharts 简介创建上海地图代码说明运行结果总结简介在数据可视化领域,创建交互式地

利用python实现对excel文件进行加密

《利用python实现对excel文件进行加密》由于文件内容的私密性,需要对Excel文件进行加密,保护文件以免给第三方看到,本文将以Python语言为例,和大家讲讲如何对Excel文件进行加密,感兴... 目录前言方法一:使用pywin32库(仅限Windows)方法二:使用msoffcrypto-too

SpringBoot中配置文件的加载顺序解读

《SpringBoot中配置文件的加载顺序解读》:本文主要介绍SpringBoot中配置文件的加载顺序,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot配置文件的加载顺序1、命令⾏参数2、Java系统属性3、操作系统环境变量5、项目【外部】的ap

使用Python实现矢量路径的压缩、解压与可视化

《使用Python实现矢量路径的压缩、解压与可视化》在图形设计和Web开发中,矢量路径数据的高效存储与传输至关重要,本文将通过一个Python示例,展示如何将复杂的矢量路径命令序列压缩为JSON格式,... 目录引言核心功能概述1. 路径命令解析2. 路径数据压缩3. 路径数据解压4. 可视化代码实现详解1

python获取网页表格的多种方法汇总

《python获取网页表格的多种方法汇总》我们在网页上看到很多的表格,如果要获取里面的数据或者转化成其他格式,就需要将表格获取下来并进行整理,在Python中,获取网页表格的方法有多种,下面就跟随小编... 目录1. 使用Pandas的read_html2. 使用BeautifulSoup和pandas3.