Skip to content

Instantly share code, notes, and snippets.

@devdave
Created September 18, 2018 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devdave/c7355fe83c2dc7e6bc63e7e3abb4c174 to your computer and use it in GitHub Desktop.
Save devdave/c7355fe83c2dc7e6bc63e7e3abb4c174 to your computer and use it in GitHub Desktop.
Virtualenvwrapper-win post project hook to make a komodo project file.
Dislaimer
#########
I've been using Komodo IDE since the mid-90's but I AM NOT AN ACTIVE STATE EMPLOYEE. This is on your ownware with a I don't care license.
No warranty of the code is offered, read the code, and only install it if you understand what it does.
Semi-rant
#########
Pre-retirement, it was my in-house rule that all projects used virtualenv. It made it easier to `pip freeze > requirements.txt`
dependancies. Last thing I ever wanted to experience was having a "Oh my god everything is broken" call and spend an hour reverse
engineering dependencies on some piece of code that was responsible for the problem. Similarly when I was bootstrapping a new
code monkey my company probably spent 2000~5000 USD to find, the last thing I wanted was them spending a week trying to recreate our
work environment. Similarly if my company was still stock on some alpha version of something bizarre like six for development,
that was 45-110 USD a hour of lost productive use of the shiny new employee (victim).
Notes
#####
This could work for the posix compliant version of virtualenvwrapper with a change to the shell script.
That said, given the small potential audience I decided not to make this a pypi package.
I used f-strings a lot so I believe minimum version is Python 3.7
Pro-tip: WindowsKey+Break (BRK) will open the `System` setup panel, click on `Advanced system settings` on the left tab, click `Environment Variables` and
modify the TOP list box PATH variable to make global changes. Remembering this shortcut will come in handy below.
How this works is mentioned here https://github.com/davidmarble/virtualenvwrapper-win#hooks
If there is a bug in the code, the virtualenv will already exist. That's one of the reasons for printing out the context variables used
in making the project file. Worst case you can make a project and copy/paste the paths into it (assuming they were correct).
`%~dp0` is a window's stock shell magic variable that provides the directory of the actively called batch file. I have no idea what it will do
if you try to use this with powershell.
Setup
#####
Create a global/system level python install and ensure the scripts dir is in the path. Don't worry about conflicts as virtualenv
will put the activated venv at the top of $PATH (%PATH%).
1. Follow: https://github.com/davidmarble/virtualenvwrapper-win#installation
NOTE: virtualenvwrapper-win should be installed to the global/system install
2. MUST follow: https://github.com/davidmarble/virtualenvwrapper-win#workon_home-optional
3. MAY follow: https://github.com/davidmarble/virtualenvwrapper-win#pywin-optional
4. NOTE: To allow for multiple Python versions, I recommend install to your root drive (eg C:)
Copy `postmkvirtualenv.bat` and `postmkvirtualenv.py` to your global/system python `Scripts` directory.
`mkproject some_venv` will create a new venv in virtualenvwrapper-win's Env directory (if you didn't set it, it will be `%USERPROFILE%\Env`
the postmkproject hook will generate a new some_venv.komodoproject file alongside a empty .komodotools.
NOTE: Because the project is created outside of Komodo, templates are not viable (to my knowledge)
After running mkproject and if Komodo is in the $PATH/%PATH% directory. I don't think ActiveState will be motivated to bake something
like this in the IDE fork of Komodo because virtualenv's haven't become ubiquitious yet (which is unfortunate). A plugin might be
a better idea but I've never been good at those.
rem Assumes we are in the global/system python install dir.
CALL %~dp0\..\python.exe %~dp0\postmkvirtualenv.py
"""
No effort is made to prevent this from blowing up in a non-userfriendly way.
DO NOT USE a shabang line, eg #!Python37 as that will revert to the venv, you don't want that.
"""
import uuid
import os
import pprint
from pathlib import Path
#I am not using lxml or similar to generate a proper komodoproject file, that feels insane
PRJ_FILE_TEMPLATE = """
<?xml version="1.0" encoding="UTF-8"?>
<!-- Komodo Project File - DO NOT EDIT -->
<project id="{my_uuid}" kpf_version="5" name="{prj_name}.komodoproject">
<preference-set idref="{my_uuid}" id="project" preftype="project">
<string relative="path" id="import_dirname"></string>
<string id="last_local_directory">None</string>
<string id="last_remote_directory">None</string>
<string id="pip3DefaultInterpreter">{my_pip}</string>
<long id="prefs_version">1</long>
<string id="python3DefaultInterpreter">{my_exec}</string>
</preference-set>
</project>
""".strip()
def make_koprjfile():
ctx = {}
#don't do the tabbed =, apparently its against PEP8 so pyflakes, pylint, and PEP8 will probably freak out on you
venv_dir = Path(os.environ['VIRTUAL_ENV'])
prj_path = Path((venv_dir / ".project").read_text().strip()) #This will blow up if it doesn't exist
ctx["my_uuid"] = str(uuid.uuid4())
ctx["prj_name"] = venv_dir.name
ctx["my_pip"] = venv_dir / "Scripts" / "pip.exe"
ctx['my_exec'] = venv_dir / "Scripts" / "python.exe"
ctx['prj_path'] = prj_path
pprint.pprint(ctx)
for test in ["my_pip", "my_exec"]:
assert ctx[test].exists(), f"bad path for {test}"
(prj_path / f"{venv_dir.name}.komodoproject").write_text(PRJ_FILE_TEMPLATE.format(**ctx))
(prj_path / ".komodotools").mkdir()
if __name__ == "__main__":
make_koprjfile()
@devdave
Copy link
Author

devdave commented Sep 18, 2018

I meant the mid 00's and not 90's, I think it was Komodo v2 was my intro to the IDE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment