Debian Codegraph @

tools/scriptlib.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 ######################################
4 #
5 # Scriptlib
6 #
7 ######################################
8 #
9 # WxGeometrie
10 # Dynamic geometry, graph plotter, and more for french mathematic teachers.
11 # Copyright (C) 2005-2013 Nicolas Pourcelot
12 #
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #
27 ######################################
28
29 import sys, shutil, os, glob, subprocess
30
31 _path_7zip = r"C:\Program Files\7-Zip\7z.exe"
32
33 def norm(path):
34 return os.path.realpath(os.path.normpath(os.path.expanduser(path)))
35
36 def cp(src, dst, **kw):
37 src_found = False
38 for pth in glob.glob(norm(src)):
39 if os.path.isfile(pth):
40 shutil.copy(pth, norm(dst))
41 src_found = True
42 elif os.path.isdir(pth):
43 shutil.copytree(pth, norm(dst))
44 src_found = True
45 if not src_found and not kw.get('quiet', False):
46 print('Warning: %s not found.' % repr(src))
47
48
49 def mv(src, dst):
50 for pth in glob.glob(norm(src)):
51 shutil.move(pth, norm(dst))
52
53 def rm(*paths, **kw):
54 quiet = kw.get('quiet', False)
55 recursive = kw.get('recursive', False)
56 if recursive:
57 cwd = os.getcwd()
58 listcwd = os.listdir('.')
59 for path in paths:
60 pths = glob.glob(norm(path))
61 if not (pths or quiet or recursive):
62 print("Warning: %s not found, couldn't be removed." % path)
63 for pth in pths:
64 if os.path.isfile(pth):
65 os.remove(pth)
66 if recursive:
67 for pth in listcwd:
68 if os.path.isdir(pth):
69 cd(pth)
70 rm(*paths, **kw)
71 cd(cwd)
72
73 def rename(src, dst):
74 os.rename(norm(src), norm(dst))
75
76 def rmdir(*paths, **kw):
77 quiet = kw.get('quiet', False)
78 for path in paths:
79 pths = glob.glob(norm(path))
80 if not (pths or quiet):
81 print("Warning: %s not found, couldn't be removed." % path)
82 for pth in pths:
83 if os.path.isdir(pth):
84 shutil.rmtree(norm(pth))
85
86 def mkdir(path):
87 os.mkdir(norm(path))
88
89 def cd(path):
90 return os.chdir(norm(path))
91
92 def command(string, quiet=False):
93 out = subprocess.Popen(string, shell=True, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).stdout
94 output = out.read()
95 sys.stdout.write(output)
96 out.close()
97 if not quiet:
98 print("Command '%s' was executed." % string)
99 return output
100
101 def zip7(string):
102 command('"%s" %s' %(_path_7zip, string))
103
104 #~ def compil(script):
105 #~ command(sys.executable + ' -O ' + script + ' py2exe')
106
107 def append(srcs, dst):
108 with open(dst, 'wb') as dest:
109 for src in srcs:
110 with open(src, 'rb') as source:
111 dest.write(source.read())
112
113 def pause(string = "\n-- pause --\n"):
114 input(string)
115
116 def ls(path = '.'):
117 if '*' in path:
118 return glob.glob(path)
119 return os.listdir(norm(path))