Exemplo n.º 1
0
 def testFileRead_Bytes(self):
     with gclient_utils.temporary_file() as tmp:
         gclient_utils.FileWrite(tmp,
                                 b'foo \xe2\x9c bar',
                                 mode='wb',
                                 encoding=None)
         self.assertEqual('foo \ufffd bar', gclient_utils.FileRead(tmp))
Exemplo n.º 2
0
def _SendChangeSVN(options):
    """Send a change to the try server by committing a diff file on a subversion
  server."""
    if not options.svn_repo:
        raise NoTryServerAccess(
            'Please use the --svn_repo option to specify the'
            ' try server svn repository to connect to.')

    values = _ParseSendChangeOptions(options)
    description = ''.join("%s=%s\n" % (k, v) for (k, v) in values.iteritems())
    logging.info('Sending by SVN')
    logging.info(description)
    logging.info(options.svn_repo)
    logging.info(options.diff)
    if options.dry_run:
        return

    # Create a temporary directory, put a uniquely named file in it with the diff
    # content and svn import that.
    temp_dir = tempfile.mkdtemp()
    temp_file = tempfile.NamedTemporaryFile()
    try:
        try:
            # Description
            temp_file.write(description)
            temp_file.flush()

            # Diff file
            current_time = str(datetime.datetime.now()).replace(':', '.')
            file_name = (Escape(options.user) + '.' + Escape(options.name) +
                         '.%s.diff' % current_time)
            full_path = os.path.join(temp_dir, file_name)
            gclient_utils.FileWrite(full_path, options.diff, 'wb')

            # Committing it will trigger a try job.
            if sys.platform == "cygwin":
                # Small chromium-specific issue here:
                # git-try uses /usr/bin/python on cygwin but svn.bat will be used
                # instead of /usr/bin/svn by default. That causes bad things(tm) since
                # Windows' svn.exe has no clue about cygwin paths. Hence force to use
                # the cygwin version in this particular context.
                exe = "/usr/bin/svn"
            else:
                exe = "svn"
            command = [
                exe, 'import', '-q', temp_dir, options.svn_repo, '--file',
                temp_file.name
            ]
            if scm.SVN.AssertVersion("1.5")[0]:
                command.append('--no-ignore')

            subprocess2.check_call(command)
        except subprocess2.CalledProcessError, e:
            raise NoTryServerAccess(str(e))
    finally:
        temp_file.close()
        shutil.rmtree(temp_dir, True)
Exemplo n.º 3
0
def UploadCl(refactor_branch, refactor_branch_upstream, directory, files,
             description, comment, reviewers, changelist, cmd_upload,
             cq_dry_run, enable_auto_submit):
    """Uploads a CL with all changes to |files| in |refactor_branch|.

  Args:
    refactor_branch: Name of the branch that contains the changes to upload.
    refactor_branch_upstream: Name of the upstream of |refactor_branch|.
    directory: Path to the directory that contains the OWNERS file for which
        to upload a CL.
    files: List of AffectedFile instances to include in the uploaded CL.
    description: Description of the uploaded CL.
    comment: Comment to post on the uploaded CL.
    reviewers: A set of reviewers for the CL.
    changelist: The Changelist class.
    cmd_upload: The function associated with the git cl upload command.
    cq_dry_run: If CL uploads should also do a cq dry run.
    enable_auto_submit: If CL uploads should also enable auto submit.
  """
    # Create a branch.
    if not CreateBranchForDirectory(refactor_branch, directory,
                                    refactor_branch_upstream):
        print('Skipping ' + directory + ' for which a branch already exists.')
        return

    # Checkout all changes to files in |files|.
    deleted_files = [f.AbsoluteLocalPath() for f in files if f.Action() == 'D']
    if deleted_files:
        git.run(*['rm'] + deleted_files)
    modified_files = [
        f.AbsoluteLocalPath() for f in files if f.Action() != 'D'
    ]
    if modified_files:
        git.run(*['checkout', refactor_branch, '--'] + modified_files)

    # Commit changes. The temporary file is created with delete=False so that it
    # can be deleted manually after git has read it rather than automatically
    # when it is closed.
    with gclient_utils.temporary_file() as tmp_file:
        gclient_utils.FileWrite(
            tmp_file, FormatDescriptionOrComment(description, directory))
        git.run('commit', '-F', tmp_file)

    # Upload a CL.
    upload_args = ['-f', '-r', ','.join(reviewers)]
    if cq_dry_run:
        upload_args.append('--cq-dry-run')
    if not comment:
        upload_args.append('--send-mail')
    if enable_auto_submit:
        upload_args.append('--enable-auto-submit')
    print('Uploading CL for ' + directory + '.')
    cmd_upload(upload_args)
    if comment:
        changelist().AddComment(FormatDescriptionOrComment(comment, directory),
                                publish=True)
