-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathMEW.py
93 lines (69 loc) · 2.5 KB
/
MEW.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
#!/usr/bin/env python
from __future__ import print_function
import platform
import json
import os
import re
import sys
class MEW(object):
package_manager = None
command = None
arguments = None
version = None
distro = None
_package_manager = None
translated = None
_managers = ['apt-get','apt','pacman','dnf','rpm','zypper','emerge','equery']
_superDistros = ['debian','ubuntu','redhat','fedora','sles','opensuse','gentoo']
def __init__(self, package_manager, command, arguments):
self.package_manager = package_manager # package manager in cli
if not self.val_pac_manager():
sys.exit("Error : '{0}' is not a valid Package Manager".format(self.package_manager))
self.command = command # command in cli
self.arguments = arguments # args in cli
self.version = platform.version().lower() # version of linux
self.distro = self.getDistro() # linux distro
self._package_manager = self.getPackageManager() # package manager for this distro
self.translated = self.translate()
def val_pac_manager(self):
return self.package_manager in self._managers
def getDistro(self):
for distro in self._superDistros:
if distro in self.version:
return distro
def getPackageManager(self):
distroData = json.load(open(os.path.join(os.path.realpath(__file__).replace("MEW.pyc","").replace("MEW.py","") ,"data", "distro.json")))
return distroData.get(self.distro)
def translate(self):
pacman_file = os.path.join(os.path.realpath(__file__).replace("MEW.pyc","").replace("MEW.py",""), "data", self.package_manager + ".json")
pacman_data = json.load(open(pacman_file)) # load json of the package manager
if re.match("-Sy+u",self.command):
self.command = "-Syu"
try:
if not pacman_data[self.command]:
return None
return self.format(pacman_data[self.command][self.distro])
except KeyError:
return None
def format(self, translate):
if "$vars" in translate:
if self.checkArgs():
return translate.replace("$vars", ''.join(self.arguments).replace("@",' ')[1:])
else:
translate = None
return translate
def checkArgs(self):
if not len(self.arguments):
print("Error : Arguments must start with '@'")
return False
for args in self.arguments:
if not args.startswith("@"):
print("Error : Arguments must start with '@'")
return False
return True
"""
.replace("MEW.pyc","").replace("MEW.py","")
replaces both of them so that we can get the directory
from the MEW.py file. .pyc is replaced because after it is
a used once then .pyc file is the main __file__
"""