~ubuntu-branches/ubuntu/gutsy/blender/gutsy-security

« back to all changes in this revision

Viewing changes to source/blender/python/api2_2x/Sys.c

  • Committer: Bazaar Package Importer
  • Author(s): Florian Ernst
  • Date: 2005-11-06 12:40:03 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051106124003-3pgs7tcg5rox96xg
Tags: 2.37a-1.1
* Non-maintainer upload.
* Split out parts of 01_SConstruct_debian.dpatch again: root_build_dir
  really needs to get adjusted before the clean target runs - closes: #333958,
  see #288882 for reference

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* 
 
2
 * $Id: Sys.c,v 1.18 2005/04/16 05:25:42 ianwill Exp $
2
3
 *
3
4
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
4
5
 *
24
25
 *
25
26
 * This is a new part of Blender.
26
27
 *
27
 
 * Contributor(s): Willian P. Germano
 
28
 * Contributor(s): Willian P. Germano, Campbell Barton
28
29
 *
29
30
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
30
31
*/
31
32
 
32
 
#include "BKE_utildefines.h"
33
 
#include "PIL_time.h"
 
33
#include <BKE_utildefines.h>
 
34
#include <BLI_blenlib.h>
 
35
#include <DNA_scene_types.h> /* G.scene->r.cfra */
 
36
#include <PIL_time.h>
 
37
#include <Python.h>
 
38
#include <sys/stat.h>
 
39
#include "gen_utils.h"
34
40
 
35
41
#include "Sys.h"
36
42
 
37
 
static PyObject *g_sysmodule = NULL; /* pointer to Blender.sys module */
38
 
 
39
 
PyObject *sys_Init (void)
 
43
/*****************************************************************************/
 
44
/* Python API function prototypes for the sys module.                        */
 
45
/*****************************************************************************/
 
46
static PyObject *M_sys_basename( PyObject * self, PyObject * args );
 
47
static PyObject *M_sys_dirname( PyObject * self, PyObject * args );
 
48
static PyObject *M_sys_join( PyObject * self, PyObject * args );
 
49
static PyObject *M_sys_splitext( PyObject * self, PyObject * args );
 
50
static PyObject *M_sys_makename( PyObject * self, PyObject * args,
 
51
        PyObject * kw );
 
52
static PyObject *M_sys_exists( PyObject * self, PyObject * args );
 
53
static PyObject *M_sys_time( PyObject * self );
 
54
static PyObject *M_sys_sleep( PyObject * self, PyObject * args );
 
55
static PyObject *M_sys_expandpath( PyObject *self, PyObject *args);
 
56
 
 
57
/*****************************************************************************/
 
58
/* The following string definitions are used for documentation strings.      */
 
59
/* In Python these will be written to the console when doing a               */
 
60
/* Blender.sys.__doc__                                                       */
 
61
/*****************************************************************************/
 
62
static char M_sys_doc[] = "The Blender.sys submodule\n\
 
63
\n\
 
64
This is a minimal system module to supply simple functionality available\n\
 
65
in the default Python module os.";
 
66
 
 
67
static char M_sys_basename_doc[] =
 
68
        "(path) - Split 'path' in dir and filename.\n\
 
69
Return the filename.";
 
70
 
 
71
static char M_sys_dirname_doc[] =
 
72
        "(path) - Split 'path' in dir and filename.\n\
 
73
Return the dir.";
 
74
 
 
75
static char M_sys_join_doc[] =
 
76
        "(dir, file) - Join dir and file to form a full filename.\n\
 
77
Return the filename.";
 
78
 
 
79
static char M_sys_splitext_doc[] =
 
80
        "(path) - Split 'path' in root and extension:\n\
 
81
/this/that/file.ext -> ('/this/that/file','.ext').\n\
 
82
Return the pair (root, extension).";
 
83
 
 
84
static char M_sys_makename_doc[] =
 
