[[PageOutline]] = WebKit Python Guidelines and Tips = Below is an overview of WebKit's use of Python. == Basics == * Most of WebKit's Python scripts require Python 2.5 to run. * Informally, we try to follow [http://www.python.org/dev/peps/pep-0008/ PEP8]. Eventually we may make this official. * [http://trac.webkit.org/browser/trunk/WebKitTools/Scripts/test-webkitpy Scripts/test-webkitpy] unit tests the Python code. == Code Structure == * Most of the Python code is in [http://trac.webkit.org/browser/trunk/WebKitTools/Scripts/webkitpy WebKitTools/Scripts/webkitpy]. * [http://trac.webkit.org/browser/trunk/WebKitTools/Scripts WebKitTools/Scripts] also contains end-user Python scripts (which usually import from webkitpy). * Generally, we try to keep as much of the Python code in webkitpy as possible since this allows the code to be organized more nicely (for unit tests to be in companion files, etc). * Unit test files are in correspondence with modules. For example, if module.py is the name of a module, its unit test file would be module_unittest.py and would lie in the same directory. * The root-level folders in webkitpy/ generally correspond to end-user scripts in WebKitTools/Scripts. For example-- * check-webkit-style -> webkitpy/style/ * new-run-webkit-tests -> webkitpy/layout_tests/ * webkit-patch -> webkit/tool/ * Exceptions to the rule above are-- * webkitpy/common/: code shared by multiple root folders * webkitpy/python24/: code that needs to work under Python 2.4 (currently just the version-checking code) * [http://trac.webkit.org/browser/trunk/WebKitTools/Scripts/webkitpy webkitpy/thirdparty/]: all third-party code in webkitpy == Tips == * You can use the following command to clean leftover *.pyc files after a batch of file moves. {{{ find WebKitTools/Scripts -wholename '*.pyc' | xargs rm }}} This should usually not be necessary, but it ensures that you are not mistakenly using a wrong file since *.pyc files without a *.py file can still get executed. Incidentally, we try to use fully-qualified import statements throughout our code so that the location of modules in webkitpy/ is never be ambiguous. == Upgrading from Python 2.3 or Python 2.4 == [Much of these instructions are Mac-specific.] If you are upgrading from Python 2.3 or 2.4, you should upgrade to Python 2.6 rather than 2.5. You can tell what version of Python you are currently using by typing-- {{{ python -V }}} Before trying to install a new version, check whether your machine already has other versions installed. From a Mac, you can try reading the man page-- {{{ man python }}} For example, Snow Leopard comes with Python 2.5, 2.6, and 3.0. The man page provides instructions on how to switch your system between these system-installed versions. If you need to install a new version not on your machine, you can use [http://guide.macports.org/ MacPorts] to do this. MacPorts allows you to install Python versions alongside your system versions without interfering with them. After installing MacPorts, simply type (for example)-- {{{ sudo port install python26 }}} You should probably also install python_select using MacPorts-- {{{ sudo port install python_select }}} The python_select command allows you to quickly go back and forth between Python versions, like so-- {{{ > python -V Python 2.6.4 > sudo python_select python24 Selecting version "python24" for python > python -V Python 2.4.6 }}} To find out what versions of Python you can switch to using python_select, type-- {{{ python_select -l }}} == Consequences of not using Python 2.6 == Below is a list of some consequences of not using Python 2.6 (and things we should revisit in our code base should we ever upgrade to 2.6): * We can't use os.path.relpath(): [http://docs.python.org/library/os.path.html#os.path.relpath] * We can't use ZipFile.extractall(): [http://docs.python.org/library/zipfile.html#zipfile.ZipFile.extractall] * We need to use an import statement to use the "with" keyword: http://docs.python.org/whatsnew/2.5.html#pep-343-the-with-statement