Scrapy, a fast high-level web crawling & scraping framework for Python.
add .scrapy when outside spider too, add tests
Elias Dorneles25bd3b3fea44
diff --git a/scrapy/utils/project.py b/scrapy/utils/project.pyindex 9258469ee..fd457fdcb 100644--- a/scrapy/utils/project.py+++ b/scrapy/utils/project.py@@ -12,6 +12,7 @@ from scrapy.exceptions import NotConfiguredENVVAR = 'SCRAPY_SETTINGS_MODULE'DATADIR_CFG_SECTION = 'datadir'+def inside_project():scrapy_module = os.environ.get('SCRAPY_SETTINGS_MODULE')if scrapy_module is not None:@@ -23,6 +24,7 @@ def inside_project():return Truereturn bool(closest_scrapy_cfg())+def project_data_dir(project='default'):"""Return the current project data dir, creating it if it doesn't exist"""if not inside_project():@@ -39,16 +41,21 @@ def project_data_dir(project='default'):os.makedirs(d)return d+def data_path(path, createdir=False):"""If inside the project and path is relative, return the given path- as relative the project data dir, otherwise return it unmodified+ as relative to the project data dir, otherwise return it unmodified"""- if inside_project() and not isabs(path):- path = join(project_data_dir(), path)+ if not isabs(path):+ if inside_project():+ path = join(project_data_dir(), path)+ else:+ path = join('.scrapy', path)if createdir and not exists(path):os.makedirs(path)return path+def get_project_settings():if ENVVAR not in os.environ:project = os.environ.get('SCRAPY_PROJECT', 'default')diff --git a/tests/test_utils_project.py b/tests/test_utils_project.pynew file mode 100644index 000000000..d99a22272--- /dev/null+++ b/tests/test_utils_project.py@@ -0,0 +1,34 @@+import unittest+import os+import tempfile+import shutil+import contextlib+from scrapy.utils.project import data_path+++def inside_a_project():+ prev_dir = os.getcwd()+ project_dir = tempfile.mkdtemp()++ try:+ os.chdir(project_dir)+ with open('scrapy.cfg', 'w') as f:+ # create an empty scrapy.cfg+ f.close()++ yield project_dir+ finally:+ os.chdir(prev_dir)+ shutil.rmtree(project_dir)+++class ProjectUtilsTest(unittest.TestCase):+ def test_data_path_outside_project(self):+ self.assertEquals('.scrapy/somepath', data_path('somepath'))++ def test_data_path_inside_project(self):+ with inside_a_project() as proj_path:+ expected = os.path.join(proj_path, '.scrapy', 'somepath')+ self.assertEquals(expected, data_path('somepath'))+ self.assertEquals('/absolute/path', data_path('/absolute/path'))