SlideShare une entreprise Scribd logo
1  sur  20
Télécharger pour lire hors ligne
PySmbC:
C Modules are Easy
EuroPython 2012, 6th July - Firenze




          Babel Srl P.zza S. Benedetto da Norcia, 33 0040, Pomezia (RM) – www.babel.it

                            Roberto Polli - roberto.polli@babel.it
What? Who? Why?
A success story about using GitHub and Nose testing framework.

Roberto Polli - Community Manager @ Babel.it. Loves writing in C,
Java and Python. Red Hat Certified Engineer.

Fabio Isgrò – System Engineer @ Babel.it. Linux and Samba
expert.

Babel – Proud sponsor of this talk ;) Delivers large mail
infrastructures based on Open Source software for Italian ISP and
PA. Contributes to various FLOSS.



                       Roberto Polli - roberto.polli@babel.it
Agenda – 1 - prerequisites
 People enjoy FLOSS but...why is afraid of contributing?

+ GitHub: track forks, fast merges

+ Python: C Extension are easy

+ Nose Test: simplify testing process


    = Don't have to be a Guru to support FLOSS!




                      Roberto Polli - roberto.polli@babel.it
Agenda – 2 - contributing
Samba and samba-client library.

Clone a project, get in touch with the maintainer.

Write test first.

Wrapping get_xattr and set_xattr, manage ACL.


      Don't need to know SMB to extend PySmbC




                        Roberto Polli - roberto.polli@babel.it
GitHub – social coding
FLOSS Coding is a relational stuff.

Coding 1.0: send .patch to a mailing list

Coding 2.0: fork me on GitHub
https://github.com/ioggstream/pysmbc



               Did you say...fork?


                   Roberto Polli - roberto.polli@babel.it
GitHub – social coding
Forking is the nightmare of every
floss maintainer.

Sparse writes, increasing merge
efforts, lost changes.

GitHub, like the I/O Scheduler,
helps to merge writes!
●
  Tracks forks;
●
  Push changes to master.




                        Roberto Polli - roberto.polli@babel.it
GitHub – social coding
Patch and commit to your repository
●
 don't lose your changes;
●
 track your own history.
Push your changes to my repo
●
 no more for .patch;
●
 I get the change history.
Discuss for approval
●
 Github portal supports code annotations.
Merge my changes
●
 use git to merge from a remote repository!




                       Roberto Polli - roberto.polli@babel.it
Enter PySmbC - Extensions
Python wrapper around libsmbclient: run C code from python
– enjoy a stable C library;
– just manage input, output and errors:
– usually faster than a pure python implementation.

 rpolli$ ipython
 ln [1]: import smbc                                      smbc.so
 ln [2]: print smbc.XATTR_ACL
 system.nt_sec_desc.acl
                                                      libsmbclient.so.0
 ln [3]:




                       Roberto Polli - roberto.polli@babel.it
Example - Wrapping factorial() - 1
The wrapping function my_factorial():
                                                                     // Python C Extension




                                                       wrapperfy.c
●
 Parses and validates the input;                                     // uses factorial from fact.c
●
 Calls the wrapped function();                                       #include <fact.h>

                                                                     // returns a python object!
●
 Returns a python object.                                            PyObject *my_factorial(...) {
                                                                          ...
                                                                          ret = factorial(n);
                                                                           ...
                                                                         return PyLong_asLong(ret);
A given structure maps python methods to                             }

C functions.

gcc wrapperfy.c -o _wrapper.so -shared
                                                            // Maps _wrapper.factorial
                      # python script                       // to my_factorial
                      from _wrapper import factorial
                                                            PyMethodDef BabelMethods[] = {
Now we can invoke a                                           {"factorial", my_factorial, ... },
                      print factorial(4)                      {NULL, NULL, 0, NULL} /*Sentinel*/
wrapped function!                                           };




                       Roberto Polli - roberto.polli@babel.it
