Skip to content

Commit

Permalink
Script to add common constraint to the repo (#154)
Browse files Browse the repository at this point in the history
* Add script to add common constraint to the repo

* Version bump
  • Loading branch information
iamsobanjaved authored Jan 18, 2021
1 parent 092aa96 commit 4ae77a8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
59 changes: 59 additions & 0 deletions edx_repo_tools/add_common_constraint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from os import path

import click

FILES = [
'requirements/constraint.txt',
'requirements/constraints.txt',
'requirements/pins.txt',
]


class CommonConstraint:
"""
CommonConstraint class is responsible for adding common constraint pin in
the constraints file of the repository
"""

def __init__(self):
self.comment = "# Common constraints for edx repos\n"
self.constraint = "-c https://raw.githubusercontent.com/edx/edx-lint/master/edx_lint/files/common_constraints" \
".txt\n "
self.file = self._get_file_name()

def _get_file_name(self):
for file in FILES:
if path.exists(file):
return file

def _read_lines(self):
with open(self.file, 'r') as file:
lines = file.readlines()
return lines

def _insert_constraint(self, lines):
for i in range(len(lines)):
if not lines[i].lstrip().startswith('#'):
lines.insert(i, "\n")
lines.insert(i + 1, self.comment)
lines.insert(i + 2, self.constraint)
return lines

def _write_file(self, lines):
with open(self.file, 'w') as file:
file.writelines(lines)

def update_file(self):
lines = self._read_lines()
lines = self._insert_constraint(lines)
self._write_file(lines)


@click.command()
def main():
constraint = CommonConstraint()
constraint.update_file()


if __name__ == "__main__":
main()
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name='edx-repo-tools',
version='0.2.7',
version='0.2.8',
description="This repo contains a number of tools Open edX uses for working with GitHub repositories.",
long_description=long_description,
license='Apache',
Expand Down Expand Up @@ -46,6 +46,7 @@
'modernize_tox = django3_codemods.config_tools.tox_modernizer:main',
'modernize_openedx_yaml = edx_repo_tools.modernize_openedx_yaml:main',
'modernize_github_actions = django3_codemods.config_tools.github_actions_modernizer:main',
'add_common_constraint = edx_repo_tools.add_common_constraint:main',
],
},
package_data={
Expand Down

0 comments on commit 4ae77a8

Please sign in to comment.