Revision: 23719
Author:   [email protected]
Date:     Fri Sep  5 09:19:48 2014 UTC
Log:      Add cwd to all shell commands in auto roll scripts.

The v8 root directory is assumed to be the default cwd. All
commands executed in another directory (e.g. the chromium
checkout) need an explicit specification (also in the
tests).

This also fixes several small testing and robustness bugs:
- Get rid of all 'rm ...' shell calls
- Don't leak tmp files/dirs
- Add some forgotten shell calls to the test expectations
- Hardcode the DEPS location (must always be
chromium_dir/DEPS)
- Expect correct return code when terminating gracefully

BUG=chromium:408523
LOG=n
[email protected]
TEST=script_test.py

Review URL: https://codereview.chromium.org/540973002
https://code.google.com/p/v8/source/detail?r=23719

Modified:
 /branches/bleeding_edge/tools/push-to-trunk/chromium_roll.py
 /branches/bleeding_edge/tools/push-to-trunk/common_includes.py
 /branches/bleeding_edge/tools/push-to-trunk/git_recipes.py
 /branches/bleeding_edge/tools/push-to-trunk/push_to_trunk.py
 /branches/bleeding_edge/tools/push-to-trunk/releases.py
 /branches/bleeding_edge/tools/push-to-trunk/test_scripts.py

=======================================
--- /branches/bleeding_edge/tools/push-to-trunk/chromium_roll.py Thu Sep 4 08:42:21 2014 UTC +++ /branches/bleeding_edge/tools/push-to-trunk/chromium_roll.py Fri Sep 5 09:19:48 2014 UTC
@@ -9,13 +9,11 @@

 from common_includes import *

-DEPS_FILE = "DEPS_FILE"
 CHROMIUM = "CHROMIUM"

 CONFIG = {
   PERSISTFILE_BASENAME: "/tmp/v8-chromium-roll-tempfile",
   DOT_GIT_LOCATION: ".git",
-  DEPS_FILE: "DEPS",
 }


@@ -43,13 +41,14 @@

   def RunStep(self):
     self["v8_path"] = os.getcwd()
-    os.chdir(self._options.chromium)
+    cwd = self._options.chromium
+    os.chdir(cwd)
     self.InitialEnvironmentChecks()
     # Check for a clean workdir.
-    if not self.GitIsWorkdirClean():  # pragma: no cover
+    if not self.GitIsWorkdirClean(cwd=cwd):  # pragma: no cover
self.Die("Workspace is not clean. Please commit or undo your changes.")
     # Assert that the DEPS file is there.
-    if not os.path.exists(self.Config(DEPS_FILE)):  # pragma: no cover
+    if not os.path.exists(os.path.join(cwd, "DEPS")):  # pragma: no cover
       self.Die("DEPS file not present.")


@@ -57,28 +56,25 @@
   MESSAGE = "Update the checkout and create a new branch."

   def RunStep(self):
-    os.chdir(self._options.chromium)
-    self.GitCheckout("master")
-    self._side_effect_handler.Command("gclient", "sync --nohooks")
-    self.GitPull()
-    try:
-      # TODO(machenbach): Add cwd to git calls.
-      os.chdir(os.path.join(self._options.chromium, "v8"))
-      self.GitFetchOrigin()
-    finally:
-      os.chdir(self._options.chromium)
-    self.GitCreateBranch("v8-roll-%s" % self["trunk_revision"])
+    self.GitCheckout("master", cwd=self._options.chromium)
+    self.Command("gclient", "sync --nohooks", cwd=self._options.chromium)
+    self.GitPull(cwd=self._options.chromium)
+
+    # Update v8 remotes.
+    self.GitFetchOrigin()
+
+    self.GitCreateBranch("v8-roll-%s" % self["trunk_revision"],
+                         cwd=self._options.chromium)


 class UploadCL(Step):
   MESSAGE = "Create and upload CL."

   def RunStep(self):
-    os.chdir(self._options.chromium)
-
     # Patch DEPS file.
-    if self._side_effect_handler.Command(
-        "roll-dep", "v8 %s" % self["trunk_revision"]) is None:
+    if self.Command(
+        "roll-dep", "v8 %s" % self["trunk_revision"],
+        cwd=self._options.chromium) is None:
       self.Die("Failed to create deps for %s" % self["trunk_revision"])

     commit_title = "Update V8 to %s." % self["push_title"].lower()
@@ -88,18 +84,23 @@
                  % self["sheriff"])
     self.GitCommit("%s%s\n\nTBR=%s" %
                        (commit_title, sheriff, self._options.reviewer),
-                   author=self._options.author)
+                   author=self._options.author,
+                   cwd=self._options.chromium)
     if not self._options.dry_run:
       self.GitUpload(author=self._options.author,
                      force=True,
-                     cq=self._options.use_commit_queue)
+                     cq=self._options.use_commit_queue,
+                     cwd=self._options.chromium)
       print "CL uploaded."
     else:
-      self.GitCheckout("master")
-      self.GitDeleteBranch("v8-roll-%s" % self["trunk_revision"])
+      self.GitCheckout("master", cwd=self._options.chromium)
+      self.GitDeleteBranch("v8-roll-%s" % self["trunk_revision"],
+                           cwd=self._options.chromium)
       print "Dry run - don't upload."


+# TODO(machenbach): Make this obsolete. We are only in the chromium chechout
+# for the initial .git check.
 class SwitchV8(Step):
   MESSAGE = "Returning to V8 checkout."

=======================================
--- /branches/bleeding_edge/tools/push-to-trunk/common_includes.py Thu Sep 4 10:19:44 2014 UTC +++ /branches/bleeding_edge/tools/push-to-trunk/common_includes.py Fri Sep 5 09:19:48 2014 UTC
@@ -29,10 +29,12 @@
 import argparse
 import datetime
 import httplib
+import glob
 import imp
 import json
 import os
 import re
+import shutil
 import subprocess
 import sys
 import textwrap
@@ -52,6 +54,10 @@
 COMMITMSG_FILE = "COMMITMSG_FILE"
 PATCH_FILE = "PATCH_FILE"

