wemake-services / wemake-services/django-split-settings

Support loading of `.pyc` files

Open
#563 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.2k
Forks
73
Avg merge
6h 3m
Merged PRs (30d)
3

Description

Currently the process for loading the file run the file through compile and then exec.

https://github.com/wemake-services/django-split-settings/blob/d7ca866f22ff642e59976a099547047823f3259e/split_settings/tools.py#L111-L114

This works perfectly for .py files (thanks for the project, by the way, it is really awesome).

I have a project where the files are precompiled into .pyc during building the production version, and all .py files are deleted from the codebase.

This causes two issues:

  1. The names in the include call are no longer present. They've been deleted in a previous build step.
  2. Even if I use a workaround for issue 1, the .pyc files do not work with the compile + exec approach.

I would like to request the ability to load .pyc files by addressing both of these points.

  1. Allowing scripts to have a fallback, that is only loaded if the file is not found. Maybe that can be built into the optional implementation.

    include(
        optional("abc.py", "abc.pyc"),
        # ...
    )
    
  2. Allowing .pyc files to be loaded. I've done it locally using this.

    import marshal
    import types
    
    with open(included_file, 'rb') as to_compile:
        to_compile.seek(16)
        compiled_code = marshal.load(to_compile)
        if isinstance(compiled_code, types.CodeType):
            exec(compiled_code, scope)
    

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in split_settings/tools.py at the linked loading code and then trace the existing optional implementation. Determine how the include API should express a fallback and how the loader should handle the .pyc format; done means both fallback includes and precompiled files load successfully without their source files.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.