85
        "(path = Blender.Get('filename'), ext = \"\", strip = 0) -\n\
 
86
Strip dir and extension from path, leaving only a name, then append 'ext'\n\
 
87
to it (if given) and return the resulting string.\n\n\
 
88
(path) - string: a pathname -- Blender.Get('filename') if 'path' isn't given;\n\
 
89
(ext = \"\") - string: the extension to append.\n\
 
90
(strip = 0) - int: strip dirname from 'path' if given and non-zero.\n\
 
91
Ex: makename('/path/to/file/myfile.foo','-01.abc') returns 'myfile-01.abc'\n\
 
92
Ex: makename(ext='.txt') returns 'untitled.txt' if Blender.Get('filename')\n\
 
93
returns a path to the file 'untitled.blend'";
 
94
 
 
95
static char M_sys_time_doc[] =
 
96
        "() - Return a float representing time elapsed in seconds.\n\
 
97
Each successive call is garanteed to return values greater than or\n\
 
98
equal to the previous call.";
 
99
 
 
100
static char M_sys_sleep_doc[] =
 
101
        "(milliseconds = 10) - Sleep for the specified time.\n\
 
102
(milliseconds = 10) - the amount of time in milliseconds to sleep.\n\
 
103
This function can be necessary in tight 'get event' loops.";
 
104
 
 
105
static char M_sys_exists_doc[] =
 
106
        "(path) - Check if the given pathname exists.\n\
 
107
The return value is as follows:\n\
 
108
\t 0: path doesn't exist;\n\
 
109
\t 1: path is an existing filename;\n\
 
110
\t 2: path is an existing dirname;\n\
 
111
\t-1: path exists but is neither a regular file nor a dir.";
 
112
 
 
113
static char M_sys_expandpath_doc[] =
 
114
"(path) - Expand this Blender internal path to a proper file system path.\n\
 
115
(path) - the string path to convert.\n\n\
 
116
Note: internally Blender paths can contain two special character sequences:\n\
 
117
- '//' (at start) for base path directory (the current .blend's dir path);\n\
 
118
- '#' (at ending) for current frame number.\n\n\
 
119
This function expands these to their actual content, returning a valid path.\n\
 
120
If the special chars are not found in the given path, it is simply returned.";
 
121
 
 
122
/*****************************************************************************/
 
123
/* Python method structure definition for Blender.sys module:                */
 
124
/*****************************************************************************/
 
125
struct PyMethodDef M_sys_methods[] = {
 
126
        {"basename", M_sys_basename, METH_VARARGS, M_sys_basename_doc},
 
127
        {"dirname", M_sys_dirname, METH_VARARGS, M_sys_dirname_doc},
 
128
        {"join", M_sys_join, METH_VARARGS, M_sys_join_doc},
 
129
        {"splitext", M_sys_splitext, METH_VARARGS, M_sys_splitext_doc},
 
130
        {"makename", ( PyCFunction ) M_sys_makename,
 
131
         METH_VARARGS | METH_KEYWORDS,
 
132
         M_sys_makename_doc},
 
133
        {"exists", M_sys_exists, METH_VARARGS, M_sys_exists_doc},
 
134
        {"sleep", M_sys_sleep, METH_VARARGS, M_sys_sleep_doc},
 
135
        {"time", ( PyCFunction ) M_sys_time, METH_NOARGS, M_sys_time_doc},
 
136
        {"expandpath", M_sys_expandpath, METH_VARARGS, M_sys_expandpath_doc},
 
137
        {NULL, NULL, 0, NULL}
 
138
};
 
139
 
 
140
/* Module Functions */
 
141
 
 
142
static PyObject *g_sysmodule = NULL;    /* pointer to Blender.sys module */
 
143
 
 
144
PyObject *sys_Init( void )
40
145
{
41
 
        PyObject        *submodule, *dict, *sep;
 
146
        PyObject *submodule, *dict, *sep;
42
147
 
43
 
        submodule = Py_InitModule3("Blender.sys", M_sys_methods, M_sys_doc);
 
148
        submodule = Py_InitModule3( "Blender.sys", M_sys_methods, M_sys_doc );
44
149
 
45
150
        g_sysmodule = submodule;
46
151
 
47
 
        dict = PyModule_GetDict(submodule);
 
152
        dict = PyModule_GetDict( submodule );
48
153
 
49
154
#ifdef WIN32
50
 
        sep = Py_BuildValue("s", "\\");
 
155
        sep = Py_BuildValue( "s", "\\" );
51
156
#else
52
 
        sep = Py_BuildValue("s", "/");
 
157
        sep = Py_BuildValue( "s", "/" );
53
158
#endif
54
159
 
55
 
        if (sep) {
56
 
                Py_INCREF(sep);
57
 
                PyDict_SetItemString(dict, "dirsep" , sep);
58
 
                PyDict_SetItemString(dict, "sep" , sep);
 
160
        if( sep ) {
 
161
                Py_INCREF( sep );
 
162
                PyDict_SetItemString( dict, "dirsep", sep );
 
163
                PyDict_SetItemString( dict, "sep", sep );
59
164
        }
60
165
 
61
166
        return submodule;
62
167
}
63
168
 
64
 
static PyObject *M_sys_basename (PyObject *self, PyObject *args)
 
169
static PyObject *M_sys_basename( PyObject * self, PyObject * args )
65
170
{
66
171
        PyObject *c;
67
172
 
69
174
        char sep;
70
175
        int n, len;
71
176
 
72
 
        if (!PyArg_ParseTuple(args, "s", &name))
73
 
                return EXPP_ReturnPyObjError (PyExc_TypeError,
74
 
                                                        "expected string argument");
75
 
 
76
 
        len = strlen(name);
77
 
 
78
 
        c = PyObject_GetAttrString (g_sysmodule, "dirsep");
79
 
        sep = PyString_AsString(c)[0];
80
 
        Py_DECREF(c);
81
 
 
82
 
        p = strrchr(name, sep);
83
 
 
84
 
        if (p) {
85
 
                n = name + len - p - 1; /* - 1 because we don't want the sep */
86
 
 
87
 
                if (n > FILE_MAXFILE)
88
 
                        return EXPP_ReturnPyObjError(PyExc_RuntimeError, "path too long");
89
 
 
90
 
                strncpy(basename, p+1, n); /* + 1 to skip the sep */
91
 
                basename[n] = 0;
92
 
                return Py_BuildValue("s", basename);
 
177
        if( !PyArg_ParseTuple( args, "s", &name ) )
 
178
                return EXPP_ReturnPyObjError( PyExc_TypeError,
 
179
                                              "expected string argument" );
 
180
 
 
181
        len = strlen( name );
 
182
 
 
183
        c = PyObject_GetAttrString( g_sysmodule, "dirsep" );
 
184
        sep = PyString_AsString( c )[0];
 
185
        Py_DECREF( c );
 
186
 
 
187
        p = strrchr( name, sep );
 
188
 
 
189
        if( p ) {
 
190
                n = name + len - p - 1; /* - 1 because we don't want the sep */
 
191
 
 
192
                if( n > FILE_MAXFILE )
 
193
                        return EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
194
                                                      "path too long" );
 
195
 
 
196
                BLI_strncpy( basename, p + 1, n + 1 );
 
197
                return Py_BuildValue( "s", basename );
93
198
        }
94
199
 
95
 
        return Py_BuildValue("s", name);
 
200
        return Py_BuildValue( "s", name );
96
201
}
97
202
 
98
 
static PyObject *M_sys_dirname (PyObject *self, PyObject *args)
 
203
static PyObject *M_sys_dirname( PyObject * self, PyObject * args )
99
204
{
100
205
        PyObject *c;
101
206
 
103
208
        char sep;
104
209
        int n;
105
210
 
106
 
        if (!PyArg_ParseTuple(args, "s", &name))
107
 
                return EXPP_ReturnPyObjError (PyExc_TypeError,
108
 
                                                        "expected string argument");
109
 
 
110
 
        c = PyObject_GetAttrString (g_sysmodule, "dirsep");
111
 
        sep = PyString_AsString(c)[0];
112
 
        Py_DECREF(c);
113
 
 
114
 
        p = strrchr(name, sep);
115
 
 
116
 
        if (p) {
 
211
        if( !PyArg_ParseTuple( args, "s", &name ) )
 
212
                return EXPP_ReturnPyObjError( PyExc_TypeError,
 
213
                                              "expected string argument" );
 
214
 
 
215
        c = PyObject_GetAttrString( g_sysmodule, "dirsep" );
 
216
        sep = PyString_AsString( c )[0];
 
217
        Py_DECREF( c );
 
218
 
 
219
        p = strrchr( name, sep );
 
220
 
 
221
        if( p ) {
117
222
                n = p - name;
118
223
 
119
 
                if (n > FILE_MAXDIR)
120
 
                        return EXPP_ReturnPyObjError (PyExc_RuntimeError, "path too long");
121
 
 
122
 
                strncpy(dirname, name, n);
123
 
                dirname[n] = 0;
124
 
                return Py_BuildValue("s", dirname);
125
 
        }
126
 
 
127
 
        return Py_BuildValue("s", "."); /* XXX need to fix this? (is crossplatform?)*/
128
 
}
129
 
 
130
 
static PyObject *M_sys_splitext (PyObject *self, PyObject *args)
 
224
                if( n > FILE_MAXDIR )
 
225
                        return EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
226
                                                      "path too long" );
 
227
 
 
228
                BLI_strncpy( dirname, name, n + 1 );
 
229
                return Py_BuildValue( "s", dirname );
 
230
        }
 
231
 
 
232
        return Py_BuildValue( "s", "." );
 
233
}
 
234
 
 
235
static PyObject *M_sys_join( PyObject * self, PyObject * args )
 
236
{
 
237
        PyObject *c = NULL;
 
238
        char *name = NULL, *path = NULL;
 
239
        char filename[FILE_MAXDIR + FILE_MAXFILE];
 
240
        char sep;
 
241
        int pathlen = 0, namelen = 0;
 
242
 
 
243
        if( !PyArg_ParseTuple( args, "ss", &path, &name ) )
 
244
                return EXPP_ReturnPyObjError( PyExc_TypeError,
 
245
                                              "expected string argument" );
 
246
 
 
247
        pathlen = strlen( path ) + 1;
 
248
        namelen = strlen( name ) + 1;   /* + 1 to account for '\0' for BLI_strncpy */
 
249
 
 
250
        if( pathlen + namelen > FILE_MAXDIR + FILE_MAXFILE - 1 )
 
251
                return EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
252
                                              "filename is too long." );
 
253
 
 
254
        c = PyObject_GetAttrString( g_sysmodule, "dirsep" );
 
255
        sep = PyString_AsString( c )[0];
 
256
        Py_DECREF( c );
 
257
 
 
258
        BLI_strncpy( filename, path, pathlen );
 
259
 
 
260
        if( filename[pathlen - 2] != sep ) {
 
261
                filename[pathlen - 1] = sep;
 
262
                pathlen += 1;
 
263
        }
 
264
 
 
265
        BLI_strncpy( filename + pathlen - 1, name, namelen );
 
266
 
 
267
        return Py_BuildValue( "s", filename );
 
268
}
 
