I presented here a way to integrate a script language inside ABAP. To demonstrate that the these procedures work with other scripting languages too, I take the same methods and use it with Python language. Also I will show that it is possible to use the same ways with more volumminous environments. So I contain a complete Python environment inside ABAP and unpack it at the runtime of my ABAP program and in the context of the ABAP program I create a Python script an use it inside ABAP.
Steps
- Downloading of Python 2.7.5 from http://www.python.org/.
- Installation of Python in the standard default directory C:\Python27.
- Copying of python27.dll from c:\Windows\system32 into C:\Python27.
- Creating of a multi part RAR archive with a part size of max. 8,000,000 bytes. So I get three RAR archives.
- Creating of three ABAP function modules with BinFile2ABAP which contains these RAR archives.
- Creating of three function groups and upload of the function modules inside this groups.
Hint: It is not possible to store more than 65,535 literals in one function group. So it is necessary to create for each function module its own function group. - Upload of zScripXDll function module from here. It contains ScriptX ActiveX library which builds the bridge between ABAP and the scripting language.
With these preparations you build the possibility to use Python inside ABAP.
ABAP Test Program
"-Begin-----------------------------------------------------------------
Program ZSCRIPTX.
"-Constants---------------------------------------------------------
Constants CrLf(2) Type c Value %_CR_LF.
Constants SW_SHOWNORMAL Type i Value 1.
"-Variables---------------------------------------------------------
Data oScriptX Type OLE2_OBJECT.
Data Buffer Type String Value ''.
Data WorkDir Type String Value ''.
Data FileName Type String Value ''.
Data rc Type i Value 0.
Data hFileMap Type i Value 0.
Data RetCode Type String Value ''.
"-Macros------------------------------------------------------------
Define _.
Concatenate Buffer &1 CrLf Into Buffer.
End-Of-Definition.
Define Flush.
Call Function 'AC_SYSTEM_FLUSH' Exceptions Others = 1.
End-Of-Definition.
"-Main--------------------------------------------------------------
Create Object oScriptX 'ScriptX'.
If sy-subrc <> 0 Or oScriptX-Handle = 0 Or oScriptX-Type <> 'OLE2'.
Call Function 'ZSCRIPTXDLL'.
Create Object oScriptX 'ScriptX'.
EndIf.
If sy-subrc = 0 And oScriptX-Handle > 0 And oScriptX-Type = 'OLE2'.
Call Method Of oScriptX 'About'.
Flush.
"-Create multi part archive files-------------------------------
Call Function 'ZPYTHON27PART1RAR'.
Call Function 'ZPYTHON27PART2RAR'.
Call Function 'ZPYTHON27PART3RAR'.
"-Unpack archive------------------------------------------------
Call Method Of oScriptX 'Unrar' Exporting
#1 = 'Python27.part1.rar'.
Flush.
"-Delete multi part archive files-------------------------------
Call Method Of oScriptX 'DeleteFile' Exporting
#1 = 'Python27.part1.rar'.
Call Method Of oScriptX 'DeleteFile' Exporting
#1 = 'Python27.part2.rar'.
Call Method Of oScriptX 'DeleteFile' Exporting
#1 = 'Python27.part3.rar'.
Flush.
Call Method Of oScriptX 'FileMapCreate' = hFileMap
Exporting #1 = 'SAP001' #2 = 64.
Flush.
If hFileMap <> 0.
"-Python Script begin---------------------------------------------------
"-External libraries--------------------------------------------------
_ 'import ctypes'.
_ 'import sys'.
"-Constants-----------------------------------------------------------
_ 'FILE_MAP_ALL_ACCESS = 30'.
"-Main----------------------------------------------------------------
_ 'print "Python version ", sys.version'.
_ 'print "Hello World from Python"'.
_ 'var_inp = raw_input("Enter something: ")'.
_ 'print "You entered: ", var_inp'.
"-Transfer the input to the memory map file---------------------------
_ 'hMMF = ctypes.windll.kernel32.OpenFileMappingA(FILE_MAP_ALL_ACCESS, \'.
_ ' 0, "SAP001")'.
_ 'if hMMF <> 0:'.
_ ' buffer = ctypes.windll.kernel32.MapViewOfFile(hMMF, \'.
_ ' FILE_MAP_ALL_ACCESS, 0, 0, 0)'.
_ ' if buffer <> 0:'.
_ ' ctypes.cdll.msvcrt.strcpy(buffer, var_inp)'.
_ ' rc = ctypes.windll.kernel32.UnmapViewOfFile(buffer)'.
_ ' rc = ctypes.windll.kernel32.CloseHandle(hMMF)'.
_ 'raw_input("Press any key...")'.
"-Python Script end-----------------------------------------------------
"-Get SAP GUIs work directory---------------------------------
Call Method cl_gui_frontend_services=>get_sapgui_workdir
Changing SAPWORKDIR = WorkDir Exceptions Others = 1.
"-Create Python script file-----------------------------------
Concatenate WorkDir '\Python27\Test.py' Into FileName.
Call Method Of oScriptX 'WriteFile' Exporting #1 = FileName
#2 = Buffer.
Flush.
"-Execute Python script---------------------------------------
Call Method Of oScriptX 'Shell' = rc Exporting
#1 = 'Python27\python.exe' #2 = 'Python27\Test.py'
#3 = SW_SHOWNORMAL #4 = 1.
Flush.
"-Read the input from the memory map file---------------------
Call Method Of oScriptX 'FileMapRead' = RetCode
Exporting #1 = 'SAP001' #2 = 64.
Flush.
"-Destroy memory map file-------------------------------------
Call Method Of oScriptX 'FileMapClose' = rc
Exporting #1 = hFileMap.
Flush.
"-Delete Python environment-----------------------------------
Call Method Of oScriptX 'DeleteDirectory' Exporting
#1 = 'Python27'.
Flush.
"-Write the content of the memory map file--------------------
Write: / RetCode.
EndIf.
Free Object oScriptX.
EndIf.
"-End-------------------------------------------------------------------
Benefits
- Zero install, only copy.
- No admin rights necessary.
- All activities are in the user space only.
- These methods works perfect in protected and restricted environments.
- ABAP function modules contains all you need.
- No admin activities necessary. All you need you get with a simple call of an ABAP function module.
Addendum
2014/11/19 – Look here for an update.
Source: https://blogs.sap.com/2013/10/22/how-to-use-python-inside-abap/
Nenhum comentário:
Postar um comentário