Help:Toolforge/Pywikibot

From Wikitech
Jump to navigation Jump to search

The Pywikibot framework is a Python library and collection of scripts to automate work on MediaWiki sites.

Installation and setup

  1. Become your tool-account
    MAINTAINER@tools-sgebastion-10:~$ become TOOLNAME
    
  2. Create a setup script (e.g., pwb_venv.sh) that creates a virtual environment and installs dependencies.
    tools.TOOLNAME@tools-sgebastion-10:~$ touch $HOME/pwb_venv.sh
    tools.TOOLNAME@tools-sgebastion-10:~$ chmod ug+x $HOME/pwb_venv.sh  # make the script executable
    
    You need to install from git if you want to use the Pywikibot scripts.
Pywikibot installation source Example
PyPI
pwb_venv.sh
#!/bin/bash

# clean-up directories if they already exist
rm -fdr pwbvenv

# create a virtual environment
python3 -m venv pwbvenv

# activate it
source pwbvenv/bin/activate

# install dependencies
pip install --upgrade pip setuptools wheel
pip install pywikibot[mwparserfromhell,mwoauth,mysql]  # update as needed
pip install ... # any other dependencies here
git
pwb_venv.sh
#!/bin/bash

# clean-up directories if they already exist
rm -fdr pwbvenv $HOME/pywikibot-core

# create a virtual environment
python3 -m venv pwbvenv

# activate it
source pwbvenv/bin/activate

# clone Pywikibot
git clone --recursive --branch stable "https://gerrit.wikimedia.org/r/pywikibot/core" $HOME/pywikibot-core

# install dependencies
pip install --upgrade pip setuptools wheel
pip install $HOME/pywikibot-core[mwparserfromhell,mwoauth,mysql]  # update as needed
pip install ... # any other dependencies here
  1. Run the setup script using the jobs framework.
    tools.TOOLNAME@tools-sgebastion-10:~$ toolforge-jobs run setup-venv --command $HOME/pwb_venv.sh --image tf-python39 --wait
    INFO: job 'setup-venv' completed
    
  2. Configure Pywikibot
    • Interactively:
      tools.TOOLNAME@tools-sgebastion-10:~$ webservice python3.9 shell  # start an interactive Kubernetes shell
      tools.TOOLNAME@shell-#########:~$ source pwbvenv/bin/activate  # activate the virtual environment
      (pwbvenv) tools.TOOLNAME@shell-#########:~$ pwb generate_user_files  # run the generate_user_files.py Pywikibot script
      
      user-config.py and (optionally) user-password.py are generated by following the prompts.
    • Manually: See the manual on MediawWiki wiki: user-config.py, BotPasswords, and OAuth.
  3. Before exiting the shell, you may test the installation.
    (pwbvenv) tools.TOOLNAME@shell-#########:~$ pwb version  # run the version.py Pywikibot script
    Pywikibot: pywikibot/__init__.py (, -1 (unknown), 2022/11/03, 21:47:06, UNKNOWN)
    Release version: 7.7.1
    setuptools version: 65.5.0
    mwparserfromhell version: 0.6.4
    wikitextparser version: n/a
    requests version: 2.28.1
      cacerts: /data/project/TOOLNAME/pwbvenv/lib/python3.9/site-packages/certifi/cacert.pem
        certificate test: ok
    Python: 3.9.2 (default, Feb 28 2021, 17:03:44)
    [GCC 10.2.1 20210110]
    PYWIKIBOT_DIR: Not set
    PYWIKIBOT_DIR_PWB: /data/project/TOOLNAME/pwbvenv/lib/python3.9/site-packages/pywikibot/scripts
    PYWIKIBOT_NO_USER_CONFIG: Not set
    Config base dir: /data/project/TOOLNAME
    Usernames for family 'commons':
            commons: Example bot
    

Create jobs

Main article: Help:Toolforge/Jobs framework

Example of running the Pywikibot version.py script once:

tools.TOOLNAME@tools-sgebastion-10:~$ toolforge-jobs run pywikibot-version --command "$HOME/pwbvenv/bin/pwb version" --image tf-python39

Example of running your own script on a schedule:

tools.TOOLNAME@tools-sgebastion-10:~$ toolforge-jobs run my-cool-job --command "$HOME/pwbvenv/bin/python3 $HOME/my-cool-script.py" --image tf-python39 --schedule "1 2 * * 3"

Additional parameters for the job could include, for example, additional memory allocation (--mem MEM), job restart after being finished (--continuous), etc.

Communication and support

Support and administration of the WMCS resources is provided by the Wikimedia Foundation Cloud Services team and Wikimedia movement volunteers. Please reach out with questions and join the conversation:


Discuss and receive general support
Receive mail announcements about critical changes
Subscribe to the cloud-announce@ mailing list (all messages are also mirrored to the cloud@ list)
Track work tasks and report bugs
Use the Phabricator workboard #Cloud-Services for bug reports and feature requests about the Cloud VPS infrastructure itself
Learn about major near-term plans
Read the News wiki page
Read news and stories about Wikimedia Cloud Services
Read the Cloud Services Blog (for the broader Wikimedia movement, see the Wikimedia Technical Blog)