Package: hammurabi
Mass changes made easy.Hammurabi is an extensible CLI tool responsible for enforcing user-defined rules
on a git repository.FeaturesHammurabi integrates well with both git and Github to make sure that the
execution happens on a separate branch and the committed changes are pushed
to the target repository. After pushing to the target repository, a pull
request will be opened.Hammurabi supports several operations (Rules) by default. These Rules can dofile and directory operations like copy, move, create or deletemanipulation of attributes like ownership or access permissions changefile and directory manipulationspiped rule execution (output of a rule is the input of the next rule)children rule execution (output of a rule is the input of the upcoming rules)creating files from Jinja2 templatessend notification on git pushSupported file formats:plain textinijsonyaml(basic, single document operations)tomlUpcoming file format support:hoconCommunityIf you need help or you would like to be part of the Hammurabi community, join us ondiscord.InstallationHammurabi can be installed by runningpip install hammurabiand it requires
Python 3.7.0+ to run. This is the preferred method to install Hammurabi, as it
will always install the most recent stable release. If you don’t havepipinstalled, thisPython installation guidecan guide
you through the process.Installing extrasHammurabi tries to be as tiny as its possible, hence some rules are requiring extra
dependencies to be installed. Please check the documentation of the Rules to know
which dependency is required to use the specific rule.To install hammurabi with an extra package runpip installhammurabi[<EXTRA>],
where<EXTRA>is the name of the extra option. To install multiple extra packages
list the extra names separated by comma as described inpip’s examplessection point
number six.ExtraDescriptionallalias to install all the extras availableinineeded for ini/cfg based rulesujsoninstall if you need faster json manipulatyamlneeded for yaml based rulestemplatingneeded for rules which are using templatesslack-notificationsneeded for slack webhook notificationsConfigurationFor configuration instructions, please visit thedocumentationsite.Usage examplesIn every case, make sure that you clone the target repository prior using Hammurabi.
After cloning the repository, always set the current working directory to the target’s
path. Hammurabi will not clone the target repository or change its execution directory.Enforce registered laws$hammurabienforce[INFO]2020-14-0716:31-Checkoutbranch"hammurabi"[INFO]2020-14-0716:31-Executinglaw"L001"[INFO]2020-14-0716:31-Runningtaskfor"configure file exists"[INFO]2020-14-0716:31-Rule"configure file exists"finishedsuccessfully[INFO]2020-14-0716:31-Runningtaskfor"Minimum clang version is set"[INFO]2020-14-0716:31-Rule"Minimum clang version is set"finishedsuccessfully[INFO]2020-14-0716:31-Runningtaskfor"Minimum icc version is set"[INFO]2020-14-0716:31-Rule"Minimum icc version is set"finishedsuccessfully[INFO]2020-14-0716:31-Runningtaskfor"Minimum lessc version is set"[INFO]2020-14-0716:31-Rule"Minimum lessc version is set"finishedsuccessfully[INFO]2020-14-0716:31-Runningtaskfor"Maximum lessc version is set"[INFO]2020-14-0716:31-Rule"Maximum lessc version is set"finishedsuccessfully[INFO]2020-14-0716:31-Pushingchanges[INFO]2020-14-0716:35-Checkingforopenedpullrequest[INFO]2020-14-0716:35-OpeningpullrequestCustom RulesAlthough the project aims to support as many general operations as it can,
the need for adding custom rules may arise.To extend Hammurabi with custom rules, you will need to inherit a class
fromRuleand define its abstract methods.The following example will show you how to create and use a custom rule.
For more reference please check how the existing rules are implemented.# custom.pyimportshutilimportloggingfromhammurabi.mixinsimportGitMixinfromhammurabi.rules.baseimportRuleclassCustomOwnerChanged(Rule,GitMixin):"""
    Change the ownership of a file or directory to <original user>:admin.
    """def__init__(self,name:str,path:Optional[Path]=None,**kwargs):super().__init__(name,path,**kwargs)defpost_task_hook(self):self.git_add(self.param)deftask(self)->Path:# Since ``Rule`` is setting its 2nd parameter to ``self.param``,# we can use ``self.param`` to access the target file's path.logging.debug('Changing group of "%s" to admin',str(self.param))shutil.chown(self.param,group="admin")returnself.paramCustom PreconditionsRule execution supports preconditions. The logic is simple: if all preconditions
pass, the rule is executed. Otherwise it will be skipped.# custom.pyfromhammurabiimportIsLineExistsclassIsPackage(IsLineExists):def__init__(self,**kwargs):super().__init__(path=Path("Jenkinsfile"),criteria="package",**kwargs)Command line optionsUsage:hammurabi[OPTIONS]COMMAND[ARGS]...HammurabiisanextensibleCLItoolresponsibleforenforcinguser-definedrulesonagitrepository.Findmoreinformationat:https://hammurabi.readthedocs.io/latest/Options:-c,--configPATHSettheconfigurationfile.[default:pyproject.toml]--repositoryTEXTSettheremoterepository.Requiredformat:owner/repository.[default:]--tokenTEXTSetgithubaccesstoken.[default:]--log-level[DEBUG|INFO|WARNING|ERROR]Setlogginglevel.[default:INFO]--install-completion[bash|zsh|fish|powershell|pwsh]Installcompletionforthespecifiedshell.--show-completion[bash|zsh|fish|powershell|pwsh]Showcompletionforthespecifiedshell,tocopyitorcustomizetheinstallation.--helpShowthismessageandexit.Commands:enforceExecuteregisteredlaws.versionPrinthammurabiversion.ContributingHurray, You reached this section, which means you are ready
to contribute.Please read our contibutingguideline. This guideline will
walk you through how can you successfully contribute to
Hammurabi.InstallationFor development you will needpoetryandpre-commit. After poetry installed,
simply runpoetry install -E all. This command will both create the virtualenv
and install all development dependencies for you.Useful make CommandsCommandDescriptionhelpPrint available make commandscleanRemove all artifactsclean-buildRemove build artifactsclean-mypyRemove mypy artifactsclean-pycRemove Python artifactsclean-testRemove test artifactsdocsGenerate Sphinx documentationformatRun several formatterslintRun several linters after formattestRun all tests with coveragetest-unitRun unit tests with coveragetest-integrationRun integration tests with coverageWhy Hammurabi?Hammurabi was the sixth king in the Babylonian dynasty,
which ruled in central Mesopotamia from c. 1894 to 1595 B.C.The Code of Hammurabi was one of the earliest and most
complete written legal codes and was proclaimed by the
Babylonian king Hammurabi, who reigned from 1792 to 1750 B.C.
Hammurabi expanded the city-state of Babylon along the Euphrates
River to unite all of southern Mesopotamia. The Hammurabi code
of laws, a collection of 282 rules, established standards for
commercial interactions and set fines and punishments to meet
the requirements of justice. Hammurabi’s Code was carved onto
a massive, finger-shaped black stone stele (pillar) that was
looted by invaders and finally rediscovered in 1901.