From 2e342b50c88722fb5d9511a180486bc0fe231519 Mon Sep 17 00:00:00 2001 From: Alvaro Neira Ayuso Date: Mon, 13 Jan 2020 11:37:14 +0100 Subject: Modify methods to use less arguments Now, all the arguments are received from httpparser. Those arguments convert the function in long lines of codes. Passing directly the httpparser, all the function will have less arguments and will be more clear the code. --- src/linux/ogOperations.py | 30 +++++++++++++++++++++----- src/ogRest.py | 55 ++++++++++++++++++++++------------------------- 2 files changed, 51 insertions(+), 34 deletions(-) (limited to 'src') diff --git a/src/linux/ogOperations.py b/src/linux/ogOperations.py index bbdfa3d..5c136fa 100644 --- a/src/linux/ogOperations.py +++ b/src/linux/ogOperations.py @@ -15,7 +15,8 @@ def reboot(): else: subprocess.call(['/sbin/reboot']) -def execCMD(cmd): +def execCMD(httpparser): + cmd = httpparser.getCMD() cmds = cmd.split(" ") try: result = subprocess.check_output(cmds) @@ -24,11 +25,17 @@ def execCMD(cmd): return result.decode('utf-8') -def procsession(disk, partition): +def procsession(httpparser): + disk = httpparser.getDisk() + partition = httpparser.getPartition() + result = subprocess.check_output([OG_PATH + 'interfaceAdm/IniciarSesion', disk, partition], shell=True) return result.decode('utf-8') -def procsoftware(disk, partition, path): +def procsoftware(httpparser, path): + disk = httpparser.getDisk() + partition = httpparser.getPartition() + result = subprocess.check_output([OG_PATH + 'interfaceAdm/InventarioSoftware', disk, partition, path], shell=True) return result.decode('utf-8') @@ -36,11 +43,24 @@ def prochardware(path): result = subprocess.check_output([OG_PATH + 'interfaceAdm/InventarioHardware', path], shell=True) return result.decode('utf-8') -def procsetup(disk, cache, cachesize, partlist): +def procsetup(httpparser): + disk = httpparser.getDisk() + cache = httpparser.getCache() + cachesize = httpparser.getCacheSize() + partlist = httpparser.getPartitionSetup() + for part in partlist: cfg = 'dis=' + disk + '*che=' + cache + '*tch=' + cachesize + '!par=' + part["partition"] + '*cpt='+part["code"] + '*sfi=' + part['filesystem'] + '*tam=' + part['size'] + '*ope=' + part['format'] + '%' subprocess.check_output([OG_PATH + 'interfaceAdm/Configurar', disk, cfg], shell=True) -def procirestore(disk, partition, name, repo, ctype, profile, cid): +def procirestore(httpparser): + disk = httpparser.getDisk() + partition = httpparser.getPartition() + name = httpparser.getName() + repo = httpparser.getRepo() + ctype = httpparser.getType() + profile = httpparser.getProfile() + cid = httpparser.getId() + result = subprocess.check_output([OG_PATH + 'interfaceAdm/RestaurarImagen', disk, partition, name, repo, ctype], shell=True) return result.decode('utf-8') diff --git a/src/ogRest.py b/src/ogRest.py index 7efd7ea..6e13129 100644 --- a/src/ogRest.py +++ b/src/ogRest.py @@ -22,9 +22,9 @@ class jsonResponse(): class ogThread(): # Executing cmd thread - def execcmd(msgqueue, cmd): + def execcmd(msgqueue, httpparser): msgqueue.queue.clear() - msgqueue.put(ogOperations.execCMD(cmd)) + msgqueue.put(ogOperations.execCMD(httpparser)) # Powering off thread def poweroff(): @@ -36,14 +36,14 @@ class ogThread(): ogOperations.reboot() # Process session - def procsession(msgqueue, disk, partition): + def procsession(msgqueue, httpparser): msgqueue.queue.clear() - msgqueue.put(ogOperations.procsession(disk, partition)) + msgqueue.put(ogOperations.procsession(httpparser)) # Process software - def procsoftware(msgqueue, disk, partition, path): + def procsoftware(msgqueue, httpparser, path): msgqueue.queue.clear() - msgqueue.put(ogOperations.procsoftware(disk, partition, path)) + msgqueue.put(ogOperations.procsoftware(httpparser, path)) # Process hardware def prochardware(msgqueue, path): @@ -51,13 +51,13 @@ class ogThread(): msgqueue.put(ogOperations.prochardware(path)) # Process setup - def procsetup(msgqueue, disk, cache, cachesize, partlist): - ogOperations.procsetup(disk, cache, cachesize, partlist) + def procsetup(msgqueue, httpparser): + ogOperations.procsetup(httpparser) # Process image restore - def procirestore(msgqueue, disk, partition, name, repo, ctype, profile, cid): + def procirestore(msgqueue, httpparser): msgqueue.queue.clear() - msgqueue.put(ogOperations.procirestore(disk, partition, name, repo, ctype, profile, cid)) + msgqueue.put(ogOperations.procirestore(httpparser)) class ogResponses(Enum): BAD_REQUEST=0 @@ -107,18 +107,15 @@ class ogRest(): elif ("reboot" in URI): self.process_reboot(client) elif ("shell/run" in URI): - self.process_shellrun(client, httpparser.getCMD()) + self.process_shellrun(client, httpparser) elif ("session" in URI): - self.process_session(client, httpparser.getDisk(), httpparser.getPartition()) + self.process_session(client, httpparser) elif ("software" in URI): - self.process_software(client, httpparser.getDisk(), httpparser.getPartition()) + self.process_software(client, httpparser) elif ("setup" in URI): - self.process_setup(client, httpparser.getDisk(), httpparser.getCache(), httpparser.getCacheSize(), httpparser.getPartitionSetup()) + self.process_setup(client, httpparser) elif ("image/restore" in URI): - self.process_irestore(client, httpparser.getDisk(), - httpparser.getPartition(), httpparser.getName(), - httpparser.getRepo(), httpparser.getType(), - httpparser.getProfile(), httpparser.getId()) + self.process_irestore(client, httpparser) else: client.send(self.getResponse(ogResponses.BAD_REQUEST)) else: @@ -139,13 +136,13 @@ class ogRest(): def process_probe(self, client): client.send(self.getResponse(ogResponses.OK)) - def process_shellrun(self, client, cmd): - if cmd == None: + def process_shellrun(self, client, httpparser): + if httpparser.getCMD() == None: client.send(self.getResponse(ogResponses.BAD_REQUEST)) return try: - ogThread.execcmd(self.msgqueue, cmd) + ogThread.execcmd(self.msgqueue, httpparser) except ValueError as err: print(err.args[0]) client.send(self.getResponse(ogResponses.BAD_REQUEST)) @@ -162,13 +159,13 @@ class ogRest(): jsonResp.addElement('out', self.msgqueue.get()) client.send(self.getResponse(ogResponses.OK, jsonResp)) - def process_session(self, client, disk, partition): - threading.Thread(target=ogThread.procsession, args=(self.msgqueue, disk, partition,)).start() + def process_session(self, client, httpparser): + threading.Thread(target=ogThread.procsession, args=(self.msgqueue, httpparser,)).start() client.send(self.getResponse(ogResponses.OK)) - def process_software(self, client, disk, partition): + def process_software(self, client, httpparser): path = '/tmp/CSft-' + client.ip + '-' + partition - threading.Thread(target=ogThread.procsoftware, args=(self.msgqueue, disk, partition, path,)).start() + threading.Thread(target=ogThread.procsoftware, args=(self.msgqueue, httpparser, path,)).start() client.send(self.getResponse(ogResponses.OK)) def process_hardware(self, client): @@ -179,10 +176,10 @@ class ogRest(): def process_schedule(self, client): client.send(self.getResponse(ogResponses.OK)) - def process_setup(self, client, disk, cache, cachesize, partlist): - threading.Thread(target=ogThread.procsetup, args=(self.msgqueue, disk, cache, cachesize, partlist,)).start() + def process_setup(self, client, httpparser): + threading.Thread(target=ogThread.procsetup, args=(self.msgqueue, httpparser,)).start() client.send(self.getResponse(ogResponses.OK)) - def process_irestore(self, client, disk, partition, name, repo, ctype, profile, cid): - threading.Thread(target=ogThread.procirestore, args=(self.msgqueue, disk, partition, name, repo, ctype, profile, cid,)).start() + def process_irestore(self, client, httpparser): + threading.Thread(target=ogThread.procirestore, args=(self.msgqueue, httpparser,)).start() client.send(self.getResponse(ogResponses.OK)) -- cgit v1.2.3-18-g5258