Merge lp:~torkvemada/quickly/templatetools into lp:quickly

Proposed by Vsevolod Velichko
Status: Needs review
Proposed branch: lp:~torkvemada/quickly/templatetools
Merge into: lp:quickly
Diff against target: 65 lines (+39/-9)
1 file modified
quickly/templatetools.py (+39/-9)
To merge this branch: bzr merge lp:~torkvemada/quickly/templatetools
Reviewer Review Type Date Requested Status
Quickly Developers Pending
Review via email: mp+114156@code.launchpad.net

Description of the change

Fix for the update_file_contents method, since it is buggy and sometimes creates corrupted files (I faced it when quickly was generating my debian/control depends line).

To post a comment you must log in.
lp:~torkvemada/quickly/templatetools updated
677. By Vsevolod Velichko

templatetools.update_file_content fix for it to set the file coding comment if needed

Unmerged revisions

677. By Vsevolod Velichko

templatetools.update_file_content fix for it to set the file coding comment if needed

676. By Vsevolod Velichko

Fixed update_file_contents.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'quickly/templatetools.py'
2--- quickly/templatetools.py 2011-06-06 10:14:40 +0000
3+++ quickly/templatetools.py 2012-07-13 19:00:30 +0000
4@@ -108,23 +108,53 @@
5
6 skip_until_end_found = False
7 marker_found = False
8+ prepend_coding_comment = False
9+ if filename.endswith('.py'):
10+ try:
11+ replacing_content.decode('ascii')
12+ except UnicodeDecodeError:
13+ prepend_coding_comment = True
14+
15 try:
16 filename = os.path.abspath(filename)
17 ftarget_file_name = file(filename, 'r')
18 ftarget_file_name_out = file(ftarget_file_name.name + '.new', 'w')
19+ linenum = 0
20 for line in ftarget_file_name:
21+ # If we already have some coding sign, don't add
22+ if prepend_coding_comment:
23+ linenum += 1
24+ if line.startswith('# -*- ') and 'coding: ' in line:
25+ prepend_coding_comment = False
26+ # If we have initial hashbang instruction, add on second line
27+ elif linenum == 1 and line.startswith('#!'):
28+ pass
29+ else:
30+ ftarget_file_name_out.write('# -*- coding: utf-8 -*-\n')
31+ prepend_coding_comment = False
32+
33 # seek if we have to add something
34- if start_marker in line:
35- skip_until_end_found = True
36- marker_found = True
37- ftarget_file_name_out.write(replacing_content)
38-
39- if end_marker in line:
40- skip_until_end_found = False
41-
42- if not skip_until_end_found:
43+ if skip_until_end_found or not marker_found:
44+ try:
45+ i = line.index(start_marker)
46+ skip_until_end_found = True
47+ marker_found = True
48+ ftarget_file_name_out.write(line[:i])
49+ ftarget_file_name_out.write(replacing_content)
50+ except ValueError:
51+ pass
52+ try:
53+ i = line.index(end_marker)
54+ if skip_until_end_found:
55+ ftarget_file_name_out.write(line[i:])
56+ skip_until_end_found = False
57+ except ValueError:
58+ if not skip_until_end_found:
59+ ftarget_file_name_out.write(line)
60+ else:
61 ftarget_file_name_out.write(line)
62
63+
64 ftarget_file_name.close()
65 ftarget_file_name_out.close()
66

Subscribers

People subscribed via source and target branches