+# V8 base directory.
+DEFAULT_CWD = os.path.dirname(
+    os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+

 def TextToFile(text, file_name):
   with open(file_name, "w") as f:
@@ -183,16 +189,18 @@

# Some commands don't like the pipe, e.g. calling vi from within the script or
 # from subscripts like git cl upload.
-def Command(cmd, args="", prefix="", pipe=True):
+def Command(cmd, args="", prefix="", pipe=True, cwd=None):
+  cwd = cwd or os.getcwd()
   # TODO(machenbach): Use timeout.
   cmd_line = "%s %s %s" % (prefix, cmd, args)
   print "Command: %s" % cmd_line
+  print "in %s" % cwd
   sys.stdout.flush()
   try:
     if pipe:
-      return subprocess.check_output(cmd_line, shell=True)
+      return subprocess.check_output(cmd_line, shell=True, cwd=cwd)
     else:
-      return subprocess.check_call(cmd_line, shell=True)
+      return subprocess.check_call(cmd_line, shell=True, cwd=cwd)
   except subprocess.CalledProcessError:
     return None
   finally:
@@ -205,8 +213,8 @@
   def Call(self, fun, *args, **kwargs):
     return fun(*args, **kwargs)

-  def Command(self, cmd, args="", prefix="", pipe=True):
-    return Command(cmd, args, prefix, pipe)
+  def Command(self, cmd, args="", prefix="", pipe=True, cwd=None):
+    return Command(cmd, args, prefix, pipe, cwd=cwd)

   def ReadLine(self):
     return sys.stdin.readline().strip()
@@ -263,6 +271,10 @@
     self._state = state
     self._options = options
     self._side_effect_handler = handler
+
+    # The testing configuration might set a different default cwd.
+    self.default_cwd = self._config.get("DEFAULT_CWD") or DEFAULT_CWD
+
     assert self._number >= 0
     assert self._config is not None
     assert self._state is not None
@@ -341,21 +353,31 @@
     else:
       return self._side_effect_handler.ReadLine()

-  def Git(self, args="", prefix="", pipe=True, retry_on=None):
- cmd = lambda: self._side_effect_handler.Command("git", args, prefix, pipe)
+  def Command(self, name, args, cwd=None):
+    cmd = lambda: self._side_effect_handler.Command(
+        name, args, "", True, cwd=cwd or self.default_cwd)
+    return self.Retry(cmd, None, [5])
+
+  def Git(self, args="", prefix="", pipe=True, retry_on=None, cwd=None):
+    cmd = lambda: self._side_effect_handler.Command(
+        "git", args, prefix, pipe, cwd=cwd or self.default_cwd)
     result = self.Retry(cmd, retry_on, [5, 30])
     if result is None:
       raise GitFailedException("'git %s' failed." % args)
     return result

-  def SVN(self, args="", prefix="", pipe=True, retry_on=None):
- cmd = lambda: self._side_effect_handler.Command("svn", args, prefix, pipe)
+  def SVN(self, args="", prefix="", pipe=True, retry_on=None, cwd=None):
+    cmd = lambda: self._side_effect_handler.Command(
+        "svn", args, prefix, pipe, cwd=cwd or self.default_cwd)
     return self.Retry(cmd, retry_on, [5, 30])

   def Editor(self, args):
     if self._options.requires_editor:
-      return self._side_effect_handler.Command(os.environ["EDITOR"], args,
-                                               pipe=False)
+      return self._side_effect_handler.Command(
+          os.environ["EDITOR"],
+          args,
+          pipe=False,
+          cwd=self.default_cwd)

   def ReadURL(self, url, params=None, retry_on=None, wait_plan=None):
     wait_plan = wait_plan or [3, 60, 600]
@@ -399,7 +421,8 @@

     # Cancel if EDITOR is unset or not executable.
     if (self._options.requires_editor and (not os.environ.get("EDITOR") or
- Command("which", os.environ["EDITOR"]) is None)): # pragma: no cover
+        self.Command(
+            "which", os.environ["EDITOR"]) is None)):  # pragma: no cover
self.Die("Please set your EDITOR environment variable, you'll need it.")

   def CommonPrepare(self):
@@ -423,7 +446,11 @@
       self.GitDeleteBranch(self._config[BRANCHNAME])

     # Clean up all temporary files.
-    Command("rm", "-f %s*" % self._config[PERSISTFILE_BASENAME])
+    for f in glob.iglob("%s*" % self._config[PERSISTFILE_BASENAME]):
+      if os.path.isfile(f):
+        os.remove(f)
+      if os.path.isdir(f):
+        shutil.rmtree(f)

   def ReadAndPersistVersion(self, prefix=""):
     def ReadAndPersist(var_name, def_name):
@@ -607,7 +634,6 @@
     parser.add_argument("-s", "--step",
         help="Specify the step where to start work. Default: 0.",
         default=0, type=int)
-
     self._PrepareOptions(parser)

     if args is None:  # pragma: no cover
=======================================
--- /branches/bleeding_edge/tools/push-to-trunk/git_recipes.py Thu Sep 4 08:42:21 2014 UTC +++ /branches/bleeding_edge/tools/push-to-trunk/git_recipes.py Fri Sep 5 09:19:48 2014 UTC
@@ -94,54 +94,55 @@


 class GitRecipesMixin(object):
-  def GitIsWorkdirClean(self):
-    return self.Git("status -s -uno").strip() == ""
+  def GitIsWorkdirClean(self, **kwargs):
+    return self.Git("status -s -uno", **kwargs).strip() == ""

   @Strip
-  def GitBranch(self):
-    return self.Git("branch")
+  def GitBranch(self, **kwargs):
+    return self.Git("branch", **kwargs)

-  def GitCreateBranch(self, name, branch=""):
+  def GitCreateBranch(self, name, branch="", **kwargs):
     assert name
-    self.Git(MakeArgs(["checkout -b", name, branch]))
+    self.Git(MakeArgs(["checkout -b", name, branch]), **kwargs)

-  def GitDeleteBranch(self, name):
+  def GitDeleteBranch(self, name, **kwargs):
     assert name
-    self.Git(MakeArgs(["branch -D", name]))
+    self.Git(MakeArgs(["branch -D", name]), **kwargs)

-  def GitReset(self, name):
+  def GitReset(self, name, **kwargs):
     assert name
-    self.Git(MakeArgs(["reset --hard", name]))
+    self.Git(MakeArgs(["reset --hard", name]), **kwargs)