Exemplo n.º 4
0
 def Save(self):
   """Writes the changelist information to disk."""
   if self.NeedsUpload():
     needs_upload = "dirty"
   else:
     needs_upload = "clean"
   data = ChangeInfo._SEPARATOR.join([
       "%d, %d, %s" % (self.issue, self.patchset, needs_upload),
       "\n".join([f[0] + f[1] for f in self.GetFiles()]),
       self.description])
   gclient_utils.FileWrite(GetChangelistInfoFile(self.name), data)
Exemplo n.º 5
0
 def Save(self):
   """Writes the changelist information to disk."""
   data = json.dumps({
         'issue': self.issue,
         'patchset': self.patchset,
         'needs_upload': self.NeedsUpload(),
         'files': self.GetFiles(),
         'description': self.description,
         'rietveld': self.rietveld,
       }, sort_keys=True, indent=2)
   gclient_utils.FileWrite(GetChangelistInfoFile(self.name), data)
Exemplo n.º 6
0
    def _SaveEntries(self, entries):
        """Creates a .gclient_entries file to record the list of unique checkouts.

    The .gclient_entries file lives in the same directory as .gclient.

    Args:
      entries: A sequence of solution names.
    """
        text = "entries = \\\n" + pprint.pformat(entries, 2) + '\n'
        file_path = os.path.join(self._root_dir,
                                 self._options.entries_filename)
        gclient_utils.FileWrite(file_path, text)
Exemplo n.º 7
0
 def testHardToDelete(self):
     # Use the fact that tearDown will delete the directory to make it hard to do
     # so.
     l1 = os.path.join(self.root_dir, 'l1')
     l2 = os.path.join(l1, 'l2')
     l3 = os.path.join(l2, 'l3')
     f3 = os.path.join(l3, 'f3')
     os.mkdir(l1)
     os.mkdir(l2)
     os.mkdir(l3)
     gclient_utils.FileWrite(f3, 'foo')
     os.chmod(f3, 0)
     os.chmod(l3, 0)
     os.chmod(l2, 0)
     os.chmod(l1, 0)
Exemplo n.º 8
0
    def testIgnoreFile(self):
        """Tests passing the ignore list in a file."""
        expected_output = [
            self.blame_line('C', ' 1) line 1.1'),
            self.blame_line('A', '2*) line 2.1')
        ]
        outbuf = BytesIO()

        with gclient_utils.temporary_file() as ignore_file:
            gclient_utils.FileWrite(
                ignore_file, '# Line comments are allowed.\n'
                '\n'
                '{}\n'
                'xxxx\n'.format(self.repo['B']))
            retval = self.repo.run(
                self.git_hyper_blame.main,
                ['--ignore-file', ignore_file, 'tag_C', 'some/files/file'],
                outbuf)

        self.assertEqual(0, retval)
        self.assertEqual(expected_output,
                         outbuf.getvalue().rstrip().split(b'\n'))
        self.assertEqual('warning: unknown revision \'xxxx\'.\n',
                         sys.stderr.getvalue())
Exemplo n.º 9
0
 def _write_config(self):
     try:
         gclient_utils.FileWrite(CONFIG_FILE, json.dumps(self._config))
     except IOError as e:
         print(PERMISSION_DENIED_WARNING % e, file=sys.stderr)
         self._config['opt-in'] = False