269
 
 
270
static PyObject *M_sys_splitext( PyObject * self, PyObject * args )
131
271
{
132
272
        PyObject *c;
133
273
 
135
275
        char sep;
136
276
        int n, len;
137
277
 
138
 
        if (!PyArg_ParseTuple(args, "s", &name))
139
 
                return EXPP_ReturnPyObjError (PyExc_TypeError,
140
 
                                                        "expected string argument");
141
 
 
142
 
        len = strlen(name);
143
 
 
144
 
        c = PyObject_GetAttrString (g_sysmodule, "dirsep");
145
 
        sep = PyString_AsString(c)[0];
146
 
        Py_DECREF(c);
147
 
 
148
 
        dot = strrchr(name, '.');
149
 
 
150
 
        if (!dot) return Py_BuildValue("ss", name, "");
151
 
 
152
 
        p = strrchr(name, sep);
153
 
 
154
 
        if (p) {
155
 
                if (p > dot) return Py_BuildValue("ss", name, "");
 
278
        if( !PyArg_ParseTuple( args, "s", &name ) )
 
279
                return EXPP_ReturnPyObjError( PyExc_TypeError,
 
280
                                              "expected string argument" );
 
281
 
 
282
        len = strlen( name );
 
283
 
 
284
        c = PyObject_GetAttrString( g_sysmodule, "dirsep" );
 
285
        sep = PyString_AsString( c )[0];
 
286
        Py_DECREF( c );
 
287
 
 
288
        dot = strrchr( name, '.' );
 
289
 
 
290
        if( !dot )
 
291
                return Py_BuildValue( "ss", name, "" );
 
292
 
 
293
        p = strrchr( name, sep );
 
294
 
 
295
        if( p ) {
 
296
                if( p > dot )
 
297
                        return Py_BuildValue( "ss", name, "" );
156
298
        }
157
299
 
158
300
        n = name + len - dot;
159
301
 
160
302
        /* loong extensions are supported -- foolish, but Python's os.path.splitext
161
303
         * supports them, so ... */
162
 
        if (n > FILE_MAXFILE || (len - n ) > FILE_MAXFILE)
163
 
                EXPP_ReturnPyObjError(PyExc_RuntimeError, "path too long");
164
 
 
165
 
        strncpy(ext, dot, n);
166
 
        ext[n] = 0;
167
 
        strncpy(path, name, dot - name);
168
 
        path[dot - name] = 0;
169
 
 
170
 
        return Py_BuildValue("ss", path, ext);
171
 
}
172
 
 
173
 
static PyObject *M_sys_time (PyObject *self)
174
 
{
175
 
        double t = PIL_check_seconds_timer();
176
 
        return Py_BuildValue("d", t);
177
 
}
178
 
 
 
304
        if( n > FILE_MAXFILE || ( len - n ) > FILE_MAXFILE )
 
305
                EXPP_ReturnPyObjError( PyExc_RuntimeError, "path too long" );
 
306
 
 
307
        BLI_strncpy( ext, dot, n + 1 );
 
308
        BLI_strncpy( path, name, dot - name + 1 );
 
309
 
 
310
        return Py_BuildValue( "ss", path, ext );
 
311
}
 