-  def GitStash(self):
-    self.Git(MakeArgs(["stash"]))
+  def GitStash(self, **kwargs):
+    self.Git(MakeArgs(["stash"]), **kwargs)

-  def GitRemotes(self):
-    return map(str.strip, self.Git(MakeArgs(["branch -r"])).splitlines())
+  def GitRemotes(self, **kwargs):
+    return map(str.strip,
+               self.Git(MakeArgs(["branch -r"]), **kwargs).splitlines())

-  def GitCheckout(self, name):
+  def GitCheckout(self, name, **kwargs):
     assert name
-    self.Git(MakeArgs(["checkout -f", name]))
+    self.Git(MakeArgs(["checkout -f", name]), **kwargs)

-  def GitCheckoutFile(self, name, branch_or_hash):
+  def GitCheckoutFile(self, name, branch_or_hash, **kwargs):
     assert name
     assert branch_or_hash
-    self.Git(MakeArgs(["checkout -f", branch_or_hash, "--", name]))
+ self.Git(MakeArgs(["checkout -f", branch_or_hash, "--", name]), **kwargs)

-  def GitCheckoutFileSafe(self, name, branch_or_hash):
+  def GitCheckoutFileSafe(self, name, branch_or_hash, **kwargs):
     try:
-      self.GitCheckoutFile(name, branch_or_hash)
+      self.GitCheckoutFile(name, branch_or_hash, **kwargs)
     except GitFailedException:  # pragma: no cover
       # The file doesn't exist in that revision.
       return False
     return True

-  def GitChangedFiles(self, git_hash):
+  def GitChangedFiles(self, git_hash, **kwargs):
     assert git_hash
     try:
       files = self.Git(MakeArgs(["diff --name-only",
                                  git_hash,
-                                 "%s^" % git_hash]))
+                                 "%s^" % git_hash]), **kwargs)
       return map(str.strip, files.splitlines())
     except GitFailedException:  # pragma: no cover
       # Git fails using "^" at branch roots.
@@ -149,15 +150,15 @@


   @Strip
-  def GitCurrentBranch(self):
-    for line in self.Git("status -s -b -uno").strip().splitlines():
+  def GitCurrentBranch(self, **kwargs):
+ for line in self.Git("status -s -b -uno", **kwargs).strip().splitlines():
       match = re.match(r"^## (.+)", line)
       if match: return match.group(1)
     raise Exception("Couldn't find curent branch.")  # pragma: no cover

   @Strip
   def GitLog(self, n=0, format="", grep="", git_hash="", parent_hash="",
-             branch="", reverse=False):
+             branch="", reverse=False, **kwargs):
     assert not (git_hash and parent_hash)
     args = ["log"]
     if n > 0:
@@ -173,27 +174,27 @@
     if parent_hash:
       args.append("%s^" % parent_hash)
     args.append(branch)
-    return self.Git(MakeArgs(args))
+    return self.Git(MakeArgs(args), **kwargs)

-  def GitGetPatch(self, git_hash):
+  def GitGetPatch(self, git_hash, **kwargs):
     assert git_hash
-    return self.Git(MakeArgs(["log", "-1", "-p", git_hash]))
+    return self.Git(MakeArgs(["log", "-1", "-p", git_hash]), **kwargs)

   # TODO(machenbach): Unused? Remove.
-  def GitAdd(self, name):
+  def GitAdd(self, name, **kwargs):
     assert name
-    self.Git(MakeArgs(["add", Quoted(name)]))
+    self.Git(MakeArgs(["add", Quoted(name)]), **kwargs)

-  def GitApplyPatch(self, patch_file, reverse=False):
+  def GitApplyPatch(self, patch_file, reverse=False, **kwargs):
     assert patch_file
     args = ["apply --index --reject"]
     if reverse:
       args.append("--reverse")
     args.append(Quoted(patch_file))
-    self.Git(MakeArgs(args))
+    self.Git(MakeArgs(args), **kwargs)

   def GitUpload(self, reviewer="", author="", force=False, cq=False,
-                bypass_hooks=False):
+                bypass_hooks=False, **kwargs):
     args = ["cl upload --send-mail"]
     if author:
       args += ["--email", Quoted(author)]
@@ -207,9 +208,9 @@
       args.append("--bypass-hooks")
# TODO(machenbach): Check output in forced mode. Verify that all required
     # base files were uploaded, if not retry.
-    self.Git(MakeArgs(args), pipe=False)
+    self.Git(MakeArgs(args), pipe=False, **kwargs)

-  def GitCommit(self, message="", file_name="", author=None):
+  def GitCommit(self, message="", file_name="", author=None, **kwargs):
     assert message or file_name
     args = ["commit"]
     if file_name:
@@ -218,28 +219,29 @@
       args += ["-am", Quoted(message)]
     if author:
       args += ["--author", "\"%s <%s>\"" % (author, author)]
-    self.Git(MakeArgs(args))
+    self.Git(MakeArgs(args), **kwargs)

-  def GitPresubmit(self):
-    self.Git("cl presubmit", "PRESUBMIT_TREE_CHECK=\"skip\"")
+  def GitPresubmit(self, **kwargs):
+    self.Git("cl presubmit", "PRESUBMIT_TREE_CHECK=\"skip\"", **kwargs)

-  def GitDCommit(self):
-    self.Git("cl dcommit -f --bypass-hooks", retry_on=lambda x: x is None)
+  def GitDCommit(self, **kwargs):
+    self.Git(
+ "cl dcommit -f --bypass-hooks", retry_on=lambda x: x is None, **kwargs)

-  def GitDiff(self, loc1, loc2):
-    return self.Git(MakeArgs(["diff", loc1, loc2]))
+  def GitDiff(self, loc1, loc2, **kwargs):
+    return self.Git(MakeArgs(["diff", loc1, loc2]), **kwargs)

-  def GitPull(self):
-    self.Git("pull")
+  def GitPull(self, **kwargs):
+    self.Git("pull", **kwargs)

-  def GitFetchOrigin(self):
-    self.Git("fetch origin")
+  def GitFetchOrigin(self, **kwargs):
+    self.Git("fetch origin", **kwargs)

