You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by "Ditrick, Gregory" <gr...@fmr.com> on 2007/08/20 21:01:00 UTC

Testing a file if it is binary or not.

This may not be for this forum, but I'm moving a PVCS repository to a
CVS repository.  I'm using ant to do this with cvs and pvcs tasks.  I
have to test a if a file is binary or not before adding it to CVS, but I
have to do this on a Windows box.  It is easy on Unix (file <filename> |
grep...) or using <ac:if><scriptcondition> or <ac:shellscript>.  Does
anyone have any ideas other than using the files extension as a guide
and praying that you have them correct and don't miss any?  Yuck!

CVS requires you to designate the file type as binary fro binary files
when adding the file.  Other SCM usually handle this internally.

I really hate PVCS and CVS.  I wish they would be switching SVN or
Perforce.  I prefer Perforce over all SCMs.  I tried my best to convince
the company I'm contracting at to switch to Perforce, but too much of an
up hill battle.


Thanks in advance,

GregD
6-8309



RE: Testing a file if it is binary or not.

Posted by "Rebhan, Gilbert" <Gi...@huk-coburg.de>.
Hi,

-----Original Message-----
From: Ditrick, Gregory [mailto:gregory.ditrick@fmr.com] 
Sent: Monday, August 20, 2007 9:01 PM
To: Ant Users List
Subject: Testing a file if it is binary or not.

/*
Does anyone have any ideas other than using the files extension as a
guide
and praying that you have them correct and don't miss any?  Yuck!

CVS requires you to designate the file type as binary fro binary files
when adding the file.  Other SCM usually handle this internally.
*/

Subversion does it like that =
http://subversion.tigris.org/faq.html#binary-files

so maybe =

<target name="init">
  
    <script language="ruby">
      <![CDATA[
      class File
        def self.binary?(name)
          ascii = total = 0
            File.open(name, "rb") { |io| io.read(1024) }.each_byte do
|c|
              total += 1; 
              ascii +=1 if c >= 128  or c == 0
            end
            ascii.to_f / total.to_f > 0.15
        end
      end
      
      puts File.binary?("Y:/bla.zip")
      
      File.binary?("Y:/bla.zip") ? 
      ($project.setProperty "isbinary", "true") : ($project.setProperty
"isascii", "true")
      ]]>
    </script>			
  
  </target>
  
    <target name="main" depends="init">
    <echo>
      $${isbinary} == ${isbinary}
      $${isascii}  == ${isascii}
    </echo>
    </target>


init:
   [script] true
main:
     [echo] ${isbinary} == true
     [echo] ${isascii}  == ${isascii}
BUILD SUCCESSFUL
Total time: 3 seconds




Regards, Gilbert




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: Testing a file if it is binary or not.

Posted by Robert Clark <ro...@quest.com>.
On Monday August 20, 2007, "David Weintraub" <qa...@gmail.com> 
wrote:
> I would use a Perl script to read the first 8000 or so bytes of a
> file and look for characters outside of what you'd expect to be
> ASCII range.

Using Perl, the -T (for text) and -B (for binary) flags do pretty much 
exactly that and work really well. For example:

$myfile = "some.file"

if ( -B "$myfile" ) {
    # file is binary
}
else {
    # file is text
}

- Rob





---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: Testing a file if it is binary or not.

Posted by David Weintraub <qa...@gmail.com>.
I certainly wouldn't do this in Ant!

I would use a Perl script to read the first 8000 or so bytes of a file
and look for characters outside of what you'd expect to be ASCII
range.

Probably do a "cvs import" first, then go back through and see if
there are any files that were added that were binary. If there are,
you'd to a "cvs admin" and turn on the -kb flag.

On 8/20/07, Ditrick, Gregory <gr...@fmr.com> wrote:
> This may not be for this forum, but I'm moving a PVCS repository to a
> CVS repository.  I'm using ant to do this with cvs and pvcs tasks.  I
> have to test a if a file is binary or not before adding it to CVS, but I
> have to do this on a Windows box.  It is easy on Unix (file <filename> |
> grep...) or using <ac:if><scriptcondition> or <ac:shellscript>.  Does
> anyone have any ideas other than using the files extension as a guide
> and praying that you have them correct and don't miss any?  Yuck!
>
> CVS requires you to designate the file type as binary fro binary files
> when adding the file.  Other SCM usually handle this internally.
>
> I really hate PVCS and CVS.  I wish they would be switching SVN or
> Perforce.  I prefer Perforce over all SCMs.  I tried my best to convince
> the company I'm contracting at to switch to Perforce, but too much of an
> up hill battle.
>
>
> Thanks in advance,
>
> GregD
> 6-8309
>
>
>


-- 
--
David Weintraub
qazwart@gmail.com

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org