Permission denied and/or FileNotFoundError using nanopb_generator on Windows

529 views
Skip to first unread message

marco.l...@gmail.com

unread,
Apr 5, 2020, 8:49:31 AM4/5/20
to nanopb
Hello,
I'm new to nanopb, so I'm not sure if i'm doing something wrong here, or if this is just an issue on Windows systems.
When i try to run nanopb_generator.exe via CLI, I get the following error:

C:\nanopb\generator\nanopb_generator.py -D . -I ../proto ../proto/device.proto
C
:\Users\netfi\AppData\Local\Temp\tmpvkpnrowb.pb: Permission denied
Traceback (most recent call last):
 
File "C:\nanopb\generator\nanopb_generator.py", line 2032, in <module>
    main_cli
()
 
File "C:\nanopb\generator\nanopb_generator.py", line 1912, in main_cli
    invoke_protoc
(["protoc"] + include_path + ['-o' + tmp.name, filename])
 
File "C:\nanopb\generator\proto\_utils.py", line 39, in invoke_protoc
   
return subprocess.check_call(argv)
 
File "C:\Python38\lib\subprocess.py", line 364, in check_call
   
raise CalledProcessError(retcode, cmd)
subprocess
.CalledProcessError: Command '['protoc', '-I../proto', '-oC:\\Users\\netfi\\AppData\\Local\\Temp\\tmpvkpnrowb.pb', '../proto/device.proto']' returned non-zero exit status 1.

this happens regardless the .proto file and the build directory. The same happens when invoking the nanopb_generator python script instead of the executable.

The issue comes from this code:
# Load .pb files into memory and compile any .proto files.
fdescs
= {}
include_path
= ['-I%s' % p for p in options.options_path]
for filename in filenames:
   
if filename.endswith(".proto"):
       
with tempfile.NamedTemporaryFile(suffix = ".pb") as tmp:
            invoke_protoc
(["protoc"] + include_path + ['-o' + tmp.name, filename])
            data
= tmp.read()
   
else:
        data
= open(filename, 'rb').read()


According to this ( https://github.com/bravoserver/bravo/issues/111#issuecomment-826990 ) answer, I was able to fix the issue by modifying the nanopb_generator.py script into:

# Load .pb files into memory and compile any .proto files.
fdescs
= {}
include_path
= ['-I%s' % p for p in options.options_path]
for filename in filenames:
   
if filename.endswith(".proto"):
       
with tempfile.NamedTemporaryFile(suffix = ".pb", delete = False) as tmp:
            invoke_protoc
(["protoc"] + include_path + ['-o' + tmp.name, filename])              
            data
= tmp.read()
           
try:
                tmp
.close()
                os
.unlink(tmp.name)
           
except:
               
pass
   
else:
        data
= open(filename, 'rb').read()



I haven't tested it on non-windows platforms, but I guess it should work fine.

Another issue I found is that the generator fails if the output directory does not exist.
For instance, if you have the following tree:
build/
proto
/
proto
/file1.proto
proto
/protobuf/any.proto


Now running the following command from inside the build directory
python <path_to_nanopb_gen>\nanopb_generator.py -D . -I ../proto ../proto/protobuf/any.proto

results in

Writing to .\protobuf/any.pb.h and .\protobuf/any.pb.c
Traceback (most recent call last):
 
File "nanopb\generator\nanopb_generator.py", line 2040, in <module>
    main_cli
()
 
File "nanopb\generator\nanopb_generator.py", line 1951, in main_cli
   
with open(path, 'w') as f:
FileNotFoundError: [Errno 2] No such file or directory: '.\\protobuf/any.pb.h'

Passing -I ../proto/protobuf fixes the issue but generates any.pb.h and any.pb.c files directly inside the build directory, instead of build/protobuf.

A simple fix was to modify the nanopb_generator.py script from this:
for path, data in to_write:
   
with open(path, 'w') as f:
        f
.write(data)

into this:
for path, data in to_write:
    os
.makedirs(os.path.dirname(path), exist_ok=True)
   
with open(path, 'w') as f:
        f
.write(data)


Let me know if these fixes are correct.


Petteri Aimonen

unread,
Apr 5, 2020, 8:54:39 AM4/5/20
to nan...@googlegroups.com
Hi,

There have been a few related commits to the master branch:
https://github.com/nanopb/nanopb/pull/486
https://github.com/nanopb/nanopb/issues/503

Can you try with the newest git master version and see if it works
as-is?

--
Petteri

On Sun, Apr 05, 2020 at 05:49:30AM -0700, marco.l...@gmail.com
wrote:
> --
> You received this message because you are subscribed to the Google Groups "nanopb" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to nanopb+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/nanopb/46ffad37-a90a-4800-b8f1-17b439914526%40googlegroups.com.

marco.l...@gmail.com

unread,
Apr 5, 2020, 9:08:35 AM4/5/20
to nanopb
Hi,
yes it seems to fix the Permission denied error.
The FileNotFound error is still there, though.



Il giorno domenica 5 aprile 2020 14:54:39 UTC+2, Petteri Aimonen ha scritto:
Hi,

There have been a few related commits to the master branch:
https://github.com/nanopb/nanopb/pull/486
https://github.com/nanopb/nanopb/issues/503

Can you try with the newest git master version and see if it works
as-is?

--
Petteri

On Sun, Apr 05, 2020 at 05:49:30AM -0700, marco....@gmail.com
> To unsubscribe from this group and stop receiving emails from it, send an email to nan...@googlegroups.com.

Petteri Aimonen

unread,
Apr 5, 2020, 9:22:35 AM4/5/20
to nan...@googlegroups.com
Hi,

Thanks for testing.
I added the remaining issue to tracker here:
https://github.com/nanopb/nanopb/issues/512

--
Petteri

On Sun, Apr 05, 2020 at 06:08:35AM -0700, marco.l...@gmail.com
wrote:
> Hi,
> yes it seems to fix the Permission denied error.
> The FileNotFound error is still there, though.
>
>
>
> Il giorno domenica 5 aprile 2020 14:54:39 UTC+2, Petteri Aimonen ha scritto:
> >
> > Hi,
> >
> > There have been a few related commits to the master branch:
> > https://github.com/nanopb/nanopb/pull/486
> > https://github.com/nanopb/nanopb/issues/503
> >
> > Can you try with the newest git master version and see if it works
> > as-is?
> >
> > --
> > Petteri
> >
> > On Sun, Apr 05, 2020 at 05:49:30AM -0700, marco....@gmail.com
> > <javascript:>
> > an email to nan...@googlegroups.com <javascript:>.
> > > To view this discussion on the web visit
> > https://groups.google.com/d/msgid/nanopb/46ffad37-a90a-4800-b8f1-17b439914526%40googlegroups.com.
> >
> >
> >
>
> --
> You received this message because you are subscribed to the Google Groups "nanopb" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to nanopb+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/nanopb/22a83ebf-6d53-48df-aeaf-85ee2e7bae9e%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages