How to python script "Scale B-Bone"?
The pose bones can be found at bpy.data.objects['Armature'].pose.bones with each bone having a length property bones['Bone'].length which represents the visible length in pose mode.

The bbone size that you change when using "Scale BBone" is two properties of the armatures bones which can be accessed through bones['Bone'].bone.bbone_x and bones['Bone'].bone.bbone_z

import bpy

ratio = 0.3
pose_bones = bpy.date.objects['Armature'].pose.bones
b = pose_bones['Bone']

b.bone.bbone_x = 1 / b.length * ratio
b.bone.bbone_z = 1 / b.length * ratio