https://pantsbuild.org/ logo
#general
Title
# general
l

loud-laptop-17949

12/09/2021, 12:35 AM
Is there a good way with python code in pants v2 to get a path to the location of a dependency on disk? In our pre-pants code we have some stuff like
Copy code
STATICFILES_DIRS += (  # pylint: disable=undefined-variable
    os.path.join(
        os.environ['VIRTUAL_ENV'],
        'lib/python3.6/site-packages/django/contrib/admin/static',
    ),
that I need to port to pants
where VIRTUAL_ENV is set to the location where we create our virtualenv
h

hundreds-father-404

12/09/2021, 12:37 AM
Pants 2.9 adds an
export
goal that will write a virtualenv
Copy code
❯ ./pants export
17:37:06.81 [INFO] Completed: Building requirements.pex
17:37:07.04 [INFO] Completed: Create virtualenv
Wrote virtualenv for CPython==3.9.* to dist/export/python/virtualenv
Are you looking for something like that?
l

loud-laptop-17949

12/09/2021, 12:41 AM
I would like to avoid that if possible.
i think the virtual_env might be a red herring here
i just want to be able to find the path to the location on-disk of a dependency
h

hundreds-father-404

12/09/2021, 12:43 AM
I believe that would look something like this:
Copy code
❯ ~/.cache/pants/named_caches/pex_root/installed_wheels/ff59bd9da4c781186fecb96e9717c458a98db032/types_PyYAML-5.4.3-py2.py3-none-any.whl/yaml-stubs/
METADATA.toml    cyaml.pyi        events.pyi       reader.pyi       serializer.pyi
__init__.pyi     dumper.pyi       loader.pyi       representer.pyi  tokens.pyi
composer.pyi     emitter.pyi      nodes.pyi        resolver.pyi
constructor.pyi  error.pyi        parser.pyi       scanner.pyi
l

loud-laptop-17949

12/09/2021, 12:43 AM
this is to support collectstatic in django btw
👀 1
h

hundreds-father-404

12/09/2021, 12:44 AM
can you share more about how you're using collectstatic please?
l

loud-laptop-17949

12/09/2021, 12:45 AM
so basically we have python dependencies which come bundled with static assets (js + css) which need to get collected and bundled with the rest of our static assets
in your django settings you can add a list of paths to
STATICFILES_DIRS
which will be searched by the
collectstatic
command
right now, with our virtualenv set up, the paths are predictable
maybe the inpsect module is what I want - https://docs.python.org/3/library/inspect.html
a

ambitious-actor-36781

12/09/2021, 12:52 AM
Copy code
import django
Path(django.__file__).parent/“contrib/admin/static”
?
🚀 1
But doesn’t Django do the STATICFILES_DIRS automatically when you add
django.contrib.admin
to your INSTALLED_APPS ?
h

happy-kitchen-89482

12/09/2021, 6:25 AM
cc @clean-city-64472 who knows more about Pants+Django than anyone
l

loud-laptop-17949

12/09/2021, 8:32 PM
good question @ambitious-actor-36781
h

happy-kitchen-89482

12/09/2021, 8:40 PM
Echoing @ambitious-actor-36781, I'm surprised that you need the
STATICFILES_DIRS
hack pre-Pants.
In the Pants world, how would you want to run collectstatic. For example,
./pants run path/to/manage.py -- collectstatic
?
Is that what you had in mind? Or...
I think that should work for your first-party code's static files, since the CWD for
./pants run
is the repo root. And for 3rdparty stuff, I think Django does some magic based on relative paths from the app's code? I'd need to refresh my memory.
Yeah, memory refreshed. I'm not sure why you have that STATICFILES_DIRS hack. I would have expected all the standard built-in Django apps, including
django.contrib.admin
to have their static files under the
static
directory in the app
Which is collected by default
l

loud-laptop-17949

12/09/2021, 9:25 PM
yeah maybe I can just remove this, could be really old code
found answer to why we need to do this in a comment:
Copy code
# We don't include AppDirectoriesFinder because we rely on gulp to collect all files.
h

happy-kitchen-89482

12/10/2021, 11:17 PM
I don't know what gulp is... brb doing some reading
l

loud-laptop-17949

12/13/2021, 7:02 PM
something js related
I'm not sure I fully understand the process, but I just noting that we have a reason to updated STATICFILES_DIRS
h

happy-kitchen-89482

12/13/2021, 8:43 PM
Hmm the snippet you posted relates specifically to the staticfiles for the standard Django admin app, and I wouldn't have thought gulp was relevant to that?
c

clean-city-64472

12/13/2021, 11:38 PM
I think gulp.js was used 5-10 years ago to basically just bundle a whole bunch of js and css in a single file.
1
l

loud-laptop-17949

12/14/2021, 1:12 AM
yup
(and still used in some places....)
🙂 1
5 Views