解读:下载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/manifest File "/home/test//bin/repo", line 174 except OSError, e: ^SyntaxError: invalid syntax
查阅网上的分析,结论是Python版本的问题。本机的Python版本如下:
% python --versionPython 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/manifest File "/home/test//bin/repo", line 259 os.mkdir(gpg_dir, 0700) ^SyntaxError: invalid token
这同样是Python版本兼容问题,具体来讲,是Python对八进制数的表示方法有了变化。之前只需要一个0,现在则是数字0加上字母o。错误的表示方法:
% pythonPython 3.2.3 (default, Sep 7 2012, 03:04:57) [GCC 4.7.1 20120721 (prerelease)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import os>>> os.mkdir("aaa", 0700) File "", line 1 os.mkdir("aaa", 0700) ^SyntaxError: invalid token>>> os.mkdir("aaa")>>> exit()% ll总用量 4drwxr-xr-x 2 test users 4096 3月 1 13:00 aaa/%
3.2版本所支持的语法:
% pythonPython 3.2.3 (default, Sep 7 2012, 03:04:57) [GCC 4.7.1 20120721 (prerelease)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import os>>> os.mkdir("bbb", 0o700)>>> exit()% ll总用量 8drwxr-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/manifestTraceback (most recent call last): File "/home/test//bin/repo", line 602, in main(sys.argv[1:]) File "/home/test//bin/repo", line 553, in main cmd, opt, args = _ParseArguments(orig_args) File "/home/test//bin/repo", line 458, in _ParseArguments for 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/manifestTraceback (most recent call last): File "/home/test//bin/repo", line 602, in 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 _CheckGitVersion if 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_str raise CloneFailure()
这是因为Python 3.x中字符串和二进制串有了区分,为此把bytes转化成string即可:
ver_str = ver_str.decode()
继续repo init,新的错误:
% repo init -u https://android.googlesource.com/platform/manifestTraceback (most recent call last): File "/home/test//bin/repo", line 603, in main(sys.argv[1:]) File "/home/test//bin/repo", line 570, in main _Init(args) File "/home/test//bin/repo", line 186, in _Init can_verify = _SetupGnuPG(opt.quiet) File "/home/test//bin/repo", line 282, in _SetupGnuPG proc.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/manifestgpg: /home/test/.repoconfig/gnupg/trustdb.gpg:建立了信任度数据库gpg: 密钥 920F5C65:公钥“Repo Maintainer ”已导入gpg: 合计被处理的数量:1gpg: 已导入:1Traceback (most recent call last): File "/home/test//bin/repo", line 603, in main(sys.argv[1:]) File "/home/test//bin/repo", line 570, in main _Init(args) File "/home/test//bin/repo", line 191, in _Init print >>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.6Traceback (most recent call last): File "/home/test//bin/repo", line 594, in main(sys.argv[1:]) File "/home/test//bin/repo", line 561, in main _Init(args) File "/home/test//bin/repo", line 197, in _Init rev = _Verify(dst, branch, opt.quiet) File "/home/test//bin/repo", line 370, in _Verify m = 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 285 print msg ^SyntaxError: invalid syntax
现在的错误就不是repo文件了,而是已经下载的文件中的其他错误。然后在文件管理器中查看python文件,发现接下来需要修改的地方很多:
时已至此,只能选择安装一个老版本的Python,比如网上大家建议的2.7。另外下载的步骤还是官网的说明:http://source.android.com/source/downloading.html。
特别要注意这一部分:
更多相关文章
- android相机开发setParameters failed错误
- Android(安卓)GreenDao3.2.2简单使用
- adb code
- Android(安卓)开发环境安装
- android apk--程序发布前的准备
- [Android(安卓)L]Android(安卓)L RippleView 效果 低版本实现
- 【android studio】 gradle配置成本地离线zip包
- 移植SlidingMenu Android(安卓)library,和安装example出现的问题
- 移植SlidingMenu Android(安卓)library和安装example出现的问题