@@ -52,6 +52,7 @@ import (
5252	"flag" 
5353	"fmt" 
5454	"io" 
55+ 	"io/ioutil" 
5556	"os" 
5657	"os/exec" 
5758	"path/filepath" 
@@ -312,15 +313,19 @@ func (pc *pythonContext) updateCommandLine(ctx context.Context) error {
312313		if  pc .env ["WRAPPER_VERBOSE" ] !=  ""  {
313314			cmdline  =  append (cmdline , "--DEBUG" )
314315		}
315- 		if  pc .debugMode  ==  ModePydevdPycharm  {
316- 			// From the pydevd source, PyCharm wants multiproc 
317- 			cmdline  =  append (cmdline , "--multiproc" )
318- 		}
319316		if  ! pc .wait  {
320317			cmdline  =  append (cmdline , "--continue" )
321318		}
322- 		cmdline  =  append (cmdline , "--file" ) // --file is expected as last argument 
323- 		cmdline  =  append (cmdline , pc .args [1 :]... )
319+ 
320+ 		// --file is expected as last pydev argument, but it must be a file, and so launching with 
321+ 		// a module requires some special handling. 
322+ 		cmdline  =  append (cmdline , "--file" )
323+ 		file , args , err  :=  handlePydevModule (pc .args [1 :])
324+ 		if  err  !=  nil  {
325+ 			return  err 
326+ 		}
327+ 		cmdline  =  append (cmdline , file )
328+ 		cmdline  =  append (cmdline , args ... )
324329		if  pc .wait  {
325330			logrus .Warn ("pydevd does not support wait-for-client" )
326331		}
@@ -356,6 +361,47 @@ func determinePythonMajorMinor(ctx context.Context, launcherBin string, env env)
356361	return 
357362}
358363
364+ // handlePydevModule applies special pydevd handling for a python module.  When a module is 
365+ // found, we write out a python script that uses runpy to invoke the module. 
366+ func  handlePydevModule (args  []string ) (string , []string , error ) {
367+ 	switch  {
368+ 	case  len (args ) ==  0 :
369+ 		return  "" , nil , fmt .Errorf ("no python command-line specified" ) // shouldn't happen 
370+ 	case  ! strings .HasPrefix (args [0 ], "-" ):
371+ 		// this is a file 
372+ 		return  args [0 ], args [1 :], nil 
373+ 	case  ! strings .HasPrefix (args [0 ], "-m" ):
374+ 		// this is some other command-line flag 
375+ 		return  "" , nil , fmt .Errorf ("expected python module: %q" , args )
376+ 	}
377+ 	module  :=  args [0 ][2 :]
378+ 	remaining  :=  args [1 :]
379+ 	if  module  ==  ""  {
380+ 		if  len (args ) ==  1  {
381+ 			return  "" , nil , fmt .Errorf ("missing python module: %q" , args )
382+ 		}
383+ 		module  =  args [1 ]
384+ 		remaining  =  args [2 :]
385+ 	}
386+ 
387+ 	snippet  :=  strings .ReplaceAll (`import sys 
388+ import runpy 
389+ runpy.run_module('{module}', run_name="__main__",alter_sys=True) 
390+ ` , `{module}` , module )
391+ 
392+ 	// write out the temp location as other locations may not be writable 
393+ 	d , err  :=  ioutil .TempDir ("" , "pydevd*" )
394+ 	if  err  !=  nil  {
395+ 		return  "" , nil , err 
396+ 	}
397+ 	// use a skaffold-specific file name to ensure no possibility of it matching a user import 
398+ 	f  :=  filepath .Join (d , "skaffold_pydevd_launch.py" )
399+ 	if  err  :=  ioutil .WriteFile (f , []byte (snippet ), 0755 ); err  !=  nil  {
400+ 		return  "" , nil , err 
401+ 	}
402+ 	return  f , remaining , nil 
403+ }
404+ 
359405func  isEnabled (env  env ) bool  {
360406	v , found  :=  env ["WRAPPER_ENABLED" ]
361407	return  ! found  ||  (v  !=  "0"  &&  v  !=  "false"  &&  v  !=  "no" )
0 commit comments