-1

I need to pass path of subdirectory of a temporary directory to another function.

Real scenario:

User will upload a zip or tar archive then it will be extracted inside a temporary directory, now I need the path of that extracted directory.

is there a way to get path of a subdirectory in case we couldn't known the name of that directory? Here's what I'm doing in code;

views.py

            if form.is_valid():
               deployment = TarWithDocker()
               deployment.name = form.cleaned_data['name']
               deployment.user = request.user
               deployment.archive = form.cleaned_data['archive']
               deployment.save()
               tmpdir = tempfile.mkdtemp()
               saved_umask = os.umask(0o077)
               path = os.path.join(tmpdir)
               arpath = deployment.archive.path
               patoolib.extract_archive(arpath, outdir=path)
               client = docker.from_env()
               dirp = os.path.join(path)
               client.images.build(path=# Here i need to pass sub path of directory of temp dir   #}
                                , gzip=False, tag='newdep')
               os.umask(saved_umask)
               shutil.rmtree(tmpdir)

Help me, please! Thanks in Advance!

Harald Nordgren
  • 11,693
  • 6
  • 41
  • 65
Abdul Rehman
  • 5,326
  • 9
  • 77
  • 150

1 Answers1

2

Given that you are running

client = docker.from_env()
client.images.build(...)

I will assume that you are expecting a Dockerfile in the archive.

So after extracting the data into tmpdir, try this:

import glob

docker_glob = os.path.join(tmpdir, "*", "Dockerfile")
docker_file = glob.glob(docker_glob)[0]
docker_folder = os.path.dirname(docker_file)
client.images.build(path=docker_folder, ...)
Harald Nordgren
  • 11,693
  • 6
  • 41
  • 65
  • it works for me, I was needed that for two scenerios one with dockerfile but another scenario couldn't have dockerfile, so in that case I have used package.json instead of dockerfile and it works fine! Thanks @Harald – Abdul Rehman Jul 31 '17 at 16:54
  • Now, again it's throwing: `docker_file = glob.glob(docker_glob)[0] IndexError: list index out of range` Error! – Abdul Rehman Nov 04 '17 at 10:54