Created 2 years ago
Maintained by jayman
Make sure that certain files contain certain regex.
Members 1

Assert Contains Regex

Assert Contains Regex is a tool that checks to see if files contain specific regular expressions. If a file doesn’t, then the tool can optionally look for the regular expressions in a file with a similar name. I use it to make sure that files contain copying information.

I mainly use Assert Contains Regex as a pre-commit hook. For an example of what to add to your pre-commit configuration, take a look at .pre-commit-config.yaml.

For more detailed usage information, install Assert Contains Regex and run assert_contains_regex --help


The Markdown files in this repo may not appear correctly in Pagure due to this bug.

Windows support

I would like for Assert Contains Regex to work anywhere that Python does. I developed it on Linux exclusively and am unfortunately finding out that I made some poor assumptions that cause problems on Windows. Assert Contains Regex does work on Windows, but it’s a little bit rough around the edges.



Other versions and alternatives (like PyPy) probably work, but they haven’t been tested. Additionally, you’ll have to make sure that your system is using a UTF-8 locale. Run:

python -c "import codecs, locale; print(codecs.lookup(locale.getpreferredencoding(False)).name)"

If it says utf-8, then you’re good to go. If not, then you’ll have to change your system’s locale. Configuring locales is very OS specific, so start looking for documentation. If you can’t figure out how to switch to a Unicode locale or if one isn’t available, then you can try setting the PYTHONUTF8 environment variable to 1. Using PYTHONUTF8 might increase your chances of seeing garbled text, so it’s not recommended. (Source for that locale encoding command:

(Recommended) Create a virtual environment

Using a virtual environment is optional and recommended. Doing so prevents unnecessary changes to the system and makes it easier to restart if something goes wrong.

  1. Change directory to the root of this repo.
  2. Create a virtual environment. In this example, I named the virtual environment “env”. You can name it anything. I would recommend calling it “env” or “venv”.

    python -m venv env
  3. Activate the environment:

    • On Unix-like systems, run: source env/bin/activate
    • On Windows systems, run: env\Scripts\activate.bat
  4. (Optional) Install wheel. I think that this will speed things up later, but I haven’t verified it.

    pip install wheel
  5. (Optional) Update pre-installed packages:

    1. Update pip:

      python -m pip install --upgrade pip
    2. Update Setuptools:

      pip install --upgrade setuptools

The virtual environment will only be active in your current terminal. If you switch to a different one, then you’ll need to activate the virtual environment again.

Install and run

  1. Make sure that you current working directory is the root of this repo.
  2. If you haven’t already, install the build package:

    pip install build
  3. Build Assert Contains Regex:

    python -m build
  4. Install Assert Contains Regex:

    pip install --force-reinstall dist/assert_contains_regex-[…].whl
  5. Run assert_contains_regex

Hints for contributors

  • You can use pre-commit to automatically check your contributions. Follow these instructions to get started (skip the part about creating a .pre-commit-config.yaml file).
  • Some Markdown files fail one of pre-commit’s hooks, but only because lines are too long. I’m OK with those lines being too long, so I skip that hook when I update those files.
  • Every file should declare its own copying information. See the comment at the top of for an example.
  • Python scripts in the packages directory should only contain plain-ASCII characters. This is done to help support systems that aren’t using a Unicode locale.


As an experiment, this repo uses git-bug for tracking issues.