312
 
 
313
static PyObject *M_sys_makename( PyObject * self, PyObject * args,
 
314
                                 PyObject * kw )
 
315
{
 
316
        char *path = G.sce, *ext = NULL;
 
317
        int strip = 0;
 
318
        static char *kwlist[] = { "path", "ext", "strip", NULL };
 
319
        char *dot = NULL, *p = NULL, basename[FILE_MAXDIR + FILE_MAXFILE];
 
320
        char sep;
 
321
        int n, len, lenext = 0;
 
322
        PyObject *c;
 
323
 
 
324
        if( !PyArg_ParseTupleAndKeywords( args, kw, "|ssi", kwlist,
 
325
                                          &path, &ext, &strip ) )
 
326
                return EXPP_ReturnPyObjError( PyExc_TypeError,
 
327
                                              "expected one or two strings and an int (or nothing) as arguments" );
 
328
 
 
329
        len = strlen( path ) + 1;       /* + 1 to consider ending '\0' */
 
330
        if( ext )
 
331
                lenext = strlen( ext ) + 1;
 
332
 
 
333
        if( ( len + lenext ) > FILE_MAXDIR + FILE_MAXFILE )
 
334
                return EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
335
                                              "path too long" );
 
336
 
 
337
        c = PyObject_GetAttrString( g_sysmodule, "dirsep" );
 
