forked from multiSnow/mcomix3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
installer.py
94 lines (80 loc) · 2.91 KB
/
installer.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from argparse import ArgumentParser,ArgumentDefaultsHelpFormatter
from multiprocessing.dummy import Pool
from os import chmod,environ,makedirs,stat,utime,walk
from os.path import dirname,isdir,join,splitext
import shlex
from subprocess import run,DEVNULL
msgfmt_cmd=shlex.split(environ.get('MSGFMT',default='msgfmt'))
def fixdirtime(args):
srcdir,filename,target=args
while True:
sstat,d=stat(srcdir),join(target,srcdir)
if not isdir(d):return
times_ns=(sstat.st_atime_ns,sstat.st_mtime_ns)
utime(d,ns=times_ns,times=None)
chmod(d,sstat.st_mode)
srcdir=dirname(srcdir)
if not srcdir:return
def copy(srcdir,filename,target,pool):
dstdir=join(target,srcdir)
finame,foname=join(srcdir,filename),join(dstdir,filename)
fistat=stat(finame)
times_ns=(fistat.st_atime_ns,fistat.st_mtime_ns)
makedirs(dstdir,exist_ok=True)
with open(finame,mode='rb') as fi,open(foname,mode='wb') as fo:
fo.write(fi.read())
utime(foname,ns=times_ns,times=None)
chmod(foname,fistat.st_mode)
print('install:',finame,foname)
fixdirtime((srcdir,filename,target))
def check_msgfmt():
try:
run(msgfmt_cmd+['-V'],check=True,stdout=DEVNULL,stderr=DEVNULL)
except:
# msgfmt not found or usable
msgfmt_cmd.clear()
print('msgfmt not found')
return
def msgfmt(srcdir,filename,target,pool):
if not msgfmt_cmd:return
if pool:
pool.apply_async(msgfmt,(srcdir,filename,target,None),callback=fixdirtime)
return
dstdir=join(target,srcdir)
b,e=splitext(filename)
finame,foname=join(srcdir,filename),join(dstdir,'{}.mo'.format(b))
makedirs(dstdir,exist_ok=True)
run(msgfmt_cmd+['-o',foname,finame])
print('msgfmt:',foname,finame)
return srcdir,filename,target
install_methods={'.1':copy,
'.py':copy,
'.png':copy,
'.po':msgfmt}
def install(srcdir,filename,target,pool):
b,e=splitext(filename)
if e not in install_methods:return
install_methods[e](srcdir,filename,target,pool)
def scandir(root,dest,pool=None):
for r,dl,fl in walk(root):
for f in fl:
yield r,f,dest,pool
def main():
argp=ArgumentParser(prog='installer',add_help=False,formatter_class=ArgumentDefaultsHelpFormatter)
argp.add_argument('--srcdir',dest='srcdir',default='mcomix',
help='source directory.',metavar='DIR')
argp.add_argument('--target',dest='target',default='target',
help='target directory.',metavar='DIR')
argp.add_argument('-h','--help',action='help',
help='show help options.')
ns=argp.parse_args()
check_msgfmt()
with Pool() as p:
for args in scandir(ns.srcdir,ns.target,pool=p):
install(*args)
p.close()
p.join()
if __name__=='__main__':
exit(main())