-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosextra.py
75 lines (65 loc) · 2.2 KB
/
osextra.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#sdobrev 2011
'extra os-level funcs - execresult, touch, ..'
def execresult( args, encoding =None):
import subprocess, locale
#return subprocess.Popen( args, stdout= subprocess.PIPE ).communicate()[0].strip() #<py2.7
r = subprocess.check_output( args )
if not encoding: encoding= locale.getpreferredencoding()
if encoding: r = r.decode( encoding)
return r.strip()
def touch( f):
import os
return os.utime( f, None)
total = f
import time
s = time.localtime()
tm_year = s.tm_year
tm_mon = tm_mday = 1
tm_hour= int(total/3600)
tm_min = int((total+29)/60)
tm_sec = 0#int(total-tm_min*60)
tm_wday= tm_yday= tm_isdst= -1
s = tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday,tm_yday,tm_isdst
tm = time.mktime(s)
os.utime( f, (tm,tm) )
def save_if_different( filename, txt, makedirs =True,
reader =lambda filename: open( filename).read(),
writer =lambda filename,txt: open( filename, 'w').write( txt ),
writer2=None, #func( filename,...)
):
if makedirs:
import os.path
fpath = os.path.dirname( filename)
if fpath and not os.path.exists( fpath):
os.makedirs( fpath )
try:
old = reader( filename)
except IOError:
old = None
if txt != old:
if writer: writer( filename,txt)
if writer2: writer2( filename,txt)
return True
def globescape( f):
'as of fnmatch doc: no way to escape meta-chars.. hence replace with ?'
for c in '[]*':
f = f.replace( c,'?')
return f
def makedirs( x, exist_ok =True):
'exist_ok=True doesnt work.. being toooo smart. below py-2.7; py-3.5 is fixed'
import os,errno
try:
os.makedirs( x)#, exist_ok =True)
except OSError as e:
if e.errno != errno.EEXIST or not exist_ok: raise
def withoutext( fname, *exts, **kargs):
once = kargs.get('once', True)
ignorecase = kargs.get('ignorecase', False)
for e in exts:
if ignorecase: e = e.lower()
if (fname.lower() if ignorecase else fname).endswith( e):
fname = fname[:-len(e)]
if once: break
return fname
#from distutils.dep_util import newer
# vim:ts=4:sw=4:expandtab