Help:Toolforge/Pywikibot/new

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/pwb.py <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.
$ nano ~/.profile
Add the following line and save:
alias pwb.py="python3 /shared/pywikibot/core_stable/pwb.py"
Since the next time you login into Toolforge you can just use pwb.py <script>.
Bash wrapper script
The similar approach to the one above uses the bash script to do the same:
$ nano ~/pwb
Add the following code and save:
#!/bin/bash
python3 /shared/pywikibot/core_stable/pwb.py "$@"
Make it executable by:
$ chmod +x ~/pwb
Now you can use just ~/pwb <script>.
Symlink (shortcut)
Another approach uses the symlink (shortcut) in your home folder. Basic example could be:
$ ln -s /shared/pywikibot/core_stable pwb
Then you can use python3 pwb/pwb.py <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)
$ mkdir bin
$ cd bin
$ ln -s /usr/bin/python3 python
$ echo "export PATH=~/bin:$PATH:/shared/pywikibot/core_stable" >> ~/.profile
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:
$ mkdir pywikibot
$ cd pywikibot
$ for i in /shared/pywikibot/core_stable/*; do ln -s $i; done
Now you can use the library from the pywikibot folder as you may be used to from your PC:
$ cd pywikibot
$ python3 pwb.py <script>
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