Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(797)

Unified Diff: scripts/common/chromium_utils.py

Issue 12300004: Build scripts refactor to support multislave (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/build
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | scripts/master/master_utils.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: scripts/common/chromium_utils.py
diff --git a/scripts/common/chromium_utils.py b/scripts/common/chromium_utils.py
index 829d658def59616bf31823655d0ccc5b9a844389..89c1da0db29fa85aa4e6fac0a996df783334398e 100644
--- a/scripts/common/chromium_utils.py
+++ b/scripts/common/chromium_utils.py
@@ -11,6 +11,7 @@ import glob
import math
import os
import shutil
+import socket
import stat
import string # pylint: disable=W0402
import subprocess
@@ -26,6 +27,10 @@ except ImportError:
import simplejson as json
+BUILD_DIR = os.path.realpath(os.path.join(
+ os.path.dirname(__file__), os.pardir, os.pardir))
+
+
# Local errors.
class MissingArgument(Exception): pass
class PathNotFound(Exception): pass
@@ -979,10 +984,9 @@ def SshCopyTree(srctree, host, dst):
def ListMasters(cue='master.cfg', include_public=True, include_internal=True):
"""Returns all the masters found."""
# Look for "internal" masters first.
- path_internal = os.path.join(os.path.dirname(__file__), '..', '..', '..',
- 'build_internal', 'masters/*/' + cue)
- path = os.path.join(os.path.dirname(__file__), '..', '..',
- 'masters/*/' + cue)
+ path_internal = os.path.join(
+ BUILD_DIR, os.pardir, 'build_internal', 'masters/*/' + cue)
+ path = os.path.join(BUILD_DIR, 'masters/*/' + cue)
filenames = []
if include_public:
filenames += glob.glob(path)
@@ -991,26 +995,65 @@ def ListMasters(cue='master.cfg', include_public=True, include_internal=True):
return [os.path.abspath(os.path.dirname(f)) for f in filenames]
-def RunSlavesCfg(slaves_cfg):
- """Runs slaves.cfg in a consistent way."""
- if not os.path.exists(slaves_cfg):
- return []
- slaves_path = os.path.dirname(os.path.abspath(slaves_cfg))
+def GetAllSlaves():
+ """Return all slave objects from masters."""
+ slaves = []
+ for master in ListMasters(cue='slaves.cfg'):
+ cur_slaves = RunSlavesCfg(os.path.join(master, 'slaves.cfg'))
+ for slave in cur_slaves:
+ slave['mastername'] = os.path.basename(master)
+ slaves.extend(cur_slaves)
+ return slaves
+
+
+def GetActiveSlavename():
+ testing_slavename = os.getenv('TESTING_SLAVENAME')
+ if testing_slavename:
+ return testing_slavename
+ return socket.getfqdn().split('.', 1)[0].lower()
+
+
+def EntryToSlaveName(entry):
+ """Produces slave name from the slaves config dict."""
+ return entry.get('slavename') or entry.get('hostname')
+
+
+def GetActiveMaster(slavename=None):
+ """Parses all the slaves.cfg and returns the name of the active master
+ determined by the hostname. Returns None otherwise.
+
+ It will be matched against *both* the 'slavename' and 'hostname' fields
+ in slaves.cfg.
+ """
+ slavename = slavename or GetActiveSlavename()
+ for slave in GetAllSlaves():
+ if slavename == EntryToSlaveName(slave):
+ return slave['master']
+
+
+def ParsePythonCfg(cfg_filepath):
+ """Retrieves data from a python config file."""
+ base_path = os.path.dirname(os.path.abspath(cfg_filepath))
old_sys_path = sys.path
- sys.path = sys.path + [slaves_path]
+ sys.path = sys.path + [base_path]
+ old_path = os.getcwd()
try:
- old_path = os.getcwd()
- try:
- os.chdir(slaves_path)
- local_vars = {}
- execfile(os.path.join(slaves_cfg), local_vars)
- return local_vars['slaves']
- finally:
- os.chdir(old_path)
+ os.chdir(base_path)
+ local_vars = {}
+ execfile(os.path.join(cfg_filepath), local_vars)
+ return local_vars
finally:
+ os.chdir(old_path)
sys.path = old_sys_path
+def RunSlavesCfg(slaves_cfg):
+ """Runs slaves.cfg in a consistent way."""
+ if not os.path.exists(slaves_cfg):
+ return []
+ return ParsePythonCfg(slaves_cfg).get('slaves', [])
+
+
def convert_json(option, opt, value, parser):
"""Provide an OptionParser callback to unmarshal a JSON string."""
setattr(parser.values, option.dest, json.loads(value))
@@ -1061,7 +1104,6 @@ def GetCBuildbotConfigs(chromite_path=None):
except ImportError:
# To get around CQ pylint failures, because CQ doesn't check out chromite.
# TODO(maruel): Remove this try block when this issue is resolved.
- print 'cbuildbot_chromite not found! Returning empty config dictionary.'
return {}
@@ -1093,8 +1135,7 @@ def AddThirdPartyLibToPath(lib, override=False):
Setting 'override' to true will place the directory in the beginning of
sys.path, useful for overriding previously set packages.
"""
- libpath = os.path.abspath(os.path.join(os.path.dirname(__file__),
- '..', '..', 'third_party', lib))
+ libpath = os.path.abspath(os.path.join(BUILD_DIR, 'third_party', lib))
if override:
sys.path.insert(0, libpath)
else:
« no previous file with comments | « no previous file | scripts/master/master_utils.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698