Exemplo n.º 10
0
 def testTemporaryFile(self):
     with gclient_utils.temporary_file() as tmp:
         gclient_utils.FileWrite(tmp, 'test')
         self.assertEqual('test', gclient_utils.FileRead(tmp))
     self.assertFalse(os.path.exists(tmp))
Exemplo n.º 11
0
 def testFileRead_Unicode(self):
     with gclient_utils.temporary_file() as tmp:
         gclient_utils.FileWrite(tmp, 'foo ✔ bar')
         self.assertEqual('foo ✔ bar', gclient_utils.FileRead(tmp))
Exemplo n.º 12
0
def GetCachedFile(filename, max_age=60*60*24*3, use_root=False):
  """Retrieves a file from the repository and caches it in GetCacheDir() for
  max_age seconds.

  use_root: If False, look up the arborescence for the first match, otherwise go
            directory to the root repository.

  Note: The cache will be inconsistent if the same file is retrieved with both
        use_root=True and use_root=False. Don't be stupid.
  """
  if filename not in FILES_CACHE:
    # Don't try to look up twice.
    FILES_CACHE[filename] = None
    # First we check if we have a cached version.
    try:
      cached_file = os.path.join(GetCacheDir(), filename)
    except gclient_utils.Error:
      return None
    if (not os.path.exists(cached_file) or
        (time.time() - os.stat(cached_file).st_mtime) > max_age):
      dir_info = SVN.CaptureInfo('.')
      repo_root = dir_info['Repository Root']
      if use_root:
        url_path = repo_root
      else:
        url_path = dir_info['URL']
      while True:
        # Look in the repository at the current level for the file.
        for _ in range(5):
          content = None
          try:
            # Take advantage of the fact that svn won't output to stderr in case
            # of success but will do in case of failure so don't mind putting
            # stderr into content_array.
            content_array = []
            svn_path = url_path + '/' + filename
            args = ['svn', 'cat', svn_path]
            if sys.platform != 'darwin':
              # MacOSX 10.5.2 has a bug with svn 1.4.4 that will trigger the
              # 'Can\'t get username or password' and can be fixed easily.
              # The fix doesn't work if the user upgraded to svn 1.6.x. Bleh.
              # I don't have time to fix their broken stuff.
              args.append('--non-interactive')
            gclient_utils.CheckCallAndFilter(
                args, cwd='.', filter_fn=content_array.append)
            # Exit the loop if the file was found. Override content.
            content = '\n'.join(content_array)
            break
          except gclient_utils.Error:
            if content_array[0].startswith(
                'svn: Can\'t get username or password'):
              ErrorExit('Your svn credentials expired. Please run svn update '
                        'to fix the cached credentials')
            if content_array[0].startswith('svn: Can\'t get password'):
              ErrorExit('If are using a Mac and svn --version shows 1.4.x, '
                  'please hack gcl.py to remove --non-interactive usage, it\'s'
                  'a bug on your installed copy')
            if not content_array[0].startswith('svn: File not found:'):
              # Try again.
              continue
        if content:
          break
        if url_path == repo_root:
          # Reached the root. Abandoning search.
          break
        # Go up one level to try again.
        url_path = os.path.dirname(url_path)
      if content is not None or filename != CODEREVIEW_SETTINGS_FILE:
        # Write a cached version even if there isn't a file, so we don't try to
        # fetch it each time. codereview.settings must always be present so do
        # not cache negative.
        gclient_utils.FileWrite(cached_file, content or '')
    else:
      content = gclient_utils.FileRead(cached_file, 'r')
    # Keep the content cached in memory.
    FILES_CACHE[filename] = content
  return FILES_CACHE[filename]
Exemplo n.º 13
0
 def _write_config(self):
   gclient_utils.FileWrite(CONFIG_FILE, json.dumps(self._config))
Exemplo n.º 14
0
 def SaveConfig(self):
     gclient_utils.FileWrite(
         os.path.join(self._root_dir, self._options.config_filename),
         self._config_content)