Python for z OS Yoel BenAvraham Joe Bostian

Python for z. OS Yoel Ben-Avraham Joe Bostian Open Source Enablement, Ecosystems Team yoel. ben-avraham@ibm. com jbostian@us. ibm. com

Python and Rexx

REXX Interpreted Language Widely Available Simplicity By Mike Cowlishaw Nearly all IBM operating systems. Windows, Java, Linux. Freeware/Open Source Easy and elegant use. Built-in functions. Simplified error handling. Built-in debugger. Straightforward system commands. Released 1982. Modern Philosophy Simple syntax. Dynamically typed. Functions/Subroutines. ORexx.

PYTHON Interpreted Language developed by Guido Van Rossum, first released in 1991. Modern and Elegant Language and Philosophy Object Oriented. Dynamically typed. Readability. Meaningful indentation. Massively extensible. Enforced use of space. “One solution” design. Readability. Consistency. Modularity. Minimalism. Evolution of Design Ongoing development and evolution. Commitment to change. Paradigm shifts. Python 3’s disruption. Open Source OSI-approved license. MASSIVE community.

MASSIVE OPEN SOURCE pypi. org Github lists over hosts over #1 Language 2018 – IEEE 10, 000 3, 000 #1 “Most Wanted” 2019 – Stack. Overflow python packages under an OSIapproved license repositories using python

Python and Rexx serve similar functions! REXX Python – Long history – Simple syntax, easy to use – Modern philosophy – Matured beyond origins – “Workhorse” development language – Workhorse of the open source world – Easy access to system commands and facilities

CALLING LSTAT IN REXX Gets info for specified file, stores to stem variable Notice the comprehensive return values

CALLING LSTAT IN PYTHON Gets info for specified file, stores to python object Return values are largely identical!

HOW IS THIS POSSIBLE? Python written in C Includes all runtime capabilities z/OS provides C runtime library Access real system information in Python Capability of running OS commands Nearly identical to original REXX script Porting an administrative Rexx script to python is not only viable, but easy!

CASE STUDY: COPYTREE Utility that copies a directory ENTIRELY, including information which would not normally be copied by cp. Written in Rexx Useful tool for system programmers. Good example of working code. Today We’ll examine a Python implementation of this script.

COPYDENSE FUNCTION REXX Python Read buffer from file. Copy to new file. Repeat until the buffer is empty.

SYNTAX SIMILAR, LOGIC IDENTICAL 1 -1 system call equivalence!

1: 1 SYNTACTICAL CONSTRUCTS System Commands can be easily submitted by both languages Function Definitions work identically between both languages Control Structures are largely identical Stem Variables can be precisely replicated by variables, lists, dictionaries, or more complex structures as needed. IBM z/OS Academy / Spring 2020/ © 2020 IBM Corporation paths. pix. st_uid == paths[pix]["st_uid"]

WHEN IT’S NOT 1: 1 Not actually identical! Python OS. lstat doesn’t return all variables from Rexx lstat. • st_uaudit– File Audit Flags • st_type– File Type • st_accessacl– File Access Control Lists • st_fmodelacl • st_dmodelacl IBM z/OS Academy / Spring 2020/ © 2020 IBM Corporation

HANDLING ST_TYPE Python missing st_type. Does return st_mode. Call macros S_ISREG S_ISDIR S_ISCHR Determine type exhaustively. Set the value.

HANDLING AUDIT FLAGS Audit flags are not returned. Administrative tool on z/OS, so the platform itself provides. LS – IBM USS – has “-W” flag CHAUDIT system command allows modification of these flags.

HANDLING FILE ACCESS CONTROL LISTS File Access Control Lists Advanced permissions control. Read - “getfacl”, Set - “setfacl”. Not just permissions bits. z. OS make access control lists more prominent. Python is CAPABLE of addressing this.

WHAT WE LEARN FROM THIS 1: 1 conversion missing, solution often still exists. Python is ideal for this, powerful, straightforward, sturdy.

FURTHER MISCELLANY Simple examples and fun foibles

List Root Directory Contents . /. .

Read the Contents of a File b'xd 4xd 6xd 5xd 2xc 5xe 8x 15xd 4xd 6xd 5xd 2xc 5 xe 8x 15xd 4xd 6xd 5xd 2xc 5xe 8x 15xd 4xd 6xd 5 xd 2xc 5xe 8x 15xd 4xd 6xd 5xd 2xc 5xe 8x 15xd 4x d 6xd 5' file = open(“monkey. txt”)

List all Users and Groups Ctypes Library Execute C from Python import pwd, import grp

Run a shell command

Use what YOU like

LINKS TO REFERENCE IBM Resource to learn Rexx: Learning the Rexx Language Python Package Index: pypi. org Python Documentation: python. org/doc

Thank you Yoel Ben-Avraham IBM Ecosystems — yoel. ben-avraham@us. ibm. com +1 -845 -433 -2888 ibm. com Joe Bostian IBM Ecosystems — jbostian@us. ibm. com +1 -845 -435 -1388 ibm. com

- Slides: 27