Replacing all functions with 'pass', syncing private to public github repo

284 views Asked by At

I'd like to create a repository for a proprietary python module, similarly how this python package mlfinlabs. They have emptied out all functions like this:

    def bet_size_dynamic(current_pos, max_pos, market_price, forecast_price, cal_divergence=10, cal_bet_size=0.95,
                         func='sigmoid'):
        pass
    
    
    def bet_size_budget(events_t1, sides):   
        pass

I found the libcst module, that parses the source code and you can do operations on it.

Is there a better practice of doing this? eg.: github actions? I can't find any other good solution.

1

There are 1 answers

0
semyd On BEST ANSWER
  1. Github action to sync from private to public repo. Using this github action: https://github.com/marketplace/actions/github-repo-sync

  2. libcst (project site) to strip functions afterwards (also as a github action step). Below is the codemod that you should put either

  • in the libcst folder (\Lib\site-packages\libcst\codemod\commands)
  • in your repo directory and then specify it .libcst.codemod.yaml (this is needed if you run the codemod on github actions):
# String that LibCST should look for in code which indicates that the
# module is generated code.

generated_code_marker: '@generated'

# Command line and arguments for invoking a code formatter. Anything
# specified here must be capable of taking code via stdin and returning
# formatted code via stdout.

formatter: ['black', '-']

# List of regex patterns which LibCST will evaluate against filenames to
# determine if the module should be touched.

blacklist_patterns: ['.*replace_functions\.py']

# List of modules that contain codemods inside of them.

modules:

- 'libcst.codemod.commands'
- 'mycodemod' # THIS IS THE NAME OF THE FOLDER

# Absolute or relative path of the repository root, used for providing
# full-repo metadata. Relative paths should be specified with this file
# location as the base.

repo_root: '.'

put your codemod then under:

project_root
|--- mycodemod
         |--- __init__.py    (this is an empty file)
         |--- replace_functions.py (the codemod pasted below)

In replace_functions.py put this snippet:

from ast import Expression, literal_eval
from typing import Union

import libcst as cst
from libcst.codemod import CodemodContext, VisitorBasedCodemodCommand
from libcst.codemod.visitors import AddImportsVisitor


class ReplaceFunctionCommand(VisitorBasedCodemodCommand):
    # Add a description so that future codemodders can see what this does.
    DESCRIPTION: str = "Replaces the body of a function with pass."

    def __init__(self, context: CodemodContext) -> None:
        # Initialize the base class with context, and save our args.
        print("this happens")
        super().__init__(context)


    def leave_FunctionDef(self, original_node: cst.FunctionDef, updated_node: cst.FunctionDef) -> cst.FunctionDef:
        replace_function = cst.FunctionDef(
            name=updated_node.name, 
            params=cst.Parameters(), 
            body= cst.SimpleStatementSuite((cst.Pass(),)), 
            returns=None)
        
        return replace_function