338
        sep = PyString_AsString( c )[0];
 
339
        Py_DECREF( c );
 
340
 
 
341
        p = strrchr( path, sep );
 
342
 
 
343
        if( p && strip ) {
 
344
                n = path + len - p;
 
345
                BLI_strncpy( basename, p + 1, n );      /* + 1 to skip the sep */
 
346
        } else
 
347
                BLI_strncpy( basename, path, len );
 
348
 
 
349
        dot = strrchr( basename, '.' );
 
350
 
 
351
        /* now the extension: always remove the one in basename */
 
352
        if( dot || ext ) {
 
353
                if( !ext )
 
354
                        basename[dot - basename] = '\0';
 
355
                else {          /* if user gave an ext, append it */
 
356
 
 
357
                        if( dot )
 
358
                                n = dot - basename;
 
359
                        else
 
360
                                n = strlen( basename );
 
361
 
 
362
                        BLI_strncpy( basename + n, ext, lenext );
 
363
                }
 
364
        }
 
365
 
 
366
        return PyString_FromString( basename );
 
367
}
 
368
 
 
369
static PyObject *M_sys_time( PyObject * self )
 
370
{
 
371
        double t = PIL_check_seconds_timer(  );
 
372
        return Py_BuildValue( "d", t );
 
373
}
 