Example - Wrapping factorial() - 2
Parsing and validating Input and                   // returns a python object!
                                                   PyObject *my_factorial(..., *args) {
Output is crucial. We don't want python              // NULL indicates an error
                                                     if (!PyArg_ParseTuple(args, "i", &n))
to SEGFAULT!                                           return NULL;
                                                     // n! > max(long long int)
                                                     if (n>21) {
                                                       ...
                                                       PyErr_SetString(FactError, 
Create new exceptions in the                             “Bad value”);
                                                       return NULL;
initialization function.                             }
                                                     ... 
                                                     return PyLong_asLong(ret);
                                                   }

Throw exceptions in the function:                  PyObject *FactError;
●
 setting PyErr;                                    // in extension initialization...
                                                   ...
                                                   init_wrapper(void) {
●
 returning NULL.                                     ...
                                                     // define an exception
                                                     FactError = 
                                                       PyErr_NewException("_wrapper.error", 
                                                         NULL, NULL);
                                                     ...
                                                   }



                       Roberto Polli - roberto.polli@babel.it
Example - Wrapping factorial() - 3
                                                       // Python C Extension
C Extension components:                                #include <Python.h>

●
 wrapping functions;                                   // exceptions
                                                       PyObject *FactError;
                                                       PyObject *FiboError;
●
 method/function map;                                  // functions
●
 exceptions;                                           PyObject *my_factorial(...);
                                                       PyObject *my_fibonacci(...);
●
 initialization function.                              // Function/Method Maps 

                                                       PyMethodDef BabelMethods[] = {
                                                         {"factorial",my_factorial,... },
Functions and Exception should be static                 {"fibonacci",my_fibonacci,... },
                                                         {NULL, NULL, 0, NULL} /*Sentinel*/
                                                       };

                                                      PyMODINIT_FUNC

 You have to track memory usage!                      init_wrapper(void)
                                                      {
                                                        PyObject *m;
                                                        m = Py_InitModule("_wrapper",
                                                              BabelMethods);
                                                        // … Allocate Exceptions
                                                        FactError = PyErr_NewException(...)
                                                        FiboError = PyErr_NewException(...)
                                                      }



                       Roberto Polli - roberto.polli@babel.it
Enters PySmbC - Modules
Python C extensions
may enjoy both C and
Python code.

Wrap the C extension in
a Python module.

Extend the module with
python classes.

$PYTHONPATH/
  wrapper/
    __init__.py           In [1]: import wrapper
                          In [2]: assert wrapper.helpers
    _wrapper.so           In [3]: wrapper.helpers.is_integer(10)
    helpers.py

                           Roberto Polli - roberto.polli@babel.it
Nose – let's contribute - 1
Before adding features to PySmbC we checked the project
status
 # git clone https://github.com/ioggstream/pysmbc .
 # vim tests/settings.py # set samba credential
 # nosetests test/

NoseTest - a python script that auto-discovers and run test
cases. Wraps python-unittest.

Add new features only after successful tests. Verify your
environment (eg. Samba credentials, directory acls )



                       Roberto Polli - roberto.polli@babel.it
Nose – let's contribute - 2
On successful tests, we can start developing

Follow the Git way: create a separate branch. We'll merge
it on success
 # git checkout -b ioggstream_setxattr

Write the tests before writing the code. You'll be more focused on
your targets

            With nosetest it's simpler than ever!




                       Roberto Polli - roberto.polli@babel.it
Nose – is like UnitTest
UnitTest                                     Nose
from unittest import TestCase, main          import nose


class MyTest(UnitTest):                      class MyTest:
  def setUp(self):                               def setup(self):
    print”setup every”                             print ”setup”
  def tearDown(self):                            def teardown(self):
    print “teardown every”                         print “teardown”
  def test_do(self):                             def test_do(self):
    print “do 1”                                   print “do 1”


if __name__== “__main__”:                    # nose script will auto-discover
    main()                                   #   this script named test_script.py




                             Roberto Polli - roberto.polli@babel.it
