2010/3/29 Jean Parpaillon <[email protected]>:
> Le vendredi 26 mars 2010 à 12:59 -0300, Lucas Meneghel Rodrigues a
> écrit :
>> On Fri, Mar 26, 2010 at 10:18 AM, Jean Parpaillon
>> <[email protected]> wrote:
>> > Add a function to get file mimetype
>> > Use mimetype to check if a file is to be cat|zcat|bzcat (instead of
>> > suffix)
>>
>> Actually, we could use the standard lib module mimetypes to do this
>> task. the patch could be rewritten as:
>>
>> - if file.endswith('.bz2'):
>> + if mimetypes.guess_type(file) == ('application/x-tar', 'bzip2'):
>> cat = 'bzcat'
>> - elif (file.endswith('.gz') or file.endswith('.tgz')):
>> + if mimetypes.guess_type(file) == ('application/x-tar', 'gzip'):
>> cat = 'zcat'
>> else:
>> cat = 'cat'
>>
>> Provided the mimetypes module is also imported. Since the module
>> interacts with libmagic and the mimetypes database files directly,
>> it's a better solution.
>>
>
> Hi,
> On my system (Python 2.5.4) and amongst official documentation,
> mimetypes use file name and extensions to get the mimetypes, and not
> libmagic.
Ok, sorry, my fault, I confused the standard library mimetypes with
another 3rd party python package, that does use libmagic *and* the
mime database.
> And for tar.bz2 file, output is: ("application/x-tar", None)
Strange... On my python 2.6.2 system I get:
In [3]: mimetypes.guess_type("foo.tar.bz2")
Out[3]: ('application/x-tar', 'bzip2')
I did expect it to behave exactly the same in older python versions,
but turns out I was wrong.
> Where is the trick ?
Let's drop my suggestion of using mimetypes. I just looked into the
libmagic wrapper and it looks like it could be improved and considered
for autotest inclusion. Let me spend some time on it, will send a
patch soon.
> Regards,
> Jean
>
>> > ---
>> > client/bin/base_utils.py | 4 ++--
>> > client/common_lib/utils.py | 6 ++++++
>> > 2 files changed, 8 insertions(+), 2 deletions(-)
>> >
>> > diff --git a/client/bin/base_utils.py b/client/bin/base_utils.py
>> > index b4f5646..0a96efb 100644
>> > --- a/client/bin/base_utils.py
>> > +++ b/client/bin/base_utils.py
>> > @@ -47,9 +47,9 @@ def cat_file_to_cmd(file, command, ignore_status=0,
>> > return_output=False):
>> > else:
>> > run_cmd = utils.system
>> >
>> > - if file.endswith('.bz2'):
>> > + if utils.get_file_mime(file) == 'application/x-bzip2':
>> > cat = 'bzcat'
>> > - elif (file.endswith('.gz') or file.endswith('.tgz')):
>> > + elif utils.get_file_mime(file) == 'application/x-gzip':
>> > cat = 'zcat'
>> > else:
>> > cat = 'cat'
>> > diff --git a/client/common_lib/utils.py b/client/common_lib/utils.py
>> > index ddbb34b..7d5413f 100644
>> > --- a/client/common_lib/utils.py
>> > +++ b/client/common_lib/utils.py
>> > @@ -1191,3 +1191,9 @@ def sh_escape(command):
>> > command = command.replace('"', r'\"')
>> > command = command.replace('`', r'\`')
>> > return command
>> > +
>> > +
>> > +def get_file_mime(path):
>> > + """ Return path mime type
>> > + """
>> > + return system_output("file --mime --brief %s" % path).strip()
>> > --
>> > 1.7.0
>> >
>> > _______________________________________________
>> > Autotest mailing list
>> > [email protected]
>> > http://test.kernel.org/cgi-bin/mailman/listinfo/autotest
>> >
>>
>>
>>
>
> --
> Jean Parpaillon - Kerlabs
> HPC System Architect and Developer
> Bâtiment Germanium
> 80 avenue des buttes de Coësmes
> 35700 Rennes - France
> Tel.: +33 6 80 32 73 85
> web : http://kerlabs.com/
> im: [email protected]
>
> _______________________________________________
> Autotest mailing list
> [email protected]
> http://test.kernel.org/cgi-bin/mailman/listinfo/autotest
>
>
--
Lucas
_______________________________________________
Autotest mailing list
[email protected]
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest