Permalink
Browse files

Make app-envsel.py python3 compatible

  • Loading branch information...
1 parent 58b1004 commit 769f377a83fc544098895f8cbb647f39ae687ac1 @exaos committed Apr 5, 2012
Showing with 64 additions and 63 deletions.
  1. +64 −63 app-envsel/app-envsel.py
View
@@ -20,8 +20,11 @@
Author: Exaos Lee <Exaos.Lee(at)gmail.com>; Copyright: GPLv3'''
+from __future__ import print_function
+
import os, sys
import os.path as path
+
from base64 import b64decode
from zlib import decompress
@@ -61,20 +64,20 @@ def set_datadir(self, base_dir):
if path.isdir(self._data_dir): self.check_datadir()
def init_datadir(self):
- print "Initializing app-envsel data directory", self._base_dir, "..."
+ print("Initializing app-envsel data directory", self._base_dir, "...")
if not path.isdir(self._data_dir):
- print "\tCreating", self._data_dir, "..."
+ print("\tCreating", self._data_dir, "...")
os.makedirs(self._data_dir)
if not path.isdir(self._env_dir):
- print "\tCreating", self._env_dir, "..."
+ print("\tCreating", self._env_dir, "...")
os.makedirs(self._env_dir)
self.check_apprc()
self.check_sh_rc()
def check_apprc(self):
f_apprc = self._app_rc + ".sh"
if not path.isfile(f_apprc):
- print "\tGenerating %s ..."%(f_apprc)
+ print("\tGenerating %s ..."%(f_apprc))
app_rc_real=decompress(b64decode(app_rc_enc.replace('\n','')))
if self._isSys and self._base_dir != data_dir_sysd:
txt = app_rc_real.replace(data_dir_sysd, self._base_dir)
@@ -84,13 +87,13 @@ def check_apprc(self):
def check_datadir(self):
'''Check the data directory to collect versions of application'''
if not path.isdir(self._data_dir):
- print "ERROR: repository %s is not initialized!"%(self._base_dir)
+ print("ERROR: repository %s is not initialized!"%(self._base_dir))
return
rdata = os.listdir(self._data_dir)
apps = set([f.split('_')[0] for f in rdata])
self._apps = dict()
for a in apps:
- flist = filter(lambda x: x[:len(a)] == a, rdata)
+ flist = [x for x in rdata if x[:len(a)] == a]
finfo = a+'_info'
if finfo not in flist: finfo = ""
else: flist.remove(finfo)
@@ -105,7 +108,7 @@ def check_sh_rc(self, u_shrc='.bashrc', a_shrc='app-rc'):
rc_sappend = '[ -f %s ] && . %s'%(f_rc_a, f_rc_a)
rc_script = [ l.strip() for l in open(f_rc_u).readlines() ]
if rc_sappend not in rc_script:
- print "Writing env code to %s ..."%(f_rc_u)
+ print("Writing env code to %s ..."%(f_rc_u))
open(f_rc_u,'a+').write("\n%s\n"%(rc_sappend))
def check_envsel(self):
@@ -115,75 +118,74 @@ def check_envsel(self):
apps = os.listdir(self._env_dir)
for a in apps:
fn = path.join(self._env_dir, a)
- vv = filter(lambda x: x[0]!='#',
- [l.strip() for l in open(fn).readlines()])
+ vv = [x for x in [l.strip() for l in open(fn).readlines()] if x[0]!='#']
if len(vv)>0: self._envsel[a] = vv[0]
def remove(self,app,ver="",isAll=False):
self.check_datadir()
if app == "": return
- if not self._apps.has_key(app):
- print "Repository has no version for",app
+ if app not in self._apps:
+ print("Repository has no version for",app)
return
if isAll: # Remove all settings for app
for v in self._apps[app][1]:
fn = path.join(self._data_dir, app+"_"+v+".sh")
- print "Removing file",fn
+ print("Removing file",fn)
os.remove(fn)
fn = path.join(self._env_dir, app)
if path.isfile(fn): os.remove(fn)
else:
if ver == "":
- print "Null version"
+ print("Null version")
return
if ver in self._apps[app][1]:
fn = path.join(self._data_dir, app+"_"+ver+".sh")
- print "Removing file",fn
+ print("Removing file",fn)
os.remove(fn)
else:
- print "Version %s:%s is not in repository!"%(app,ver)
+ print("Version %s:%s is not in repository!"%(app,ver))
def register(self, app, ver, vpath):
if app == "" or ver == "" or vpath == "": return
fname = path.join(self._data_dir, app+"_"+ver+".sh")
status = os.system("cp %s %s"%(vpath, fname))
if status != 0:
- print "Failed to register %s_%s"%(app,ver)
+ print("Failed to register %s_%s"%(app,ver))
def get_list(self, app):
if app == "": return
if app not in self._apps:
- print "Warning: %s is not in env-data"%(app)
+ print("Warning: %s is not in env-data"%(app))
else: return self._apps[app]
def list(self):
self.check_datadir()
- for app in self._apps.keys():
- print app,":",self._apps[app][1]
+ for app in list(self._apps.keys()):
+ print(app,":",self._apps[app][1])
def list_long(self):
self.check_datadir()
self.check_envsel()
if self._isSys: r_mark = "s"
else: r_mark = "u"
- for app in self._apps.keys():
+ for app in list(self._apps.keys()):
vsel = ""
- if self._envsel.has_key(app): vsel = self._envsel[app]
- print "\n-------- Application: %s"%(app)
- print "Info file:",
+ if app in self._envsel: vsel = self._envsel[app]
+ print("\n-------- Application: %s"%(app))
+ print("Info file:", sep=' ')
if self._apps[app][0] != "":
- print path.join(self._data_dir,self._apps[app][0])
- else: print "Not set"
+ print(path.join(self._data_dir,self._apps[app][0]))
+ else: print("Not set")
vlen = len(self._apps[app][1])
- print "Versions available:",
- if vlen==0: print "None"
+ print("Versions available:", end=' ')
+ if vlen==0: print("None")
else:
- print vlen
+ print(vlen)
for v in self._apps[app][1]:
if vsel == v: m_sel = "*"
else: m_sel = " "
- print " %s%s %s"%(r_mark,m_sel,v)
- print
+ print(" %s%s %s"%(r_mark,m_sel,v))
+ print()
class app_envsel:
'''
@@ -192,26 +194,25 @@ class app_envsel:
_name --- application's short name
'''
def __init__(self,**kws):
- if not kws.has_key("name"):
+ if "name" not in kws:
raise Exception("App's name must be provided!")
self._name = kws['name']
- if kws.has_key('repo'): self._repo = kws['repo']
+ if 'repo' in kws: self._repo = kws['repo']
if not self._repo:
raise Exception("App's env repository must be given!")
self._elist = self._repo.get_list(self._name)
- if kws.has_key('edir'): self._dir = kws['edir']
+ if 'edir' in kws: self._dir = kws['edir']
else: self._dir = path.join(data_dir_user,"env")
self._fsel = path.join(self._dir, self._name)
- if kws.has_key('finfo'): self._finfo = kws['finfo']
+ if 'finfo' in kws: self._finfo = kws['finfo']
elif self._elist: self._finfo = self._elist[0]
def check_envsel(self):
self._vset = ""
if path.isfile(self._fsel):
- aa = filter(lambda x: x[0]!='#',
- [l.strip() for l in open(self._fsel).readlines()])
+ aa = [x for x in [l.strip() for l in open(self._fsel).readlines()] if x[0]!='#']
if len(aa)>0: self._vset = aa[0]
def remove(self,aver="",allVer=False):
@@ -224,7 +225,7 @@ def remove(self,aver="",allVer=False):
def register(self,ver,vpath): self._repo.register(self._name,ver,vpath)
def list(self):
if not self._elist or len(self._elist[1]) == 0:
- print "No version found for", self._name
+ print("No version found for", self._name)
return
self.check_envsel()
self._repo.check_envsel()
@@ -233,24 +234,24 @@ def list(self):
for v in self._elist[1]:
sel_u, sel_s = " ", " "
if v == self._vset: sel_u = "U"
- if self._repo._envsel.has_key(self._name):
+ if self._name in self._repo._envsel:
if self._repo._isSys and v == self._repo._envsel[self._name]:
sel_s = "S"
- print "%s%s%s %s"%(r_mark, sel_s, sel_u, v)
+ print("%s%s%s %s"%(r_mark, sel_s, sel_u, v))
def set(self,ver):
if ver in self._elist[1]:
if not path.isdir(self._dir):
- print "Creating user selection directory",self._dir
+ print("Creating user selection directory",self._dir)
os.makedirs(self._dir)
open(self._fsel,'w').write("%s\n"%(ver))
self._repo.check_sh_rc()
else:
- print "Version %s is not in repository"%(ver)
+ print("Version %s is not in repository"%(ver))
def unset(self,ver=""):
self.check_envsel()
if self._vset == ver or ver == "" and path.isfile(self._fsel):
- print "Removing %s ..."%(self._fsel)
+ print("Removing %s ..."%(self._fsel))
os.remove(self._fsel)
def cmd_process(args):
@@ -265,14 +266,14 @@ def cmd_process(args):
args.remove("--sys")
args.remove(repo_dir_sys)
else:
- print __doc__
+ print(__doc__)
return
else:
repo_dir_sys = data_dir_sysd
arglen = len(args)
if arglen==0 or args[0]=="":
- print __doc__
+ print(__doc__)
return
if path.isdir(path.join(repo_dir_sys,'data')):
@@ -282,15 +283,15 @@ def cmd_process(args):
repo_user = app_envdata(repo_dir_user)
else: repo_user = None
if args[0] != "init" and not repo_sys and not repo_user:
- print errmsg[0]
+ print(errmsg[0])
return
# init
if args[0]=="init":
if arglen<2:
- print "Usage: app-envsel init <-s/-u>"
- print " -s --- system-wide initial"
- print " -u --- user-wide initial"
+ print("Usage: app-envsel init <-s/-u>")
+ print(" -s --- system-wide initial")
+ print(" -u --- user-wide initial")
elif args[1]=="-s": # system-wide init
app_envdata(repo_dir_sys,isSys=True).init_datadir()
else: # user-wide init
@@ -300,24 +301,24 @@ def cmd_process(args):
elif args[0]=="register" or args[0]=="reg":
usage = "Usage: app-envsel register <app> <ver> <path-to-ver-sh>"
if arglen<4:
- print usage
+ print(usage)
return
else: # register <app> <path-to-ver-sh>
if repo_sys and repo_sys._isOK:
- print "Registering %s:%s to system repository ..."%(args[1], args[2])
+ print("Registering %s:%s to system repository ..."%(args[1], args[2]))
repo_sys.register(args[1], args[2], args[3])
elif repo_user and repo_user._isOK:
- print "Registering %s:%s to user repository ..."%(args[1], args[2])
+ print("Registering %s:%s to user repository ..."%(args[1], args[2]))
repo_user.register(args[1], args[2], args[3])
else:
- print errmsg[0]
+ print(errmsg[0])
return
# remove/rm
elif args[0]=="remove" or args[0]=="rm":
usage = '''Usage: app-envsel remove <app> <-a | ver>'''
if arglen<3:
- print usage
+ print(usage)
else:
ver, isAll = "", False
if args[2] == "-a": isAll = True
@@ -344,8 +345,8 @@ def cmd_process(args):
# set
elif args[0]=="set":
if arglen<3:
- print "Usage: app-envsel set <app> [-s] <ver>"
- print "Option: -s --- set the system-wide version"
+ print("Usage: app-envsel set <app> [-s] <ver>")
+ print("Option: -s --- set the system-wide version")
else:
isSys = False
if arglen>3 and args[2]=="-s": # system-wide
@@ -357,7 +358,7 @@ def cmd_process(args):
ver = args[2]
if isSys:
if not repo_sys or repo_sys._isOK:
- print errmsg[1]
+ print(errmsg[1])
return
repo = repo_sys
else:
@@ -367,18 +368,18 @@ def cmd_process(args):
asel = app_envsel(name=args[1],repo=repo,edir=env_dir)
asel.set(ver)
except:
- print "Failed to set %s:%s!"%(args[1],ver)
+ print("Failed to set %s:%s!"%(args[1],ver))
# unset
elif args[0]=="unset":
if arglen<2:
- print "Usage: app-envsel unset <app> [-s]"
- print "Option: -s --- unset the system-wide version"
+ print("Usage: app-envsel unset <app> [-s]")
+ print("Option: -s --- unset the system-wide version")
else:
isSys = False
if arglen>2 and args[2]=="-s": # system-wide
if not repo_sys or repo_sys._isOK:
- print errmsg[1]
+ print(errmsg[1])
return
isSys = True
repo = repo_sys
@@ -391,12 +392,12 @@ def cmd_process(args):
asel = app_envsel(name=args[1],repo=repo,edir=env_dir)
asel.unset()
except:
- print "Failed to unset %s current version!"%(args[1])
+ print("Failed to unset %s current version!"%(args[1]))
# Display help
else:
- print "Unknown command:", args[0]
- print __doc__
+ print("Unknown command:", args[0])
+ print(__doc__)
if __name__=='__main__':
import sys

0 comments on commit 769f377

Please sign in to comment.