Package: reorder-editable
reorder_editableNaive implementation to reorder myeasy-install.pthfileIt is meant to be used to make sure editable namespace packages in myeasy-install.pthare in a specific order.Editable Namespace Packages?To expand:Editable: A package that is installed in editable mode (likepip install -e), i.e., if you make any changes to the code, your changes are reflected immediately. Is useful for packages that you change very often, or while developing. See thesitemodule docsfor more information on how this modifiessys.pathNamespace Packages: Namespace packages let you split a package across multiple directories on disk, merging any submodules into the parent package. For more info, seePEP420Sidenote: A namespace package is typically installed usingsetuptools.find_namespace_packages, instead ofsetuptools.find_packagesSo, an editable, namespace package is multiple directories on disk all installed as a single package. If any changes are made to any of the directories, the package updates immediately.This is the current strategyHPIuses for extension. I can keep up to date withupstreamand managemy own modulesby installing both (or more) like:pip install -e /local/clone/of/karlicoss/HPI
pip install -e /local/clone/of/seanbreckenridge/HPIThis creates a file in your python installation that looks like this:$cat~/.local/lib/python3.9/site-packages/easy-install.pth
/home/sean/Repos/karlicoss/HPI
/home/sean/Repos/seanbreckenridge/HPI... to link those installs to the paths you specified.However, for namespace packages in particular, the order that those directories appear in theeasy-install.pthmatter. Since items ineasy-install.pthare added tosys.pathin order, that determines which order python searches for packages in when trying to resolve imports.For example, given the following structure:.
├── my_HPI
│   ├── package_name
│   │   ├── a.py
│   │   └── b.py
│   └── setup.py
└── upstream_HPI
    ├── package_name
    │   ├── a.py
    │   └── c.py
    └── setup.pyIfeasy-install.pthwas ordered:my_HPI
upstream_HPIimport package_name.awould importmy_HPI/package_name/a.py, instead ofupstream_HPI/package_name/a.py, because that's the first item it matched in theeasy-install.pth. This process is described much more technically in thePEPThis is pretty much a native plugin system, as it lets me overlay specific files/modules with personal changes in my directory structure, while keeping up to date with the upstream changes. There's very little overhead since all I'm doing is adding python files to a local directory and the globally installed package immediately updates.In the example above, I can still importpackage_name.bandpackage_name.cas normal, even though they're in different directory structures.Now - to the problem this aims to solve.There is no way to manage youreasy-install.pthfile, to make sure packages are in a defined order.In particular, I wantmy repositoryto be abovemy fork of the upstream repo, as that means I'm able to override files from upstream with my own changes, while maintaining two separate directories - which prevents me from running into merge conflicts. While developing, I may end up uninstalling/reinstalling one or more of my local clones of theHPIpackages, and that leads to it resolving to a file from the upstream repository, when I was expecting my own -- leading to confusing and difficult to debug errors.The script itself is pretty basic. Alleasy-install.pthis lines of absolute paths pointing to directories, so this just takes the directories you pass as positional arguments and makes sure they're in that order in youreasy-install.pthfile by shuffling it around.I don't believe this breaks and built-in python/pip behaviour, but please open a PR/Issue if you think there's an issue/this could be improved.Should be noted that if you've already imported a namespace modulethe__path__is cached(which determines the import order), so this (probably?) won't work if you re-order theeasy-install.pthfile after the module has already been loaded -- at least not for that python process/unless you re-import it by messing withsys.modulesand re-import the library.Still - at least this tells me when it breaks, and fixes it for the next time python runs, so I don't have to worry about it/do it manually.The actual hack that runs in myHPIconfiguration script, so I never have to think about this again:defrepo(name:str)->str:returnpath.join(environ["REPOS"],name)# https://github.com/seanbreckenridge/reorder_editable# if my easy-install.pth file was ordered wrong, fix it and exit!fromreorder_editableimportEditableifEditable().reorder([repo("HPI"),repo("HPI-fork")]):# this is true if we actually reordered the path, else path was already orderedprint("easy-install.pth was ordered wrong! It has been reordered, exiting to apply changes...",file=sys.stderr,)sys.exit(0)Note: Either install all editable packages as--user, or all at the system level. Otherwise, the lines of text/packages are split across two separateeasy-install.pthfiles, and its not possible to reorder.InstallationRequirespython3.7+To install with pip, run:python3 -m pip install reorder_editableCan always be accessed likepython3 -m reorder_editable, if your python local bin directory isn't on your$PATHUsageUsage: reorder_editable [OPTIONS] COMMAND [ARGS]...

  Manage your editable packages - your easy-install.pth file

Options:
  --help  Show this message and exit.

Commands:
  cat      print easy-install.pth contents
  check    check easy-install.pth
  locate   print easy-install.pth file location
  reorder  reorder easy-install.pthTo reorder:Usage: reorder_editable reorder [OPTIONS] DIRECTORY...

  If the order specified in your easy-install.pth doesn't match the order of
  the directories specified as positional arguments, reorder them so that it
  does. This always places items you're reordering at the end of your easy-
  install.pth so make sure to include all items you care about the order of

  Also fails if one of the paths you provide doesn't exist, or it isn't
  already in you easy-install.pth

  e.g.
  reorder_editable reorder ./path/to/repo /another/path/to/repo

  If ./path/to/repo was below /another/path/to/repo, this would reorder
  items in your config file to fix it so that ./path/to/repo is above
  /another/path/to/repo

Options:
  -e, --easy-install-location TEXT
                                  Manually provide path to easy-install.pth
  --help                          Show this message and exit.As an example using the descriptions above, If I wanted to make suremy_HPIwas aboveupstream_HPI, I'd run:$python3-mreorder_editablereorder./my_HPI./upstream_HPITestsgitclone'https://github.com/seanbreckenridge/reorder_editable'cd./reorder_editable
pipinstall'.[testing]'mypy./reorder_editable
pytest