#14952: New find_commands(management_dir) to support .pyc and .pyo
-------------------------------+--------------------------------------
Reporter: lgx@… | Owner: nobody
Type: Uncategorized | Status: closed
Component: Core (Other) | Version: 1.2
Severity: Normal | Resolution: wontfix
Keywords: find_commands | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Comment (by andreydani):
I'm developing an application that can run either as a local webserver
(frozen using cx_freeze) or as a regular webserver on aws.
Freezing has its advantages for non advanced users: makes the deploy
simpler since they don't have to install python, django, rest-framework
and all dependencies on a user. We also don't have to control multiple
environments in the case user has already a python version.
Although some may consider Django isn't made for this purpose, I consider
this an issue because python, in fact, supports importing from zipfiles
(PEP 273) and also supports running pyc/pyo compiled files.
In fact, django find_commands tries to stick to import behaviour finding
command modules that can be imported, but it fails because it only
considers regular folders (and not folders inside zip) and only commands
with .py extension for this purpose.
I've made a small patch that can solve both problems, although I believe
this could be accomplished using some kind of setting in the case of the
file extension:
{{{
def listdir(path):
"""
Replacement for os.listdir that list dir inside zip files
"""
if not os.path.isdir(path):
pos = path.lower().find('.zip')
if pos >= 0:
pos += 4
zip = path[:pos]
path_within = path[pos + 1:].replace('\\', '/')
from zipfile import ZipFile
zip = ZipFile(zip)
return [a[len(path_within)+1:] for a in zip.namelist()
if a.startswith(path_within)]
return os.listdir(path)
def find_commands(management_dir):
"""
Given a path to a management directory, returns a list of all the
command
names that are available.
Returns an empty list if no commands are defined.
"""
command_dir = os.path.join(management_dir, 'commands')
try:
return [os.path.splitext(f)[0] for f in listdir(command_dir)
if not f.startswith('_') and (f.endswith(('.py', '.pyc'))]
except OSError:
return []
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/14952#comment:7>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/070.de7e20a5291d5a626517535159b95701%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.