Nose – is simpler than UnitTest
Nose: simple test                         Nose: annotations
# don't need to import nose               from nose import SkipTest,with_setup
# or define a class
                                          def pre(): print “setup”
def setup():                              def post(): print “teardown”
  print”setup once for all tests”
def teardown():                           @with_setup(pre,post)
  print “teardown once for all test”      def test_do():
                                             print “do 1”

def test_do():
                                          @SkipTest
  print “do 1”
                                          def test_dont():
def test_fail():
                                              Print “not done yet”
  assert False




                          Roberto Polli - roberto.polli@babel.it
Nose – Invocation
You can run your all tests in a given directory
 # nosetests ./path/
Or just one file
 # nosetests ./path/test_sample.py
Or even a single test method
 # nosetests ./path/test_sample.py:test_do1
Or suite, eventually setting the working directory
 ex1# nosetests ./path/test_class.py:TestOne
 ex2# nosetests -w ./path test_class:TestOne
For a verbose output just use:
 #nosetests -sv [args]


                     Roberto Polli - roberto.polli@babel.it
PySmbC – add getxattr
                                                             # from test_context.py
Nose ensures that we're not going to                         def test_xattr_constants():
break anything.                                                '''reuse variable defined
                                                               in smbclient.h'''
                                                               assert smbc.XATTR_ACL
Start writing tests, not coding                                assert smbc.XATTR_OWNER
                                                               assert smbc.XATTR_GROUP
functionalities.
                                                             def test_xattr_get():
You can @SkipTest until new functions are ready.               '''test xattr with all
                                                                possible values'''
                                                               . . .
Play with the wrapped functions.                               for xa in valid_xatts:
                                                                 assert ctx.getxattr(url, xa)

                                                             def test_xattr_get_error():
Start with the simpler one: getxattr()                         '''xattr_get should
●embed C constants into python;                                 recognize bad values'''
                                                               . . .
●test good values;
                                                               for xa in invalid_xatts:
●check behavior with bad values.
                                                                 try:
                                                                   ctx.getxattr(url, xa)
                                                                   assert False
                                                                 except RuntimeError as e:
Code until tests are successful.                                   . . . #get errno
                                                                   assert errno == EINVAL


                             Roberto Polli - roberto.polli@babel.it
PySmbC – add setxattr and futures
Helper methods for parsing and                             # from test_context.py

creating ACL                                               def test_xattr_set():
attrs_new = u'REVISION:1'                                   . . .
                                                             ctx.setxattr(url, a_name,
 + ',OWNER:RPOLLIbabel' 
                                                               attrs_new, REPLACE)
 + ',GROUP:Unix Groupbabel'                               attrs_1 = ctx.getxattr(url,
 + ',ACL:RPOLLIbabel:0/0/0x001e01ff'                    a_name)
 + ',ACL:Unix Groupbabel:0/0/0x00120089'                  assert attrs_1 == attrs_new

 + ',ACL:Unix Groupgames:0/0/0x001e01ff'                def test_xattr_set_error():
 + ',ACL:Everyone:0/0/0x00120089'                          '''setxattr should
                                                              recognize bad values'''
                                                             . . .
Shift from smbc.so to smbc module:                           for xa in invalid_xatts:
                                                               try:
●
  smbc/_smbc.so                                                  ctx.setxattr(url, a_name,
                                                                    xa, REPLACE)
●
  smbc/__init__.py                                               assert False
●
  smbc/helper.py                                               except RuntimeError as e:
                                                                 . . . #get errno
                                                                 assert errno == EINVAL
                                                               except TypeError
                                                                 pass

                              Roberto Polli - roberto.polli@babel.it
PySmbC
https://github.com/ioggstream/pysmbc
http://pypi.python.org/pypi/pysmbc/
http://www.samba.org


Babel
http://www.babel.it
http://vaunaspada.babel.it/blog




                Thank You!
         roberto.polli@babel.it

                Roberto Polli - roberto.polli@babel.it

Contenu connexe

Plus de Babel

Sophos Complete Security: arte e scienza della sicurezza
Sophos Complete Security: arte e scienza della sicurezzaSophos Complete Security: arte e scienza della sicurezza
Sophos Complete Security: arte e scienza della sicurezzaBabel
 
L’innovazione a difesa della tradizione: il caso dell’Archivio Storico della ...
L’innovazione a difesa della tradizione: il caso dell’Archivio Storico della ...L’innovazione a difesa della tradizione: il caso dell’Archivio Storico della ...
L’innovazione a difesa della tradizione: il caso dell’Archivio Storico della ...Babel
 
L’innovazione a difesa della tradizione: il caso dell’Archivio Storico della ...
L’innovazione a difesa della tradizione: il caso dell’Archivio Storico della ...L’innovazione a difesa della tradizione: il caso dell’Archivio Storico della ...
L’innovazione a difesa della tradizione: il caso dell’Archivio Storico della ...Babel
 
Intercettazioni: guida alle nuove norme per i provider
Intercettazioni: guida alle nuove norme per i providerIntercettazioni: guida alle nuove norme per i provider
Intercettazioni: guida alle nuove norme per i providerBabel
 
Dalla virtualizzazione al private cloud: Il Patronato INCA rinnova la fiducia...
Dalla virtualizzazione al private cloud: Il Patronato INCA rinnova la fiducia...Dalla virtualizzazione al private cloud: Il Patronato INCA rinnova la fiducia...
Dalla virtualizzazione al private cloud: Il Patronato INCA rinnova la fiducia...Babel
 
Will iPython replace Bash?
Will iPython replace Bash?Will iPython replace Bash?
Will iPython replace Bash?Babel
 
La gestione integrata della sicurezza in ANSA: dal firewalling all'UTM
La gestione integrata della sicurezza in ANSA: dal firewalling all'UTMLa gestione integrata della sicurezza in ANSA: dal firewalling all'UTM
La gestione integrata della sicurezza in ANSA: dal firewalling all'UTMBabel
 
Installare i server via rete con Cobbler
Installare i server via rete con CobblerInstallare i server via rete con Cobbler
Installare i server via rete con CobblerBabel
 
Shell Control Box - Il Gusto della Sicurezza
Shell Control Box - Il Gusto della SicurezzaShell Control Box - Il Gusto della Sicurezza
Shell Control Box - Il Gusto della SicurezzaBabel
 
Tracciamento delle attività di amministrazione mediante i BalaBit Shell Contr...
Tracciamento delle attività di amministrazione mediante i BalaBit Shell Contr...Tracciamento delle attività di amministrazione mediante i BalaBit Shell Contr...
Tracciamento delle attività di amministrazione mediante i BalaBit Shell Contr...Babel
 
La Desktop Virtualization
La Desktop VirtualizationLa Desktop Virtualization
La Desktop VirtualizationBabel
 
Crittografia e integrazione dei sistemi con Python
Crittografia e integrazione dei sistemi con PythonCrittografia e integrazione dei sistemi con Python
Crittografia e integrazione dei sistemi con PythonBabel
 
Intercettazioni e posta elettronica: le misure di sicurezza per i gestori
Intercettazioni e posta elettronica: le misure di sicurezza per i gestoriIntercettazioni e posta elettronica: le misure di sicurezza per i gestori
Intercettazioni e posta elettronica: le misure di sicurezza per i gestoriBabel
 
Babel presenta: Opsview
Babel presenta: OpsviewBabel presenta: Opsview
Babel presenta: OpsviewBabel
 
Monitoraggio di infrastrutture IT mediante Opsview Enteprise V4
Monitoraggio di infrastrutture IT mediante Opsview Enteprise V4Monitoraggio di infrastrutture IT mediante Opsview Enteprise V4
Monitoraggio di infrastrutture IT mediante Opsview Enteprise V4Babel
 
