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
1=== modified file 'CMakeLists.txt'
2--- CMakeLists.txt 2014-04-15 08:13:37 +0000
3+++ CMakeLists.txt 2014-05-01 16:33:48 +0000
4@@ -76,7 +76,7 @@
5 include_directories(include/shared)
6
7 # Check for boost
8-find_package(Boost 1.48.0 COMPONENTS chrono date_time filesystem system thread program_options regex REQUIRED)
9+find_package(Boost 1.48.0 COMPONENTS chrono date_time filesystem system thread program_options regex iostreams REQUIRED)
10 include_directories (
11 ${Boost_INCLUDE_DIRS}
12 )
13
14=== modified file 'debian/control'
15--- debian/control 2014-04-15 05:32:16 +0000
16+++ debian/control 2014-05-01 16:33:48 +0000
17@@ -18,6 +18,7 @@
18 libboost-thread-dev,
19 libboost-regex-dev,
20 libboost-filesystem-dev,
21+ libboost-iostreams-dev,
22 protobuf-compiler,
23 libdrm-dev,
24 libegl1-mesa-dev,
25
26=== added file 'include/test/mir_test/popen.h'
27--- include/test/mir_test/popen.h 1970-01-01 00:00:00 +0000
28+++ include/test/mir_test/popen.h 2014-05-01 16:33:48 +0000
29@@ -0,0 +1,59 @@
30+/*
31+ * Copyright © 2014 Canonical Ltd.
32+ *
33+ * This program is free software: you can redistribute it and/or modify it
34+ * under the terms of the GNU General Public License version 3,
35+ * as published by the Free Software Foundation.
36+ *
37+ * This program is distributed in the hope that it will be useful,
38+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
39+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40+ * GNU General Public License for more details.
41+ *
42+ * You should have received a copy of the GNU General Public License
43+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
44+ *
45+ * Authored by: Alberto Aguirre <alberto.aguirre@canonical.com>
46+ */
47+
48+
49+#ifndef MIR_TEST_POPEN_H_
50+#define MIR_TEST_POPEN_H_
51+
52+#include <cstdio>
53+#include <memory>
54+#include <string>
55+#include <streambuf>
56+#include <istream>
57+
58+namespace mir
59+{
60+namespace test
61+{
62+/**
63+ * Popen - A popen c++ wrapper
64+ */
65+class Popen
66+{
67+public:
68+ Popen(std::string const& cmd);
69+
70+ /**
71+ * Read a line from the output of the executed command
72+ * returns false if there is nothing more to read
73+ */
74+ bool get_line(std::string& line);
75+
76+private:
77+ Popen() = delete;
78+ Popen(Popen const&) = delete;
79+ Popen& operator=(Popen const&) = delete;
80+ std::unique_ptr<std::FILE, void(*)(FILE* f)> raw_stream;
81+ std::unique_ptr<std::streambuf> stream_buffer;
82+ std::istream stream;
83+};
84+
85+}
86+}
87+
88+#endif
89
90=== modified file 'tests/mir_test/CMakeLists.txt'
91--- tests/mir_test/CMakeLists.txt 2014-04-15 05:31:19 +0000
92+++ tests/mir_test/CMakeLists.txt 2014-05-01 16:33:48 +0000
93@@ -5,4 +5,5 @@
94 display_config_matchers.cpp
95 cross_process_action.cpp
96 spin_wait.cpp
97-)
98+ popen.cpp
99+)
100\ No newline at end of file
101
102=== added file 'tests/mir_test/popen.cpp'
103--- tests/mir_test/popen.cpp 1970-01-01 00:00:00 +0000
104+++ tests/mir_test/popen.cpp 2014-05-01 16:33:48 +0000
105@@ -0,0 +1,65 @@
106+/*
107+ * Copyright © 2014 Canonical Ltd.
108+ *
109+ * This program is free software: you can redistribute it and/or modify it
110+ * under the terms of the GNU General Public License version 3,
111+ * as published by the Free Software Foundation.
112+ *
113+ * This program is distributed in the hope that it will be useful,
114+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
115+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
116+ * GNU General Public License for more details.
117+ *
118+ * You should have received a copy of the GNU General Public License
119+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
120+ *
121+ * Authored by: Alberto Aguirre <alberto.aguirre@canonical.com>
122+ */
123+
124+#include <mir_test/popen.h>
125+
126+#include <boost/throw_exception.hpp>
127+#include <boost/exception/errinfo_errno.hpp>
128+#include <boost/iostreams/device/file_descriptor.hpp>
129+#include <boost/iostreams/stream_buffer.hpp>
130+
131+#include <stdexcept>
132+
133+namespace mt = mir::test;
134+namespace io = boost::iostreams;
135+
136+namespace
137+{
138+std::unique_ptr<std::streambuf> create_stream_buffer(FILE* raw_stream)
139+{
140+ if (raw_stream == nullptr)
141+ {
142+ BOOST_THROW_EXCEPTION(
143+ boost::enable_error_info(std::runtime_error("popen failed"))
144+ << boost::errinfo_errno(errno));
145+ }
146+
147+ int fd = fileno(raw_stream);
148+ if (fd == -1)
149+ {
150+ BOOST_THROW_EXCEPTION(
151+ boost::enable_error_info(std::runtime_error("invalid file stream"))
152+ << boost::errinfo_errno(errno));
153+ }
154+ auto raw = new io::stream_buffer<io::file_descriptor_source>{
155+ fd, io::never_close_handle};
156+ return std::unique_ptr<std::streambuf>(raw);
157+}
158+}
159+
160+mt::Popen::Popen(std::string const& cmd)
161+ : raw_stream{popen(cmd.c_str(), "r"), [](FILE* f){ pclose(f); }},
162+ stream_buffer{create_stream_buffer(raw_stream.get())},
163+ stream{stream_buffer.get()}
164+{
165+}
166+
167+bool mt::Popen::get_line(std::string& line)
168+{
169+ return std::getline(stream, line);
170+}

Subscribers

People subscribed via source and target branches