Merge ~cjwatson/launchpad:open-not-file into launchpad:master
- Git
- lp:~cjwatson/launchpad
- open-not-file
- Merge into master
Proposed by
Colin Watson
Status: | Merged |
---|---|
Approved by: | Colin Watson |
Approved revision: | e39b1b1e4911a14e194b6e55426a15833803e7ed |
Merge reported by: | Otto Co-Pilot |
Merged at revision: | not available |
Proposed branch: | ~cjwatson/launchpad:open-not-file |
Merge into: | launchpad:master |
Diff against target: |
561 lines (+120/-92) 23 files modified
lib/lp/app/browser/launchpad.py (+2/-1) lib/lp/app/stories/basics/xx-maintenance-message.txt (+4/-2) lib/lp/app/stories/folder.txt (+18/-16) lib/lp/archivepublisher/scripts/generate_contents_files.py (+16/-14) lib/lp/archivepublisher/scripts/publish_ftpmaster.py (+1/-1) lib/lp/archivepublisher/tests/test_ftparchive.py (+11/-7) lib/lp/archivepublisher/tests/test_generate_contents_files.py (+8/-6) lib/lp/archivepublisher/tests/test_publish_ftpmaster.py (+8/-6) lib/lp/services/config/__init__.py (+2/-2) lib/lp/services/profile/mem.py (+1/-1) lib/lp/services/profile/profile.py (+8/-9) lib/lp/services/profile/profiling.txt (+4/-4) lib/lp/services/sitesearch/tests/test_testservices.py (+1/-1) lib/lp/services/sitesearch/testservice.py (+9/-7) lib/lp/services/tests/test_utils.py (+2/-1) lib/lp/soyuz/scripts/gina/changelog.py (+2/-1) lib/lp/translations/scripts/language_pack.py (+4/-1) lib/lp/translations/scripts/validate_translations_file.py (+2/-1) lib/lp/translations/tests/test_translationtemplatesbuildbehaviour.py (+6/-6) scripts/process-one-mail.py (+3/-1) utilities/audit-security-settings.py (+4/-2) utilities/format-imports (+2/-1) utilities/update-copyright (+2/-1) |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Thiago F. Pappacena (community) | Approve | ||
Review via email: mp+380385@code.launchpad.net |
Commit message
Use open() rather than file()
Description of the change
Also convert to the context manager form where appropriate.
To post a comment you must log in.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | diff --git a/lib/lp/app/browser/launchpad.py b/lib/lp/app/browser/launchpad.py |
2 | index 4aa9a39..e1a5c71 100644 |
3 | --- a/lib/lp/app/browser/launchpad.py |
4 | +++ b/lib/lp/app/browser/launchpad.py |
5 | @@ -544,7 +544,8 @@ class MaintenanceMessage: |
6 | |
7 | def __call__(self): |
8 | if os.path.exists('+maintenancetime.txt'): |
9 | - message = file('+maintenancetime.txt').read() |
10 | + with open('+maintenancetime.txt') as f: |
11 | + message = f.read() |
12 | try: |
13 | maintenancetime = parseDatetimetz(message) |
14 | except DateTimeError: |
15 | diff --git a/lib/lp/app/stories/basics/xx-maintenance-message.txt b/lib/lp/app/stories/basics/xx-maintenance-message.txt |
16 | index 776abf9..db47ea2 100644 |
17 | --- a/lib/lp/app/stories/basics/xx-maintenance-message.txt |
18 | +++ b/lib/lp/app/stories/basics/xx-maintenance-message.txt |
19 | @@ -73,14 +73,16 @@ When the time is in the past, the time is still given as "very very soon". |
20 | |
21 | If the time doesn't make sense, or is empty, then no message is displayed. |
22 | |
23 | - >>> file('+maintenancetime.txt', 'w').write('xxxx') |
24 | + >>> with open('+maintenancetime.txt', 'w') as f: |
25 | + ... f.write('xxxx') |
26 | >>> content = front_page_content() |
27 | >>> okay200 in content |
28 | True |
29 | >>> maintenance_text not in content |
30 | True |
31 | |
32 | - >>> file('+maintenancetime.txt', 'w').write('') |
33 | + >>> with open('+maintenancetime.txt', 'w') as f: |
34 | + ... f.write('') |
35 | >>> content = front_page_content() |
36 | >>> okay200 in content |
37 | True |
38 | diff --git a/lib/lp/app/stories/folder.txt b/lib/lp/app/stories/folder.txt |
39 | index b66678a..7764753 100644 |
40 | --- a/lib/lp/app/stories/folder.txt |
41 | +++ b/lib/lp/app/stories/folder.txt |
42 | @@ -12,14 +12,15 @@ the directory to expose. |
43 | >>> import os |
44 | >>> import tempfile |
45 | >>> resource_dir = tempfile.mkdtemp(prefix='resources') |
46 | - >>> file(os.path.join(resource_dir, 'test.txt'), 'w').write('Text file') |
47 | - >>> file(os.path.join(resource_dir, 'image1.gif'), 'w').write( |
48 | - ... 'GIF file') |
49 | - >>> file(os.path.join(resource_dir, 'image2.png'), 'w').write( |
50 | - ... 'PNG file') |
51 | + >>> with open(os.path.join(resource_dir, 'test.txt'), 'w') as f: |
52 | + ... f.write('Text file') |
53 | + >>> with open(os.path.join(resource_dir, 'image1.gif'), 'w') as f: |
54 | + ... f.write('GIF file') |
55 | + >>> with open(os.path.join(resource_dir, 'image2.png'), 'w') as f: |
56 | + ... f.write('PNG file') |
57 | >>> os.mkdir(os.path.join(resource_dir, 'a_dir')) |
58 | - >>> file(os.path.join(resource_dir, 'other.txt'), 'w').write( |
59 | - ... 'Other file') |
60 | + >>> with open(os.path.join(resource_dir, 'other.txt'), 'w') as f: |
61 | + ... f.write('Other file') |
62 | |
63 | >>> from lp.app.browser.folder import ExportedFolder |
64 | >>> class MyFolder(ExportedFolder): |
65 | @@ -123,10 +124,10 @@ image_extensions property. |
66 | |
67 | If a file without extension exists, that one will be served. |
68 | |
69 | - >>> file(os.path.join(resource_dir, 'image3'), 'w').write( |
70 | - ... 'Image without extension') |
71 | - >>> file(os.path.join(resource_dir, 'image3.gif'), 'w').write( |
72 | - ... 'Image with extension') |
73 | + >>> with open(os.path.join(resource_dir, 'image3'), 'w') as f: |
74 | + ... f.write('Image without extension') |
75 | + >>> with open(os.path.join(resource_dir, 'image3.gif'), 'w') as f: |
76 | + ... f.write('Image with extension') |
77 | |
78 | >>> view = MyImageFolder(object(), FakeRequest(version="devel")) |
79 | >>> view = view.publishTraverse(view.request, 'image3') |
80 | @@ -146,12 +147,13 @@ export_subdirectories is set to True, it will allow traversing to |
81 | subdirectories. |
82 | |
83 | >>> os.mkdir(os.path.join(resource_dir, 'public')) |
84 | - >>> file(os.path.join( |
85 | - ... resource_dir, 'public', 'test1.txt'), 'w').write('Public File') |
86 | + >>> with open(os.path.join( |
87 | + ... resource_dir, 'public', 'test1.txt'), 'w') as f: |
88 | + ... f.write('Public File') |
89 | >>> os.mkdir(os.path.join(resource_dir, 'public', 'subdir1')) |
90 | - >>> file(os.path.join( |
91 | - ... resource_dir, 'public', 'subdir1', 'test1.txt'), 'w').write( |
92 | - ... 'Sub file 1') |
93 | + >>> with open(os.path.join( |
94 | + ... resource_dir, 'public', 'subdir1', 'test1.txt'), 'w') as f: |
95 | + ... f.write('Sub file 1') |
96 | |
97 | >>> class MyTree(ExportedFolder): |
98 | ... folder = resource_dir |
99 | diff --git a/lib/lp/archivepublisher/scripts/generate_contents_files.py b/lib/lp/archivepublisher/scripts/generate_contents_files.py |
100 | index d99258e..4893523 100644 |
101 | --- a/lib/lp/archivepublisher/scripts/generate_contents_files.py |
102 | +++ b/lib/lp/archivepublisher/scripts/generate_contents_files.py |
103 | @@ -48,9 +48,10 @@ def differ_in_content(one_file, other_file): |
104 | one_exists = file_exists(one_file) |
105 | other_exists = file_exists(other_file) |
106 | if any([one_exists, other_exists]): |
107 | - return ( |
108 | - one_exists != other_exists or |
109 | - file(one_file).read() != file(other_file).read()) |
110 | + if one_exists != other_exists: |
111 | + return True |
112 | + with open(one_file, 'rb') as one_f, open(other_file, 'rb') as other_f: |
113 | + return one_f.read() != other_f.read() |
114 | else: |
115 | return False |
116 | |
117 | @@ -161,23 +162,24 @@ class GenerateContentsFiles(LaunchpadCronScript): |
118 | output_dirname = '%s-misc' % self.distribution.name |
119 | output_path = os.path.join( |
120 | self.content_archive, output_dirname, "apt-contents.conf") |
121 | - output_file = file(output_path, 'w') |
122 | |
123 | parameters = { |
124 | 'content_archive': self.content_archive, |
125 | 'distribution': self.distribution.name, |
126 | } |
127 | |
128 | - header = get_template('apt_conf_header.template') |
129 | - output_file.write(file(header).read() % parameters) |
130 | - |
131 | - dist_template = file(get_template('apt_conf_dist.template')).read() |
132 | - for suite in suites: |
133 | - parameters['suite'] = suite |
134 | - parameters['architectures'] = ' '.join(self.getArchs(suite)) |
135 | - output_file.write(dist_template % parameters) |
136 | - |
137 | - output_file.close() |
138 | + with open(output_path, 'w') as output_file: |
139 | + header = get_template('apt_conf_header.template') |
140 | + with open(header) as header_file: |
141 | + output_file.write(header_file.read() % parameters) |
142 | + |
143 | + with open(get_template( |
144 | + 'apt_conf_dist.template')) as dist_template_file: |
145 | + dist_template = dist_template_file.read() |
146 | + for suite in suites: |
147 | + parameters['suite'] = suite |
148 | + parameters['architectures'] = ' '.join(self.getArchs(suite)) |
149 | + output_file.write(dist_template % parameters) |
150 | |
151 | def createComponentDirs(self, suites): |
152 | """Create the content archive's tree for all of its components.""" |
153 | diff --git a/lib/lp/archivepublisher/scripts/publish_ftpmaster.py b/lib/lp/archivepublisher/scripts/publish_ftpmaster.py |
154 | index 47aa73d..5fcd7d3 100644 |
155 | --- a/lib/lp/archivepublisher/scripts/publish_ftpmaster.py |
156 | +++ b/lib/lp/archivepublisher/scripts/publish_ftpmaster.py |
157 | @@ -238,7 +238,7 @@ class PublishFTPMaster(LaunchpadCronScript): |
158 | longer needs archive indexes to be set up. |
159 | """ |
160 | marker_name = self.locateIndexesMarker(distribution, suite) |
161 | - with file(marker_name, "w") as marker: |
162 | + with open(marker_name, "w") as marker: |
163 | marker.write( |
164 | "Indexes for %s were created on %s.\n" |
165 | % (suite, datetime.now(utc))) |
166 | diff --git a/lib/lp/archivepublisher/tests/test_ftparchive.py b/lib/lp/archivepublisher/tests/test_ftparchive.py |
167 | index 5e77ec4..a8a6727 100755 |
168 | --- a/lib/lp/archivepublisher/tests/test_ftparchive.py |
169 | +++ b/lib/lp/archivepublisher/tests/test_ftparchive.py |
170 | @@ -156,8 +156,10 @@ class TestFTPArchive(TestCaseWithFactory): |
171 | if samplename is None: |
172 | samplename = leafname |
173 | leaf = os.path.join(self._sampledir, samplename) |
174 | - leafcontent = file(leaf).read() |
175 | - file(fullpath, "w").write(leafcontent) |
176 | + with open(leaf) as leaf_file: |
177 | + leafcontent = leaf_file.read() |
178 | + with open(fullpath, "w") as f: |
179 | + f.write(leafcontent) |
180 | |
181 | def _setUpFTPArchiveHandler(self): |
182 | return FTPArchiveHandler( |
183 | @@ -483,7 +485,8 @@ class TestFTPArchive(TestCaseWithFactory): |
184 | # Test that a publisher run now will generate an empty apt |
185 | # config and nothing else. |
186 | apt_conf = fa.generateConfig() |
187 | - self.assertEqual(22, len(file(apt_conf).readlines())) |
188 | + with open(apt_conf) as apt_conf_file: |
189 | + self.assertEqual(22, len(apt_conf_file.readlines())) |
190 | |
191 | # XXX cprov 2007-03-21: see above, do not run a-f on dev machines. |
192 | fa.runApt(apt_conf) |
193 | @@ -532,10 +535,11 @@ class TestFTPArchive(TestCaseWithFactory): |
194 | # comparing is weak. |
195 | apt_conf = fa.generateConfig(fullpublish=True) |
196 | self.assertTrue(os.path.exists(apt_conf)) |
197 | - apt_conf_content = file(apt_conf).read() |
198 | - sample_content = file( |
199 | - os.path.join( |
200 | - self._sampledir, 'apt_conf_single_empty_suite_test')).read() |
201 | + with open(apt_conf) as apt_conf_file: |
202 | + apt_conf_content = apt_conf_file.read() |
203 | + with open(os.path.join( |
204 | + self._sampledir, 'apt_conf_single_empty_suite_test')) as f: |
205 | + sample_content = f.read() |
206 | self.assertEqual(apt_conf_content, sample_content) |
207 | |
208 | # XXX cprov 2007-03-21: see above, do not run a-f on dev machines. |
209 | diff --git a/lib/lp/archivepublisher/tests/test_generate_contents_files.py b/lib/lp/archivepublisher/tests/test_generate_contents_files.py |
210 | index cb75954..4534570 100644 |
211 | --- a/lib/lp/archivepublisher/tests/test_generate_contents_files.py |
212 | +++ b/lib/lp/archivepublisher/tests/test_generate_contents_files.py |
213 | @@ -231,9 +231,10 @@ class TestGenerateContentsFiles(TestCaseWithFactory): |
214 | distro = self.makeDistro() |
215 | script = self.makeScript(distro) |
216 | script.writeAptContentsConf([]) |
217 | - apt_contents_conf = file( |
218 | - "%s/%s-misc/apt-contents.conf" |
219 | - % (script.content_archive, distro.name)).read() |
220 | + with open( |
221 | + "%s/%s-misc/apt-contents.conf" |
222 | + % (script.content_archive, distro.name)) as f: |
223 | + apt_contents_conf = f.read() |
224 | self.assertIn('\nDefault\n{', apt_contents_conf) |
225 | self.assertIn(distro.name, apt_contents_conf) |
226 | |
227 | @@ -247,9 +248,10 @@ class TestGenerateContentsFiles(TestCaseWithFactory): |
228 | script = self.makeScript(distro) |
229 | content_archive = script.content_archive |
230 | script.writeAptContentsConf([distroseries.name]) |
231 | - apt_contents_conf = file( |
232 | - "%s/%s-misc/apt-contents.conf" |
233 | - % (script.content_archive, distro.name)).read() |
234 | + with open( |
235 | + "%s/%s-misc/apt-contents.conf" |
236 | + % (script.content_archive, distro.name)) as f: |
237 | + apt_contents_conf = f.read() |
238 | self.assertIn( |
239 | 'tree "dists/%s"\n' % distroseries.name, apt_contents_conf) |
240 | overrides_path = os.path.join( |
241 | diff --git a/lib/lp/archivepublisher/tests/test_publish_ftpmaster.py b/lib/lp/archivepublisher/tests/test_publish_ftpmaster.py |
242 | index d20c7a8..8c99b73 100644 |
243 | --- a/lib/lp/archivepublisher/tests/test_publish_ftpmaster.py |
244 | +++ b/lib/lp/archivepublisher/tests/test_publish_ftpmaster.py |
245 | @@ -214,7 +214,8 @@ class TestPublishFTPMasterScript( |
246 | |
247 | def readReleaseFile(self, filename): |
248 | """Read a Release file, return as a keyword/value dict.""" |
249 | - sections = list(TagFile(file(filename))) |
250 | + with open(filename) as f: |
251 | + sections = list(TagFile(f)) |
252 | self.assertEqual(1, len(sections)) |
253 | return dict(sections[0]) |
254 | |
255 | @@ -233,9 +234,8 @@ class TestPublishFTPMasterScript( |
256 | script_dir = os.path.join(parts_base, distro.name, parts_dir) |
257 | os.makedirs(script_dir) |
258 | script_path = os.path.join(script_dir, self.factory.getUniqueString()) |
259 | - script_file = file(script_path, "w") |
260 | - script_file.write(script_code) |
261 | - script_file.close() |
262 | + with open(script_path, "w") as script_file: |
263 | + script_file.write(script_code) |
264 | os.chmod(script_path, 0o755) |
265 | |
266 | def test_script_runs_successfully(self): |
267 | @@ -308,10 +308,12 @@ class TestPublishFTPMasterScript( |
268 | |
269 | dsc = os.path.join( |
270 | archive_root, 'pool', 'main', 'f', 'foo', 'foo_666.dsc') |
271 | - self.assertEqual("I do not care about sources.", file(dsc).read()) |
272 | + with open(dsc) as dsc_file: |
273 | + self.assertEqual("I do not care about sources.", dsc_file.read()) |
274 | overrides = os.path.join( |
275 | archive_root + '-overrides', distroseries.name + '_main_source') |
276 | - self.assertEqual(dsc, file(overrides).read().rstrip()) |
277 | + with open(overrides) as overrides_file: |
278 | + self.assertEqual(dsc, overrides_file.read().rstrip()) |
279 | self.assertTrue(path_exists( |
280 | dists_root, distroseries.name, 'main', 'source', 'Sources.gz')) |
281 | self.assertTrue(path_exists( |
282 | diff --git a/lib/lp/services/config/__init__.py b/lib/lp/services/config/__init__.py |
283 | index ae94cf0..3c6ad51 100644 |
284 | --- a/lib/lp/services/config/__init__.py |
285 | +++ b/lib/lp/services/config/__init__.py |
286 | @@ -73,8 +73,8 @@ def find_instance_name(): |
287 | if instance_name is None: |
288 | for config_lookup_file in CONFIG_LOOKUP_FILES: |
289 | if os.path.exists(config_lookup_file): |
290 | - instance_name = file( |
291 | - config_lookup_file, 'r').read()[:80].strip() |
292 | + with open(config_lookup_file) as f: |
293 | + instance_name = f.read()[:80].strip() |
294 | break |
295 | |
296 | # Of instance_name falls back for developers. |
297 | diff --git a/lib/lp/services/profile/mem.py b/lib/lp/services/profile/mem.py |
298 | index 19bc988..73ea820 100644 |
299 | --- a/lib/lp/services/profile/mem.py |
300 | +++ b/lib/lp/services/profile/mem.py |
301 | @@ -207,7 +207,7 @@ def readCounts(file, marker=None): |
302 | |
303 | |
304 | def logInThread(n=30): |
305 | - reflog = file('/tmp/refs.log', 'w') |
306 | + reflog = open('/tmp/refs.log', 'w') |
307 | t = threading.Thread(target=_logRefsEverySecond, args=(reflog, n)) |
308 | # Allow process to exit without explicitly stopping thread. |
309 | t.daemon = True |
310 | diff --git a/lib/lp/services/profile/profile.py b/lib/lp/services/profile/profile.py |
311 | index c44778a..400f96e 100644 |
312 | --- a/lib/lp/services/profile/profile.py |
313 | +++ b/lib/lp/services/profile/profile.py |
314 | @@ -354,15 +354,14 @@ def end_request(event): |
315 | del prof_stats |
316 | # Dump memory profiling info. |
317 | if 'memory_profile_start' in actions: |
318 | - log = file(config.profiling.memory_profile_log, 'a') |
319 | - vss_start, rss_start = actions.pop('memory_profile_start') |
320 | - vss_end, rss_end = memory(), resident() |
321 | - if oopsid is None: |
322 | - oopsid = '-' |
323 | - log.write('%s %s %s %f %d %d %d %d\n' % ( |
324 | - timestamp, pageid, oopsid, da.get_request_duration(), |
325 | - vss_start, rss_start, vss_end, rss_end)) |
326 | - log.close() |
327 | + with open(config.profiling.memory_profile_log, 'a') as log: |
328 | + vss_start, rss_start = actions.pop('memory_profile_start') |
329 | + vss_end, rss_end = memory(), resident() |
330 | + if oopsid is None: |
331 | + oopsid = '-' |
332 | + log.write('%s %s %s %f %d %d %d %d\n' % ( |
333 | + timestamp, pageid, oopsid, da.get_request_duration(), |
334 | + vss_start, rss_start, vss_end, rss_end)) |
335 | trace = None |
336 | if 'sql' in actions: |
337 | trace = da.stop_sql_logging() or () |
338 | diff --git a/lib/lp/services/profile/profiling.txt b/lib/lp/services/profile/profiling.txt |
339 | index 9a16054..844698b 100644 |
340 | --- a/lib/lp/services/profile/profiling.txt |
341 | +++ b/lib/lp/services/profile/profiling.txt |
342 | @@ -195,10 +195,10 @@ log file. |
343 | ... profile_request: False |
344 | ... memory_profile_log: %s""" % memory_profile_log)) |
345 | >>> response = http('GET / HTTP/1.0') |
346 | - >>> memory_profile_fh = file(memory_profile_log) |
347 | - >>> (timestamp, page_id, oops_id, duration, |
348 | - ... start_vss, start_rss, end_vss, end_rss) = ( |
349 | - ... memory_profile_fh.readline().split()) |
350 | + >>> with open(memory_profile_log) as memory_profile_fh: |
351 | + ... (timestamp, page_id, oops_id, duration, |
352 | + ... start_vss, start_rss, end_vss, end_rss) = ( |
353 | + ... memory_profile_fh.readline().split()) |
354 | >>> print timestamp |
355 | 20... |
356 | >>> print oops_id |
357 | diff --git a/lib/lp/services/sitesearch/tests/test_testservices.py b/lib/lp/services/sitesearch/tests/test_testservices.py |
358 | index 83130c0..2474003 100644 |
359 | --- a/lib/lp/services/sitesearch/tests/test_testservices.py |
360 | +++ b/lib/lp/services/sitesearch/tests/test_testservices.py |
361 | @@ -36,7 +36,7 @@ class TestServiceUtilities(WithScenarios, unittest.TestCase): |
362 | |
363 | # Create a stale/bogus PID file. |
364 | filepath = pidfile_path(self.testservice.service_name) |
365 | - with file(filepath, 'w') as pidfile: |
366 | + with open(filepath, 'w') as pidfile: |
367 | pidfile.write(str(bogus_pid)) |
368 | |
369 | # The PID clean-up code should silently remove the file and return. |
370 | diff --git a/lib/lp/services/sitesearch/testservice.py b/lib/lp/services/sitesearch/testservice.py |
371 | index 72c3aeb..430bb85 100644 |
372 | --- a/lib/lp/services/sitesearch/testservice.py |
373 | +++ b/lib/lp/services/sitesearch/testservice.py |
374 | @@ -49,7 +49,8 @@ class RequestHandler(BaseHTTPRequestHandler): |
375 | self.end_headers() |
376 | |
377 | filepath = os.path.join(self.content_dir, filename) |
378 | - content_body = file(filepath).read() |
379 | + with open(filepath) as f: |
380 | + content_body = f.read() |
381 | self.wfile.write(content_body) |
382 | |
383 | def log_message(self, format, *args): |
384 | @@ -66,12 +67,13 @@ class RequestHandler(BaseHTTPRequestHandler): |
385 | def url_to_file_map(mapfile): |
386 | """Return our URL-to-file mapping as a dictionary.""" |
387 | mapping = {} |
388 | - for line in file(mapfile): |
389 | - if line.startswith('#') or len(line.strip()) == 0: |
390 | - # Skip comments and blank lines. |
391 | - continue |
392 | - url, fname = line.split() |
393 | - mapping[url.strip()] = fname.strip() |
394 | + with open(mapfile) as f: |
395 | + for line in f: |
396 | + if line.startswith('#') or len(line.strip()) == 0: |
397 | + # Skip comments and blank lines. |
398 | + continue |
399 | + url, fname = line.split() |
400 | + mapping[url.strip()] = fname.strip() |
401 | |
402 | return mapping |
403 | |
404 | diff --git a/lib/lp/services/tests/test_utils.py b/lib/lp/services/tests/test_utils.py |
405 | index e753a82..02922bb 100644 |
406 | --- a/lib/lp/services/tests/test_utils.py |
407 | +++ b/lib/lp/services/tests/test_utils.py |
408 | @@ -346,7 +346,8 @@ class TestFileExists(TestCase): |
409 | self.useTempDir() |
410 | |
411 | def test_finds_file(self): |
412 | - file("a-real-file.txt", "w").write("Here I am.") |
413 | + with open("a-real-file.txt", "w") as f: |
414 | + f.write("Here I am.") |
415 | self.assertTrue(file_exists("a-real-file.txt")) |
416 | |
417 | def test_finds_directory(self): |
418 | diff --git a/lib/lp/soyuz/scripts/gina/changelog.py b/lib/lp/soyuz/scripts/gina/changelog.py |
419 | index ab6ce3a..bd637da 100644 |
420 | --- a/lib/lp/soyuz/scripts/gina/changelog.py |
421 | +++ b/lib/lp/soyuz/scripts/gina/changelog.py |
422 | @@ -100,4 +100,5 @@ def parse_changelog(changelines): |
423 | |
424 | if __name__ == '__main__': |
425 | import pprint |
426 | - pprint.pprint(parse_changelog(file(sys.argv[1], "r"))) |
427 | + with open(sys.argv[1]) as f: |
428 | + pprint.pprint(parse_changelog(f)) |
429 | diff --git a/lib/lp/translations/scripts/language_pack.py b/lib/lp/translations/scripts/language_pack.py |
430 | index 34d28c7..5e38bc8 100644 |
431 | --- a/lib/lp/translations/scripts/language_pack.py |
432 | +++ b/lib/lp/translations/scripts/language_pack.py |
433 | @@ -251,9 +251,12 @@ def export_language_pack(distribution_name, series_name, logger, |
434 | if output_file == '-': |
435 | output_filehandle = sys.stdout |
436 | else: |
437 | - output_filehandle = file(output_file, 'wb') |
438 | + output_filehandle = open(output_file, 'wb') |
439 | |
440 | copyfileobj(filehandle, output_filehandle) |
441 | + |
442 | + if output_filehandle != sys.stdout: |
443 | + output_filehandle.close() |
444 | else: |
445 | # Upload the tarball to the librarian. |
446 | |
447 | diff --git a/lib/lp/translations/scripts/validate_translations_file.py b/lib/lp/translations/scripts/validate_translations_file.py |
448 | index bcf2ff6..d828dc3 100644 |
449 | --- a/lib/lp/translations/scripts/validate_translations_file.py |
450 | +++ b/lib/lp/translations/scripts/validate_translations_file.py |
451 | @@ -128,5 +128,6 @@ class ValidateTranslationsFile: |
452 | :param filename: Name of a file to read. |
453 | :return: Whether the file was parsed successfully. |
454 | """ |
455 | - content = file(filename).read() |
456 | + with open(filename) as f: |
457 | + content = f.read() |
458 | return self._validateContent(filename, content) |
459 | diff --git a/lib/lp/translations/tests/test_translationtemplatesbuildbehaviour.py b/lib/lp/translations/tests/test_translationtemplatesbuildbehaviour.py |
460 | index 32dd553..525ff4a 100644 |
461 | --- a/lib/lp/translations/tests/test_translationtemplatesbuildbehaviour.py |
462 | +++ b/lib/lp/translations/tests/test_translationtemplatesbuildbehaviour.py |
463 | @@ -297,8 +297,8 @@ class TestTTBuildBehaviourTranslationsQueue( |
464 | def test_uploadTarball(self): |
465 | # Files from the tarball end up in the import queue. |
466 | behaviour = self.makeBehaviour() |
467 | - behaviour._uploadTarball( |
468 | - self.branch, file(self.dummy_tar).read(), None) |
469 | + with open(self.dummy_tar) as f: |
470 | + behaviour._uploadTarball(self.branch, f.read(), None) |
471 | |
472 | entries = self.queue.getAllEntries(target=self.productseries) |
473 | expected_templates = [ |
474 | @@ -313,8 +313,8 @@ class TestTTBuildBehaviourTranslationsQueue( |
475 | def test_uploadTarball_approved(self): |
476 | # Uploaded template files are automatically approved. |
477 | behaviour = self.makeBehaviour() |
478 | - behaviour._uploadTarball( |
479 | - self.branch, file(self.dummy_tar).read(), None) |
480 | + with open(self.dummy_tar) as f: |
481 | + behaviour._uploadTarball(self.branch, f.read(), None) |
482 | |
483 | entries = self.queue.getAllEntries(target=self.productseries) |
484 | statuses = [entry.status for entry in entries] |
485 | @@ -324,8 +324,8 @@ class TestTTBuildBehaviourTranslationsQueue( |
486 | def test_uploadTarball_importer(self): |
487 | # Files from the tarball are owned by the branch owner. |
488 | behaviour = self.makeBehaviour() |
489 | - behaviour._uploadTarball( |
490 | - self.branch, file(self.dummy_tar).read(), None) |
491 | + with open(self.dummy_tar) as f: |
492 | + behaviour._uploadTarball(self.branch, f.read(), None) |
493 | |
494 | entries = self.queue.getAllEntries(target=self.productseries) |
495 | self.assertEqual(self.branch.owner, entries[0].importer) |
496 | diff --git a/scripts/process-one-mail.py b/scripts/process-one-mail.py |
497 | index c2caab6..420f0f1 100755 |
498 | --- a/scripts/process-one-mail.py |
499 | +++ b/scripts/process-one-mail.py |
500 | @@ -30,11 +30,13 @@ class ProcessMail(LaunchpadScript): |
501 | # NB: This somewhat duplicates handleMail, but there it's mixed in |
502 | # with handling a mailbox, which we're avoiding here. |
503 | if len(self.args) >= 1: |
504 | - from_file = file(self.args[0], 'rb') |
505 | + from_file = open(self.args[0], 'rb') |
506 | else: |
507 | from_file = sys.stdin |
508 | self.logger.debug("reading message from %r" % (from_file,)) |
509 | raw_mail = from_file.read() |
510 | + if from_file != sys.stdin: |
511 | + from_file.close() |
512 | self.logger.debug("got %d bytes" % len(raw_mail)) |
513 | file_alias = save_mail_to_librarian(raw_mail) |
514 | self.logger.debug("saved to librarian as %r" % (file_alias,)) |
515 | diff --git a/utilities/audit-security-settings.py b/utilities/audit-security-settings.py |
516 | index b41238d..dfdb314 100755 |
517 | --- a/utilities/audit-security-settings.py |
518 | +++ b/utilities/audit-security-settings.py |
519 | @@ -28,10 +28,12 @@ SECURITY_PATH = os.path.join( |
520 | |
521 | |
522 | def main(): |
523 | - data = file(SECURITY_PATH).read() |
524 | + with open(SECURITY_PATH) as f: |
525 | + data = f.read() |
526 | auditor = SettingsAuditor(data) |
527 | settings = auditor.audit() |
528 | - file(SECURITY_PATH, 'w').write(settings) |
529 | + with open(SECURITY_PATH, 'w') as f: |
530 | + f.write(settings) |
531 | print(auditor.error_data) |
532 | |
533 | if __name__ == '__main__': |
534 | diff --git a/utilities/format-imports b/utilities/format-imports |
535 | index 78ecd51..b408b86 100755 |
536 | --- a/utilities/format-imports |
537 | +++ b/utilities/format-imports |
538 | @@ -347,7 +347,8 @@ def format_imports(imports): |
539 | |
540 | def reformat_importsection(filename): |
541 | """Replace the given file with a reformatted version of it.""" |
542 | - pyfile = file(filename).read() |
543 | + with open(filename) as f: |
544 | + pyfile = f.read() |
545 | import_start, import_end = find_imports_section(pyfile) |
546 | if import_start is None: |
547 | # Skip files with no import section. |
548 | diff --git a/utilities/update-copyright b/utilities/update-copyright |
549 | index 5df0212..b714447 100755 |
550 | --- a/utilities/update-copyright |
551 | +++ b/utilities/update-copyright |
552 | @@ -57,7 +57,8 @@ def update_files(filenames): |
553 | if not os.access(filename, os.W_OK): |
554 | print("Skipped: %s is not writeable." % filename) |
555 | continue |
556 | - lines = file(filename).readlines() |
557 | + with open(filename) as f: |
558 | + lines = f.readlines() |
559 | changed = update_copyright(lines) |
560 | if changed: |
561 | newfile = open(filename, 'w') |
LGTM