user3085931
user3085931

Reputation: 1803

Why is Python not using the working directory and how to fix it?

I have a problem, which takes rather long to explain, so I'm gonna break it down to the interesting parts. I try to parse some kconfigs with the kconfiglib.py. Here is my code (of config.in)

[intro_code]
source "b.in"

My code is exactly doing what it should at [intro_code]. I'm not sure sure if source is a python-keyword, but it's about entering/including a different file (here with the name b.in, located in the same folder as the executed script)

The error message: Now when running the script, the following error message appears:

IOError: config.in:51: sourced file "b.in" (expands to
"b.in") not found. Perhaps base_dir
(argument to Config.__init__(), currently
"$srctree") is set to the wrong value.

What i did to solve: I tried to change the working directory to Sourcecodes (that's where config.in and b.in are located) on top of the config.in:

os.chdir("/home/fedor/Sourcecodes") 
retval = os.getcwd()
print "Directory changed successfully %s" % retval

during execution it returns:

Directory changed successfully /home/fedor/Sourcecodes

So the directory seems to be alright, but the same error message appears anyways.

It is working when: using the absolute path to b.in (like source "/home/fedor/Sourcecodes/b.in") then it's working, but that's not how I could use the script.

Does anybody know how to tell python to look in the same directory as the executed script ?

[Edit:] as wished, the full code:

I call python /home/fedor/Sourcecodes/Kconfiglib/examples/print_tree.py /home/fedor/Sourcecodes/config.in

the print_tree.py from kconfiglib-examples:

# Prints a tree of all items in the configuration

import kconfiglib
import sys
import os

os.chdir("/home/fedor/BR1311") 


retval = os.getcwd()

print "Directory changed successfully %s" % retval

def print_with_indent(s, indent):
    print (" " * indent) + s

def print_items(items, indent):
    for item in items:
        if item.is_symbol():
            print_with_indent("config {0}".format(item.get_name()), indent)
        elif item.is_menu():
            print_with_indent('menu "{0}"'.format(item.get_title()), indent)
            print_items(item.get_items(), indent + 2)
        elif item.is_choice():
            print_with_indent('choice', indent)
            print_items(item.get_items(), indent + 2)
        elif item.is_comment():
            print_with_indent('comment "{0}"'.format(item.get_text()), indent)

conf = kconfiglib.Config(sys.argv[1])
print_items(conf.get_top_level_items(), 0)

the config.in

menu "Audio and video applications"
config BR2_PACKAGE_WIPE
    bool "wipe"
    help
      Wipe is a little command for securely erasing files
      from magnetic media. It compiles under various unix platforms.

      http://wipe.sourceforge.net

config BR2_PACKAGE_BONNIE
    bool "bonnie++"
    depends on BR2_INSTALL_LIBSTDCPP
    depends on BR2_USE_MMU # fork()
    help
      Filesystem tester

      http://www.coker.com.au/bonnie++/

comment "bonnie++ needs a toolchain w/ C++"
    depends on BR2_USE_MMU
    depends on !BR2_INSTALL_LIBSTDCPP
endmenu

source "b.in"

and the b.in (which is exactly the same as config.in but of the missing source command in the end):

menu "Audio and video applications"
config BR2_PACKAGE_WIPE
    bool "wipe"
    help
      Wipe is a little command for securely erasing files
      from magnetic media. It compiles under various unix platforms.

      http://wipe.sourceforge.net

config BR2_PACKAGE_BONNIE
    bool "bonnie++"
    depends on BR2_INSTALL_LIBSTDCPP
    depends on BR2_USE_MMU # fork()
    help
      Filesystem tester

      http://www.coker.com.au/bonnie++/

comment "bonnie++ needs a toolchain w/ C++"
    depends on BR2_USE_MMU
    depends on !BR2_INSTALL_LIBSTDCPP
endmenu

Upvotes: 0

Views: 243

Answers (1)

bgporter
bgporter

Reputation: 36564

The answer is right in the error message you posted:

Perhaps base_dir (argument to Config.__init__(), currently "$srctree") 
is set to the wrong value.

Looking at the source on github, the __init__ for the Config class takes multiple parameters, all with defaults.

def __init__(self,
             filename = "Kconfig",
             base_dir = "$srctree",
             print_warnings = True,
             print_undef_assign = False):

...and in the docstring for the class, the base_dir parameter is explained:

base_dir (default: "$srctree") -- The base directory relative to which
'source' statements within Kconfig files will work. For the
Linux kernel this should be the top-level directory of the
kernel tree. $-references to environment variables will be
expanded.

The environment variable 'srctree' is set by the Linux makefiles
to the top-level kernel directory. A default of "." would not
work if an alternative build directory is used.

I suspect that if you pass in '/home/fedor/BR1311' to this __init__ instead of changing to it, like:

conf = kconfiglib.Config(sys.argv[1], '/home/fedor/BR1311')

things will work much better.

Upvotes: 1

Related Questions