how to use pssh python script using Python ProtoBuf library? - protocol-buffers

I am not able to figure out how Python ProtoBuf library is related to use pssh python script in this documentation https://github.com/google/shaka-packager/tree/master/packager/tools/pssh
How can I build the pssh.py script without the proto file?

You can either use the build-script with the shaka-packager, or you can simply generate the python protobuf files from the widevine header proto file directly.
The Protocol Buffers tutorial for python describes nicely how to use protoc to compile the necessary python code.
If you don't want or need any of the other shaka-packager stuff, but just want to use the pssh.py, then you can just modify this part:
# Append the local protobuf location. Use a path relative to the tools/pssh
# folder where this file should be found. This allows the file to be executed
# from any directory.
_pssh_dir = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, os.path.join(_pssh_dir, '../../third_party/protobuf/python'))
# Import the widevine protobuf. Use either Release or Debug.
_proto_path_format = os.path.join(
_pssh_dir, '../../../out/%s/pyproto/packager/media/base')
if os.path.isdir(_proto_path_format % 'Release'):
sys.path.insert(0, _proto_path_format % 'Release')
else:
sys.path.insert(0, _proto_path_format % 'Debug')
try:
import widevine_pssh_data_pb2 # pylint: disable=g-import-not-at-top
except ImportError:
print >> sys.stderr, 'Cannot find proto file, make sure to build first'
raise
Just keep import widevine_pssh_data_pb2 and make sure that the code you generated with protoc is in the path of the pssh.py file. Then it should work nicely.
What are you planning to use the pssh.py for?

Related

protoc-gen-go: unable to determine Go import path for "simple.proto"

I have simple proto file with following content.
syntax="proto3";
package main;
message Person {
string name = 1;
int32 age = 2;
}
I am trying to generate go code for it using protoc. I run:
protoc --go_out=. simple.proto
I receive following error:
protoc-gen-go: unable to determine Go import path for "simple.proto"
Please specify either:
• a "go_package" option in the .proto source file, or
• a "M" argument on the command line.
main.go, go.mod and simple.proto is in the same folder. Both protoc and protoc-gen-go are defined in PATH enviroement.
You forgot to linkedlist the file with it by adding:
option go_package = "./";
You need to linkedlist it first to make it work. It was same issues here
You are missing option go_package.
The name you will give to option go_package will be the name of the package that will be generated by the protoc. By doing so, you can import thus access message fields.
Protoc requires that the package be specified, then the solution is to add
option go_package = "./your-package-name";
to make your file looks like the following:
syntax="proto3";
package main;
option go_package = "./your-package-name";
message Person {
string name = 1;
int32 age = 2;
}
then you can run the command e.g:
protoc -I src/ --go_out=src/ src/simple/simple.proto
where --go_out=src/ specifies where your file will be generated then the relative path to your proto file.
Note: Don't forget to prefix the option go_package with ./
I have a similar problem.
I thought protocol buffers were supposed to be language neutral. If we are adding go_package to the proto files, then if we try to compile these proto files to a different language, we will have to make changes to the files.
The solutions I have seen works if you are looking at generating only go files.
first make sure you installed compiler correctly
sudo apt install protobuf-compiler
sudo apt install golang-goprotobuf-dev
use this command
protoc -I=src/ --go_out=src/ src/simple.proto
-I = IPATH -Specify the directory in which to search for imports
--go_out= output directory

NIFI EXECUTESCRIPT Processor failing - No MODULE FOUND

I am trying to import modules into executescript processor in nifi.
As suggested , I. am giving full path into the modules directory.
example:
Module Directory: /var/lib/nifi/Levenshtein --> which contains necessary files for the script.
Furthermore, In the script also I have set the system path pointing to use that module directory
My code Looks something like this
import re
import datetime
import sys
sys.path.append('/var/lib/nifi/Levenshtein')
import Levenshtein
When I am running the processor with above code it fails.
ERROR: No Module named Levenshtein in at line number 3.
If this particular library is a "native module" (compiled C code), Jython (the Python execution engine used by ExecuteScript) will not be able to load it. ExecuteScript in NiFi using Python can only use pure Python code.
The work-around is to use ExecuteProcess or ExecuteStreamCommand and invoke python <my_script.py> on the command-line, which can handle various Python versions, native modules, etc. This execution will occur outside the JVM and use real Python, not Jython.
To sum up what Andy said, this Levensthein module is written in C and cannot be executed by a Java virtual machine, assuming you are running a Jython implementation.

