Skip to content

Commit 0230d91

Browse files
Cleanup chromite_wrapper.
The original design of this had some issues: 1) forced targets to be importable via 'buildbot.cbuildbot', rather than the proper/full 'chromite.buildbot.cbuildbot'. Scripts worked around this, but it's an unwanted limitation. 2) That importation requirement means that within the chroot, we've had to export cros_sdk *and* cros_sdk.py in the PATH. This is undesirable clutter, and introduces potential errors as scripts localize themselves to cros_sdk.py rather than invoking cros_sdk (the consumers should be agnostic to the language the tool is written in). 3) chromite_wrapper enforced assumptions about python namespace w/in the targets- specifically that 'main' must always be invokable without any arguments. This limits refactoring/cleanup in chromite via having to support ancient API assumptions (api's that weren't public); modern chromite has repurposed main changing the prototype, and using it's own wrapper doing signal handler setup, and general framework behaviour. Longer term, that 'main' functor is unlikely to even exist. The strong coupling chromite_wrapper forced limits are refactoring possibilities. 4) In modern chromite, all user consumable tools are now required to exist w/in chromite/bin/, and be executable and invokable. This is what we want going forward. 5) Implied we want chromite_wrapper used w/in the chroot; we don't, thus drop all CROS_WORKON_SRCROOT awareness. 6) Exposed a chromite_wrapper invokable (that didn't work) into the PATH outside the chroot; this is resolved via moving it into a support directory and repointing symlinks to it. At this point, if it's working with a modern chromite checkout the script is a simple execv pass thru. If it isn't, then it will fallback to the old import trickery. This has been tested against R16, R17, R18, ToT, 0.11.241.B, factory-*, basically all branches w/in chromite without issue. git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@128555 0039d316-1c4b-4281-b951-d872f2087c98
1 parent a2cbbbb commit 0230d91

File tree

5 files changed

+92
-104
lines changed

5 files changed

+92
-104
lines changed

cbuildbot

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
chromite_wrapper
1+
support/chromite_wrapper

chrome_set_ver

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
chromite_wrapper
1+
support/chromite_wrapper

chromite_wrapper

-101
This file was deleted.

cros_sdk

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
chromite_wrapper
1+
support/chromite_wrapper

support/chromite_wrapper

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env python
2+
# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
3+
# Use of this source code is governed by a BSD-style license that can be
4+
# found in the LICENSE file.
5+
6+
"""Wrapper for chromite tools.
7+
8+
The script is intend to be symlinked to any number of chromite tools, attempts
9+
to find the path for chromite, and hands off to the right tool via exec if
10+
possible.
11+
12+
It is intended to used strictly outside of the chroot.
13+
14+
If you're looking at a copy and want to know where the original looks at, look
15+
here:
16+
http://git.chromium.org/gitweb/?p=chromite.git;a=blob;f=bin/chromite
17+
18+
Since this script is _copied_, it should remain small and not use internal libs.
19+
20+
"""
21+
22+
import errno
23+
import os
24+
import sys
25+
26+
# Due to historical reasons, and the fact depot_tools ToT is used by older
27+
# factory branches (lacking chromite script cleanups), note we have to
28+
# fallback to some odd import locations. This is the only reason for the
29+
# fallback code- any/all new scripts symlinked to this script *must* exist
30+
# in chromite/bin/ .
31+
32+
def _FindRoot(path):
33+
"""Find the root of a repo checkout"""
34+
path = os.path.abspath(path)
35+
while path != '/':
36+
# Look for the chromite repository itself- it's always been at the root
37+
# of a repo checkout.
38+
if all(os.path.exists(os.path.join(path, x))
39+
for x in ['.repo', 'chromite/.git']):
40+
return path
41+
path = os.path.dirname(path)
42+
return None
43+
44+
45+
def _MissingErrorOut(target):
46+
sys.stderr.write(
47+
"""ERROR: Couldn't find the chromite tool %s.
48+
49+
Please change to a directory inside your Chromium OS source tree
50+
and retry. If you need to setup a Chromium OS source tree, see
51+
http://www.chromium.org/chromium-os/developer-guide
52+
""" % target)
53+
return 127
54+
55+
56+
def main():
57+
root = _FindRoot(os.getcwd())
58+
target = os.path.basename(sys.argv[0])
59+
if root is None:
60+
return _MissingErrorOut(target)
61+
62+
path = os.path.join(root, 'chromite/bin', target)
63+
try:
64+
os.execv(path, [path] + sys.argv[1:])
65+
except EnvironmentError, e:
66+
if e.errno not in (errno.ENOENT, errno.EPERM):
67+
raise
68+
69+
# Reaching here means it's either a bad target, or we're working against
70+
# an old (pre 6be2efcf5bb575b03862113eec097c44d8d7f93e) revision of
71+
# chromite. Fallback to trying to import it; this code works at least as
72+
# far back as branch 0.11.241.B; likely further.
73+
74+
if target == 'cbuildbot':
75+
target = 'chromite.buildbot.cbuildbot'
76+
else:
77+
target = 'chromite.bin.%s' % (target,)
78+
79+
# Adjust the path importation so we can import our our target.
80+
sys.path.insert(0, root)
81+
82+
try:
83+
module = __import__(target, fromlist=['main'])
84+
except ImportError:
85+
return _MissingErrorOut(target)
86+
return module.main()
87+
88+
if __name__ == '__main__':
89+
sys.exit(main())

0 commit comments

Comments
 (0)