Merge lp:~jelmer/brz/cachedir into lp:brz

Proposed by Jelmer Vernooij
Status: Merged
Approved by: Jelmer Vernooij
Approved revision: no longer in the source branch.
Merge reported by: The Breezy Bot
Merged at revision: not available
Proposed branch: lp:~jelmer/brz/cachedir
Merge into: lp:brz
Diff against target: 190 lines (+80/-28)
7 files modified
breezy/config.py (+0/-10)
breezy/git/cache.py (+1/-7)
breezy/osutils.py (+33/-0)
breezy/plugins/launchpad/lp_api.py (+1/-1)
breezy/plugins/launchpad/test_lp_api.py (+2/-2)
breezy/tests/test_config.py (+0/-8)
breezy/tests/test_osutils.py (+43/-0)
To merge this branch: bzr merge lp:~jelmer/brz/cachedir
Reviewer Review Type Date Requested Status
Martin Packman Approve
Review via email: mp+362708@code.launchpad.net

Commit message

Add osutils.cache_dir function and use it where we create Breezy-specific caches.

Description of the change

Add osutils.cache_dir function and use it where we create Breezy-specific caches.

To post a comment you must log in.
Revision history for this message
Martin Packman (gz) wrote :

Okay. We need to do a bit more tidying up here, can tackle later.

review: Approve
Revision history for this message
The Breezy Bot (the-breezy-bot) wrote :
Revision history for this message
The Breezy Bot (the-breezy-bot) wrote :
Revision history for this message
The Breezy Bot (the-breezy-bot) wrote :
Revision history for this message
Martin Packman (gz) wrote :

Attempting land again to see what the failures were.