-  def GitConvertToSVNRevision(self, git_hash):
-    result = self.Git(MakeArgs(["rev-list", "-n", "1", git_hash]))
+  def GitConvertToSVNRevision(self, git_hash, **kwargs):
+ result = self.Git(MakeArgs(["rev-list", "-n", "1", git_hash]), **kwargs)
     if not result or not SHA1_RE.match(result):
       raise GitFailedException("Git hash %s is unknown." % git_hash)
-    log = self.GitLog(n=1, format="%B", git_hash=git_hash)
+    log = self.GitLog(n=1, format="%B", git_hash=git_hash, **kwargs)
     for line in reversed(log.splitlines()):
       match = ROLL_DEPS_GIT_SVN_ID_RE.match(line.strip())
       if match:
@@ -248,7 +250,7 @@

   @Strip
   # Copied from bot_update.py and modified for svn-like numbers only.
-  def GetCommitPositionNumber(self, git_hash):
+  def GetCommitPositionNumber(self, git_hash, **kwargs):
"""Dumps the 'git' log for a specific revision and parses out the commit
     position number.

@@ -257,7 +259,7 @@
Otherwise, we will search for a 'git-svn' metadata entry. If one is found,
     its SVN revision value is returned.
     """
-    git_log = self.GitLog(format='%B', n=1, git_hash=git_hash)
+    git_log = self.GitLog(format='%B', n=1, git_hash=git_hash, **kwargs)
     footer_map = GetCommitMessageFooterMap(git_log)

     # Search for commit position metadata
@@ -277,29 +279,31 @@

   ### Git svn stuff

-  def GitSVNFetch(self):
-    self.Git("svn fetch")
+  def GitSVNFetch(self, **kwargs):
+    self.Git("svn fetch", **kwargs)

-  def GitSVNRebase(self):
-    self.Git("svn rebase")
+  def GitSVNRebase(self, **kwargs):
+    self.Git("svn rebase", **kwargs)

   # TODO(machenbach): Unused? Remove.
   @Strip
-  def GitSVNLog(self):
-    return self.Git("svn log -1 --oneline")
+  def GitSVNLog(self, **kwargs):
+    return self.Git("svn log -1 --oneline", **kwargs)

   @Strip
-  def GitSVNFindGitHash(self, revision, branch=""):
+  def GitSVNFindGitHash(self, revision, branch="", **kwargs):
     assert revision
-    return self.Git(MakeArgs(["svn find-rev", "r%s" % revision, branch]))
+    return self.Git(
+        MakeArgs(["svn find-rev", "r%s" % revision, branch]), **kwargs)

   @Strip
-  def GitSVNFindSVNRev(self, git_hash, branch=""):
-    return self.Git(MakeArgs(["svn find-rev", git_hash, branch]))
+  def GitSVNFindSVNRev(self, git_hash, branch="", **kwargs):
+    return self.Git(MakeArgs(["svn find-rev", git_hash, branch]), **kwargs)

-  def GitSVNDCommit(self):
-    return self.Git("svn dcommit 2>&1", retry_on=lambda x: x is None)
+  def GitSVNDCommit(self, **kwargs):
+ return self.Git("svn dcommit 2>&1", retry_on=lambda x: x is None, **kwargs)

-  def GitSVNTag(self, version):
+  def GitSVNTag(self, version, **kwargs):
     self.Git(("svn tag %s -m \"Tagging version %s\"" % (version, version)),
-             retry_on=lambda x: x is None)
+             retry_on=lambda x: x is None,
+             **kwargs)
=======================================
--- /branches/bleeding_edge/tools/push-to-trunk/push_to_trunk.py Wed Jul 23 09:25:36 2014 UTC +++ /branches/bleeding_edge/tools/push-to-trunk/push_to_trunk.py Fri Sep 5 09:19:48 2014 UTC
@@ -27,6 +27,7 @@
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 import argparse
+import os
 import sys
 import tempfile
 import urllib2
@@ -312,7 +313,7 @@

   def RunStep(self):
     self.ApplyPatch(self.Config(PATCH_FILE))
-    Command("rm", "-f %s*" % self.Config(PATCH_FILE))
+    os.remove(self.Config(PATCH_FILE))


 class AddChangeLog(Step):
@@ -345,7 +346,7 @@

   def RunStep(self):
     self.GitCommit(file_name = self.Config(COMMITMSG_FILE))
-    Command("rm", "-f %s*" % self.Config(COMMITMSG_FILE))
+    os.remove(self.Config(COMMITMSG_FILE))


 class SanityCheck(Step):
=======================================
--- /branches/bleeding_edge/tools/push-to-trunk/releases.py Thu Sep 4 08:42:21 2014 UTC +++ /branches/bleeding_edge/tools/push-to-trunk/releases.py Fri Sep 5 09:19:48 2014 UTC
@@ -20,7 +20,6 @@

 from common_includes import *

-DEPS_FILE = "DEPS_FILE"
 CHROMIUM = "CHROMIUM"

 CONFIG = {
@@ -28,7 +27,6 @@
   PERSISTFILE_BASENAME: "/tmp/v8-releases-tempfile",
   DOT_GIT_LOCATION: ".git",
   VERSION_FILE: "src/version.cc",
-  DEPS_FILE: "DEPS",
 }

# Expression for retrieving the bleeding edge revision from a commit message.
@@ -268,60 +266,42 @@
                               reverse=True)


-# TODO(machenbach): Parts of the Chromium setup are c/p from the chromium_roll
-# script -> unify.
-class CheckChromium(Step):
-  MESSAGE = "Check the chromium checkout."
-
-  def Run(self):
-    self["chrome_path"] = self._options.chromium
-
-
 class SwitchChromium(Step):
   MESSAGE = "Switch to Chromium checkout."
-  REQUIRES = "chrome_path"

   def RunStep(self):
-    self["v8_path"] = os.getcwd()
-    os.chdir(self["chrome_path"])
+    cwd = self._options.chromium
     # Check for a clean workdir.
-    if not self.GitIsWorkdirClean():  # pragma: no cover
+    if not self.GitIsWorkdirClean(cwd=cwd):  # pragma: no cover
self.Die("Workspace is not clean. Please commit or undo your changes.")
     # Assert that the DEPS file is there.
