Debian Codegraph @

python/dolfin_utils/test/paths.py

raw
1 # -*- coding: utf-8 -*-
2 """Utilities for getting dolfin repository paths."""
3
4 # Copyright (C) 2014-2014 Martin Sandve Alnæs
5 #
6 # This file is part of DOLFIN.
7 #
8 # DOLFIN is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Lesser General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # DOLFIN is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public License
19 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
20
21
22 import os
23
24
25 def find_parent_dir_with_file(filepath, filename):
26 "Return the first parent directory containing a file with filename."
27 # Start with given file path
28 d = os.path.dirname(os.path.abspath(filepath))
29 t = ''
30 # Look for a particular filename
31 while not os.path.isfile(os.path.join(d, filename)):
32 d, t = os.path.split(d)
33 # Return the directory where we found the file
34 return d
35
36
37 def find_parent_dir_with_name(filepath, dirname):
38 "Return the first parent directory called dirname."
39 # Start with given file path
40 d = os.path.dirname(os.path.abspath(filepath))
41 t = ''
42 # Look for a particular dirname
43 while t != dirname:
44 d, t = os.path.split(d)
45 # Return the found directory
46 return os.path.join(d, t)
47
48
49 def find_testdir(filepath):
50 "Return the test/ directory filepath is contained in."
51 return find_parent_dir_with_name(filepath, "test")
52
53
54 def find_demodir(filepath):
55 "Return the demo/ directory filepath is contained in."
56 return find_parent_dir_with_name(filepath, "demo")
57
58
59 def find_rootdir(filepath):
60 """Return the root directory filepath is contained in.
61
62 Assuming the existence of the files "INSTALL" and "AUTHORS".
63 """
64 a = find_parent_dir_with_file(filepath, "INSTALL")
65 b = find_parent_dir_with_file(filepath, "AUTHORS")
66 assert a == b
67 return a
68
69
70 def find_cppsourcedir(filepath):
71 d = find_rootdir(filepath)
72 d = os.path.join(d, "dolfin")
73 return d
74
75
76 def find_pysourcedir(filepath):
77 d = find_rootdir(filepath)
78 d = os.path.join(d, "site-packages")
79 return d
80
81
82 def find_docdir(filepath):
83 d = find_rootdir(filepath)
84 d = os.path.join(d, "doc")
85 return d
86
87
88 def find_benchdir(filepath):
89 d = find_rootdir(filepath)
90 d = os.path.join(d, "bench")
91 return d