+# pylint: disable=c0111, c0103
+
import random
import base64
import os
from PLC.Logger import logger
-from PLC.Accessors.Accessors_standard import * # import node accessors
+from PLC.Accessors.Accessors_standard import * # node accessors
# could not define this in the class..
# create a dict with the allowed actions for each type of node
# compute a new key
def compute_key():
# Generate 32 random bytes
- bytes = random.sample(xrange(0, 256), 32)
+ int8s = random.sample(range(0, 256), 32)
# Base64 encode their string representation
- key = base64.b64encode("".join(map(chr, bytes)))
+ key = base64.b64encode(bytes(int8s))
# Boot Manager cannot handle = in the key
- # XXX this sounds wrong, as it might prevent proper decoding
- key = key.replace("=", "")
+ key = key.replace(b"=", b"")
return key
class GetBootMedium(Method):
def bootcd_version (self):
try:
- return file(self.BOOTCDDIR + "/build/version.txt").readline().strip()
+ with open(self.BOOTCDDIR + "/build/version.txt") as feed:
+ return feed.readline().strip()
except:
raise Exception("Unknown boot cd version - probably wrong bootcd dir : {}"\
.format(self.BOOTCDDIR))
if filedir:
if not os.path.exists(filedir):
try:
- os.makedirs (filedir,0777)
+ os.makedirs (filedir,0o777)
except:
raise PLCPermissionDenied("Could not create dir {}".format(filedir))
if node_type not in [ 'regular', 'reservable' ]:
logger.error("GetBootMedium.build_command: unexpected node_type {}".format(node_type))
return command, None
-
+
build_sh_options=""
if "cramfs" in build_sh_spec:
type += "_cramfs"
type,
build_sh_options,
log_file)
-
+
logger.info("The build command line is {}".format(command))
return command, log_file
else:
node = None
# compute a 8 bytes random number
- tempbytes = random.sample (xrange(0,256), 8);
+ tempbytes = random.sample (range(0,256), 8);
def hexa2 (c): return chr((c>>4)+65) + chr ((c&16)+65)
nodename = "".join(map(hexa2,tempbytes))
.format(generic_path, filename))
else:
### return the generic medium content as-is, just base64 encoded
- return base64.b64encode(file(generic_path).read())
+ with open(generic_path) as feed:
+ return base64.b64encode(feed.read())
### config file preview or regenerated
if action == 'node-preview' or action == 'node-floppy':
floppy = self.floppy_contents (node,renew_key)
if filename:
try:
- file(filename,'w').write(floppy)
+ with open(filename, 'w') as writer:
+ writer.write(floppy)
except:
raise PLCPermissionDenied("Could not write into {}".format(filename))
return filename
# create the workdir if needed
if not os.path.isdir(self.WORKDIR):
try:
- os.makedirs(self.WORKDIR,0777)
- os.chmod(self.WORKDIR,0777)
+ os.makedirs(self.WORKDIR,0o777)
+ os.chmod(self.WORKDIR,0o777)
except:
raise PLCPermissionDenied("Could not create dir {}".format(self.WORKDIR))
# store it
floppy_file = "{}/{}.txt".format(self.WORKDIR, nodename)
try:
- file(floppy_file,"w").write(floppy_text)
+ with open(floppy_file, "w") as writer:
+ writer.write(floppy_text)
except:
raise PLCPermissionDenied("Could not write into {}".format(floppy_file))
raise PLCAPIError("{} failed Command line was: {} See logs in {}"\
.format(self.BOOTCDBUILD, command, log_file))
- if not os.path.isfile (node_image):
+ if not os.path.isfile(node_image):
raise PLCAPIError("Unexpected location of build.sh output - {}".format(node_image))
# handle result
self.cleantrash()
return filename
else:
- result = file(node_image).read()
+ with open(node_image) as feed:
+ result = feed.read()
self.trash.append(node_image)
self.cleantrash()
logger.info("GetBootMedium - done with build.sh")