OpenVAS, lo strumento open source per il vulnerability assessment
OpenVAS, lo strumento open source per il vulnerability assessmentOpenVAS, lo strumento open source per il vulnerability assessment
OpenVAS, lo strumento open source per il vulnerability assessmentBabel
 
Testing with MySQL embedded
Testing with MySQL embeddedTesting with MySQL embedded
Testing with MySQL embeddedBabel
 
Cross compiler per uso domestico
Cross compiler per uso domesticoCross compiler per uso domestico
Cross compiler per uso domesticoBabel
 
Sicurezza flessibile con SELinux: architettura e configurazione
Sicurezza flessibile con SELinux: architettura e configurazioneSicurezza flessibile con SELinux: architettura e configurazione
Sicurezza flessibile con SELinux: architettura e configurazioneBabel
 
Ridirezionamento di I/O con Bash: un breve approfondimento
Ridirezionamento di I/O con Bash: un breve approfondimentoRidirezionamento di I/O con Bash: un breve approfondimento
Ridirezionamento di I/O con Bash: un breve approfondimentoBabel
 

Plus de Babel (20)

Sophos Complete Security: arte e scienza della sicurezza
Sophos Complete Security: arte e scienza della sicurezzaSophos Complete Security: arte e scienza della sicurezza
Sophos Complete Security: arte e scienza della sicurezza
 
L’innovazione a difesa della tradizione: il caso dell’Archivio Storico della ...
L’innovazione a difesa della tradizione: il caso dell’Archivio Storico della ...L’innovazione a difesa della tradizione: il caso dell’Archivio Storico della ...
L’innovazione a difesa della tradizione: il caso dell’Archivio Storico della ...
 
L’innovazione a difesa della tradizione: il caso dell’Archivio Storico della ...
L’innovazione a difesa della tradizione: il caso dell’Archivio Storico della ...L’innovazione a difesa della tradizione: il caso dell’Archivio Storico della ...
L’innovazione a difesa della tradizione: il caso dell’Archivio Storico della ...
 
Intercettazioni: guida alle nuove norme per i provider
Intercettazioni: guida alle nuove norme per i providerIntercettazioni: guida alle nuove norme per i provider
Intercettazioni: guida alle nuove norme per i provider
 
Dalla virtualizzazione al private cloud: Il Patronato INCA rinnova la fiducia...
Dalla virtualizzazione al private cloud: Il Patronato INCA rinnova la fiducia...Dalla virtualizzazione al private cloud: Il Patronato INCA rinnova la fiducia...
Dalla virtualizzazione al private cloud: Il Patronato INCA rinnova la fiducia...
 
Will iPython replace Bash?
Will iPython replace Bash?Will iPython replace Bash?
Will iPython replace Bash?
 
La gestione integrata della sicurezza in ANSA: dal firewalling all'UTM
La gestione integrata della sicurezza in ANSA: dal firewalling all'UTMLa gestione integrata della sicurezza in ANSA: dal firewalling all'UTM
La gestione integrata della sicurezza in ANSA: dal firewalling all'UTM
 
Installare i server via rete con Cobbler
Installare i server via rete con CobblerInstallare i server via rete con Cobbler
Installare i server via rete con Cobbler
 
Shell Control Box - Il Gusto della Sicurezza
Shell Control Box - Il Gusto della SicurezzaShell Control Box - Il Gusto della Sicurezza
Shell Control Box - Il Gusto della Sicurezza
 
Tracciamento delle attività di amministrazione mediante i BalaBit Shell Contr...
Tracciamento delle attività di amministrazione mediante i BalaBit Shell Contr...Tracciamento delle attività di amministrazione mediante i BalaBit Shell Contr...
Tracciamento delle attività di amministrazione mediante i BalaBit Shell Contr...
 
La Desktop Virtualization
La Desktop VirtualizationLa Desktop Virtualization
La Desktop Virtualization
 
Crittografia e integrazione dei sistemi con Python
Crittografia e integrazione dei sistemi con PythonCrittografia e integrazione dei sistemi con Python
Crittografia e integrazione dei sistemi con Python
 
Intercettazioni e posta elettronica: le misure di sicurezza per i gestori
Intercettazioni e posta elettronica: le misure di sicurezza per i gestoriIntercettazioni e posta elettronica: le misure di sicurezza per i gestori
Intercettazioni e posta elettronica: le misure di sicurezza per i gestori
 
Babel presenta: Opsview
Babel presenta: OpsviewBabel presenta: Opsview
Babel presenta: Opsview
 
Monitoraggio di infrastrutture IT mediante Opsview Enteprise V4
Monitoraggio di infrastrutture IT mediante Opsview Enteprise V4Monitoraggio di infrastrutture IT mediante Opsview Enteprise V4
Monitoraggio di infrastrutture IT mediante Opsview Enteprise V4
 
OpenVAS, lo strumento open source per il vulnerability assessment
OpenVAS, lo strumento open source per il vulnerability assessmentOpenVAS, lo strumento open source per il vulnerability assessment
OpenVAS, lo strumento open source per il vulnerability assessment
 
Testing with MySQL embedded
Testing with MySQL embeddedTesting with MySQL embedded
Testing with MySQL embedded
 
Cross compiler per uso domestico
Cross compiler per uso domesticoCross compiler per uso domestico
Cross compiler per uso domestico
 
Sicurezza flessibile con SELinux: architettura e configurazione
Sicurezza flessibile con SELinux: architettura e configurazioneSicurezza flessibile con SELinux: architettura e configurazione
Sicurezza flessibile con SELinux: architettura e configurazione
 
Ridirezionamento di I/O con Bash: un breve approfondimento
Ridirezionamento di I/O con Bash: un breve approfondimentoRidirezionamento di I/O con Bash: un breve approfondimento
Ridirezionamento di I/O con Bash: un breve approfondimento
 

Dernier

The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 

Dernier (20)

The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 

