From Wikitech
Jump to navigation Jump to search

Pywikibot is a Python library and collection of scripts that automate work on MediaWiki sites. Pywikibot provides support for Toolforge environment and actively maintains its shared library there.

Setup Pywikibot on Toolforge

There are several ways to setup Pywikibot on Toolforge.

Using a shared library (recommended)

The most basic way to use Pywikibot on Toolforge is the shared library in the /shared/pywikibot folder (/data/project/shared/pywikibot folder on Kubernetes). The library here is automatically updated and contains two versions of Pywikibot: core_stable and core.

core_stable is created about every month if framework tests passed. Therefore it is considered as a stable release and we highly recommend using it.

core is packaged twice every day from current bleeding edge, known as master or core branch. It contains all the newest features, but also all the newest bugs. It is recommended to be used mainly for testing, development, or similar purposes.

The folders contain the Pywikibot library, as well as some additional optional assets. The structure is read-only, the configuration and user files are stored in ~/.pywikibot folder in your home directory instead.

The basic usage then looks as follows:

$ python3 /shared/pywikibot/core_stable/ <script> <parameters>

Further steps and details to setup the Pywikibot user configuration are the same as for your PC and can be found in Pywikibot Manual.

There are also several approaches to make the use of shared library easier:

.profile alias
You can make the use much smoother by adding an alias to the .profile file in your home directory.
<source lang=shell-session>$ nano ~/.profile</syntaxhighlight>
Add the following line and save:
<source lang=bash>alias"python3 /shared/pywikibot/core_stable/"</syntaxhighlight>
Since the next time you login into Toolforge you can just use <script>.
Bash wrapper script
The similar approach to the one above uses the bash script to do the same:
<source lang=shell-session>$ nano ~/pwb</syntaxhighlight>
Add the following code and save:
<source lang=bash>
  1. !/bin/bash

python3 /shared/pywikibot/core_stable/ "$@" </syntaxhighlight>

Make it executable by:
<source lang=shell-session>$ chmod +x ~/pwb</syntaxhighlight>
Now you can use just ~/pwb <script>.
Symlink (shortcut)
Another approach uses the symlink (shortcut) in your home folder. Basic example could be:
<source lang=shell-session>$ ln -s /shared/pywikibot/core_stable pwb</syntaxhighlight>
Then you can use python3 pwb/ <script>.
Advanced techniques
PATH environment variable
You can take the advantage of the .profile alias approach and modify the PATH variable here instead. This approach will create and use configuration and user files in the folder you are currently located in, not in .pywikibot. This can be helpful if you need to switch between several possible configurations or accounts (as addressed in T67196)
<source lang=shell-session>

$ mkdir bin $ cd bin $ ln -s /usr/bin/python3 python $ echo "export PATH=~/bin:$PATH:/shared/pywikibot/core_stable" >> ~/.profile </syntaxhighlight>

The use since the next time you login into Toolforge is the same as for the alias.
Folder of symlinks
You can also take the advantage of the Symlink (shortcut) approach and create symlinks for the files inside the firmly created pywikibot folder instead. This will give you more control over the library folder:
<source lang=shell-session>

$ mkdir pywikibot $ cd pywikibot $ for i in /shared/pywikibot/core_stable/*; do ln -s $i; done </syntaxhighlight>

Now you can use the library from the pywikibot folder as you may be used to from your PC:
<source lang=shell-session>

$ cd pywikibot $ python3 <script> </syntaxhighlight>

Be aware of two issues with this advanced approach:
  1. You can not edit anything in the subfolders. This can be worked around by creating them too and symlinking also everything inside them, but you must keep track of added/deleted files/folders in Pywikibot development and have the structure up-to-date.
  2. The new files/folders in the library's root directory will be missing in your directory. This can be worked around by running the ln -s command for the contents regularly (either by hand or by using Crontab schedule).

Using a local clone

This works:


mv core core.old
mv core_stable core
rm -Rf core/.git
cd core
pip install -r requirements.txt

cd scripts/i18n
git pull
rm -Rf .git

cd ../..
if [ ! -e '' ]
  then cp