def test_mkstemp1(self): """test simple mkstemp""" tmpfile = mkstemp(dir=self._tmpdir) self.assertTrue(os.path.isfile(tmpfile)) self.assertEqual(tmpfile.name, tmpfile) self.assertIsNotNone(os.stat(tmpfile)) tmpfile.close() self.assertFalse(os.path.isfile(tmpfile))
def test_mkstemp3(self): """write to tmpfile""" with mkstemp(dir=self._tmpdir) as tmpfile: tmpfile.write('foobar') tmpfile.flush() with open(tmpfile, 'r') as f: self.assertEqual(f.read(), 'foobar') self.assertFalse(os.path.isfile(tmpfile))
def test_builder18(self): """test run method (write stdout to tmpfile)""" build_cmd = self.fixture_file('dummy.sh') builder = Builder(build_cmd=build_cmd, su_cmd=None, out='blah') with mkstemp(dir=self._tmp_dir) as f: ret = builder.run(stdout=f) self.assertEqual(ret, 0) f.seek(0, os.SEEK_SET) self.assertEqual(f.read(), 'blah\n')
def test_builder20(self): """test run method (specify spec file)""" build_cmd = self.fixture_file('dummy.sh') builder = Builder(build_cmd=build_cmd, su_cmd=None) with mkstemp(dir=self._tmp_dir) as f: ret = builder.run('foo.spec', stdout=f) self.assertEqual(ret, 0) f.seek(0, os.SEEK_SET) # output is the build descr self.assertEqual(f.read(), 'foo.spec\n')
def test_builder19(self): """test run method (no shell expansion)""" build_cmd = self.fixture_file('dummy.sh') builder = Builder(build_cmd=build_cmd, su_cmd=None, out='$PATH') with mkstemp(dir=self._tmp_dir) as f: ret = builder.run(stdout=f) self.assertEqual(ret, 0) f.seek(0, os.SEEK_SET) # path is not expanded because subprocess.call is invoked # with shell=False self.assertEqual(f.read(), '$PATH\n')
def _init_fobj(self, read_required): read_required = read_required or self.append if read_required: self._init_read() if self._remote_size >= self.tmp_size or self.use_tmp: new_fobj = mkstemp() else: new_fobj = StringIO() if read_required: # we read/write _everything_ (otherwise this class needs # a bit more logic - can be added if needed) self.write_to(new_fobj) # close it because it isn't needed anymore self._fobj.close() new_fobj.seek(0, os.SEEK_SET) self._fobj = new_fobj
def run_pager(source, pager="less", suffix=""): """Runs pager on source source. If the env variable $PAGER is not set the pager keyword argument is used. Either source is a str or file or file-like object. Keyword arguments: pager -- the default pager (default: less) suffix -- the suffix of the tempfile (default: '') """ cmd = [os.getenv("PAGER", default=pager)] with mkstemp(prefix="osc_", suffix=suffix) as f: if hasattr(source, "read"): copy_file(source, f) else: f.write(source) f.flush() cmd.append(f) return subprocess.call(cmd, shell=False)
def run_pager(source, pager='less', suffix=''): """Runs pager on source source. If the env variable $PAGER is not set the pager keyword argument is used. Either source is a str or file or file-like object. Keyword arguments: pager -- the default pager (default: less) suffix -- the suffix of the tempfile (default: '') """ cmd = [os.getenv('PAGER', default=pager)] with mkstemp(prefix='osc_', suffix=suffix) as f: if hasattr(source, 'read'): copy_file(source, f) else: f.write(source) f.flush() cmd.append(f) return subprocess.call(cmd, shell=False)
def _write_storefile(path, filename, data): """Write a wc file. path is the path to the working copy, filename is the name of the storefile and data is the data which should be written. If data is not empty a trailing \\n character will be added. Raises a ValueError if path has no storedir. """ if not _has_storedir(path): raise ValueError("path \"%s\" has no storedir" % path) fname = _storefile(path, filename) tmpfile = None try: tmpfile = mkstemp(dir=_storedir(path), delete=False) tmpfile.write(data) if data: tmpfile.write('\n') finally: if tmpfile is not None: tmpfile.close() os.rename(tmpfile, fname)
def edit_message(template=None, footer=None, suffix=""): """Opens $EDITOR and displays the template and the footer. Returns the data entered by the user (everything after the delimeter is ignored). Keyword arguments: template -- the template str (default: None) footer -- the footer str (default: None) suffix -- the suffix of the tempfile (default: '') """ delimeter = "--This line, and those below, will be ignored--\n" with mkstemp(prefix="osc_", suffix=suffix) as f: if template is not None: f.write(template) f.write("\n" + delimeter) if footer is not None: f.write("\n" + footer) f.flush() message = "" while not message: run_editor(f) f.seek(0, os.SEEK_SET) message = f.read().split(delimeter, 1)[0].rstrip() if not message or message == template: choices = "(a)bort command, (i)gnore, (e)dit> " msg = "no message specified: " + choices if template == message: msg = "template was not changed: " + choices repl = input(msg) if repl.lower() == "i": return message elif repl.lower() != "e": raise UserAbort() return message
def edit_message(template=None, footer=None, suffix=''): """Opens $EDITOR and displays the template and the footer. Returns the data entered by the user (everything after the delimeter is ignored). Keyword arguments: template -- the template str (default: None) footer -- the footer str (default: None) suffix -- the suffix of the tempfile (default: '') """ delimeter = "--This line, and those below, will be ignored--\n" with mkstemp(prefix='osc_', suffix=suffix) as f: if template is not None: f.write(template) f.write("\n" + delimeter) if footer is not None: f.write("\n" + footer) f.flush() message = '' while not message: run_editor(f) f.seek(0, os.SEEK_SET) message = f.read().split(delimeter, 1)[0].rstrip() if not message or message == template: choices = "(a)bort command, (i)gnore, (e)dit> " msg = "no message specified: " + choices if template == message: msg = "template was not changed: " + choices repl = raw_input(msg) if repl.lower() == 'i': return message elif repl.lower() != 'e': raise UserAbort() return message
def test_mkstemp2(self): """test context manager""" with mkstemp(dir=self._tmpdir) as tmpfile: self.assertTrue(os.path.isfile(tmpfile)) self.assertFalse(os.path.isfile(tmpfile))