Ottimizzare il ciclo di sviluppo e test mediante Python, Nose e GitHub

  • 1. PySmbC: C Modules are Easy EuroPython 2012, 6th July - Firenze Babel Srl P.zza S. Benedetto da Norcia, 33 0040, Pomezia (RM) – www.babel.it Roberto Polli - roberto.polli@babel.it
  • 2. What? Who? Why? A success story about using GitHub and Nose testing framework. Roberto Polli - Community Manager @ Babel.it. Loves writing in C, Java and Python. Red Hat Certified Engineer. Fabio Isgrò – System Engineer @ Babel.it. Linux and Samba expert. Babel – Proud sponsor of this talk ;) Delivers large mail infrastructures based on Open Source software for Italian ISP and PA. Contributes to various FLOSS. Roberto Polli - roberto.polli@babel.it
  • 3. Agenda – 1 - prerequisites People enjoy FLOSS but...why is afraid of contributing? + GitHub: track forks, fast merges + Python: C Extension are easy + Nose Test: simplify testing process = Don't have to be a Guru to support FLOSS! Roberto Polli - roberto.polli@babel.it
  • 4. Agenda – 2 - contributing Samba and samba-client library. Clone a project, get in touch with the maintainer. Write test first. Wrapping get_xattr and set_xattr, manage ACL. Don't need to know SMB to extend PySmbC Roberto Polli - roberto.polli@babel.it
  • 5. GitHub – social coding FLOSS Coding is a relational stuff. Coding 1.0: send .patch to a mailing list Coding 2.0: fork me on GitHub https://github.com/ioggstream/pysmbc Did you say...fork? Roberto Polli - roberto.polli@babel.it
  • 6. GitHub – social coding Forking is the nightmare of every floss maintainer. Sparse writes, increasing merge efforts, lost changes. GitHub, like the I/O Scheduler, helps to merge writes! ● Tracks forks; ● Push changes to master. Roberto Polli - roberto.polli@babel.it
  • 7. GitHub – social coding Patch and commit to your repository ● don't lose your changes; ● track your own history. Push your changes to my repo ● no more for .patch; ● I get the change history. Discuss for approval ● Github portal supports code annotations. Merge my changes ● use git to merge from a remote repository! Roberto Polli - roberto.polli@babel.it
  • 8. Enter PySmbC - Extensions Python wrapper around libsmbclient: run C code from python – enjoy a stable C library; – just manage input, output and errors: – usually faster than a pure python implementation. rpolli$ ipython ln [1]: import smbc smbc.so ln [2]: print smbc.XATTR_ACL system.nt_sec_desc.acl libsmbclient.so.0 ln [3]: Roberto Polli - roberto.polli@babel.it
  • 9. Example - Wrapping factorial() - 1 The wrapping function my_factorial(): // Python C Extension wrapperfy.c ● Parses and validates the input; // uses factorial from fact.c ● Calls the wrapped function(); #include <fact.h> // returns a python object! ● Returns a python object. PyObject *my_factorial(...) { ... ret = factorial(n); ... return PyLong_asLong(ret); A given structure maps python methods to } C functions. gcc wrapperfy.c -o _wrapper.so -shared // Maps _wrapper.factorial # python script // to my_factorial from _wrapper import factorial PyMethodDef BabelMethods[] = { Now we can invoke a {"factorial", my_factorial, ... }, print factorial(4) {NULL, NULL, 0, NULL} /*Sentinel*/ wrapped function! }; Roberto Polli - roberto.polli@babel.it
  • 10. Example - Wrapping factorial() - 2 Parsing and validating Input and // returns a python object! PyObject *my_factorial(..., *args) { Output is crucial. We don't want python   // NULL indicates an error   if (!PyArg_ParseTuple(args, "i", &n)) to SEGFAULT!     return NULL;   // n! > max(long long int)   if (n>21) {     ...     PyErr_SetString(FactError,  Create new exceptions in the       “Bad value”);     return NULL; initialization function.   }   ...    return PyLong_asLong(ret); } Throw exceptions in the function: PyObject *FactError; ● setting PyErr; // in extension initialization... ... init_wrapper(void) { ● returning NULL.   ...   // define an exception   FactError =      PyErr_NewException("_wrapper.error",        NULL, NULL);   ... } Roberto Polli - roberto.polli@babel.it
  • 11. Example - Wrapping factorial() - 3 // Python C Extension C Extension components: #include <Python.h> ● wrapping functions; // exceptions PyObject *FactError; PyObject *FiboError; ● method/function map; // functions ● exceptions; PyObject *my_factorial(...); PyObject *my_fibonacci(...); ● initialization function. // Function/Method Maps  PyMethodDef BabelMethods[] = {   {"factorial",my_factorial,... }, Functions and Exception should be static   {"fibonacci",my_fibonacci,... },   {NULL, NULL, 0, NULL} /*Sentinel*/ }; PyMODINIT_FUNC You have to track memory usage! init_wrapper(void) { PyObject *m; m = Py_InitModule("_wrapper", BabelMethods); // … Allocate Exceptions FactError = PyErr_NewException(...) FiboError = PyErr_NewException(...) } Roberto Polli - roberto.polli@babel.it
  • 12. Enters PySmbC - Modules Python C extensions may enjoy both C and Python code. Wrap the C extension in a Python module. Extend the module with python classes. $PYTHONPATH/ wrapper/ __init__.py In [1]: import wrapper In [2]: assert wrapper.helpers _wrapper.so In [3]: wrapper.helpers.is_integer(10) helpers.py Roberto Polli - roberto.polli@babel.it
  • 13. Nose – let's contribute - 1 Before adding features to PySmbC we checked the project status # git clone https://github.com/ioggstream/pysmbc . # vim tests/settings.py # set samba credential # nosetests test/ NoseTest - a python script that auto-discovers and run test cases. Wraps python-unittest. Add new features only after successful tests. Verify your environment (eg. Samba credentials, directory acls ) Roberto Polli - roberto.polli@babel.it
  • 14. Nose – let's contribute - 2 On successful tests, we can start developing Follow the Git way: create a separate branch. We'll merge it on success # git checkout -b ioggstream_setxattr Write the tests before writing the code. You'll be more focused on your targets With nosetest it's simpler than ever! Roberto Polli - roberto.polli@babel.it
  • 15. Nose – is like UnitTest UnitTest Nose from unittest import TestCase, main import nose class MyTest(UnitTest): class MyTest: def setUp(self): def setup(self): print”setup every” print ”setup” def tearDown(self): def teardown(self): print “teardown every” print “teardown” def test_do(self): def test_do(self): print “do 1” print “do 1” if __name__== “__main__”: # nose script will auto-discover main() # this script named test_script.py Roberto Polli - roberto.polli@babel.it
  • 16. Nose – is simpler than UnitTest Nose: simple test Nose: annotations # don't need to import nose from nose import SkipTest,with_setup # or define a class def pre(): print “setup” def setup(): def post(): print “teardown” print”setup once for all tests” def teardown(): @with_setup(pre,post) print “teardown once for all test” def test_do(): print “do 1” def test_do(): @SkipTest print “do 1” def test_dont(): def test_fail(): Print “not done yet” assert False Roberto Polli - roberto.polli@babel.it
  • 17. Nose – Invocation You can run your all tests in a given directory # nosetests ./path/ Or just one file # nosetests ./path/test_sample.py Or even a single test method # nosetests ./path/test_sample.py:test_do1 Or suite, eventually setting the working directory ex1# nosetests ./path/test_class.py:TestOne ex2# nosetests -w ./path test_class:TestOne For a verbose output just use: #nosetests -sv [args] Roberto Polli - roberto.polli@babel.it
  • 18. PySmbC – add getxattr # from test_context.py Nose ensures that we're not going to def test_xattr_constants(): break anything. '''reuse variable defined in smbclient.h''' assert smbc.XATTR_ACL Start writing tests, not coding assert smbc.XATTR_OWNER assert smbc.XATTR_GROUP functionalities. def test_xattr_get(): You can @SkipTest until new functions are ready. '''test xattr with all possible values''' . . . Play with the wrapped functions. for xa in valid_xatts: assert ctx.getxattr(url, xa) def test_xattr_get_error(): Start with the simpler one: getxattr() '''xattr_get should ●embed C constants into python; recognize bad values''' . . . ●test good values; for xa in invalid_xatts: ●check behavior with bad values. try: ctx.getxattr(url, xa) assert False except RuntimeError as e: Code until tests are successful. . . . #get errno assert errno == EINVAL Roberto Polli - roberto.polli@babel.it
  • 19. PySmbC – add setxattr and futures Helper methods for parsing and # from test_context.py creating ACL def test_xattr_set(): attrs_new = u'REVISION:1' . . . ctx.setxattr(url, a_name, + ',OWNER:RPOLLIbabel' attrs_new, REPLACE) + ',GROUP:Unix Groupbabel' attrs_1 = ctx.getxattr(url, + ',ACL:RPOLLIbabel:0/0/0x001e01ff' a_name) + ',ACL:Unix Groupbabel:0/0/0x00120089' assert attrs_1 == attrs_new + ',ACL:Unix Groupgames:0/0/0x001e01ff' def test_xattr_set_error(): + ',ACL:Everyone:0/0/0x00120089' '''setxattr should recognize bad values''' . . . Shift from smbc.so to smbc module: for xa in invalid_xatts: try: ● smbc/_smbc.so ctx.setxattr(url, a_name, xa, REPLACE) ● smbc/__init__.py assert False ● smbc/helper.py except RuntimeError as e: . . . #get errno assert errno == EINVAL except TypeError pass Roberto Polli - roberto.polli@babel.it