Repository: ioos/compliance-checker

Issue:
Running compliace-checker on Windows returns a lxml.etree.XMLSyntaxError
@lukecampbell and @daf,

@abirger reported this on   the conda-recipes repo (https://github.com/ioos/conda-recipes/pull/242), but I think we should open an issue here.  Here is his first attempt to run `compliance-checker` (CC) on Windows:

``` shell
[compliance_checker] C:\NOAA\IOOS\SOS-Compliance-Tester\CARA_test\netCDF files>compliance-checker --test=acdd Copy.nc
Running Compliance Checker on the dataset from: Copy.nc
Traceback (most recent call last):
  File "C:\Users\Alex Birger\Anaconda\envs\compliance_checker\Scripts\compliance-checker-script.py", line 5, in <module>
    sys.exit(main())
  File "C:\Users\Alex Birger\Anaconda\envs\compliance_checker\Scripts\cchecker.py", line 21, in main
    args.criteria)
  File "C:\Users\Alex Birger\Anaconda\envs\compliance_checker\lib\site-packages\compliance_checker\runner.py", line 42, in run_checker
    ds = cs.load_dataset(ds_loc)
  File "C:\Users\Alex Birger\Anaconda\envs\compliance_checker\lib\site-packages\compliance_checker\suite.py", line 343, in load_dataset
    xml_doc = ET.fromstring(str(doc))
  File "lxml.etree.pyx", line 3103, in lxml.etree.fromstring (src\lxml\lxml.etree.c:70569)
  File "parser.pxi", line 1828, in lxml.etree._parseMemoryDocument (src\lxml\lxml.etree.c:106403)
  File "parser.pxi", line 1716, in lxml.etree._parseDoc (src\lxml\lxml.etree.c:105194)
  File "parser.pxi", line 1086, in lxml.etree._BaseParser._parseDoc (src\lxml\lxml.etree.c:99876)
  File "parser.pxi", line 580, in lxml.etree._ParserContext._handleParseResultDoc (src\lxml\lxml.etree.c:94350)
  File "parser.pxi", line 690, in lxml.etree._handleParseResult (src\lxml\lxml.etree.c:95786)
  File "parser.pxi", line 620, in lxml.etree._raiseParseError (src\lxml\lxml.etree.c:94853)
lxml.etree.XMLSyntaxError: Start tag expected, '<' not found, line 1, column 1

[compliance_checker] C:\NOAA\IOOS\SOS-Compliance-Tester\CARA_test\netCDF files>
```

I tested the same CC version on the same file on Linux.  The offending code is located at an `if clause` that never gets executed on Linux, where everything works fine BTW. I do not have a Windows machine to investigate this further now, but it seems that this bug is unrelated to the units module change.

PS: Bare in mind that is happens using our conda package, so it could be conda's default `lxml` fault.



Discussion:
Looks like the logic used to determine if a file is binary or not (based on http://stackoverflow.com/a/7392391/84732) is not working properly on windows (I shouldn't be surprised).  I now have access to a Windows machine so I'll see what I can do.

Thanks @daf!


Resolving patch:
diff --git a/compliance_checker/suite.py b/compliance_checker/suite.py
--- a/compliance_checker/suite.py
+++ b/compliance_checker/suite.py
@@ -330,7 +330,7 @@ def load_dataset(self, ds_str):
             textchars = ''.join(map(chr, [7,8,9,10,12,13,27] + range(0x20, 0x100)))
             is_binary_string = lambda bytes: bool(bytes.translate(None, textchars))
 
-            with open(ds_str) as f:
+            with open(ds_str, 'rb') as f:
                 first_chunk = f.read(1024)
                 if is_binary_string(first_chunk):
                     # likely netcdf file


Test patch: