SVN maintenance.
[blender.git] / source / blender / python / intern / bpy_app.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Contributor(s): Campbell Barton
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 #include "bpy_app.h"
26
27 #include "BLI_path_util.h"
28
29 #include "BKE_blender.h"
30 #include "BKE_global.h"
31 #include "structseq.h"
32
33 #ifdef BUILD_DATE
34 extern char build_date[];
35 extern char build_time[];
36 extern char build_rev[];
37 extern char build_platform[];
38 extern char build_type[];
39 #endif
40
41 static PyTypeObject BlenderAppType;
42
43 static PyStructSequence_Field app_info_fields[] = {
44         {"version", "The Blender version as a tuple of 3 numbers. eg. (2, 50, 11)"},
45         {"version_string", "The Blender version formatted as a string"},
46         {"home", "The blender home directory, normally matching $HOME"},
47         {"binary_path", "The location of blenders executable, useful for utilities that spawn new instances"},
48         {"debug", "Boolean, set when blender is running in debug mode (started with -d)"},
49         {"background", "Boolean, True when blender is running without a user interface (started with -b)"},
50
51         /* buildinfo */
52         {"build_date", "The date this blender instance was built"},
53         {"build_time", "The time this blender instance was built"},
54         {"build_revision", "The subversion revision this blender instance was built with"},
55         {"build_platform", "The platform this blender instance was built for"},
56         {"build_type", "The type of build (Release, Debug)"},
57         {0}
58 };
59
60 static PyStructSequence_Desc app_info_desc = {
61         "bpy.app",     /* name */
62         "This module contains application values that remain unchanged during runtime.",    /* doc */
63         app_info_fields,    /* fields */
64         (sizeof(app_info_fields)/sizeof(PyStructSequence_Field)) - 1
65 };
66
67 static PyObject *make_app_info(void)
68 {
69         extern char bprogname[]; /* argv[0] from creator.c */
70
71         PyObject *app_info;
72         int pos = 0;
73
74         app_info = PyStructSequence_New(&BlenderAppType);
75         if (app_info == NULL) {
76                 return NULL;
77         }
78
79 #define SetIntItem(flag) \
80         PyStructSequence_SET_ITEM(app_info, pos++, PyLong_FromLong(flag))
81 #define SetStrItem(str) \
82         PyStructSequence_SET_ITEM(app_info, pos++, PyUnicode_FromString(str))
83 #define SetObjItem(obj) \
84         PyStructSequence_SET_ITEM(app_info, pos++, obj)
85
86         SetObjItem(Py_BuildValue("(iii)", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION));
87         SetObjItem(PyUnicode_FromFormat("%d.%02d (sub %d)", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION));
88         SetStrItem(BLI_gethome());
89         SetStrItem(bprogname);
90         SetObjItem(PyBool_FromLong(G.f & G_DEBUG));
91         SetObjItem(PyBool_FromLong(G.background));
92
93         /* build info */
94 #ifdef BUILD_DATE
95         SetStrItem(build_date);
96         SetStrItem(build_time);
97         SetStrItem(build_rev);
98         SetStrItem(build_platform);
99         SetStrItem(build_type);
100 #else
101         SetStrItem("Unknown");
102         SetStrItem("Unknown");
103         SetStrItem("Unknown");
104         SetStrItem("Unknown");
105         SetStrItem("Unknown");
106 #endif
107
108 #undef SetIntItem
109 #undef SetStrItem
110 #undef SetObjItem
111
112         if (PyErr_Occurred()) {
113                 Py_CLEAR(app_info);
114                 return NULL;
115         }
116         return app_info;
117 }
118
119 PyObject *BPY_app_struct(void)
120 {
121         PyObject *ret;
122
123         PyStructSequence_InitType(&BlenderAppType, &app_info_desc);
124
125         ret= make_app_info();
126
127         /* prevent user from creating new instances */
128         BlenderAppType.tp_init = NULL;
129         BlenderAppType.tp_new = NULL;
130         
131         return ret;
132 }