Merge lp:~mterry/unity-scopes-api/snap-root into lp:unity-scopes-api

Proposed by Michael Terry
Status: Superseded
Proposed branch: lp:~mterry/unity-scopes-api/snap-root
Merge into: lp:unity-scopes-api
Diff against target: 198 lines (+42/-15)
9 files modified
data/scope-registry.conf.in (+1/-1)
data/smart-scopes-proxy.conf.in (+1/-1)
doc/tutorial.dox (+2/-1)
include/unity/scopes/internal/ConfigBase.h (+2/-0)
src/scopes/internal/ConfigBase.cpp (+18/-1)
src/scopes/internal/RegistryConfig.cpp (+2/-2)
src/scopes/internal/RuntimeConfig.cpp (+4/-4)
src/scopes/internal/ScopeConfig.cpp (+11/-4)
test/gtest/scopes/internal/ScopeConfig/ScopeConfig_test.cpp (+1/-1)
To merge this branch: bzr merge lp:~mterry/unity-scopes-api/snap-root
Reviewer Review Type Date Requested Status
Michi Henning (community) Needs Fixing
Review via email: mp+307848@code.launchpad.net

This proposal has been superseded by a proposal from 2016-10-17.

Commit message

Handle running inside a snap by paying attention to the $SNAP prefix.

Description of the change

Handle running inside a snap by paying attention to the $SNAP prefix.

To post a comment you must log in.
373. By Michael Terry

Add $SNAP to Art and Icon keys too, if needed

Revision history for this message
Marcus Tomlinson (marcustomlinson) wrote :

Thanks Michael! Michi and I do acknowledged this MP, but we'd like to investigate the possibility of a cleaner approach whereby we reduce some of the redundancy (especially with getenv()) that's introduced here.

This is not a stab at your work by any means ;) Thanks for your help with this!

Revision history for this message
Michi Henning (michihenning) wrote :

Could you re-target this MR at the devel branch instead of trunk please? That allows us to use our normal staging process.

Is $SNAP always going to have a trailing slash? If not, constructs such as

$SNAP@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@/@UNITY_SCOPES_LIB@/scoperegistry

and

snap_root + base_dflt_file

won't create the path name correctly. Wouldn't it be safer to append a slash regardless? If we end up with two adjacent slashes, they won't do any harm.

This does not work:

const string snap_root = getenv("SNAP");

If SNAP isn't set, getenv() returns a nullptr, which causes the string constructor to throw. That causes a bunch of tests to fail.

The repeated calls to getenv("SNAP") are a bit of a pain (and dangerous because of the needed check for a nullptr return). It would be better to do this is ConfigBase and and provide a protected accessor for the value. That puts the code to sanitize the value in a single place.

review: Needs Fixing
Revision history for this message
Michi Henning (michihenning) wrote :

Here is a diff against this branch that makes some changes. Could you review please?

Thanks,

Michi.

Revision history for this message
Michael Terry (mterry) wrote :

> Is $SNAP always going to have a trailing slash? If not, constructs such as
> [snip[
> won't create the path name correctly. Wouldn't it be safer to append a slash
> regardless? If we end up with two adjacent slashes, they won't do any harm.

@CMAKE_INSTALL_PREFIX@ is guaranteed to either be empty or start with a slash, right? So that's fine. And base_dflt_file always starts with a slash. So I think we're fine there.

> If SNAP isn't set, getenv() returns a nullptr, which causes the string
> constructor to throw. That causes a bunch of tests to fail.

Ick, right you are. I wrote so many of these in a row, I got confused between QString and std::string.

> It would be better to do this is ConfigBase and and provide a protected
> accessor for the value. That puts the code to sanitize the value in a single
> place.

OK.

> Here is a diff against this branch that makes some changes. Could you review please?

I would go make your suggested changes, but it sounds like you were trying to upload a diff to make them for me? I don't see one. I don't think LP MPs can have attachments.

Revision history for this message
Michi Henning (michihenning) wrote :

So sorry, got distracted by my wife with an imap issue in the middle of things.

It's a it of a let-down that I can't add an attachment to comments here.

Here's a link to the diff:

https://www.dropbox.com/s/qbbs3yeo6f5ovom/snap_root.diff?dl=0

Revision history for this message
Michi Henning (michihenning) wrote :

> @CMAKE_INSTALL_PREFIX@ is guaranteed to either be empty or start with a slash,
> right? So that's fine. And base_dflt_file always starts with a slash. So I
> think we're fine there.

Not necessarily. I can set it to anything when I run the configure step.

I think having the slash there is safer, and it won't do any harm if there is an extra one.

374. By Michael Terry

Apply Michi's patch to fix review nits, thanks

Revision history for this message
Michael Terry (mterry) wrote :

Thanks! I've applied your patch.

Revision history for this message
Michi Henning (michihenning) wrote :

Still need to re-target this at the devel branch, otherwise Jenkins won't test it. Could you do that please?

375. By Michael Terry

Merge devel

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'data/scope-registry.conf.in'
2--- data/scope-registry.conf.in 2014-05-07 11:45:50 +0000
3+++ data/scope-registry.conf.in 2016-10-17 08:49:38 +0000
4@@ -7,4 +7,4 @@
5 respawn
6 respawn limit 10 60
7
8-exec @CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@/@UNITY_SCOPES_LIB@/scoperegistry
9+exec $SNAP/@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@/@UNITY_SCOPES_LIB@/scoperegistry
10
11=== modified file 'data/smart-scopes-proxy.conf.in'
12--- data/smart-scopes-proxy.conf.in 2014-05-07 11:45:50 +0000
13+++ data/smart-scopes-proxy.conf.in 2016-10-17 08:49:38 +0000
14@@ -9,4 +9,4 @@
15
16 expect stop
17
18-exec @CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@/@UNITY_SCOPES_LIB@/smartscopesproxy upstart
19+exec $SNAP/@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@/@UNITY_SCOPES_LIB@/smartscopesproxy upstart
20
21=== modified file 'doc/tutorial.dox'
22--- doc/tutorial.dox 2016-06-15 09:16:47 +0000
23+++ doc/tutorial.dox 2016-10-17 08:49:38 +0000
24@@ -1560,7 +1560,8 @@
25
26 The `IdleTimeout` key controls how long a scope can remain idle before it is told to stop by the registry (or
27 killed if it does not stop within 4 seconds). The default idle timeout is 40 seconds, meaning that a
28-scope will be told to stop if no query was sent to it for that amount of time.
29+scope will be told to stop if no query was sent to it for that amount of time. Only values in the range 1 to
30+300 seconds are accepted.
31
32 `ResultTtl` determines how long results should be cached by the UI before they are considered "stale"
33 and should be refreshed. `None` indicates that results remain valid indefinitely; `Small` indicates
34
35=== modified file 'include/unity/scopes/internal/ConfigBase.h'
36--- include/unity/scopes/internal/ConfigBase.h 2014-11-03 05:31:30 +0000
37+++ include/unity/scopes/internal/ConfigBase.h 2016-10-17 08:49:38 +0000
38@@ -59,6 +59,7 @@
39 virtual std::string get_middleware(std::string const& group, std::string const& key) const;
40
41 protected:
42+ std::string snap_root() const;
43 void throw_ex(::std::string const& reason) const;
44 bool path_exists(::std::string const& path) const;
45
46@@ -70,6 +71,7 @@
47 private:
48 unity::util::IniParser::SPtr parser_;
49 std::string configfile_;
50+ std::string snap_root_;
51 };
52
53 } // namespace internal
54
55=== modified file 'src/scopes/internal/ConfigBase.cpp'
56--- src/scopes/internal/ConfigBase.cpp 2014-08-28 00:20:56 +0000
57+++ src/scopes/internal/ConfigBase.cpp 2016-10-17 08:49:38 +0000
58@@ -43,10 +43,22 @@
59 // If configfile is the empty string, we create a default instance that returns "Zmq" for the middleware
60 // and throws for the other methods.
61
62-ConfigBase::ConfigBase(string const& configfile, string const& dflt_file) :
63+ConfigBase::ConfigBase(string const& configfile, string const& base_dflt_file) :
64 parser_(nullptr),
65 configfile_(configfile)
66 {
67+ char const* sroot = getenv("SNAP");
68+ if (sroot)
69+ {
70+ snap_root_ = sroot;
71+ if (!snap_root_.empty() && snap_root_[snap_root_.size() - 1] != '/')
72+ {
73+ snap_root_ += '/';
74+ }
75+ }
76+
77+ const string dflt_file = base_dflt_file.empty() ? "" : snap_root_ + base_dflt_file;
78+
79 if (!configfile.empty())
80 {
81 boost::filesystem::path path(configfile);
82@@ -157,6 +169,11 @@
83 return val;
84 }
85
86+string ConfigBase::snap_root() const
87+{
88+ return snap_root_;
89+}
90+
91 void ConfigBase::throw_ex(string const& reason) const
92 {
93 string s = "\"" + configfile_ + "\": " + reason;
94
95=== modified file 'src/scopes/internal/RegistryConfig.cpp'
96--- src/scopes/internal/RegistryConfig.cpp 2016-08-31 17:54:04 +0000
97+++ src/scopes/internal/RegistryConfig.cpp 2016-10-17 08:49:38 +0000
98@@ -53,7 +53,7 @@
99 identity_ = identity;
100 mw_kind_ = get_middleware(registry_config_group, mw_kind_key);
101 mw_configfile_ = get_optional_string(registry_config_group, mw_kind_ + configfile_key);
102- scope_installdir_ = get_optional_string(registry_config_group, scope_installdir_key, DFLT_SCOPE_INSTALL_DIR);
103+ scope_installdir_ = get_optional_string(registry_config_group, scope_installdir_key, snap_root() + DFLT_SCOPE_INSTALL_DIR);
104 oem_installdir_ = get_optional_string(registry_config_group, oem_installdir_key, DFLT_OEM_INSTALL_DIR);
105 click_installdir_ = get_optional_string(registry_config_group, click_installdir_key);
106 if (click_installdir_.empty())
107@@ -65,7 +65,7 @@
108 }
109 click_installdir_ = string(home) + "/.local/share/unity-scopes/";
110 }
111- scoperunner_path_ = get_optional_string(registry_config_group, scoperunner_path_key, DFLT_SCOPERUNNER_PATH);
112+ scoperunner_path_ = get_optional_string(registry_config_group, scoperunner_path_key, snap_root() + DFLT_SCOPERUNNER_PATH);
113 if (scoperunner_path_[0] != '/')
114 {
115 throw ConfigException(configfile + ": " + scoperunner_path_key + " must be an absolute path");
116
117=== modified file 'src/scopes/internal/RuntimeConfig.cpp'
118--- src/scopes/internal/RuntimeConfig.cpp 2016-02-22 01:29:01 +0000
119+++ src/scopes/internal/RuntimeConfig.cpp 2016-10-17 08:49:38 +0000
120@@ -63,11 +63,11 @@
121 if (configfile.empty()) // Default config
122 {
123 registry_identity_ = DFLT_REGISTRY_ID;
124- registry_configfile_ = DFLT_REGISTRY_INI;
125+ registry_configfile_ = snap_root() + DFLT_REGISTRY_INI;
126 ss_registry_identity_ = DFLT_SS_REGISTRY_ID;
127- ss_configfile_ = DFLT_SS_REGISTRY_INI;
128+ ss_configfile_ = snap_root() + DFLT_SS_REGISTRY_INI;
129 default_middleware_ = DFLT_MIDDLEWARE;
130- default_middleware_configfile_ = DFLT_ZMQ_MIDDLEWARE_INI;
131+ default_middleware_configfile_ = snap_root() + DFLT_ZMQ_MIDDLEWARE_INI;
132 reap_expiry_ = DFLT_REAP_EXPIRY;
133 reap_interval_ = DFLT_REAP_INTERVAL;
134 cache_directory_ = default_cache_directory();
135@@ -83,7 +83,7 @@
136 default_middleware_ = get_middleware(runtime_config_group, default_middleware_key);
137 default_middleware_configfile_ = get_optional_string(runtime_config_group,
138 default_middleware_ + default_middleware_configfile_key,
139- DFLT_MIDDLEWARE_INI);
140+ snap_root() + DFLT_MIDDLEWARE_INI);
141 reap_expiry_ = get_optional_int(runtime_config_group, reap_expiry_key, DFLT_REAP_EXPIRY);
142 if (reap_expiry_ < 1 && reap_expiry_ != -1)
143 {
144
145=== modified file 'src/scopes/internal/ScopeConfig.cpp'
146--- src/scopes/internal/ScopeConfig.cpp 2016-03-31 09:27:33 +0000
147+++ src/scopes/internal/ScopeConfig.cpp 2016-10-17 08:49:38 +0000
148@@ -115,6 +115,10 @@
149 try
150 {
151 string art = parser()->get_string(scope_config_group, art_key);
152+ if (!art.empty() && art[0] == '/')
153+ {
154+ art = snap_root() + art;
155+ }
156 art_.reset(new string(art));
157 }
158 catch (LogicException const&)
159@@ -123,6 +127,10 @@
160 try
161 {
162 string icon = parser()->get_string(scope_config_group, icon_key);
163+ if (!icon.empty() && icon[0] == '/')
164+ {
165+ icon = snap_root() + icon;
166+ }
167 icon_.reset(new string(icon));
168 }
169 catch (LogicException const&)
170@@ -173,12 +181,11 @@
171
172 idle_timeout_ = get_optional_int(scope_config_group, idle_timeout_key, DFLT_SCOPE_IDLE_TIMEOUT);
173
174- // Negative values and values greater than max int (once multiplied by 1000 (s to ms)) are illegal
175- const int max_idle_timeout = std::numeric_limits<int>::max() / 1000;
176- if ((idle_timeout_ < 0 || idle_timeout_ > max_idle_timeout) && idle_timeout_ != -1)
177+ // Values less than 1s and greater than 300s are illegal
178+ if (idle_timeout_ < 1 || idle_timeout_ > 300)
179 {
180 throw_ex("Illegal value (" + std::to_string(idle_timeout_) + ") for " + idle_timeout_key +
181- ": value must be >= 0 and <= " + std::to_string(max_idle_timeout));
182+ ": value must be >= 1 and <= 300");
183 }
184
185 results_ttl_type_ = ScopeMetadata::ResultsTtlType::None;
186
187=== modified file 'test/gtest/scopes/internal/ScopeConfig/ScopeConfig_test.cpp'
188--- test/gtest/scopes/internal/ScopeConfig/ScopeConfig_test.cpp 2015-05-14 02:50:35 +0000
189+++ test/gtest/scopes/internal/ScopeConfig/ScopeConfig_test.cpp 2016-10-17 08:49:38 +0000
190@@ -156,7 +156,7 @@
191 catch(ConfigException const& e)
192 {
193 boost::regex r("unity::scopes::ConfigException: \".*\": Illegal value \\(-2\\) for IdleTimeout: "
194- "value must be >= 0 and <= [0-9]+");
195+ "value must be >= 1 and <= [0-9]+");
196 EXPECT_TRUE(boost::regex_match(e.what(), r));
197 }
198 }

Subscribers

People subscribed via source and target branches

to all changes: