Merge lp:~rafalcieslak256/ubuntu-accomplishments-daemon/982246 into lp:ubuntu-accomplishments-daemon

Proposed by Rafał Cieślak
Status: Merged
Merged at revision: 13
Proposed branch: lp:~rafalcieslak256/ubuntu-accomplishments-daemon/982246
Merge into: lp:ubuntu-accomplishments-daemon
Diff against target: 164 lines (+77/-26)
5 files modified
accomplishments/daemon/api.py (+8/-3)
accomplishments/util/__init__.py (+3/-0)
accomplishments/util/paths.py (+44/-15)
bin/accomplishments-daemon (+11/-4)
bin/accomplishments-daemon-fg (+11/-4)
To merge this branch: bzr merge lp:~rafalcieslak256/ubuntu-accomplishments-daemon/982246
Reviewer Review Type Date Requested Status
Jono Bacon Pending
Review via email: mp+102030@code.launchpad.net

Description of the change

This branch is meant to fix #982246 in daemon, by introducing proper PATH setting.

It allows to install the daemon with a given prefix. (python setup.py install --prefix /where/to/install
Although this is possible at the moment, the daemon will start only if the prefix is /usr, or it is being run without installation, in any other case it will crash on startup. This branch fixes this issue.

This feature is quite cool, I am delighted to be able to successfully install the application to /opt/accomplishments (or any other directory), it makes it much easier to browse it's installed files, makes me sure I won't accidentally hurt any significant system file, and simplifies uninstallation (just remove whole directory)

=========
First part of the fix is in the bin/accomplishments-daemon file. It failed to import anything from accomplishments.* modules, because python was looking for them in /usr/lib/python2.7/... while our modules were installed to the prefixed directory (e.g. /opt/accomplishments/lib/python2.7/...). This is fixed by determining the prefix (stored in basepath variable), and adding required directories to the PATH. The basepath itself is added to the PATH too, which is exactly the same as adding ./ when the daemon is run from branch.

The same code is added to bin/accomplishments-daemon-fg file.

Next piece of fix is a complete rewrite of accomplishments/utils/paths.py. The problem was that it has supported only two cases (run from branch and installed to /usr), as it had hadr-coded paths. Proposed code determines the prefix (to basepath) and sets the required variables as needed.

Lastly, the new PATH variable is not passed to subprocesses - I've solved that by setting module_dir variables in paths.py, storing the path to accomplishment.* modules. This way they can be used to prepare os.environ in the api.py file.

To post a comment you must log in.
16. By Rafał Cieślak

unneeded import

17. By Rafał Cieślak

this unsafe function is not used anymore

Revision history for this message
Rafał Cieślak (rafalcieslak256) wrote :

Notes concerning integration with viewer when installing both are available in related viewer's branch's merge request: https://code.launchpad.net/~rafalcieslak256/ubuntu-accomplishments-viewer/982246/+merge/102031

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'accomplishments/daemon/api.py'
2--- accomplishments/daemon/api.py 2012-04-14 17:07:55 +0000
3+++ accomplishments/daemon/api.py 2012-04-15 16:19:19 +0000
4@@ -38,13 +38,18 @@
5 from ubuntuone.platform.tools import SyncDaemonTool
6 from ubuntuone.couch import auth
7
8-os.environ["PYTHONPATH"] = "$PYTHONPATH:."
9-
10 import accomplishments
11 from accomplishments import exceptions
12 from accomplishments.daemon import dbusapi
13 from accomplishments.util import get_data_file, SubprocessReturnCodeProtocol
14-from accomplishments.util.paths import media_dir
15+from accomplishments.util.paths import media_dir, module_dir1, module_dir2, installed
16+
17+os.environ["PYTHONPATH"] = "$PYTHONPATH:."
18+# The directories with accomplishment.* modules, that are being used by scripts,
19+# may happen to be in a completelly different directory, if the daemon was
20+# installed using a non-default prefix.
21+if installed:
22+ os.environ["PYTHONPATH"] = module_dir1 + ":" + module_dir2 + ":" + os.environ["PYTHONPATH"]
23
24 MATRIX_USERNAME = "openiduser155707"
25 LOCAL_USERNAME = getpass.getuser()
26
27=== modified file 'accomplishments/util/__init__.py'
28--- accomplishments/util/__init__.py 2012-04-12 03:02:29 +0000
29+++ accomplishments/util/__init__.py 2012-04-15 16:19:19 +0000
30@@ -52,6 +52,9 @@
31
32
33 def get_data_path():
34+ # XXX: NOTE: This function will most likely work incorrectly when daemon is installed in non-default path.
35+ # Luckily, this function is no longer used anywhere.
36+ # If you feel you need to get this path, please refer to utils/paths.py instead.
37 """Retrieve accomplishments-daemon data path
38
39 This path is by default <accomplishments_daemon_lib_path>/../data/ in trunk
40
41=== modified file 'accomplishments/util/paths.py'
42--- accomplishments/util/paths.py 2012-04-12 03:02:29 +0000
43+++ accomplishments/util/paths.py 2012-04-15 16:19:19 +0000
44@@ -1,16 +1,45 @@
45 import os
46-from accomplishments.util import get_data_path
47-
48-def uses_system_lib():
49- return os.path.dirname(__file__).startswith("/usr")
50-
51-runs_in_branch = not uses_system_lib()
52-#systemdata_dir = "data/daemon/"
53-#media_dir = "data/media/"
54-if not runs_in_branch:
55- systemdata_dir = "/usr/share/accomplishments-daemon/"
56- media_dir = "/usr/share/accomplishments-daemon/media/"
57-else:
58- localdatapath = get_data_path()
59- systemdata_dir = os.path.join(localdatapath, "daemon")
60- media_dir = os.path.join(localdatapath, "media")
61+
62+if __file__[0] is '/':
63+ #the script run with absolute path
64+ scriptpath = __file__
65+else:
66+ scriptpath = os.path.join(os.getcwd(),__file__)
67+
68+# okay, so as we have the full path, we're ready to analise it...
69+
70+# start by looking 2 levels higher. This is either the branch directory,
71+# or the python2.7/...-packages directory, if the application is run installed.
72+branchpath = os.path.split(os.path.split(os.path.split(scriptpath)[0])[0])[0]
73+branchdir_name = os.path.split(branchpath)[1]
74+if ((branchdir_name == "site-packages") or (branchdir_name == 'dist-packages')):
75+ installed = True
76+else:
77+ installed = False
78+
79+# Great, at that level we know whether the application is run from branch, or
80+# has been installed. This allows us to interpret the directories further...
81+
82+
83+if installed:
84+ basepath = os.path.split(os.path.split(os.path.split(branchpath)[0])[0])[0]
85+ # basepath should equal to prefix given on installation.
86+ print "Daemon seems to be installed to: " + basepath
87+
88+ # finally, setting these significant data directories...
89+ systemdata_dir = os.path.join(basepath, 'share/accomplishments-daemon')
90+ media_dir = os.path.join(basepath, 'share/accomplishments-daemon/media')
91+ # these two may need to be set for the accomplismhents scripts, so that they
92+ # can use OUR accomplishments module, using this installation.
93+ module_dir1 = os.path.join(basepath, 'lib/python2.7/site-packages')
94+ module_dir2 = os.path.join(basepath, 'lib/python2.7/dist-packages')
95+else:
96+ # using branch root directory as the base path
97+ basepath = branchpath
98+ print "Daemon seems to be run not installed, branch base path used: " + basepath
99+
100+ # finally, setting these significant data directories...
101+ systemdata_dir = os.path.join(basepath, 'data/daemon')
102+ media_dir = os.path.join(basepath, 'data/media')
103+ module_dir1 = None # always using default
104+ module_dir2 = None # always using default
105
106=== modified file 'bin/accomplishments-daemon'
107--- bin/accomplishments-daemon 2012-04-12 00:20:10 +0000
108+++ bin/accomplishments-daemon 2012-04-15 16:19:19 +0000
109@@ -11,14 +11,21 @@
110 import dbus
111 from dbus.mainloop.glib import DBusGMainLoop
112
113+# Importing from accomplishments.paths would fail, if the path is set wrongly.
114+# We need to ensure that the script will load the correct version of
115+# accomplishment.* that is installed with this script.
116+scriptpath = os.path.abspath(__file__)
117+# basepaths shall be equal to prefix used while installing - if the application is run from source, it does not really matter.
118+basepath = os.path.split(os.path.split(scriptpath)[0])[0]
119+sys.path.insert(0, basepath)
120+sys.path.insert(0, basepath + "/lib/python2.7")
121+sys.path.insert(0, basepath + "/lib/python2.7/site-packages")
122+sys.path.insert(0, basepath + "/lib/python2.7/dist-packages")
123+
124 from accomplishments.daemon import app
125
126 from accomplishments.util import paths
127
128-if paths.runs_in_branch:
129- # PYTHONPATH mangling
130- sys.path.insert(0, ".")
131-
132 dbus_loop = DBusGMainLoop(set_as_default=True)
133 application = app.applicationFactory(
134 app_name="Ubuntu Accomplishments",
135
136=== modified file 'bin/accomplishments-daemon-fg'
137--- bin/accomplishments-daemon-fg 2012-04-12 00:20:10 +0000
138+++ bin/accomplishments-daemon-fg 2012-04-15 16:19:19 +0000
139@@ -11,14 +11,21 @@
140 import dbus
141 from dbus.mainloop.glib import DBusGMainLoop
142
143+# Importing from accomplishments.paths would fail, if the path is set wrongly.
144+# We need to ensure that the script will load the correct version of
145+# accomplishment.* that is installed with this script.
146+scriptpath = os.path.abspath(__file__)
147+# basepaths shall be equal to prefix used while installing - if the application is run from source, it does not really matter.
148+basepath = os.path.split(os.path.split(scriptpath)[0])[0]
149+sys.path.insert(0, basepath)
150+sys.path.insert(0, basepath + "/lib/python2.7")
151+sys.path.insert(0, basepath + "/lib/python2.7/site-packages")
152+sys.path.insert(0, basepath + "/lib/python2.7/dist-packages")
153+
154 from accomplishments.daemon import app
155
156 from accomplishments.util import paths
157
158-if paths.runs_in_branch:
159- # PYTHONPATH mangling
160- sys.path.insert(0, ".")
161-
162 dbus_loop = DBusGMainLoop(set_as_default=True)
163 application = app.applicationFactory(
164 app_name="Ubuntu Accomplishments",

Subscribers

People subscribed via source and target branches