374
 
 
375
static PyObject *M_sys_sleep( PyObject * self, PyObject * args )
 
376
{
 
377
        int millisecs = 10;
 
378
 
 
379
        if( !PyArg_ParseTuple( args, "|i", &millisecs ) )
 
380
                return EXPP_ReturnPyObjError( PyExc_TypeError,
 
381
                                              "expected int argument" );
 
382
 
 
383
        PIL_sleep_ms( millisecs );
 
384
 
 
385
        return EXPP_incr_ret( Py_None );
 
386
}
 
387
 
 
388
static PyObject *M_sys_exists( PyObject * self, PyObject * args )
 
389
{
 
390
        struct stat st;
 
391
        char *fname = NULL;
 
392
        int res = 0, i = -1;
 
393
 
 
394
        if( !PyArg_ParseTuple( args, "s", &fname ) )
 
395
                return EXPP_ReturnPyObjError( PyExc_TypeError,
 
396
                                              "expected string (pathname) argument" );
 
397
 
 
398
        res = stat( fname, &st );
 
399
 
 
400
        if( res == -1 )
 
401
                i = 0;
 
402
        else if( S_ISREG( st.st_mode ) )
 
403
                i = 1;
 
404
        else if( S_ISDIR( st.st_mode ) )
 
405
                i = 2;
 
406
        /* i stays as -1 if path exists but is neither a regular file nor a dir */
 
407
 
 
408
        return Py_BuildValue( "i", i );
 
409
}
 
410
 
 
411
static PyObject *M_sys_expandpath( PyObject * self, PyObject * args )
 
412
{
 
413
        char *path = NULL;
 
414
        char expanded[FILE_MAXDIR + FILE_MAXFILE];
 
415
 
 
416
        if (!PyArg_ParseTuple( args, "s", &path))
 
417
                return EXPP_ReturnPyObjError( PyExc_TypeError,
 
418
                        "expected string argument" );
 
419
 
 
420
        BLI_strncpy(expanded, path, FILE_MAXDIR + FILE_MAXFILE);
 
421
        BLI_convertstringcode(expanded, G.sce, G.scene->r.cfra);
 
422
 
 
423
        return PyString_FromString(expanded);
 
424
}