-    if not os.path.exists(self.Config(DEPS_FILE)):  # pragma: no cover
+    if not os.path.exists(os.path.join(cwd, "DEPS")):  # pragma: no cover
       self.Die("DEPS file not present.")


 class UpdateChromiumCheckout(Step):
   MESSAGE = "Update the checkout and create a new branch."
-  REQUIRES = "chrome_path"

   def RunStep(self):
-    os.chdir(self["chrome_path"])
-    self.GitCheckout("master")
-    self.GitPull()
-    self.GitCreateBranch(self.Config(BRANCHNAME))
+    cwd = self._options.chromium
+    self.GitCheckout("master", cwd=cwd)
+    self.GitPull(cwd=cwd)
+    self.GitCreateBranch(self.Config(BRANCHNAME), cwd=cwd)


 def ConvertToCommitNumber(step, revision):
   # Simple check for git hashes.
   if revision.isdigit() and len(revision) < 8:
     return revision
-  try:
-    # TODO(machenbach): Add cwd to git calls.
-    os.chdir(os.path.join(step["chrome_path"], "v8"))
-    return step.GitConvertToSVNRevision(revision)
-  finally:
-    os.chdir(step["chrome_path"])
+  return step.GitConvertToSVNRevision(
+      revision, cwd=os.path.join(step._options.chromium, "v8"))


 class RetrieveChromiumV8Releases(Step):
   MESSAGE = "Retrieve V8 releases from Chromium DEPS."
-  REQUIRES = "chrome_path"

   def RunStep(self):
-    os.chdir(self["chrome_path"])
-
+    cwd = self._options.chromium
     releases = filter(
lambda r: r["branch"] in ["trunk", "bleeding_edge"], self["releases"])
     if not releases:  # pragma: no cover
@@ -329,26 +309,22 @@
       return True

     # Update v8 checkout in chromium.
-    try:
-      # TODO(machenbach): Add cwd to git calls.
-      os.chdir(os.path.join(self["chrome_path"], "v8"))
-      self.GitFetchOrigin()
-    finally:
-      os.chdir(self["chrome_path"])
+    self.GitFetchOrigin(cwd=os.path.join(cwd, "v8"))

     oldest_v8_rev = int(releases[-1]["revision"])

     cr_releases = []
     try:
-      for git_hash in self.GitLog(format="%H", grep="V8").splitlines():
-        if self._config[DEPS_FILE] not in self.GitChangedFiles(git_hash):
+      for git_hash in self.GitLog(
+          format="%H", grep="V8", cwd=cwd).splitlines():
+        if "DEPS" not in self.GitChangedFiles(git_hash, cwd=cwd):
           continue
-        if not self.GitCheckoutFileSafe(self._config[DEPS_FILE], git_hash):
+        if not self.GitCheckoutFileSafe("DEPS", git_hash, cwd=cwd):
           break  # pragma: no cover
-        deps = FileToText(self.Config(DEPS_FILE))
+        deps = FileToText(os.path.join(cwd, "DEPS"))
         match = DEPS_RE.search(deps)
         if match:
-          cr_rev = self.GetCommitPositionNumber(git_hash)
+          cr_rev = self.GetCommitPositionNumber(git_hash, cwd=cwd)
           if cr_rev:
             v8_rev = ConvertToCommitNumber(self, match.group(1))
             cr_releases.append([cr_rev, v8_rev])
@@ -364,7 +340,7 @@
       pass

     # Clean up.
-    self.GitCheckoutFileSafe(self._config[DEPS_FILE], "HEAD")
+    self.GitCheckoutFileSafe("DEPS", "HEAD", cwd=cwd)

     # Add the chromium ranges to the v8 trunk and bleeding_edge releases.
     all_ranges = BuildRevisionRanges(cr_releases)
@@ -376,11 +352,9 @@
 # TODO(machenbach): Unify common code with method above.
 class RietrieveChromiumBranches(Step):
   MESSAGE = "Retrieve Chromium branch information."
-  REQUIRES = "chrome_path"

   def RunStep(self):
-    os.chdir(self["chrome_path"])
-
+    cwd = self._options.chromium
trunk_releases = filter(lambda r: r["branch"] == "trunk", self["releases"])
     if not trunk_releases:  # pragma: no cover
       print "No trunk releases detected. Skipping chromium history."
@@ -390,7 +364,7 @@

     # Filter out irrelevant branches.
     branches = filter(lambda r: re.match(r"branch-heads/\d+", r),
-                      self.GitRemotes())
+                      self.GitRemotes(cwd=cwd))

     # Transform into pure branch numbers.
branches = map(lambda r: int(re.match(r"branch-heads/(\d+)", r).group(1)),
@@ -401,10 +375,11 @@
     cr_branches = []
     try:
       for branch in branches:
-        if not self.GitCheckoutFileSafe(self._config[DEPS_FILE],
-                                        "branch-heads/%d" % branch):
+        if not self.GitCheckoutFileSafe("DEPS",
+                                        "branch-heads/%d" % branch,
+                                        cwd=cwd):
           break  # pragma: no cover
-        deps = FileToText(self.Config(DEPS_FILE))
+        deps = FileToText(os.path.join(cwd, "DEPS"))
         match = DEPS_RE.search(deps)
         if match:
           v8_rev = ConvertToCommitNumber(self, match.group(1))
@@ -421,7 +396,7 @@
       pass

     # Clean up.
-    self.GitCheckoutFileSafe(self._config[DEPS_FILE], "HEAD")
+    self.GitCheckoutFileSafe("DEPS", "HEAD", cwd=cwd)

     # Add the chromium branches to the v8 trunk releases.
     all_ranges = BuildRevisionRanges(cr_branches)
@@ -430,20 +405,12 @@
       trunk_dict.get(revision, {})["chromium_branch"] = ranges


-class SwitchV8(Step):
-  MESSAGE = "Returning to V8 checkout."
-  REQUIRES = "chrome_path"
-
-  def RunStep(self):
-    self.GitCheckout("master")
-    self.GitDeleteBranch(self.Config(BRANCHNAME))
-    os.chdir(self["v8_path"])
-
-
 class CleanUp(Step):
   MESSAGE = "Clean up."

   def RunStep(self):
+    self.GitCheckout("master", cwd=self._options.chromium)
+ self.GitDeleteBranch(self.Config(BRANCHNAME), cwd=self._options.chromium)
     self.CommonCleanup()


@@ -488,12 +455,10 @@
     return [
       Preparation,
       RetrieveV8Releases,
-      CheckChromium,
       SwitchChromium,
       UpdateChromiumCheckout,
       RetrieveChromiumV8Releases,
       RietrieveChromiumBranches,
-      SwitchV8,
       CleanUp,
       WriteOutput,
     ]
=======================================
--- /branches/bleeding_edge/tools/push-to-trunk/test_scripts.py Thu Sep 4 09:56:29 2014 UTC +++ /branches/bleeding_edge/tools/push-to-trunk/test_scripts.py Fri Sep 5 09:19:48 2014 UTC
@@ -27,6 +27,7 @@
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 import os
+import shutil
 import tempfile
 import traceback
 import unittest
@@ -44,7 +45,6 @@
 from push_to_trunk import *
 import chromium_roll
 from chromium_roll import CHROMIUM
-from chromium_roll import DEPS_FILE
 from chromium_roll import ChromiumRoll
 import releases
 from releases import Releases
@@ -56,6 +56,7 @@


 TEST_CONFIG = {
+  "DEFAULT_CWD": "[DEFAULT_CWD]",
   BRANCHNAME: "test-prepare-push",
   TRUNKBRANCH: "test-trunk-push",
   PERSISTFILE_BASENAME: "/tmp/test-v8-push-to-trunk-tempfile",
@@ -66,7 +67,6 @@
   PATCH_FILE: "/tmp/test-v8-push-to-trunk-tempfile-patch",
   COMMITMSG_FILE: "/tmp/test-v8-push-to-trunk-tempfile-commitmsg",
   CHROMIUM: "/tmp/test-v8-push-to-trunk-tempfile-chromium",
-  DEPS_FILE: "/tmp/test-v8-push-to-trunk-tempfile-chromium/DEPS",
   SETTINGS_LOCATION: None,
   ALREADY_MERGING_SENTINEL_FILE:
       "/tmp/test-merge-to-branch-tempfile-already-merging",
@@ -259,12 +259,19 @@
     "args": args,
     "ret": args[-1],
     "cb": kwargs.get("cb"),
+    "cwd": kwargs.get("cwd", "[DEFAULT_CWD]"),
   }


 def RL(text, cb=None):
   """Convenience function returning a readline test expectation."""
-  return {"name": "readline", "args": [], "ret": text, "cb": cb}
+  return {
+    "name": "readline",
+    "args": [],
+    "ret": text,
+    "cb": cb,
+    "cwd": None,
+  }


 def URL(*args, **kwargs):
@@ -274,6 +281,7 @@
     "args": args[:-1],
     "ret": args[-1],
     "cb": kwargs.get("cb"),
+    "cwd": None,
   }


@@ -285,7 +293,7 @@
   def Expect(self, recipe):
     self._recipe = recipe

-  def Call(self, name, *args):  # pragma: no cover
+  def Call(self, name, *args, **kwargs):  # pragma: no cover
     self._index += 1
     try:
       expected_call = self._recipe[self._index]
@@ -300,6 +308,14 @@
       raise NoRetryException("Expected action: %s %s - Actual: %s" %
           (expected_call["name"], expected_call["args"], name))

+    # Check if the given working directory matches the expected one.
+    if expected_call["cwd"] != kwargs.get("cwd"):
+      raise NoRetryException("Expected cwd: %s in %s %s - Actual: %s" %
+          (expected_call["cwd"],
+           expected_call["name"],
+           expected_call["args"],
+           kwargs.get("cwd")))
+
     # The number of arguments in the expectation must match the actual
     # arguments.
     if len(args) > len(expected_call['args']):
@@ -339,6 +355,12 @@
     os.close(handle)
     self._tmp_files.append(name)
     return name
+
+  def MakeEmptyTempDirectory(self):
+    name = tempfile.mkdtemp()
+    self._tmp_files.append(name)
+    return name
+

   def WriteFakeVersionFile(self, minor=22, build=4, patch=0):
     with open(TEST_CONFIG[VERSION_FILE], "w") as f:
@@ -366,9 +388,10 @@
   def Call(self, fun, *args, **kwargs):
     print "Calling %s with %s and %s" % (str(fun), str(args), str(kwargs))

-  def Command(self, cmd, args="", prefix="", pipe=True):
+  def Command(self, cmd, args="", prefix="", pipe=True, cwd=None):
     print "%s %s" % (cmd, args)
-    return self._mock.Call("command", cmd + " " + args)
+    print "in %s" % cwd
+    return self._mock.Call("command", cmd + " " + args, cwd=cwd)

   def ReadLine(self):
     return self._mock.Call("readline")
@@ -403,18 +426,18 @@
     self._state = {}

   def tearDown(self):
-    Command("rm", "-rf %s*" % TEST_CONFIG[PERSISTFILE_BASENAME])
+    if os.path.exists(TEST_CONFIG[PERSISTFILE_BASENAME]):
+      shutil.rmtree(TEST_CONFIG[PERSISTFILE_BASENAME])

     # Clean up temps. Doesn't work automatically.
     for name in self._tmp_files:
-      if os.path.exists(name):
+      if os.path.isfile(name):
         os.remove(name)
+      if os.path.isdir(name):
+        shutil.rmtree(name)

     self._mock.AssertFinished()

-  def testGitOrig(self):
-    self.assertTrue(Command("git", "--version").startswith("git version"))
-
   def testGitMock(self):
     self.Expect([Cmd("git --version", "git version 1.2.3"),
                  Cmd("git dummy", "")])
@@ -462,6 +485,9 @@
   def testInitialEnvironmentChecks(self):
     TEST_CONFIG[DOT_GIT_LOCATION] = self.MakeEmptyTempFile()
     os.environ["EDITOR"] = "vi"
+    self.Expect([
+      Cmd("which vi", "/usr/bin/vi"),
+    ])
     self.MakeStep().InitialEnvironmentChecks()

   def testReadAndPersistVersion(self):
@@ -711,7 +737,10 @@
           change_log)

     force_flag = " -f" if not manual else ""
