Bug 63528

Summary: Complete functions in filesystem.py
Product: WebKit Reporter: Roland Steiner <rolandsteiner>
Component: Tools / TestsAssignee: Roland Steiner <rolandsteiner>
Status: RESOLVED FIXED    
Severity: Normal CC: dpranke, tony
Priority: P3    
Version: 528+ (Nightly build)   
Hardware: All   
OS: All   
Bug Depends on: 63514    
Bug Blocks: 63527, 61773, 64149    
Attachments:
Description Flags
Patch
none
patch, reduced
none
patch, reduced
none
Patch tony: review+

Description Roland Steiner 2011-06-28 05:15:48 PDT
E.g., there exists a function open_text_file_for_writing(), but not the corresponding open_text_file_for_reading().
Comment 1 Roland Steiner 2011-06-28 05:26:14 PDT
Created attachment 98902 [details]
Patch

Patch probably also needs some new tests, but 'testing the waters' first... ;)
Comment 2 Roland Steiner 2011-06-28 18:00:37 PDT
I should add that this is largely similar to the patch in https://bugs.webkit.org/show_bug.cgi?id=63419 , except without the 'encoding' parameters, plus a bit more polishing.

The main reason for this patch is to provide functions needed in https://bugs.webkit.org/show_bug.cgi?id=63527 .
Comment 3 Tony Chang 2011-06-30 10:35:06 PDT
Comment on attachment 98902 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=98902&action=review

> Tools/Scripts/webkitpy/common/system/filesystem.py:232
> +    def open_text_file_for_appending(self, path):
> +        """Returns a file handle suitable for appending to."""
> +        return codecs.open(path, 'a', 'utf8')

Do you need all these methods?  I think we should only add the functions you need.

> Tools/Scripts/webkitpy/common/system/filesystem_mock.py:340
> +    def __init__(self, fs, path):
> +        WritableBinaryFileObject.__init__(self, fs, path)

I believe this will be done implicitly even if you don't declare __init__ here.

> Tools/Scripts/webkitpy/common/system/filesystem_mock.py:373
> +    def __init__(self, fs, path):
> +        ReadableBinaryFileObject.__init__(self, fs, path)

This too.
Comment 4 Roland Steiner 2011-06-30 18:27:37 PDT
(In reply to comment #3)
> Do you need all these methods?  I think we should only add the functions you need.

Good question ;) I won't be needing the 'append' functions, but otherwise I can't say yet (have to work my way through the rest of the project files first). OTOH I felt it's nice to have a complete set without having to extend it in the future.

I guess I'll leave this as a "work-in-progress" patch and update it once I can narrow it down.
Comment 5 Tony Chang 2011-07-01 09:51:41 PDT
Comment on attachment 98902 [details]
Patch

Sounds good to me.  I'm going to remove the r? flag to get it out of the review queue.
Comment 6 Roland Steiner 2011-07-06 21:28:23 PDT
Created attachment 99927 [details]
patch, reduced

Somewhat reduced patch, removed binary file functions that I probably won't be using
Comment 7 Roland Steiner 2011-07-06 22:16:27 PDT
Created attachment 99935 [details]
patch, reduced

Forgot to include unit test in last patch.
Comment 8 Tony Chang 2011-07-07 09:43:32 PDT
Comment on attachment 99935 [details]
patch, reduced

View in context: https://bugs.webkit.org/attachment.cgi?id=99935&action=review

Mostly just some style nits.

> Tools/Scripts/webkitpy/common/system/filesystem_mock.py:-317
> -        if path not in self.fs.files or not append:

Why is the check for 'path not in self.fs.files' no longer needed?

> Tools/Scripts/webkitpy/common/system/filesystem_mock.py:340
> +    def __init__(self, fs, path):
> +        WritableBinaryFileObject.__init__(self, fs, path)

Nit: I don't think we need this, it's called implicitly.

> Tools/Scripts/webkitpy/common/system/filesystem_unittest.py:149
> +        unicode_text_string = u'ŪnÄ­cÅde̽'

