解读:下载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: 多模块(.py)中全局变量的导入

文章目录 global关键字可变类型和不可变类型数据的内存地址单模块(单个py文件)的全局变量示例总结 多模块(多个py文件)的全局变量from x import x导入全局变量示例 import x导入全局变量示例 总结 global关键字 global 的作用范围是模块(.py)级别: 当你在一个模块(文件)中使用 global 声明变量时,这个变量只在该模块的全局命名空

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听

常用的jdk下载地址

jdk下载地址 安装方式可以看之前的博客: mac安装jdk oracle 版本:https://www.oracle.com/java/technologies/downloads/ Eclipse Temurin版本:https://adoptium.net/zh-CN/temurin/releases/ 阿里版本: github:https://github.com/

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

Android平台播放RTSP流的几种方案探究(VLC VS ExoPlayer VS SmartPlayer)

技术背景 好多开发者需要遴选Android平台RTSP直播播放器的时候,不知道如何选的好,本文针对常用的方案,做个大概的说明: 1. 使用VLC for Android VLC Media Player(VLC多媒体播放器),最初命名为VideoLAN客户端,是VideoLAN品牌产品,是VideoLAN计划的多媒体播放器。它支持众多音频与视频解码器及文件格式,并支持DVD影音光盘,VCD影

【机器学习】高斯过程的基本概念和应用领域以及在python中的实例

引言 高斯过程(Gaussian Process,简称GP)是一种概率模型,用于描述一组随机变量的联合概率分布,其中任何一个有限维度的子集都具有高斯分布 文章目录 引言一、高斯过程1.1 基本定义1.1.1 随机过程1.1.2 高斯分布 1.2 高斯过程的特性1.2.1 联合高斯性1.2.2 均值函数1.2.3 协方差函数(或核函数) 1.3 核函数1.4 高斯过程回归(Gauss

【学习笔记】 陈强-机器学习-Python-Ch15 人工神经网络(1)sklearn

系列文章目录 监督学习:参数方法 【学习笔记】 陈强-机器学习-Python-Ch4 线性回归 【学习笔记】 陈强-机器学习-Python-Ch5 逻辑回归 【课后题练习】 陈强-机器学习-Python-Ch5 逻辑回归(SAheart.csv) 【学习笔记】 陈强-机器学习-Python-Ch6 多项逻辑回归 【学习笔记 及 课后题练习】 陈强-机器学习-Python-Ch7 判别分析 【学

MCU7.keil中build产生的hex文件解读

1.hex文件大致解读 闲来无事,查看了MCU6.用keil新建项目的hex文件 用FlexHex打开 给我的第一印象是:经过软件的解释之后,发现这些数据排列地十分整齐 :02000F0080FE71:03000000020003F8:0C000300787FE4F6D8FD75810702000F3D:00000001FF 把解释后的数据当作十六进制来观察 1.每一行数据

Java ArrayList扩容机制 (源码解读)

结论:初始长度为10,若所需长度小于1.5倍原长度,则按照1.5倍扩容。若不够用则按照所需长度扩容。 一. 明确类内部重要变量含义         1:数组默认长度         2:这是一个共享的空数组实例,用于明确创建长度为0时的ArrayList ,比如通过 new ArrayList<>(0),ArrayList 内部的数组 elementData 会指向这个 EMPTY_EL