-    expectations = [
+    expectations = []
+    if not force:
+      expectations.append(Cmd("which vi", "/usr/bin/vi"))
+    expectations += [
       Cmd("git status -s -uno", ""),
       Cmd("git status -s -b -uno", "## some_branch\n"),
       Cmd("git svn fetch", ""),
@@ -827,16 +856,18 @@
 def get_list():
   pass""")

+    # Setup fake directory structures.
     TEST_CONFIG[DOT_GIT_LOCATION] = self.MakeEmptyTempFile()
-    if not os.path.exists(TEST_CONFIG[CHROMIUM]):
-      os.makedirs(TEST_CONFIG[CHROMIUM])
-    if not os.path.exists(os.path.join(TEST_CONFIG[CHROMIUM], "v8")):
-      os.makedirs(os.path.join(TEST_CONFIG[CHROMIUM], "v8"))
+    TEST_CONFIG[CHROMIUM] = self.MakeEmptyTempDirectory()
+    chrome_dir = TEST_CONFIG[CHROMIUM]
+    os.makedirs(os.path.join(chrome_dir, "v8"))
+
+    # Write fake deps file.
     TextToFile("Some line\n   \"v8_revision\": \"123444\",\n  some line",
-               TEST_CONFIG[DEPS_FILE])
+               os.path.join(chrome_dir, "DEPS"))
     def WriteDeps():
       TextToFile("Some line\n   \"v8_revision\": \"22624\",\n  some line",
-                 TEST_CONFIG[DEPS_FILE])
+                 os.path.join(chrome_dir, "DEPS"))

     expectations = [
       Cmd("git fetch origin", ""),
@@ -848,29 +879,30 @@
           "Version 3.22.5 (based on bleeding_edge revision r22622)\n"),
       URL("https://chromium-build.appspot.com/p/chromium/sheriff_v8.js";,
           "document.write('g_name')"),
-      Cmd("git status -s -uno", ""),
-      Cmd("git checkout -f master", ""),
-      Cmd("gclient sync --nohooks", "syncing..."),
-      Cmd("git pull", ""),
+      Cmd("git status -s -uno", "", cwd=chrome_dir),
+      Cmd("git checkout -f master", "", cwd=chrome_dir),
+      Cmd("gclient sync --nohooks", "syncing...", cwd=chrome_dir),
+      Cmd("git pull", "", cwd=chrome_dir),
       Cmd("git fetch origin", ""),
-      Cmd("git checkout -b v8-roll-22624", ""),
-      Cmd("roll-dep v8 22624", "rolled", cb=WriteDeps),
+      Cmd("git checkout -b v8-roll-22624", "", cwd=chrome_dir),
+      Cmd("roll-dep v8 22624", "rolled", cb=WriteDeps, cwd=chrome_dir),
       Cmd(("git commit -am \"Update V8 to version 3.22.5 "
            "(based on bleeding_edge revision r22622).\n\n"
            "Please reply to the V8 sheriff [email protected] in "
            "case of problems.\n\[email protected]\" "
            "--author \"[email protected] <[email protected]>\""),
-          ""),
- Cmd("git cl upload --send-mail --email \"[email protected]\" -f", ""),
+          "", cwd=chrome_dir),
+ Cmd("git cl upload --send-mail --email \"[email protected]\" -f", "",
+          cwd=chrome_dir),
     ]
     self.Expect(expectations)

-    args = ["-a", "[email protected]", "-c", TEST_CONFIG[CHROMIUM],
+    args = ["-a", "[email protected]", "-c", chrome_dir,
             "--sheriff", "--googlers-mapping", googlers_mapping_py,
             "-r", "[email protected]"]
     ChromiumRoll(TEST_CONFIG, self).Run(args)

-    deps = FileToText(TEST_CONFIG[DEPS_FILE])
+    deps = FileToText(os.path.join(chrome_dir, "DEPS"))
     self.assertTrue(re.search("\"v8_revision\": \"22624\"", deps))

   def testCheckLastPushRecently(self):
@@ -955,7 +987,7 @@

     result = auto_roll.AutoRoll(TEST_CONFIG, self).Run(
         AUTO_PUSH_ARGS + ["-c", TEST_CONFIG[CHROMIUM]])
-    self.assertEquals(1, result)
+    self.assertEquals(0, result)

   # Snippet from the original DEPS file.
   FAKE_DEPS = """
@@ -970,7 +1002,7 @@
 """

   def testAutoRollUpToDate(self):
-    os.makedirs(TEST_CONFIG[CHROMIUM])
+    TEST_CONFIG[CHROMIUM] = self.MakeEmptyTempDirectory()
     TextToFile(self.FAKE_DEPS, os.path.join(TEST_CONFIG[CHROMIUM], "DEPS"))
     self.Expect([
       URL("https://codereview.chromium.org/search";,
@@ -985,10 +1017,10 @@

     result = auto_roll.AutoRoll(TEST_CONFIG, self).Run(
         AUTO_PUSH_ARGS + ["-c", TEST_CONFIG[CHROMIUM]])
-    self.assertEquals(1, result)
+    self.assertEquals(0, result)

   def testAutoRoll(self):
-    os.makedirs(TEST_CONFIG[CHROMIUM])
+    TEST_CONFIG[CHROMIUM] = self.MakeEmptyTempDirectory()
     TextToFile(self.FAKE_DEPS, os.path.join(TEST_CONFIG[CHROMIUM], "DEPS"))
     TEST_CONFIG[CLUSTERFUZZ_API_KEY_FILE]  = self.MakeEmptyTempFile()
     TextToFile("fake key", TEST_CONFIG[CLUSTERFUZZ_API_KEY_FILE])
@@ -1194,13 +1226,13 @@
     self.WriteFakeVersionFile()

     TEST_CONFIG[DOT_GIT_LOCATION] = self.MakeEmptyTempFile()
-    if not os.path.exists(TEST_CONFIG[CHROMIUM]):
-      os.makedirs(TEST_CONFIG[CHROMIUM])
-    if not os.path.exists(os.path.join(TEST_CONFIG[CHROMIUM], "v8")):
-      os.makedirs(os.path.join(TEST_CONFIG[CHROMIUM], "v8"))
+    TEST_CONFIG[CHROMIUM] = self.MakeEmptyTempDirectory()
+    chrome_dir = TEST_CONFIG[CHROMIUM]
+    chrome_v8_dir = os.path.join(chrome_dir, "v8")
+    os.makedirs(chrome_v8_dir)
     def WriteDEPS(revision):
       TextToFile("Line\n   \"v8_revision\": \"%s\",\n  line\n" % revision,
-                 TEST_CONFIG[DEPS_FILE])
+                 os.path.join(chrome_dir, "DEPS"))
     WriteDEPS(567)

     def ResetVersion(minor, build, patch=0):
@@ -1262,34 +1294,38 @@
       Cmd("git svn find-rev r22624", "hash_22624"),
       Cmd("git svn find-rev hash_22624", "22624"),
       Cmd("git log -1 --format=%ci hash_22624", "02:34"),
-      Cmd("git status -s -uno", ""),
-      Cmd("git checkout -f master", ""),
-      Cmd("git pull", ""),
-      Cmd("git checkout -b %s" % TEST_CONFIG[BRANCHNAME], ""),
-      Cmd("git fetch origin", ""),
- Cmd("git log --format=%H --grep=\"V8\"", "c_hash1\nc_hash2\nc_hash3\n"),
-      Cmd("git diff --name-only c_hash1 c_hash1^", ""),
-      Cmd("git diff --name-only c_hash2 c_hash2^", TEST_CONFIG[DEPS_FILE]),
-      Cmd("git checkout -f c_hash2 -- %s" % TEST_CONFIG[DEPS_FILE], "",
-          cb=ResetDEPS("0123456789012345678901234567890123456789")),
-      Cmd("git log -1 --format=%B c_hash2", c_hash2_commit_log),
+      Cmd("git status -s -uno", "", cwd=chrome_dir),
+      Cmd("git checkout -f master", "", cwd=chrome_dir),
+      Cmd("git pull", "", cwd=chrome_dir),
+ Cmd("git checkout -b %s" % TEST_CONFIG[BRANCHNAME], "", cwd=chrome_dir),
+      Cmd("git fetch origin", "", cwd=chrome_v8_dir),
+ Cmd("git log --format=%H --grep=\"V8\"", "c_hash1\nc_hash2\nc_hash3\n",
+          cwd=chrome_dir),
+      Cmd("git diff --name-only c_hash1 c_hash1^", "", cwd=chrome_dir),
+      Cmd("git diff --name-only c_hash2 c_hash2^", "DEPS", cwd=chrome_dir),
+      Cmd("git checkout -f c_hash2 -- DEPS", "",
+          cb=ResetDEPS("0123456789012345678901234567890123456789"),
+          cwd=chrome_dir),
+      Cmd("git log -1 --format=%B c_hash2", c_hash2_commit_log,
+          cwd=chrome_dir),
       Cmd("git rev-list -n 1 0123456789012345678901234567890123456789",
-          "0123456789012345678901234567890123456789"),
+          "0123456789012345678901234567890123456789", cwd=chrome_v8_dir),
Cmd("git log -1 --format=%B 0123456789012345678901234567890123456789",
-          self.C_V8_22624_LOG),
-      Cmd("git diff --name-only c_hash3 c_hash3^", TEST_CONFIG[DEPS_FILE]),
-      Cmd("git checkout -f c_hash3 -- %s" % TEST_CONFIG[DEPS_FILE], "",
-          cb=ResetDEPS(345)),
-      Cmd("git log -1 --format=%B c_hash3", c_hash3_commit_log),
-      Cmd("git checkout -f HEAD -- %s" % TEST_CONFIG[DEPS_FILE], "",
-          cb=ResetDEPS(567)),
-      Cmd("git branch -r", " weird/123\n  branch-heads/7\n"),
- Cmd("git checkout -f branch-heads/7 -- %s" % TEST_CONFIG[DEPS_FILE], "",
-          cb=ResetDEPS(345)),
-      Cmd("git checkout -f HEAD -- %s" % TEST_CONFIG[DEPS_FILE], "",
-          cb=ResetDEPS(567)),
-      Cmd("git checkout -f master", ""),
-      Cmd("git branch -D %s" % TEST_CONFIG[BRANCHNAME], ""),
+          self.C_V8_22624_LOG, cwd=chrome_v8_dir),
+      Cmd("git diff --name-only c_hash3 c_hash3^", "DEPS", cwd=chrome_dir),
+      Cmd("git checkout -f c_hash3 -- DEPS", "", cb=ResetDEPS(345),
+          cwd=chrome_dir),
+      Cmd("git log -1 --format=%B c_hash3", c_hash3_commit_log,
+          cwd=chrome_dir),
+      Cmd("git checkout -f HEAD -- DEPS", "", cb=ResetDEPS(567),
+          cwd=chrome_dir),
+ Cmd("git branch -r", " weird/123\n branch-heads/7\n", cwd=chrome_dir),
+      Cmd("git checkout -f branch-heads/7 -- DEPS", "", cb=ResetDEPS(345),
+          cwd=chrome_dir),
+      Cmd("git checkout -f HEAD -- DEPS", "", cb=ResetDEPS(567),
+          cwd=chrome_dir),
+      Cmd("git checkout -f master", "", cwd=chrome_dir),
+ Cmd("git branch -D %s" % TEST_CONFIG[BRANCHNAME], "", cwd=chrome_dir),
       Cmd("git checkout -f some_branch", ""),
       Cmd("git branch -D %s" % TEST_CONFIG[BRANCHNAME], ""),
     ])
@@ -1439,7 +1475,7 @@
           TEST_CONFIG[VERSION_FILE]),
     ])

-    self.assertEquals(1,
+    self.assertEquals(0,
         self.RunStep(BumpUpVersion, LastChangeBailout, ["--dry_run"]))

   # Test that we bail out if the lkgr was a version change.
@@ -1452,7 +1488,7 @@
           TEST_CONFIG[VERSION_FILE]),
     ])

-    self.assertEquals(1,
+    self.assertEquals(0,
self.RunStep(BumpUpVersion, LKGRVersionUpToDateBailout, ["--dry_run"]))

# Test that we bail out if the last version is already newer than the lkgr's
@@ -1467,7 +1503,7 @@
       Cmd("git diff --name-only lkgr_hash lkgr_hash^", ""),
     ])

-    self.assertEquals(1,
+    self.assertEquals(0,
self.RunStep(BumpUpVersion, LKGRVersionUpToDateBailout, ["--dry_run"]))


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to