All of lore.kernel.org
 help / color / mirror / Atom feed
* [Cocci] sys.path for Python?
@ 2015-07-24 17:09 Peter Senna Tschudin
  2015-07-24 17:17 ` Luis R. Rodriguez
  2015-07-24 17:21 ` SF Markus Elfring
  0 siblings, 2 replies; 4+ messages in thread
From: Peter Senna Tschudin @ 2015-07-24 17:09 UTC (permalink / raw)
  To: cocci

Hi,

I'm writing a set of .cocci files that will use python. I'm making
some helper function that I want to include in multiple files. I'm
doing something like:

@initialize:python@
@@
from Common import *

which works fine, given spatch is called from the folder where
Commom.py is. If I move to the folder where I want to run spatch, and
try the same semantic patch it fails. The solution for now is:

@initialize:python@
@@
import sys
sys.path.append("/home/peter/devel/git/the-conference/devel/featureccinelle/cocci-files/")
from Common import *

So I ask:

- Can I access from Python the path to the semantic patch? Maybe I
could get that from the cmdline of spatch but that would be
complicated.

- Does it make sense to have a python command to extend the path to
append the folder where the semantic patch is in?

Thank you!


-- 
Peter

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Cocci] sys.path for Python?
  2015-07-24 17:09 [Cocci] sys.path for Python? Peter Senna Tschudin
@ 2015-07-24 17:17 ` Luis R. Rodriguez
  2015-07-24 17:30   ` Peter Senna Tschudin
  2015-07-24 17:21 ` SF Markus Elfring
  1 sibling, 1 reply; 4+ messages in thread
From: Luis R. Rodriguez @ 2015-07-24 17:17 UTC (permalink / raw)
  To: cocci

On Fri, Jul 24, 2015 at 07:09:59PM +0200, Peter Senna Tschudin wrote:
> Hi,
> 
> I'm writing a set of .cocci files that will use python. I'm making
> some helper function that I want to include in multiple files. I'm
> doing something like:
> 
> @initialize:python@
> @@
> from Common import *
> 
> which works fine, given spatch is called from the folder where
> Commom.py is. If I move to the folder where I want to run spatch, and
> try the same semantic patch it fails. The solution for now is:
> 
> @initialize:python@
> @@
> import sys
> sys.path.append("/home/peter/devel/git/the-conference/devel/featureccinelle/cocci-files/")
> from Common import *
> 
> So I ask:
> 
> - Can I access from Python the path to the semantic patch? Maybe I
> could get that from the cmdline of spatch but that would be
> complicated.
> 
> - Does it make sense to have a python command to extend the path to
> append the folder where the semantic patch is in?

Can you try

source_dir = os.path.abspath(os.path.dirname(__file__))
sys.path.append(source_dir)

  Luis

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Cocci] sys.path for Python?
  2015-07-24 17:09 [Cocci] sys.path for Python? Peter Senna Tschudin
  2015-07-24 17:17 ` Luis R. Rodriguez
@ 2015-07-24 17:21 ` SF Markus Elfring
  1 sibling, 0 replies; 4+ messages in thread
From: SF Markus Elfring @ 2015-07-24 17:21 UTC (permalink / raw)
  To: cocci

> sys.path.append("/home/peter/devel/git/the-conference/devel/featureccinelle/cocci-files/")
> from Common import *

Do you prefer to store such a string in your SmPL script
as a temporary workaround?

How do you think about to make the directory name configurable
by a corresponding variable?
Should other shell scripts control and adjust the desired source code
analysis eventually?

Regards,
Markus

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Cocci] sys.path for Python?
  2015-07-24 17:17 ` Luis R. Rodriguez
@ 2015-07-24 17:30   ` Peter Senna Tschudin
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Senna Tschudin @ 2015-07-24 17:30 UTC (permalink / raw)
  To: cocci

Hi Luis,

Thank you for the prompt reply.

> Can you try
>
> source_dir = os.path.abspath(os.path.dirname(__file__))
> sys.path.append(source_dir)

Looks like __file__  is not defined, but cocci.cocci_file contains the
path to the semantic patch, so:

source_dir = os.path.abspath(os.path.dirname(cocci.cocci_file))
sys.path.append(source_dir)

works perfectly fine! Thanks a lot!




-- 
Peter

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-07-24 17:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-07-24 17:09 [Cocci] sys.path for Python? Peter Senna Tschudin
2015-07-24 17:17 ` Luis R. Rodriguez
2015-07-24 17:30   ` Peter Senna Tschudin
2015-07-24 17:21 ` SF Markus Elfring

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.