def testWriteExclusive(self): file_util.Write(self.file_path, 'original contents') self.assertRaises(OSError, file_util.Write, self.file_path, self.sample_contents, overwrite_existing=False)
def FinalizeChange(self, commit_message, report): """Describe the state we're in.""" self._commit_message = commit_message msg_filename = os.path.join(self.client.checkout, 'hg-commit.tmp') if os.path.exists(msg_filename): raise RuntimeError( '%s exists, but I want to put commit message there' % os.path.abspath(msg_filename)) status = self.RunHg(['status'], need_stdout=True) if status: self._modified = True else: self._modified = False return self._diff = self.RunHg(['diff'], need_stdout=True) file_util.Write(msg_filename, commit_message) patches_message = ('Patches applied against %s in %s' % (self.client.repository_url, self.client.checkout)) if self.commit_strategy != base.LEAVE_PENDING: report.AddStep(name=patches_message, cmd='') return report.AddTodo('Refine commit message in %s/hg-commit.tmp ' '(for code words, brevity, etc.)' % self.client.checkout) commit_todo_lines = [patches_message] commit_todo_lines.append( ' To submit, run: (cd %s && hg commit -l hg-commit.tmp ' '&& rm hg-commit.tmp )' % (self.client.checkout)) report.AddTodo('\n'.join(commit_todo_lines))
def Run(self, argv): project = db_client.MakeProjectContext() try: revision = FLAGS.source_revision repository = FLAGS.repository if repository not in base.REPOSITORIES: raise app.UsageError('repository should be one of %s' % str(base.REPOSITORIES)) print 'Generating from and pushing into %s for MOE project %s' % ( repository, project.config.name) if repository == base.INTERNAL_STR: codebase_creator = project.internal_codebase_creator elif destination == base.PUBLIC_STR: codebase_creator = project.public_codebase_creator else: raise base.Error('Unexpected repository: %s' % destination) original_codebase = codebase_creator.Create(revision) codebase = codebase_utils.CreateModifiableCopy(original_codebase) for relative_filename in codebase.Walk(): filename = codebase.FilePath(relative_filename) if os.path.splitext(filename)[1] in EDITABLE_FILETYPES: file_util.Write(filename, file_util.Read(filename) + '\nMOE was here.\n') migration_strategy = config.MigrationStrategy( merge_strategy=base.ERROR, commit_strategy=base.LEAVE_PENDING, separate_revisions=False, copy_metadata=False) # TODO(dbentley): this creates the codebase just to make an editor. # We should make it possible to skip this step. destination_editor = codebase_creator.Create( revision).MakeEditor(migration_strategy) # TODO(dbentley): # We should be able to remove this additional_files_re, as it is # accounted for in the call to Codebase.Walk(). Unfortunately, we can't. # Why? Because a generated codebase is currently, incorrectly, getting # generated with the additional_files_re from the internal codebase, # even when it should be in the public project space. additional_files_re = ( project.config.public_repository_config.additional_files_re) pusher = push_codebase.CodebasePusher( codebase, destination_editor, files_to_ignore_re=additional_files_re) pusher.Push() moe_app.RUN.report.PrintSummary() finally: project.db.Disconnect()
def RunScenario(self, scenario_name, filter_to_test): UNRUN_SCENARIOS.remove(scenario_name) scenario_base = os.path.join(SCENARIOS_DIR, scenario_name) in_file = os.path.join(scenario_base, 'input') input_txt = file_util.Read(in_file) output = filter_to_test(input_txt) expected = os.path.join(scenario_base, 'expected') out_file = os.path.join(FLAGS.test_tmpdir, scenario_name + '.out.txt') file_util.Write(out_file, output) basetest.DiffTestFiles(expected, out_file)
def RunScenario(self, scenario_name, extractor=None): if not extractor: extractor = comment_scrubber.CLikeCommentExtractor() scrubber = comment_scrubber.CommentScrubber(extractor) scenario_dir = os.path.join(TEST_DATA_DIR, 'directives', scenario_name) unstripped = os.path.join(scenario_dir, 'input.txt') file_obj = test_util.FakeFile(filename=unstripped) scrubber.ScrubFile(file_obj, None) expected = os.path.join(scenario_dir, 'expected.txt') out = os.path.join(FLAGS.test_tmpdir, scenario_name + '.out.txt') file_util.Write(out, file_obj.new_contents) basetest.DiffTestFiles(out, expected)
def testWriteGroup(self): self.mox.StubOutWithMock(os, 'open') self.mox.StubOutWithMock(os, 'write') self.mox.StubOutWithMock(os, 'close') self.mox.StubOutWithMock(os, 'chown') gid = 'new gid' os.open(self.file_path, os.O_WRONLY | os.O_TRUNC | os.O_CREAT, 0666).AndReturn(self.fd) os.write(self.fd, self.sample_contents) os.close(self.fd) os.chown(self.file_path, -1, gid) self.mox.ReplayAll() file_util.Write(self.file_path, self.sample_contents, gid=gid) self.mox.VerifyAll()
def WriteToFile(self, filename, original=False): """Write (possibly original) contents to filename, properly encoded. Args: filename: str, the filename to write to original: bool, whether to write the original file """ self.Contents() # make sure it's loaded if original: encoded_contents = self._PossiblyEncode( *self._ReadContents(self.filename)) else: encoded_contents = self._PossiblyEncode(self._contents, self._in_unicode) file_util.Write(filename, encoded_contents, mode=self.Mode())
def FinalizeChange(self, commit_message, report): """Describe the state we're in.""" # TODO(dbentley): refactor into _IsWorkingDirectoryDirty for svn. msg_filename = os.path.join(self.client.checkout, 'svn-commit.tmp') if os.path.exists(msg_filename): raise RuntimeError( '%s exists, but I want to put commit message there' % os.path.abspath(msg_filename)) status = self.RunSvn(['status', '--xml'], need_stdout=True) status_tree = ElementTree.XML(status.encode('UTF-8')) if not status_tree.find('target').find('entry'): self._modified = False return else: self._modified = True try: self._diff = self.RunSvn(['diff'], need_stdout=True) except UnicodeDecodeError: self._diff = '<Error reading diff>' file_util.Write(msg_filename, commit_message) patches_message = ('Patches applied against %s in %s' % (self.client.repository_url, self.client.checkout)) if self.commit_strategy != base.LEAVE_PENDING: report.AddStep(name=patches_message, cmd='') return report.AddTodo('Refine commit message in %s/svn-commit.tmp ' '(for code words, brevity, etc.)' % self.client.checkout) commit_todo_lines = [patches_message] if self.client.repository_url.startswith('http:'): commit_todo_lines.append( ' (Using http: You may https: to submit your change)') commit_todo_lines.append( ' (to do that, run svn switch --relocate %s %s )' % (self.client.repository_url, self.client.repository_url.replace('http:', 'https:'))) commit_todo_lines.append( ' To submit, run: (cd %s && svn ci -F svn-commit.tmp ' '&& rm svn-commit.tmp )' % (self.client.checkout)) report.AddTodo('\n'.join(commit_todo_lines))
def ContentsFilename(self): file_util.Write(self._contents_filename, self._contents) return self._contents_filename
def testWriteMode(self): mode = 0744 file_util.Write(self.file_path, self.sample_contents, mode=mode) s = os.stat(self.file_path) self.assertEqual(stat.S_IMODE(s.st_mode), mode)
def testWriteOverwrite(self): file_util.Write(self.file_path, 'original contents') file_util.Write(self.file_path, self.sample_contents) with open(self.file_path) as fp: self.assertEquals(fp.read(), self.sample_contents)