How load a yaml file inside an ansible custom module

I have an ansible custom module, that have a configuration file in YAML format.
Now the question is how should I load that YAML file inside the module?
NOTE as I understand I can't simply use something like PyYAML since ansible will run my module on the node that it is configuring and maybe that system does not have PyYAML installed.
NOTE Also ansible itself have ansible.parsing.utils.yaml.from_yaml it is not usable by the modules.
So funny as it may sound, I don't know how to load a YAML file in custom ansible module. Please help
It's a great question. It does sound funny and you'd expect a simple answer but as far as i can see these are the facts.
The latest development branch of ansible has /lib/ansible/module_utils/common/yaml.py which can be used by modules because it is under module_utils. see here
If you look at the source code all it's doing is import yaml as _yaml, which you could do yourself inside your custom module. My understanding is this is using PyYAML, which is documented here. (someone correct me if I"m wrong! I don't fully understand the comment in that file stating "preferring the YAML compiled C extensions...")
Anyway, if your target machine does not have PyYAML you can always add a task to ensure its there. e.g.
- name: Install PyYAML python package
pip:
name: pyyaml
and then use it in your own module with:
from yaml import load, dump
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
# ...
data = load(stream, Loader=Loader)
# ...
output = dump(data, Dumper=Dumper)

EYAML syntax validator?

I had an issue with an eyaml file used to store password for DB connection and it seems that I missed a "[".
I want to know if there is a command or script to check eyaml syntax
One thing you can do, if you have python installed somewhere, is install ruamel.yaml (disclaimer: I am the author of that package) and run the following:
python check.py your_eyaml_file
with check.py being:
import sys
from ruamel.yaml import YAML
yaml = YAML()
yaml.load(sys.argv[1])
This will do a safe load of your YAML file and will throw an error if your file doesn't conform to the YAML specification.
There are also online parsers when you can run such checks, but I would not want to use them with sensitive information (encrypted or not).

protoc not generating service stub files

I have just started playing with google proto. When I try to compile proto file present in proto-java example, it does not generate any grpc file.
proto file,
https://github.com/grpc/grpc-java/blob/master/examples/src/main/proto/hello_world.proto
terminal output,
rsonkhla#raman-OptiPlex-9020:~/sandbox/grpc-java/examples$ protoc
--version libprotoc 3.0.0 rsonkhla#raman-OptiPlex-9020:~/sandbox/grpc-java/examples$ protoc
--java_out=test/ -I../../grpc-java/examples ../../grpc-java/examples/src/main/proto/hello_world.proto
rsonkhla#raman-OptiPlex-9020:~/sandbox/grpc-java/examples$ ls -R test/
test/: io
test/io: grpc
test/io/grpc: examples
test/io/grpc/examples: helloworld
test/io/grpc/examples/helloworld: HelloRequest.java
HelloResponse.java HelloWorldProto.java
HelloRequestOrBuilder.java HelloResponseOrBuilder.java
Has anybody else faced this issue?
The command line you are showing does not enable the grpc plugin. You need to specify an _out argument for the grpc plugin, which enables the plugin and specifies where it should output files. Since the plugin is likely not in your PATH, you also need to tell protoc how to find the plugin with --plugin.
So you need to add two arguments:
--plugin=protoc-gen-grpc-java=path/to/protoc-gen-grpc-java --grpc-java_out=path/to/output/dir
For more info, see the gRPC compiler documentation.
You can add these option to your .proto (base on your language) to generate abstract services:
option cc_generic_services = true;
option java_generic_services = true;
option py_generic_services = true;
You can also add --plugin=EXECUTABLE option in your protoc cmd to use custom code generator plugin to generate code more specific to each system, rather than rely on the "abstract" services. Just like Eric's suggestion.

Resources