Revision history for this message
The Breezy Bot (the-breezy-bot) wrote :
Revision history for this message
The Breezy Bot (the-breezy-bot) wrote :
Revision history for this message
The Breezy Bot (the-breezy-bot) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'breezy/config.py'
2--- breezy/config.py 2019-05-11 18:38:21 +0000
3+++ breezy/config.py 2019-06-15 15:54:32 +0000
4@@ -1560,16 +1560,6 @@
5 return os.environ.get('APPORT_CRASH_DIR', '/var/crash')
6
7
8-def xdg_cache_dir():
9- # See http://standards.freedesktop.org/basedir-spec/latest/ar01s03.html
10- # Possibly this should be different on Windows?
11- e = os.environ.get('XDG_CACHE_HOME', None)
12- if e:
13- return e
14- else:
15- return os.path.expanduser('~/.cache')
16-
17-
18 def _get_default_mail_domain(mailname_file='/etc/mailname'):
19 """If possible, return the assumed default email domain.
20
21
22=== modified file 'breezy/git/cache.py'
23--- breezy/git/cache.py 2019-02-24 03:46:16 +0000
24+++ breezy/git/cache.py 2019-06-15 15:54:32 +0000
25@@ -51,13 +51,7 @@
26
27
28 def get_cache_dir():
29- try:
30- from xdg.BaseDirectory import xdg_cache_home
31- except ImportError:
32- from ..config import config_dir
33- ret = os.path.join(config_dir(), "git")
34- else:
35- ret = os.path.join(xdg_cache_home, "breezy", "git")
36+ ret = os.path.join(osutils.cache_dir(), "git")
37 if not os.path.isdir(ret):
38 os.makedirs(ret)
39 return ret
40
41=== modified file 'breezy/osutils.py'
42--- breezy/osutils.py 2019-06-03 21:25:01 +0000
43+++ breezy/osutils.py 2019-06-15 15:54:32 +0000
44@@ -2634,6 +2634,39 @@
45 return False
46
47
48+def cache_dir():
49+ """Return the cache directory to use."""
50+ if sys.platform in ("nt", "win32"):
51+ from bzrlib.win32utils import get_local_appdata_location
52+ s = get_local_appdata_location()
53+ # This can return a unicode string or a plain string in
54+ # user encoding
55+ if isinstance(s, bytes):
56+ s = s.decode(bzrlib.user_encoding)
57+ cache_dir = s.encode(_fs_enc)
58+ else:
59+ try:
60+ from xdg import BaseDirectory
61+ except ImportError:
62+ xdg_cache_dir = os.environ.get('XDG_CACHE_HOME', None)
63+ else:
64+ xdg_cache_dir = BaseDirectory.xdg_cache_home
65+ if xdg_cache_dir is not None:
66+ cache_dir = os.path.join(xdg_cache_dir, "breezy")
67+ else:
68+ cache_dir = None
69+ if cache_dir is not None and not isinstance(cache_dir, text_type):
70+ cache_dir = cache_dir.encode(_fs_enc)
71+
72+ if cache_dir is None:
73+ cache_dir = os.path.expanduser('~/.cache/breezy')
74+
75+ if not os.path.exists(cache_dir):
76+ os.makedirs(cache_dir)
77+
78+ return cache_dir
79+
80+
81 def get_fs_type(path):
82 """Return the filesystem type for the partition a path is in.
83
84
85=== modified file 'breezy/plugins/launchpad/lp_api.py'
86--- breezy/plugins/launchpad/lp_api.py 2019-05-29 09:48:08 +0000
87+++ breezy/plugins/launchpad/lp_api.py 2019-06-15 15:54:32 +0000
88@@ -88,7 +88,7 @@
89
90 def get_cache_directory():
91 """Return the directory to cache launchpadlib objects in."""
92- return osutils.pathjoin(config.config_dir(), 'launchpad')
93+ return osutils.pathjoin(osutils.cache_dir(), 'launchpad')
94
95
96 def parse_launchpadlib_version(version_number):
97
98=== modified file 'breezy/plugins/launchpad/test_lp_api.py'
99--- breezy/plugins/launchpad/test_lp_api.py 2018-12-19 01:29:48 +0000
100+++ breezy/plugins/launchpad/test_lp_api.py 2019-06-15 15:54:32 +0000
101@@ -77,7 +77,7 @@
102
103 def test_get_cache_directory(self):
104 # get_cache_directory returns the path to a directory inside the
105- # Bazaar configuration directory.
106+ # Breezy cache directory.
107 from . import lp_api
108- expected_path = osutils.pathjoin(config.config_dir(), 'launchpad')
109+ expected_path = osutils.pathjoin(osutils.cache_dir(), 'launchpad')
110 self.assertEqual(expected_path, lp_api.get_cache_directory())
111
112=== modified file 'breezy/tests/test_config.py'
113--- breezy/tests/test_config.py 2018-11-17 16:53:10 +0000
114+++ breezy/tests/test_config.py 2019-06-15 15:54:32 +0000
115@@ -550,10 +550,6 @@
116 self.assertEqual(config.authentication_config_filename(),
117 self.brz_home + '/authentication.conf')
118
119- def test_xdg_cache_dir(self):
120- self.assertEqual(config.xdg_cache_dir(),
121- '/home/bogus/.cache')
122-
123
124 class TestConfigPathFallback(tests.TestCaseInTempDir):
125
126@@ -582,10 +578,6 @@
127 self.assertEqual(config.authentication_config_filename(),
128 self.bzr_home + '/authentication.conf')
129
130- def test_xdg_cache_dir(self):
131- self.assertEqual(config.xdg_cache_dir(),
132- os.path.join(self.test_dir, '.cache'))
133-
134
135 class TestXDGConfigDir(tests.TestCaseInTempDir):
136 # must be in temp dir because config tests for the existence of the bazaar
137
138=== modified file 'breezy/tests/test_osutils.py'
139--- breezy/tests/test_osutils.py 2019-03-04 00:16:27 +0000
140+++ breezy/tests/test_osutils.py 2019-06-15 15:54:32 +0000
141@@ -2344,6 +2344,49 @@
142 pywintypes.error(errno.EINVAL, "Invalid parameter", "Caller")))
143
144
145+class TestXDGCacheDir(tests.TestCaseInTempDir):
146+ # must be in temp dir because tests for the existence of the breezy
147+ # subdirectory of $XDG_CACHE_HOME
148+
149+ def setUp(self):
150+ super(TestXDGCacheDir, self).setUp()
151+ if sys.platform in ('darwin', 'win32'):
152+ raise tests.TestNotApplicable(
153+ 'XDG cache dir not used on this platform')
154+ self.overrideEnv('HOME', self.test_home_dir)
155+ # BZR_HOME overrides everything we want to test so unset it.
156+ self.overrideEnv('BZR_HOME', None)
157+
158+ def test_xdg_cache_dir_exists(self):
159+ """When ~/.cache/breezy exists, use it as the cache dir."""
160+ cachedir = osutils.pathjoin(self.test_home_dir, '.cache')
161+ newdir = osutils.pathjoin(cachedir, 'breezy')
162+ try:
163+ from xdg import BaseDirectory
164+ except ImportError:
165+ pass
166+ else:
167+ self.overrideAttr(BaseDirectory, "xdg_cache_home", cachedir)
168+ os.makedirs(newdir)
169+ self.assertEqual(osutils.cache_dir(), newdir)
170+
171+ def test_xdg_cache_home_unix(self):
172+ """When XDG_CACHE_HOME is set, use it."""
173+ if sys.platform in ('nt', 'win32'):
174+ raise tests.TestNotApplicable(
175+ 'XDG cache dir not used on this platform')
176+ xdgcachedir = osutils.pathjoin(self.test_home_dir, 'xdgcache')
177+ self.overrideEnv('XDG_CACHE_HOME', xdgcachedir)
178+ try:
179+ from xdg import BaseDirectory
180+ except ImportError:
181+ pass
182+ else:
183+ self.overrideAttr(BaseDirectory, "xdg_cache_home", xdgcachedir)
184+ newdir = osutils.pathjoin(xdgcachedir, 'breezy')
185+ os.makedirs(newdir)
186+ self.assertEqual(osutils.cache_dir(), newdir)
187+
188 class SupportsExecutableTests(tests.TestCaseInTempDir):
189
190 def test_returns_bool(self):

Subscribers

People subscribed via source and target branches