Mir

Merge lp:~mir-team/mir/popen-cpp-wrapper into lp:mir

Proposed by Alberto Aguirre
Status: Merged
Approved by: Alberto Aguirre
Approved revision: no longer in the source branch.
Merged at revision: 1603
Proposed branch: lp:~mir-team/mir/popen-cpp-wrapper
Merge into: lp:mir
Diff against target: 170 lines (+128/-2)
5 files modified
CMakeLists.txt (+1/-1)
debian/control (+1/-0)
include/test/mir_test/popen.h (+59/-0)
tests/mir_test/CMakeLists.txt (+2/-1)
tests/mir_test/popen.cpp (+65/-0)
To merge this branch: bzr merge lp:~mir-team/mir/popen-cpp-wrapper
Reviewer Review Type Date Requested Status
Cemil Azizoglu (community) Approve
Robert Carr (community) Approve
Josh Arenson (community) Approve
PS Jenkins bot (community) continuous-integration Approve
Review via email: mp+217831@code.launchpad.net

Commit message

Add a popen c++ wrapper

Description of the change

Introduce a popen c++ wrapper

May be useful for http://code.launchpad.net/~josharenson/mir/install_glmark2/+merge/216504

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
Alberto Aguirre (albaguirre) wrote :

^--"W: Failed to fetch file:/tmp/archive/./Packages File not found

E: Some index files failed to download. They have been ignored, or old ones used instead.
16 KB/s (1436 bytes in 0.082s)
1307 KB/s (174630 bytes in 0.130s)
rm: cannot remove '.setup_complete': No such file or directory
remote object '/.setup_complete' does not exist
SETUP FAILED - CAN'T CONTINUE"

Looks like we are getting the same CI failure on multiple MPs.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Josh Arenson (josharenson) wrote :

Looks good and works in practice.

review: Approve
Revision history for this message
Robert Carr (robertcarr) wrote :

LGTM

review: Approve
Revision history for this message
Cemil Azizoglu (cemil-azizoglu) wrote :

Good to go.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'CMakeLists.txt'
--- CMakeLists.txt 2014-04-15 08:13:37 +0000
+++ CMakeLists.txt 2014-05-01 16:33:48 +0000
@@ -76,7 +76,7 @@
76include_directories(include/shared)76include_directories(include/shared)
7777
78# Check for boost78# Check for boost
79find_package(Boost 1.48.0 COMPONENTS chrono date_time filesystem system thread program_options regex REQUIRED)79find_package(Boost 1.48.0 COMPONENTS chrono date_time filesystem system thread program_options regex iostreams REQUIRED)
80include_directories (80include_directories (
81 ${Boost_INCLUDE_DIRS}81 ${Boost_INCLUDE_DIRS}
82)82)
8383
=== modified file 'debian/control'
--- debian/control 2014-04-15 05:32:16 +0000
+++ debian/control 2014-05-01 16:33:48 +0000
@@ -18,6 +18,7 @@
18 libboost-thread-dev,18 libboost-thread-dev,
19 libboost-regex-dev,19 libboost-regex-dev,
20 libboost-filesystem-dev,20 libboost-filesystem-dev,
21 libboost-iostreams-dev,
21 protobuf-compiler,22 protobuf-compiler,
22 libdrm-dev,23 libdrm-dev,
23 libegl1-mesa-dev,24 libegl1-mesa-dev,
2425
=== added file 'include/test/mir_test/popen.h'
--- include/test/mir_test/popen.h 1970-01-01 00:00:00 +0000
+++ include/test/mir_test/popen.h 2014-05-01 16:33:48 +0000
@@ -0,0 +1,59 @@
1/*
2 * Copyright © 2014 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authored by: Alberto Aguirre <alberto.aguirre@canonical.com>
17 */
18
19
20#ifndef MIR_TEST_POPEN_H_
21#define MIR_TEST_POPEN_H_
22
23#include <cstdio>
24#include <memory>
25#include <string>
26#include <streambuf>
27#include <istream>
28
29namespace mir
30{
31namespace test
32{
33/**
34 * Popen - A popen c++ wrapper
35 */
36class Popen
37{
38public:
39 Popen(std::string const& cmd);
40
41 /**
42 * Read a line from the output of the executed command
43 * returns false if there is nothing more to read
44 */
45 bool get_line(std::string& line);
46
47private:
48 Popen() = delete;
49 Popen(Popen const&) = delete;
50 Popen& operator=(Popen const&) = delete;
51 std::unique_ptr<std::FILE, void(*)(FILE* f)> raw_stream;
52 std::unique_ptr<std::streambuf> stream_buffer;
53 std::istream stream;
54};
55
56}
57}
58
59#endif
060
=== modified file 'tests/mir_test/CMakeLists.txt'
--- tests/mir_test/CMakeLists.txt 2014-04-15 05:31:19 +0000
+++ tests/mir_test/CMakeLists.txt 2014-05-01 16:33:48 +0000
@@ -5,4 +5,5 @@
5 display_config_matchers.cpp5 display_config_matchers.cpp
6 cross_process_action.cpp6 cross_process_action.cpp
7 spin_wait.cpp7 spin_wait.cpp
8)8 popen.cpp
9)
9\ No newline at end of file10\ No newline at end of file
1011
=== added file 'tests/mir_test/popen.cpp'
--- tests/mir_test/popen.cpp 1970-01-01 00:00:00 +0000
+++ tests/mir_test/popen.cpp 2014-05-01 16:33:48 +0000
@@ -0,0 +1,65 @@
1/*
2 * Copyright © 2014 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authored by: Alberto Aguirre <alberto.aguirre@canonical.com>
17 */
18
19#include <mir_test/popen.h>
20
21#include <boost/throw_exception.hpp>
22#include <boost/exception/errinfo_errno.hpp>
23#include <boost/iostreams/device/file_descriptor.hpp>
24#include <boost/iostreams/stream_buffer.hpp>
25
26#include <stdexcept>
27
28namespace mt = mir::test;
29namespace io = boost::iostreams;
30
31namespace
32{
33std::unique_ptr<std::streambuf> create_stream_buffer(FILE* raw_stream)
34{
35 if (raw_stream == nullptr)
36 {
37 BOOST_THROW_EXCEPTION(
38 boost::enable_error_info(std::runtime_error("popen failed"))
39 << boost::errinfo_errno(errno));
40 }
41
42 int fd = fileno(raw_stream);
43 if (fd == -1)
44 {
45 BOOST_THROW_EXCEPTION(
46 boost::enable_error_info(std::runtime_error("invalid file stream"))
47 << boost::errinfo_errno(errno));
48 }
49 auto raw = new io::stream_buffer<io::file_descriptor_source>{
50 fd, io::never_close_handle};
51 return std::unique_ptr<std::streambuf>(raw);
52}
53}
54
55mt::Popen::Popen(std::string const& cmd)
56 : raw_stream{popen(cmd.c_str(), "r"), [](FILE* f){ pclose(f); }},
57 stream_buffer{create_stream_buffer(raw_stream.get())},
58 stream{stream_buffer.get()}
59{
60}
61
62bool mt::Popen::get_line(std::string& line)
63{
64 return std::getline(stream, line);
65}

Subscribers

People subscribed via source and target branches