It might be safer to have this be unicode escaped (e.g., u'\uXXXX\uXXXX') rather than trying to inline directly.  I think we expect all our python files to be utf8.
Comment 9 Dirk Pranke 2011-07-07 10:21:00 PDT
Comment on attachment 99935 [details]
patch, reduced

View in context: https://bugs.webkit.org/attachment.cgi?id=99935&action=review

>> Tools/Scripts/webkitpy/common/system/filesystem_mock.py:-317
>> -        if path not in self.fs.files or not append:
> 
> Why is the check for 'path not in self.fs.files' no longer needed?

I think the 'path not in self.fs.files' part was just wrong and should never have been there. When it was there, if the file already existed (and had content), that content would've been preserved, which I think is probably the wrong thing to do in most if not all cases.
Comment 10 Roland Steiner 2011-07-07 19:33:37 PDT
Created attachment 100067 [details]
Patch

Escaped Unicode strings, using super() for initialization.
Comment 11 Roland Steiner 2011-07-07 19:41:50 PDT
(In reply to comment #8)
> > Tools/Scripts/webkitpy/common/system/filesystem_mock.py:-317
> > -        if path not in self.fs.files or not append:
> 
> Why is the check for 'path not in self.fs.files' no longer needed?

AFAICT that was to make sure not to clobber the file contents if 'append' was True. Without the appending case we can always clobber the file contents. And 'append' never was True anyways...
 
> > Tools/Scripts/webkitpy/common/system/filesystem_mock.py:340
> > +    def __init__(self, fs, path):
> > +        WritableBinaryFileObject.__init__(self, fs, path)
> 
> Nit: I don't think we need this, it's called implicitly.

I personally hate having to go hunting for constructor parameters in super-classes, so I would prefer if I could leave it in. Changed the call to use super() in the new patch, though, which should be a bit more idiosyncratic.

> > Tools/Scripts/webkitpy/common/system/filesystem_unittest.py:149
> > +        unicode_text_string = u'ŪnÄ­cÅde̽'
> 
> It might be safer to have this be unicode escaped (e.g., u'\uXXXX\uXXXX') rather than trying to inline directly.  I think we expect all our python files to be utf8.

Heh, who says it wasn't? ;) But I do agree - escaped the string and the original where I copied it from in the new patch.
Comment 12 Tony Chang 2011-07-08 09:55:39 PDT
Comment on attachment 100067 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=100067&action=review

> Tools/Scripts/webkitpy/common/system/filesystem_mock.py:341
> +    def __init__(self, fs, path):
> +        super(WritableTextFileObject, self).__init__(fs, path)

I think super is worse :)  If you insist on having __init__, let's change this back to WritableTextFileObject.__init__(self, fs, path).

I don't like the pass through constructors because if you add a param to the base class, you have to find and fix all the subclasses.  It's easy to miss some because it's not a compile error.  But calling .__init__ explicitly is OK too.
Comment 13 Roland Steiner 2011-07-10 19:00:18 PDT
Committed r90702: <http://trac.webkit.org/changeset/90702>
Comment 14 Roland Steiner 2011-07-10 19:02:17 PDT
(In reply to comment #12)
> I think super is worse :)  If you insist on having __init__, let's change this back to WritableTextFileObject.__init__(self, fs, path).
> 
> I don't like the pass through constructors because if you add a param to the base class, you have to find and fix all the subclasses.  It's easy to miss some because it's not a compile error.  But calling .__init__ explicitly is OK too.

I'm not that set on either form, so I just removed it upon commit ;).
Comment 15 Adam Barth 2011-07-10 19:36:41 PDT
Comment on attachment 100067 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=100067&action=review

>> Tools/Scripts/webkitpy/common/system/filesystem_mock.py:341
>> +        super(WritableTextFileObject, self).__init__(fs, path)
> 
> I think super is worse :)  If you insist on having __init__, let's change this back to WritableTextFileObject.__init__(self, fs, path).
> 
> I don't like the pass through constructors because if you add a param to the base class, you have to find and fix all the subclasses.  It's easy to miss some because it's not a compile error.  But calling .__init__ explicitly is OK too.

Or just omit the __init__.  That's what do usually.