-
Notifications
You must be signed in to change notification settings - Fork 2
/
winetricks.py
121 lines (106 loc) · 4 KB
/
winetricks.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# -*- coding: utf-8 -*-
############################################################################
# Copyright (C) 2012 by Dennis Schwerdel, Thomas Schmidt #
# #
# #
# This program is free software; you can redistribute it and or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the #
# Free Software Foundation, Inc., #
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
############################################################################
import sys, os, json, subprocess
from config import *
try:
from collections import OrderedDict
def json_load(fp):
return json.load(fp, object_pairs_hook=OrderedDict)
except: #Python 2.6
OrderedDict = dict
json_load = json.load
CACHEFILE = os.path.join(SWINE_PATH, "winetricks.cache")
VERSION_FIELD = "_version"
WINETRICKS = "winetricks"
def discover():
global WINETRICKS
for path in [SWINE_PATH] + os.environ['PATH'].split(":"):
path = os.path.join(path, "winetricks")
if os.path.exists(path):
WINETRICKS = path
break
def loadCache():
cache = {}
if os.path.exists(CACHEFILE):
with open(CACHEFILE, "r") as fp:
cache = json_load(fp)
return cache
def saveCache():
cache[VERSION_FIELD] = version
with open(CACHEFILE, "w") as fp:
json.dump(cache, fp, indent=2)
def call(args, plain=False, shell=False):
if isinstance(args, str):
args = [args]
if not plain:
args = [WINETRICKS]+args
proc = subprocess.Popen(args, stdout=subprocess.PIPE, shell=shell)
(stdout, stderr) = proc.communicate()
if proc.returncode:
raise Exception(tr("Command failed: %s\nReturn code: %d\nOutput: %s\nError: %s") % (" ".join(args), proc.returncode, stdout, stderr))
return stdout
def showHelp():
call("help")
def download():
dst = os.path.join(SWINE_PATH, "winetricks")
call("curl http://winetricks.org/winetricks --silent --remote-time --time-cond %(path)s -o %(path)s && chmod +x %(path)s" % {"path": dst}, plain=True, shell=True)
init()
def getCacheVersion():
return cache.get(VERSION_FIELD, None)
def loadOptions():
global cache
cache = OrderedDict()
sections = call("list").splitlines()
for sec in sections:
cache[sec] = OrderedDict()
items = call([sec, "list"]).splitlines()
for item in items:
(name, desc) = item.split(" ", 1)
cache[sec][name] = desc.strip()
def sections():
return filter(lambda s: s != VERSION_FIELD, cache.keys())
def options(section):
return cache[section].iteritems()
def init():
global cache, version
cache = loadCache()
discover()
try:
version = call("--version").strip()
except:
version = None
if version:
if getCacheVersion() != version:
print >>sys.stderr, tr("Loading winetricks entries...")
try:
loadOptions()
except:
cache = {}
saveCache()
else:
print >>sys.stderr, tr("WARNING: Winetricks binary not found")
cache = {}
saveCache()
def debug():
for sec in sections():
print sec
for (name, desc) in options(sec):
print "\t%s\t%s" % (name, desc)