Commit c0cf24d1 authored by Marco Mariani's avatar Marco Mariani

runner: simpler file handling + open() defaults to 'r'

parent 6980a891
...@@ -219,6 +219,6 @@ class FileBrowser(object): ...@@ -219,6 +219,6 @@ class FileBrowser(object):
if not isText(realfile): if not isText(realfile):
return "FILE ERROR: Cannot display binary file, please open a text file only!" return "FILE ERROR: Cannot display binary file, please open a text file only!"
if not truncate: if not truncate:
return open(realfile, 'r').read() return open(realfile).read()
else: else:
return tail(open(realfile, 'r'), 0) return tail(open(realfile), 0)
...@@ -290,7 +290,7 @@ class SlaprunnerTestCase(unittest.TestCase): ...@@ -290,7 +290,7 @@ class SlaprunnerTestCase(unittest.TestCase):
self.assertEqual(response['code'], 1) self.assertEqual(response['code'], 1)
realFolder = response['result'].split('#')[0] realFolder = response['result'].split('#')[0]
#Check git configuration #Check git configuration
config = open(os.path.join(realFolder, '.git/config'), 'r').read() config = open(os.path.join(realFolder, '.git/config')).read()
assert "slaprunner@nexedi.com" in config and "Slaprunner test" in config assert "slaprunner@nexedi.com" in config and "Slaprunner test" in config
#Checkout to slaprunner branch, this supose that branch slaprunner exit #Checkout to slaprunner branch, this supose that branch slaprunner exit
response = loadJson(self.app.post('/newBranch', response = loadJson(self.app.post('/newBranch',
...@@ -366,7 +366,7 @@ class SlaprunnerTestCase(unittest.TestCase): ...@@ -366,7 +366,7 @@ class SlaprunnerTestCase(unittest.TestCase):
self.assertTrue(response['result']) self.assertTrue(response['result'])
self.assertTrue(os.path.exists(self.app.config['software_root'])) self.assertTrue(os.path.exists(self.app.config['software_root']))
self.assertTrue(os.path.exists(self.app.config['software_log'])) self.assertTrue(os.path.exists(self.app.config['software_log']))
assert "test-application" in open(self.app.config['software_log'], 'r').read() assert "test-application" in open(self.app.config['software_log']).read()
sr_dir = os.listdir(self.app.config['software_root']) sr_dir = os.listdir(self.app.config['software_root'])
self.assertEqual(len(sr_dir), 1) self.assertEqual(len(sr_dir), 1)
createdFile = os.path.join(self.app.config['software_root'], sr_dir[0], createdFile = os.path.join(self.app.config['software_root'], sr_dir[0],
...@@ -436,7 +436,7 @@ class SlaprunnerTestCase(unittest.TestCase): ...@@ -436,7 +436,7 @@ class SlaprunnerTestCase(unittest.TestCase):
follow_redirects=True)) follow_redirects=True))
self.assertTrue(response['result']) self.assertTrue(response['result'])
#Check that all partitions has been created #Check that all partitions has been created
assert "create-file" in open(self.app.config['instance_log'], 'r').read() assert "create-file" in open(self.app.config['instance_log']).read()
instanceDir = os.listdir(self.app.config['instance_root']) instanceDir = os.listdir(self.app.config['instance_root'])
for num in range(int(self.app.config['partition_amount'])): for num in range(int(self.app.config['partition_amount'])):
partition = os.path.join(self.app.config['instance_root'], partition = os.path.join(self.app.config['instance_root'],
......
...@@ -43,17 +43,11 @@ def getSession(config): ...@@ -43,17 +43,11 @@ def getSession(config):
""" """
Get the session data of current user. Get the session data of current user.
Returns: Returns:
a list of user information or False if fail to read data. a list of user information or None if the file does not exist.
""" """
user_path = os.path.join(config['etc_dir'], '.users') user_path = os.path.join(config['etc_dir'], '.users')
user = ""
if os.path.exists(user_path): if os.path.exists(user_path):
f = open(user_path, 'r') return open(user_path).read().split(';')
user = f.read().split(';')
f.close()
if type(user) == type(""):
return False
return user
def saveSession(config, account): def saveSession(config, account):
...@@ -74,11 +68,9 @@ def saveSession(config, account): ...@@ -74,11 +68,9 @@ def saveSession(config, account):
backup = False backup = False
try: try:
if os.path.exists(user): if os.path.exists(user):
f = open(user, 'r')
#backup previous data #backup previous data
data = f.read() data = open(user).read()
open('%s.back' % user, 'w').write(data) open('%s.back' % user, 'w').write(data)
f.close()
backup = True backup = True
if not account[1]: if not account[1]:
account[1] = data.split(';')[1] account[1] = data.split(';')[1]
...@@ -125,7 +117,7 @@ def requestInstance(config, software_type=None): ...@@ -125,7 +117,7 @@ def requestInstance(config, software_type=None):
# Write it to conf file for later use # Write it to conf file for later use
open(software_type_path, 'w').write(software_type) open(software_type_path, 'w').write(software_type)
elif os.path.exists(software_type_path): elif os.path.exists(software_type_path):
software_type = open(software_type_path, 'r').read() software_type = open(software_type_path).read()
else: else:
software_type = 'default' software_type = 'default'
...@@ -281,15 +273,14 @@ def config_SR_folder(config): ...@@ -281,15 +273,14 @@ def config_SR_folder(config):
for path in os.listdir(config['software_link']): for path in os.listdir(config['software_link']):
cfg_path = os.path.join(config['software_link'], path, config_name) cfg_path = os.path.join(config['software_link'], path, config_name)
if os.path.exists(cfg_path): if os.path.exists(cfg_path):
cfg = open(cfg_path, 'r').read().split("#") cfg = open(cfg_path).read().split("#")
if len(cfg) != 2: if len(cfg) != 2:
continue # there is a broken config file continue # there is a broken config file
list.append(cfg[1]) list.append(cfg[1])
folder_list = os.listdir(config['software_root']) folder_list = os.listdir(config['software_root'])
if len(folder_list) < 1: if len(folder_list) < 1:
return return
curent_project = open(os.path.join(config['etc_dir'], ".project"), curent_project = open(os.path.join(config['etc_dir'], ".project")).read()
'r').read()
projects = curent_project.split("/") projects = curent_project.split("/")
name = projects[len(projects) - 2] name = projects[len(projects) - 2]
for folder in folder_list: for folder in folder_list:
...@@ -317,7 +308,7 @@ def loadSoftwareRList(config): ...@@ -317,7 +308,7 @@ def loadSoftwareRList(config):
for path in os.listdir(config['software_link']): for path in os.listdir(config['software_link']):
cfg_path = os.path.join(config['software_link'], path, config_name) cfg_path = os.path.join(config['software_link'], path, config_name)
if os.path.exists(cfg_path): if os.path.exists(cfg_path):
cfg = open(cfg_path, 'r').read().split("#") cfg = open(cfg_path).read().split("#")
if len(cfg) != 2: if len(cfg) != 2:
continue # there is a broken config file continue # there is a broken config file
list.append(dict(md5=cfg[1], path=cfg[0], title=path)) list.append(dict(md5=cfg[1], path=cfg[0], title=path))
......
...@@ -104,7 +104,7 @@ def dologout(): ...@@ -104,7 +104,7 @@ def dologout():
@login_required() @login_required()
def configRepo(): def configRepo():
public_key = open(app.config['public_key'], 'r').read() public_key = open(app.config['public_key']).read()
account = getSession(app.config) account = getSession(app.config)
return render_template('cloneRepository.html', workDir='workspace', return render_template('cloneRepository.html', workDir='workspace',
public_key=public_key, name=account[3].decode('utf-8'), public_key=public_key, name=account[3].decode('utf-8'),
...@@ -162,7 +162,7 @@ def runSoftwareProfile(): ...@@ -162,7 +162,7 @@ def runSoftwareProfile():
@login_required() @login_required()
def viewSoftwareLog(): def viewSoftwareLog():
if os.path.exists(app.config['software_log']): if os.path.exists(app.config['software_log']):
result = tail(open(app.config['software_log'], 'r'), lines=1500) result = tail(open(app.config['software_log']), lines=1500)
else: else:
result = 'Not found yet' result = 'Not found yet'
return render_template('viewLog.html', type='software', return render_template('viewLog.html', type='software',
...@@ -246,7 +246,7 @@ def runInstanceProfile(): ...@@ -246,7 +246,7 @@ def runInstanceProfile():
@login_required() @login_required()
def viewInstanceLog(): def viewInstanceLog():
if os.path.exists(app.config['instance_log']): if os.path.exists(app.config['instance_log']):
result = open(app.config['instance_log'], 'r').read() result = open(app.config['instance_log']).read()
else: else:
result = 'Not found yet' result = 'Not found yet'
return render_template('viewLog.html', type='instance', return render_template('viewLog.html', type='instance',
...@@ -392,10 +392,10 @@ def getFileContent(): ...@@ -392,10 +392,10 @@ def getFileContent():
return jsonify(code=0, return jsonify(code=0,
result="Can not open a binary file, please select a text file!") result="Can not open a binary file, please select a text file!")
if 'truncate' in request.form: if 'truncate' in request.form:
content = tail(open(file_path, 'r'), int(request.form['truncate'])) content = tail(open(file_path), int(request.form['truncate']))
return jsonify(code=1, result=content) return jsonify(code=1, result=content)
else: else:
return jsonify(code=1, result=open(file_path, 'r').read()) return jsonify(code=1, result=open(file_path).read())
else: else:
return jsonify(code=0, result="Error: No such file!") return jsonify(code=0, result="Error: No such file!")
...@@ -494,8 +494,8 @@ def slapgridResult(): ...@@ -494,8 +494,8 @@ def slapgridResult():
request.form['log'] == "instance": request.form['log'] == "instance":
log_file = request.form['log'] + "_log" log_file = request.form['log'] + "_log"
if os.path.exists(app.config[log_file]): if os.path.exists(app.config[log_file]):
log_result = readFileFrom(open(app.config[log_file], 'r'), log_result = readFileFrom(open(app.config[log_file]),
int(request.form['position'])) int(request.form['position']))
return jsonify(software=software_state, instance=instance_state, return jsonify(software=software_state, instance=instance_state,
result=(instance_state or software_state), content=log_result) result=(instance_state or software_state), content=log_result)
...@@ -561,7 +561,7 @@ def saveParameterXml(): ...@@ -561,7 +561,7 @@ def saveParameterXml():
def getSoftwareType(): def getSoftwareType():
software_type_path = os.path.join(app.config['etc_dir'], ".software_type.xml") software_type_path = os.path.join(app.config['etc_dir'], ".software_type.xml")
if os.path.exists(software_type_path): if os.path.exists(software_type_path):
return jsonify(code=1, result=open(software_type_path, 'r').read()) return jsonify(code=1, result=open(software_type_path).read())
return jsonify(code=1, result="default") return jsonify(code=1, result="default")
...@@ -573,7 +573,7 @@ def getParameterXml(request): ...@@ -573,7 +573,7 @@ def getParameterXml(request):
default = '<?xml version="1.0" encoding="utf-8"?>\n<instance>\n</instance>' default = '<?xml version="1.0" encoding="utf-8"?>\n<instance>\n</instance>'
return jsonify(code=1, result=default) return jsonify(code=1, result=default)
if request == "xml": if request == "xml":
parameters = open(param_path, 'r').read() parameters = open(param_path).read()
else: else:
parameters = readParameters(param_path) parameters = readParameters(param_path)
if type(parameters) == type('') and request != "xml": if type(parameters) == type('') and request != "xml":
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment