Commit b99801b7 authored by Nsukami Patrick's avatar Nsukami Patrick
Browse files

feat: new command replace-set-attributes-by-set-cw

parent fffb02454642
......@@ -11,7 +11,7 @@ CLI pattern looks like:
::
GITLAB_TOKEN=... python3 doctor_hg.py [-h] [-a] [-i] [-m] [-n NUMBER] [-q QUERY] {fix-README,add-tox,add-pytest-deprecated-warnings-cmd}
GITLAB_TOKEN=... python3 doctor_hg.py [-h] [-a] [-i] [-m] [-n NUMBER] [-q QUERY] {fix-README,add-tox,add-pytest-deprecated-warnings-cmd,replace-set-attributes-by-set-cw}
Example:
......@@ -29,9 +29,9 @@ The current workflow for cube doctor is:
- download our generated heptapod.trig that contains informations about all the public repositories of logilab's forge https://forge.extranet.logilab.fr/
- do a sparql query on that heptapod.trig to select repositories to target (each command has a default query to select only repositories on which it will be able to work)
- alternatively, if no query is provided (but right now all commands provides a query) all cubes of the cubicweb/cube group will be used
- for each of those cube, apply the refactoring command and show what it would have done but do nothing
- for each of those cubes, apply the refactoring command, show what would have been done, but do nothing
- except if the "-a"/"--apply" flag is provided then the modification is pushed and a new merge request is created (and it is logged in create_mr.log)
- or if the "-i"/"--interactif" flag is provided then for each cube the user will be prompt to tell if it wants to create a MR
- or if the "-i"/"--interactif" flag is provided then for each cube, the user will be prompted to tell if he wants to create a MR
Commands
--------
......@@ -64,6 +64,14 @@ This command is a bit WIP. It will:
- add a base tox.ini in a repository if it doesn't exist
- it will also launch this tox and fail if the tox fails
replace-set-attributes-by-set-cw
--------------------------------
This command will:
- retrieve all the Python files inside the cube
- for each file, replace all occurences of set_attributes calls by set_cw calls
Command CLI arguments
---------------------
......
......@@ -72,6 +72,21 @@ def hg_command(dir, command):
subprocess.check_call(f"cd {dir} && {command}", shell=True)
def check_if_python_script(filename):
if os.path.isdir(filename):
return False
if filename.endswith(".py"):
return True
try:
is_python = "python script" in subprocess.check_output(["file", filename]).decode().lower()
except Exception as exc:
print("Warning: 'file' command not found, cannot check file type because of %s" % exc)
raise
return is_python
class Repository:
def __init__(self, url):
self.url = url
......@@ -95,6 +110,10 @@ class Repository:
def append_to_file(self, file_name, content):
open(os.path.join(self.temporary_directory.name, file_name), "a").write(content)
def all_python_files(self):
repo_files = (f.as_posix() for f in Path(self.temporary_directory.name).glob('**/*'))
yield from (f for f in repo_files if check_if_python_script(f))
def __del__(self):
del self.temporary_directory
......@@ -338,11 +357,25 @@ class AddPypiPublish(Command):
repo.write_file("tox.ini", tox_content)
class ReplaceSetAttributesBySetCW(Command):
BRANCH_NAME = "topic/default/replace-set-attribtues-by-set-cw"
commit_message = "fix: entity.set_attributes renamed to entity.cw_set in cw 3.15 !"
base_query = ('?project lgg:is_cube true . ')
def modify_code(self, cube, repo, root_files):
for f in repo.all_python_files():
file_content_before = repo.read_file(f)
if 'set_attributes' in file_content_before:
file_content_after = file_content_before.replace('set_attributes', 'set_cw')
repo.write_file(f, file_content_after)
commands = {
"fix-README": READMERst().workflow,
"add-tox": AddTox().workflow,
"add-pytest-deprecated-warnings-cmd": CaptureDeprecatedWarnings().workflow,
"add-pypi-publish": AddPypiPublish().workflow,
"replace-set-attributes-by-set-cw": ReplaceSetAttributesBySetCW().workflow,
}
if __name__ == "__main__":
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment