def change_dir_by_fd(self,directory_path):
     '''
     The method fchdir() change the current working directory to the directory represented by the file descriptor fd. 
     The descriptor must refer to an opened directory, not an open file.
     #!/usr/bin/python
     Example
     import os, sys
     # First go to the "/var/www/html" directory
     os.chdir("/var/www/html" )
     # Print current working directory
     print "Current working dir : %s" % os.getcwd()
     # Now open a directory "/tmp"
     fd = os.open( "/tmp", os.O_RDONLY )
     # Use os.fchdir() method to change the dir
     os.fchdir(fd)
     # Print current working directory
     print "Current working dir : %s" % os.getcwd()
     # Close opened directory.
     os.close( fd )
     
     http://www.tutorialspoint.com/python/os_open.htm
     '''
     fd = os.open(directory_path,os.O_RDONLY)
     os.fchdir(fd)
     return os.getcwd()
Example #2
0
File: misc.py Project: fluxer/spm
def system_chroot(command):
    ''' Execute command in chroot environment '''
    # prevent stupidity
    if config.ROOT_DIR == '/':
        return

    real_root = os.open('/', os.O_RDONLY)
    mount = whereis('mount')
    try:
        for s in ('/proc', '/dev', '/sys'):
            sdir = config.ROOT_DIR + s
            if not os.path.ismount(sdir):
                if not os.path.isdir(sdir):
                    os.makedirs(sdir)
                subprocess.check_call((mount, '--rbind', s, sdir))
        os.chroot(config.ROOT_DIR)
        os.chdir('/')
        subprocess.check_call(command)
    finally:
        os.fchdir(real_root)
        os.chroot('.')
        os.close(real_root)
        for s in ('/proc', '/dev', '/sys'):
            sdir = config.ROOT_DIR + s
            if os.path.ismount(sdir):
                subprocess.check_call((mount, '--force', '--lazy', sdir))
Example #3
0
def scissors(string, dirname):
    """
    Creates a directory dirname, and populates it with files 'out_*.txt', the nth of which contains the nth
    character of the provided string.

    @param string: a string which is to be disassembled.
    @param dirname: the directory containing text files, in turn containing characters from the disassembled string.
    @return: returns dirname, for convenience of later applying the glue function.
    """

    l = list(string)
    n = len(l)

    current = os.open(os.getcwd(), os.O_DIRECTORY)    # keep track of current directory
    os.mkdir(dirname)                               # put letters in their own directory
    os.chdir(dirname)

    for i in range(1, n+1):
        if i < 10:                      # for consistent filenames
            fnum = repr(0)+repr(i)
        else:
            fnum = repr(i)
        fname = 'out_'+fnum+'.txt'
        f = open(fname, 'w')
        f.write(l[i-1])          # write a character to its file
        f.close()

    os.fchdir(current)
    return dirname
Example #4
0
def get_bitbake_variables(target, env_setup="true"):
    current_dir = os.open(".", os.O_RDONLY)
    os.chdir(os.environ['BUILDDIR'])

    output = subprocess.Popen("%s && bitbake -e %s" % (env_setup, target),
                              stdout=subprocess.PIPE,
                              shell=True,
                              executable="/bin/bash")
    matcher = re.compile('^(?:export )?([A-Za-z][^=]*)="(.*)"$')
    ret = {}
    for line in output.stdout:
        line = line.strip()
        match = matcher.match(line)
        if match is not None:
            ret[match.group(1)] = match.group(2)

    output.wait()
    os.fchdir(current_dir)

    # For some unknown reason, 'MACHINE' is not included in the above list. Add
    # it automagically by looking in environment and local.conf, in that order,
    # if it doesn't exist already.
    if ret.get('MACHINE') is None:
        if os.environ.get('MACHINE'):
            ret['MACHINE'] = os.environ.get('MACHINE')
        else:
            local_fd = open(os.path.join(os.environ['BUILDDIR'], "conf", "local.conf"))
            for line in local_fd:
                match = re.match('^ *MACHINE *\?*= *"([^"]*)" *$', line)
                if match is not None:
                    ret['MACHINE'] = match.group(1)
            local_fd.close()

    return ret
Example #5
0
 def __exit__(self, typ, exc, trc):
     log.debug('Leaving chroot')
     os.fchdir(self.real_root)
     os.chroot('.')
     os.chdir(self.cwd)
     log.debug('Outside chroot')
     return False
Example #6
0
def run_cspp(*viirs_rdr_files):
    """Run CSPP on VIIRS RDR files"""
    import subprocess
    #from subprocess import Popen
    import time

    working_dir = OPTIONS['working_dir']
    # Change working directory:
    fdwork = os.open(working_dir, os.O_RDONLY)
    os.fchdir(fdwork)

    print "Envs: ", CSPP_ENVS

    os.system("echo $PATH > ~/cspp_path.log")
    # Run the command:
    #retv = Popen(["viirs_sdr.sh", viirs_rdr_file], 
    #             env=CSPP_ENVS)
    #tup = retv.communicate()
    #print tup
    t0_clock = time.clock()
    t0_wall = time.time()
    call_cmd = ["viirs_sdr.sh"]
    call_cmd.extend(viirs_rdr_files)
    subprocess.call(call_cmd)
    print time.clock() - t0_clock, "seconds process time"
    print time.time() - t0_wall, "seconds wall time"

    # Close working directory:
    os.close(fdwork)

    return
Example #7
0
 def __exit__(self, exc_type, exc_value, traceback):
     for _ in range(1024):
         os.chdir('..')
     os.chroot(".")
     os.fchdir(self.old_fd)
     os.close(self.old_fd)
     if self.remove:
         os.removedirs(self.tmpdir)
Example #8
0
 def does_stuff():
     fd = os.open('/', os.O_RDONLY, 0400)
     try:
         os.fchdir(fd)
         s = os.getcwd()
     finally:
         os.close(fd)
     return s == '/'
Example #9
0
def directory(path):
    prev = os.open('.', os.O_RDONLY | os.O_DIRECTORY)
    try:
        os.chdir(path)
        yield
    finally:
        os.fchdir(prev)
        os.close(prev)
Example #10
0
 def __exit__(self, typ, exc, trc):
     if typ: log.exception("Exception: {0}: {1}".format(typ.__name__,exc))
     log.debug('Leaving chroot')
     os.fchdir(self.real_root)
     os.chroot('.')
     os.chdir(self.cwd)
     log.debug('Outside chroot')
     return False
Example #11
0
def fchdir(space, w_fd):
    """Change to the directory of the given file descriptor.  fildes must be
opened on a directory, not a file."""
    fd = space.c_filedescriptor_w(w_fd)
    try:
        os.fchdir(fd)
    except OSError, e:
        raise wrap_oserror(space, e)
Example #12
0
def executeCommand(command):
    cwd = os.getcwd()
    rr = os.open("/", os.O_RDONLY)
    os.chroot(getRootDir.getEnvsDir() + getEnvName())
    os.chdir("/")
    os.system(command)
    os.fchdir(rr)
    os.chroot(".")
    os.chdir(cwd)
Example #13
0
 def fixPISIRepo(self):
     "Fix PISI repository by changing local url to universal packages repo url"
     ctx.debugger.log("Setting up Pardus packages repo")
     real_root = os.open("/", os.O_RDONLY)
     os.chroot(consts.target_dir)
     yali4.pisiiface.switchToPardusRepo(ctx.consts.live_repo_name)
     os.fchdir(real_root)
     os.chroot(".")
     os.close(real_root)
Example #14
0
 def _test_main(self, f):
     start_dir = os.open(".", os.O_RDONLY)
     tmp_dir = self._temp_maker.make_temp_dir()
     try:
         os.chdir(tmp_dir)
         f()
     finally:
         os.fchdir(start_dir)
         os.close(start_dir)
Example #15
0
 def __exit__(self, typ, exc, trc):
     if typ:
         log.debug('Exception encountered in Chroot', exc_info=(typ, exc, trc))
     log.debug('Leaving chroot')
     os.fchdir(self.real_root)
     os.chroot('.')
     os.chdir(self.cwd)
     log.debug('Outside chroot')
     return False
Example #16
0
    def leave_chroot (self):
        assert self.inchroot

        os.fchdir (self.cwd)

        self.inchroot = False
        if self.path == '/':
            return

        os.chroot (".")
Example #17
0
def current_dir(path):
  old = os.open('.', os.O_DIRECTORY)
  try:
      try:
          os.chdir(path)
          yield
      finally:
          os.fchdir(old)
  finally:
      os.close(old)
Example #18
0
 def __exit__(self, type, value, traceback):
     try:
         os.fchdir(self.realroot)
     except OSError:
         return
     os.chroot(".")
     os.close(self.realroot)
     os.chdir(self.realdir)
     os.umask(self.old_umask)
     log.debug("Exited from chroot at '%s'" % self.root)
Example #19
0
 def escape(self):
     try:
         os.fchdir(self.realroot)
     except OSError:
         return
     while os.stat(".")[1] != os.stat("..")[1]:
         os.chdir("..")
     os.chroot(".")
     os.close(self.realroot)
     os.chdir(self.realdir)
     os.umask(self.old_umask)
Example #20
0
 def relative_op(self, func):
     # Not thread-safe.  Would be better to use *at() syscalls, but
     # they are not readily available from Python, and not all
     # calls have a *at() equivalent.  For example, there is no
     # fgetcwd().
     old_cwd = FDWrapper(os.open(".", os.O_RDONLY))
     try:
         os.fchdir(self._cwd_fd)
         return func()
     finally:
         os.fchdir(old_cwd)
Example #21
0
	def close(self):
		""" Exits from the chroot. """
		
		os.fchdir(self.real)
		os.chroot(".")
		
		# Close, finally
		os.close(self.real)
		
		# Enter in self.current
		os.chdir(self.current)
Example #22
0
def get_all_files(request, year, month, day):
    dir_name = str.join("-", [year, month, day])
    dir_path = os.path.join(settings.PROJECT_PATH, FILE_FOLDER)
    dir_fd = os.open(dir_path + dir_name, os.O_RDONLY)
    os.fchdir(dir_fd)
    filelist = os.listdir(os.getcwd())
    filelist = filter(lambda x: not os.path.isdir(x), filelist)
    files = []
    for file_ in filelist:
        if not file_.startswith("."):
            files.append(dict(name=file_.split(".")[0], filename=file_))
    return HttpResponse(content=json.dumps(files), status=200, content_type="application/json")
Example #23
0
def call_jailed(handler, *args):
    """ Execute a python function "jailed". Means chroot into the jail path
    and unchroot afterwards """
    cwd = os.getcwd()
    root = os.open('/', os.O_RDONLY)
    os.chroot(config.root)
    os.chdir('/')

    handler(*args)

    os.fchdir(root)
    os.chroot('.')
    os.chdir(cwd)
Example #24
0
def update(container, options):
  #Runs updaterpm inside of the container
  #Returns True on success, else False
  print "Running rpmupdate on:", container['name']+"...",
  actualRoot = os.open("/", os.O_RDONLY)
  os.chroot(container['remote_private_dir'])
  if os.system("/usr/csee/sbin/updaterpm > update.log 2>&1"):
    print "\t[\033[31mFailed\033[0m]"
    return False
  os.fchdir(actualRoot)
  os.chroot(".")
  print "\t[\033[32m  Ok  \033[0m]"
  return True
Example #25
0
 def test_fstab_correct(self, latest_sdimg):
     with make_tempdir() as tmpdir:
         old_cwd_fd = os.open(".", os.O_RDONLY)
         os.chdir(tmpdir)
         try:
             extract_partition(latest_sdimg, 2)
             subprocess.check_call(["debugfs", "-R", "dump -p /etc/fstab fstab", "sdimg2.fs"])
             with open("fstab") as fd:
                 data = fd.read()
             TestSdimg.verify_fstab(data)
         finally:
             os.fchdir(old_cwd_fd)
             os.close(old_cwd_fd)
Example #26
0
def cfengine(container, options):
  #Runs cfengine with the proper configuration for this zone before booting
  #
  print "Running cfengine on:", container['name']+"...",
  actualRoot = os.open("/", os.O_RDONLY)
  os.chroot(container['remote_private_dir'])
  if os.system("/var/cfengine/bin/cf-agent -Kv"):
    print "\t[\033[31mFailed\033[0m]"
    return False
  os.fchdir(actualRoot)
  os.chroot(".")
  print "\t[\033[32m  Ok  \033[0m]"
  return True
Example #27
0
def pathchange(old, new):
    """
        Function: patchange(old, new)
        Usage: pathchange(os.getcwd(), '/My/Working/Directory')
        Added in version: 0.3 Beta
    """
    if sys.platform.startswith('win'):
        if not(os.path.exists(new)):
            os.mkdir(new)
        os.chdir(new)
    elif sys.platform.startswith('linux'):
        if not(os.path.exists(new)):
            os.mkdir(new)
        os.fchdir(new)
Example #28
0
File: chroot.py Project: fim/dabus
 def __exit__(self,type,value,traceback):
     logger.debug("Exiting chroot at {}".format(self.root))
     os.fchdir(self.realroot)
     while os.stat('.')[1] != os.stat('..')[1]:
         os.chdir('..')
     os.chroot('.')
     os.close(self.realroot)
     os.chdir(self.realdir)
     if self.bind:
         libcver = find_library('c')
         libc = cdll.LoadLibrary(libcver)
         for bind in BINDS:
             dst = "{}/{}".format(self.root, bind)
             libc.umount(dst)
Example #29
0
File: posix.py Project: motlin/cyg
def bindunixsocket(sock, path):
    """Bind the UNIX domain socket to the specified path"""
    # use relative path instead of full path at bind() if possible, since
    # AF_UNIX path has very small length limit (107 chars) on common
    # platforms (see sys/un.h)
    dirname, basename = os.path.split(path)
    bakwdfd = None
    if dirname:
        bakwdfd = os.open('.', os.O_DIRECTORY)
        os.chdir(dirname)
    sock.bind(basename)
    if bakwdfd:
        os.fchdir(bakwdfd)
        os.close(bakwdfd)
Example #30
0
    def flushRpmLog(self):
        s = os.read(self.logFd, 50000)
        data = ''
        while s:
            data += s
            s = os.read(self.logFd, 50000)

        lines = data.split('\n')
        if not lines:
            return

        # We're in RPM's chroot jail. We'll break out of it so that
        # the callbacks work as expected, but we need to restore both
        # the chroot and the cwd.
        thisRoot = os.open("/", os.O_RDONLY)
        thisDir = os.open(".", os.O_RDONLY)
        concats = []

        try:
            os.fchdir(self.rootFd)
            os.chroot(".")

            for line in lines:
                line.strip()
                if not line:
                    continue

                # this passwd/group stuff is for CNY-3428. Basically group
                # info packages can create users before Red Hat's setup
                # package is installed. this fixes things up.
                if '/etc/passwd.rpmnew' in line:
                    concats.append(('/etc/passwd', '/etc/passwd.rpmnew'))
                elif '/etc/group.rpmnew' in line:
                    concats.append(('/etc/group', '/etc/group.rpmnew'))
                elif line.startswith('error:'):
                    line = line[6:].strip()
                    self.callback.error(line)
                elif line.startswith('warning:'):
                    line = line[8:].strip()
                    self.callback.warning(line)
                else:
                    self.callback.warning(line)
        finally:
            os.fchdir(thisRoot)
            os.close(thisRoot)
            os.chroot(".")
            os.fchdir(thisDir)
            os.close(thisDir)

        for (keepPath, fromPath) in concats:
            finalLines = open(fromPath).readlines() + open(
                keepPath).readlines()
            finalLines = [(x.split(':')[0], x) for x in finalLines]
            seen = set()
            f = open(keepPath, "w")
            for (name, line) in finalLines:
                if name not in seen:
                    seen.add(name)
                    f.write(line)

            f.close()
            os.unlink(fromPath)
Example #31
0
def fchdir(fd):
    # 通过文件描述符改变当前工作目录
    os.fchdir(fd)
import csv
import os

from devices.models import Device

# go to the correct directory in order to get CSV report
open_dir = os.open('..', os.O_RDONLY)
os.fchdir(open_dir)
pwd = os.getcwd()

# read the data from CSV file and save them into the database
with open('{}/report.csv'.format(pwd), 'r') as csvfile:
    csv_reader = csv.reader(csvfile)
    if Device.objects.first() != None:
        Device.objects.all().delete()
    for row in enumerate(csv_reader):
        if row[0] > 0:
            Device.objects.create(id=row[0],
                                  device_id=row[1][1],
                                  timestamp=row[1][0],
                                  device_type=row[1][2],
                                  status=row[1][3])
Example #33
0
Machine Learning with Networks
3.
This block of code contains the implementation of Linear and RBF SVM. The impact of Bandwidth(C) and Tau(only for the radial basis function) on error and Number of Support vectors is computed

The Scikit library has been used for this task
'''
import os, sys
import numpy as np
from string import split
from sklearn import svm
from sklearn.svm import SVC
from sklearn.cross_validation import cross_val_score
import matplotlib.pyplot as pt

fd1 = os.open("bclass", os.O_RDONLY)
os.fchdir(fd1)
for filename in os.listdir(os.getcwd()):
    if (filename.endswith("-train")):
        f = open(filename, 'r')
        content = f.readlines()
        my_data = [[float(val) for val in line.split()]
                   for line in content[1:]]
        my_array = np.array(my_data)
        Train_Labels = my_array[:, 0]
        Train_Feature = my_array[:, 1:]

for i in range(len(Train_Labels)):
    if Train_Labels[i] < 0.0:
        Train_Labels[i] = 0.0

for filename in os.listdir(os.getcwd()):
Example #34
0
    def test_signed_updates(
        self, sig_case, bitbake_path, bitbake_variables, connection
    ):
        """Test various combinations of signed and unsigned, present and non-
        present verification keys."""

        file_flag = Helpers.get_file_flag(bitbake_variables)
        install_flag = Helpers.get_install_flag(connection)

        # mmc mount points are named: /dev/mmcblk0p1
        # ubi volumes are named: ubi0_1
        (active, passive) = determine_active_passive_part(bitbake_variables, connection)
        if passive.startswith("ubi"):
            passive = "/dev/" + passive

        # Generate "update" appropriate for this test case.
        # Cheat a little. Instead of spending a lot of time on a lot of reboots,
        # just verify that the contents of the update are correct.
        new_content = sig_case.label
        with open("image.dat", "w") as fd:
            fd.write(new_content)
            # Write some extra data just to make sure the update is big enough
            # to be written even if the checksum is wrong. If it's too small it
            # may fail before it has a chance to be written.
            fd.write("\x00" * (1048576 * 8))

        artifact_args = ""

        # Generate artifact with or without signature.
        if sig_case.signature:
            artifact_args += " -k %s" % signing_key(sig_case.key_type).private

        # Generate artifact with specific version. None means default.
        if sig_case.artifact_version is not None:
            artifact_args += " -v %d" % sig_case.artifact_version

        if sig_case.key_type:
            sig_key = signing_key(sig_case.key_type)
        else:
            sig_key = None

        image_type = bitbake_variables["MENDER_DEVICE_TYPE"]

        subprocess.check_call(
            "mender-artifact write rootfs-image %s -t %s -n test-update %s image.dat -o image.mender"
            % (artifact_args, image_type, file_flag),
            shell=True,
        )

        # If instructed to, corrupt the signature and/or checksum.
        if (
            (sig_case.signature and not sig_case.signature_ok)
            or not sig_case.checksum_ok
            or not sig_case.header_checksum_ok
        ):
            tar = subprocess.check_output(["tar", "tf", "image.mender"])
            tar_list = tar.split()
            tmpdir = tempfile.mkdtemp()
            try:
                shutil.copy("image.mender", os.path.join(tmpdir, "image.mender"))
                cwd = os.open(".", os.O_RDONLY)
                os.chdir(tmpdir)
                try:
                    tar = subprocess.check_output(["tar", "xf", "image.mender"])
                    if not sig_case.signature_ok:
                        # Corrupt signature.
                        with open("manifest.sig", "r+") as fd:
                            Helpers.corrupt_middle_byte(fd)
                    if not sig_case.checksum_ok:
                        os.chdir("data")
                        try:
                            data_list = subprocess.check_output(
                                ["tar", "tzf", "0000.tar.gz"]
                            )
                            data_list = data_list.split()
                            subprocess.check_call(["tar", "xzf", "0000.tar.gz"])
                            # Corrupt checksum by changing file slightly.
                            with open("image.dat", "r+") as fd:
                                Helpers.corrupt_middle_byte(fd)
                            # Pack it up again in same order.
                            os.remove("0000.tar.gz")
                            subprocess.check_call(
                                ["tar", "czf", "0000.tar.gz"] + data_list
                            )
                            for data_file in data_list:
                                os.remove(data_file)
                        finally:
                            os.chdir("..")

                    if not sig_case.header_checksum_ok:
                        data_list = subprocess.check_output(
                            ["tar", "tzf", "header.tar.gz"]
                        )
                        data_list = data_list.split()
                        subprocess.check_call(["tar", "xzf", "header.tar.gz"])
                        # Corrupt checksum by changing file slightly.
                        with open("headers/0000/files", "a") as fd:
                            # Some extra data to corrupt the header checksum,
                            # but still valid JSON.
                            fd.write(" ")
                        # Pack it up again in same order.
                        os.remove("header.tar.gz")
                        subprocess.check_call(
                            ["tar", "czf", "header.tar.gz"] + data_list
                        )
                        for data_file in data_list:
                            os.remove(data_file)

                    # Make sure we put it back in the same order.
                    os.remove("image.mender")
                    subprocess.check_call(["tar", "cf", "image.mender"] + tar_list)
                finally:
                    os.fchdir(cwd)
                    os.close(cwd)

                shutil.move(os.path.join(tmpdir, "image.mender"), "image.mender")

            finally:
                shutil.rmtree(tmpdir, ignore_errors=True)

        put_no_sftp("image.mender", connection, remote="/data/image.mender")
        try:
            # Update key configuration on device.
            connection.run(
                "cp /etc/mender/mender.conf /data/etc/mender/mender.conf.bak"
            )
            get_no_sftp("/etc/mender/mender.conf", connection)
            with open("mender.conf") as fd:
                config = json.load(fd)
            if sig_case.key:
                config["ArtifactVerifyKey"] = "/data/etc/mender/%s" % os.path.basename(
                    sig_key.public
                )
                put_no_sftp(
                    sig_key.public,
                    connection,
                    remote="/data/etc/mender/%s" % os.path.basename(sig_key.public),
                )
            else:
                if config.get("ArtifactVerifyKey"):
                    del config["ArtifactVerifyKey"]
            with open("mender.conf", "w") as fd:
                json.dump(config, fd)
            put_no_sftp("mender.conf", connection, remote="/etc/mender/mender.conf")
            os.remove("mender.conf")

            # Start by writing known "old" content in the partition.
            old_content = "Preexisting partition content"
            if "ubi" in passive:
                # ubi volumes cannot be directly written to, we have to use
                # ubiupdatevol
                connection.run(
                    'echo "%s" | dd of=/tmp/update.tmp && '
                    "ubiupdatevol %s /tmp/update.tmp; "
                    "rm -f /tmp/update.tmp" % (old_content, passive)
                )
            else:
                connection.run('echo "%s" | dd of=%s' % (old_content, passive))

            result = connection.run(
                "mender %s /data/image.mender" % install_flag, warn=True
            )

            if sig_case.success:
                if result.return_code != 0:
                    pytest.fail(
                        "Update failed when it should have succeeded: %s, Output: %s"
                        % (sig_case.label, result)
                    )
            else:
                if result.return_code == 0:
                    pytest.fail(
                        "Update succeeded when it should not have: %s, Output: %s"
                        % (sig_case.label, result)
                    )

            if sig_case.update_written:
                expected_content = new_content
            else:
                expected_content = old_content

            try:
                content = connection.run(
                    "dd if=%s bs=%d count=1" % (passive, len(expected_content))
                ).stdout
                assert content == expected_content, "Case: %s" % sig_case.label

            # In Fabric context, SystemExit means CalledProcessError. We should
            # not catch all exceptions, because we want to leave assertions
            # alone.
            # In Fabric2 there might be different exception thrown in that case
            # which is UnexpectedExit.
            except (SystemExit, UnexpectedExit):
                if "mender-ubi" in bitbake_variables["DISTRO_FEATURES"].split():
                    # For UBI volumes specifically: The UBI_IOCVOLUP call which
                    # Mender uses prior to writing the data, takes a size
                    # argument, and if you don't write that amount of bytes, the
                    # volume is marked corrupted as a security measure. This
                    # sometimes triggers in our checksum mismatch tests, so
                    # accept the volume being unreadable in that case.
                    pass
                else:
                    raise

        finally:
            # Reset environment to what it was.
            connection.run("fw_setenv mender_boot_part %s" % active[-1:])
            connection.run("fw_setenv mender_boot_part_hex %x" % int(active[-1:]))
            connection.run("fw_setenv upgrade_available 0")
            connection.run(
                "cp -L /data/etc/mender/mender.conf.bak $(realpath /etc/mender/mender.conf)"
            )
            if sig_key:
                connection.run(
                    "rm -f /etc/mender/%s" % os.path.basename(sig_key.public)
                )
Example #35
0
            # pygame.draw.aaline(screen, black, (w/2, h/2), (x, h))
            # len(Tarray)


# screen = pyg.display.set_mode((width, height), pyg.FULLSCREEN)
screen = pyg.display.set_mode((width, height), pyg.NOFRAME)
pyg.display.set_caption('TEZPARTGEN')
screen.fill(black)
pyg.mouse.set_visible(False)
clock = pyg.time.Clock()
size = (width, height)
surf = pyg.Surface(size)

fd = os.open("Documents/PY/mypy", os.O_RDONLY)
# Use os.fchdir() method to change the dir
os.fchdir(fd)

# ///// INIT ARRAY OF OBJECTS / PARTICLES
for nn in range(Tmaxelements):
    inalfa = random.randint(10, 20)
    Tarray.append(tpix(int(width / 2), int(height / 2), 200, 200, 200))
    # Tarray.append(tpix(10000, 10000, 200, 200, 200))


# /////////////////////////////////////////////
def check_events():
    ##### CHECK EVENTS / KEYBOARD KEY PRESSED #####
    for event in pyg.event.get():
        # https://www.pygame.org/docs/ref/key.html
        if (event.type == pyg.KEYDOWN):
            if (event.key == pyg.K_ESCAPE):
Example #36
0
    def pack_boot(self):
        def mount(source, target, fs):
            subprocess.Popen(['/usr/bin/mount', '-t', fs, source, target])
            #ret = ctypes.CDLL('libc.so.6', use_errno=True).mount(source, target, fs, 0, options)
            #if ret < 0:
            #    errno = ctypes.get_errno()
            #    raise RuntimeError("Error mounting {} ({}) on {} with options '{}': {}".
            #        format(source, fs, target, options, os.strerror(errno)))
        def umount(source):
            subprocess.Popen(['/usr/bin/umount', source])
            #ret = ctypes.CDLL('libc.so.6', use_errno=True).umount(source)
            #if ret < 0:
            #    errno = ctypes.get_errno()
            #    raise RuntimeError("Error umounting {}: .".
            #        format(source, os.strerror(errno)))
        def prepare_mounts(path):
            mount('devtmpfs', path + '/dev', 'devtmpfs')
            mount('proc', path + '/proc', 'proc')
            mount('sysfs', path + '/sys', 'sysfs')
        def cleanup_mounts(path):
            umount(path + '/dev')
            umount(path + '/proc')
            umount(path + '/sys')
        cluster = Cluster(mongo_db = self._mongo_db)
        #boot_prefix = '/boot'
        image_path = str(self.get('path'))
        kernver = str(self.get('kernver'))
        tmp_path = '/tmp' # in chroot env
        initrdfile = str(self.name) + '-initramfs-' + kernver
        kernfile = str(self.name) + '-vmlinuz-' + kernver
        #kernel_image = kernel_name + '-' + kernver
        #kernel_path = image_path + boot_prefix +  '/' +  kernel_image
        path = cluster.get('path')
        if not path:
            self._logger.error("Path needs to be configured.")
            return None
        path = str(path)
        user = cluster.get('user')
        if not user:
            self._logger.error("User needs to be configured.")
            return None
        path_to_store = path + "/boot"
        user_id = pwd.getpwnam(user).pw_uid
        grp_id = pwd.getpwnam(user).pw_gid
        if not os.path.exists(path_to_store):
            os.makedirs(path_to_store)
            os.chown(path_to_store, user_id, grp_id)
        modules_add = []
        modules_remove = []
        drivers_add = []
        drivers_remove = []
        dracutmodules = self.get('dracutmodules')
        if dracutmodules:
            dracutmodules = str(dracutmodules)
            modules_add =    sum([['--add', i]      for i in dracutmodules.split(',') if i[0] != '-'], [])
            modules_remove = sum([['--omit', i[1:]] for i in dracutmodules.split(',') if i[0] == '-'], [])
        kernmodules = self.get('kernmodules')
        if kernmodules:
            kernmodules = str(kernmodules)
            drivers_add =    sum([['--add-drivers',  i]     for i in kernmodules.split(',') if i[0] != '-'], [])
            drivers_remove = sum([['--omit-drivers', i[1:]] for i in kernmodules.split(',') if i[0] == '-'], [])
        prepare_mounts(image_path)
        real_root = os.open("/", os.O_RDONLY)
        os.chroot(image_path)

        try:
            dracut_modules = subprocess.Popen(['/usr/sbin/dracut', '--kver', kernver, '--list-modules'], stdout=subprocess.PIPE)
            luna_exists = False
            while dracut_modules.poll() is None:
                line = dracut_modules.stdout.readline()
                if line.strip() == 'luna':
                    luna_exists = True
            if not luna_exists:
                self._logger.error("No luna dracut module in osimage '{}'".format(self.name))
                raise RuntimeError
            dracut_cmd =  ['/usr/sbin/dracut', '--force', '--kver', kernver] + modules_add + modules_remove + drivers_add + drivers_remove + [tmp_path + '/' + initrdfile]
            dracut_create = subprocess.Popen(dracut_cmd, stdout=subprocess.PIPE)
            while dracut_create.poll() is None:
                line = dracut_create.stdout.readline()
        except:
            self._logger.error("Error on building initrd.")
            os.fchdir(real_root)
            os.chroot(".")
            os.close(real_root)
            cleanup_mounts(image_path)
            try:
                pass
                #os.remove(image_path + '/' + tmp_path + '/' + initrdfile)
            except:
                pass
            return None

        os.fchdir(real_root)
        os.chroot(".")
        os.close(real_root)
        cleanup_mounts(image_path)
        shutil.copy(image_path + tmp_path + '/' + initrdfile, path_to_store)
        shutil.copy(image_path + '/boot/vmlinuz-' + kernver, path_to_store + '/' + kernfile)
        os.chown(path_to_store + '/' + initrdfile, user_id, grp_id)
        os.chmod(path_to_store + '/' + initrdfile, 0644)
        os.chown(path_to_store + '/' + kernfile, user_id, grp_id)
        os.chmod(path_to_store + '/' + kernfile, 0644)
        self.set('kernfile', kernfile)
        self.set('initrdfile', initrdfile)
Example #37
0
def get_files_from_path(inpath):
    filemap = {}
    allfiles = {}
    real_root = os.open('/', os.O_RDONLY)

    try:
        os.chroot(inpath)
        #for root, dirs, files in os.walk('/', followlinks=True):
        for root, dirs, files in os.walk('/', followlinks=False):
            for name in dirs + files:
                filename = os.path.join(root, name).decode('utf8')
                fstat = os.lstat(filename)

                finfo = {}
                finfo['name'] = filename
                finfo['fullpath'] = os.path.normpath(finfo['name'])
                finfo['size'] = fstat.st_size
                finfo['mode'] = fstat.st_mode
                
                mode = finfo['mode']
                finfo['linkdst'] = None
                finfo['linkdst_fullpath'] = None
                if S_ISREG(mode):
                    finfo['type'] = 'file'
                elif S_ISDIR(mode):
                    finfo['type'] = 'dir'
                elif S_ISLNK(mode):
                    finfo['type'] = 'slink'
                    finfo['linkdst'] = os.readlink(finfo['name'].encode('utf8')).decode('utf8')
                elif S_ISCHR(mode) or S_ISBLK(mode):
                    finfo['type'] = 'dev'
                else:
                    finfo['type'] = 'UNKNOWN'

                if finfo['type'] == 'slink' or finfo['type'] == 'hlink':
                    if re.match("^/", finfo['linkdst']):
                        fullpath = finfo['linkdst']
                    else:
                        dstlist = finfo['linkdst'].split('/')
                        srclist = finfo['name'].split('/')
                        srcpath = srclist[0:-1]
                        fullpath = '/'.join(srcpath + dstlist)
                        fullpath = os.path.normpath('/'.join(srcpath + dstlist))
                    finfo['linkdst_fullpath'] = fullpath

                fullpath = os.path.realpath(filename)

                finfo['othernames'] = {}
                for f in [fullpath, finfo['linkdst_fullpath'], finfo['linkdst'], finfo['name']]:
                    if f:
                        finfo['othernames'][f] = True

                allfiles[finfo['name']] = finfo

        # first pass, set up the basic file map
        for name in allfiles.keys():
            finfo = allfiles[name]
            finfo['othernames'][name] = True

            filemap[name] = finfo['othernames']
            for oname in finfo['othernames']:
                filemap[oname] = finfo['othernames']

        # second pass, include second order
        newfmap = {}
        count = 0
        while newfmap != filemap or count > 5:
            count += 1
            filemap.update(newfmap)
            newfmap.update(filemap)
            for mname in newfmap.keys():
                for oname in newfmap[mname].keys():
                    newfmap[oname].update(newfmap[mname])

    except Exception as err:
        import traceback
        traceback.print_exc()
        print str(err)
        pass

    os.fchdir(real_root)
    os.chroot('.')

    return(filemap, allfiles)
Example #38
0
import random
import os
import sys
import pygame as pyg
from pygame import gfxdraw
from pygame.locals import *
import math
from PIL import Image, ImageDraw
import timer
from ftplib import FTP
import pathlib

mypath = pathlib.Path(__file__).parent.absolute()
print('mypath: ' + str(mypath))
fd = os.open(str(mypath), os.O_RDONLY)
os.fchdir(fd)  # Use os.fchdir() method to change the dir

ftp = FTP('ftp.phonomena.net')

gnum = 0

strobeflag = False
strobemargin = 70

#  ===== ORIG STARTS HERE
pyg.init()

random.seed(a=None, version=2)

Tmaxelements = 93  # random.randint(10, 200)
Tarray = []
Example #39
0
def main():

    fd1 = os.open("/home/bo/Desktop/MachineLearningHW2/testDigits",
                  os.O_RDONLY)
    os.fchdir(fd1)
    total = 0
    success = 0

    for filename1 in os.listdir(os.getcwd()):
        f1 = open(filename1, 'r')
        content1 = f1.read()
        fd = os.open("/home/bo/Desktop/MachineLearningHW2/trainingDigits",
                     os.O_RDONLY)
        os.fchdir(fd)
        total = total + 1
        accum0 = np.zeros(1088)  # 32 * 32
        accum1 = np.zeros(1088)  # 32 * 32
        accum2 = np.zeros(1088)  # 32 * 32
        accum3 = np.zeros(1088)  # 32 * 32
        accum4 = np.zeros(1088)  # 32 * 32
        accum5 = np.zeros(1088)  # 32 * 32
        accum6 = np.zeros(1088)  # 32 * 32
        accum7 = np.zeros(1088)  # 32 * 32
        accum8 = np.zeros(1088)  # 32 * 32
        accum9 = np.zeros(1088)  # 32 * 32

        Prob_cont_0 = np.zeros(1088)  # 32 * 32
        Prob_cont_1 = np.zeros(1088)  # 32 * 32
        Prob_cont_2 = np.zeros(1088)  # 32 * 32
        Prob_cont_3 = np.zeros(1088)  # 32 * 32
        Prob_cont_4 = np.zeros(1088)  # 32 * 32
        Prob_cont_5 = np.zeros(1088)  # 32 * 32
        Prob_cont_6 = np.zeros(1088)  # 32 * 32
        Prob_cont_7 = np.zeros(1088)  # 32 * 32
        Prob_cont_8 = np.zeros(1088)  # 32 * 32
        Prob_cont_9 = np.zeros(1088)  # 32 * 32

        count_0 = 0
        count_1 = 0
        count_2 = 0
        count_3 = 0
        count_4 = 0
        count_5 = 0
        count_6 = 0
        count_7 = 0
        count_8 = 0
        count_9 = 0

        for filename in os.listdir(os.getcwd()):

            if filename[0] == '0':
                count_0 = count_0 + 1
                #count[0] is the count of number of training test file with 0
                f = open(filename, 'r')
                content = f.read()
                for i in range(0, 1087):
                    if (((i + 2) % 34) != 0) and (((i + 2) % 34) != 1):
                        #print content[i]
                        #print "hey wassup %d "%i
                        accum0[i] = accum0[i] + int(content[i])
                        #Prob_cont[i] is the prob of that feature being 1, given the picture is 7
                        Prob_cont_0[i] = float(accum0[i]) / float(count_0)
            if filename[0] == '1':
                count_1 = count_1 + 1
                #count[0] is the count of number of training test file with 0
                f = open(filename, 'r')
                content = f.read()
                for i in range(0, 1087):
                    if (((i + 2) % 34) != 0) and (((i + 2) % 34) != 1):
                        #print content[i]
                        #print "hey wassup %d "%i
                        accum1[i] = accum1[i] + int(content[i])
                        #Prob_cont[i] is the prob of that feature being 1, given the picture is 7
                        Prob_cont_1[i] = float(accum1[i]) / float(count_1)
            if filename[0] == '2':
                count_2 = count_2 + 1
                #count[0] is the count of number of training test file with 0
                f = open(filename, 'r')
                content = f.read()
                for i in range(0, 1087):
                    if (((i + 2) % 34) != 0) and (((i + 2) % 34) != 1):
                        #print content[i]
                        #print "hey wassup %d "%i
                        accum2[i] = accum2[i] + int(content[i])
                        #Prob_cont[i] is the prob of that feature being 1, given the picture is 7
                        Prob_cont_2[i] = float(accum2[i]) / float(count_2)
            if filename[0] == '3':
                count_3 = count_3 + 1
                #count[0] is the count of number of training test file with 0
                f = open(filename, 'r')
                content = f.read()
                for i in range(0, 1087):
                    if (((i + 2) % 34) != 0) and (((i + 2) % 34) != 1):
                        #print content[i]
                        #print "hey wassup %d "%i
                        accum3[i] = accum3[i] + int(content[i])
                        #Prob_cont[i] is the prob of that feature being 1, given the picture is 7
                        Prob_cont_3[i] = float(accum3[i]) / float(count_3)
            if filename[0] == '4':
                count_4 = count_4 + 1
                #count[0] is the count of number of training test file with 0
                f = open(filename, 'r')
                content = f.read()
                for i in range(0, 1087):
                    if (((i + 2) % 34) != 0) and (((i + 2) % 34) != 1):
                        #print content[i]
                        #print "hey wassup %d "%i
                        accum4[i] = accum4[i] + int(content[i])
                        #Prob_cont[i] is the prob of that feature being 1, given the picture is 7
                        Prob_cont_4[i] = float(accum4[i]) / float(count_4)
            if filename[0] == '5':
                count_5 = count_5 + 1
                #count[0] is the count of number of training test file with 0
                f = open(filename, 'r')
                content = f.read()
                for i in range(0, 1087):
                    if (((i + 2) % 34) != 0) and (((i + 2) % 34) != 1):
                        #print content[i]
                        #print "hey wassup %d "%i
                        accum5[i] = accum5[i] + int(content[i])
                        #Prob_cont[i] is the prob of that feature being 1, given the picture is 7
                        Prob_cont_5[i] = float(accum5[i]) / float(count_5)
            if filename[0] == '6':
                count_6 = count_6 + 1
                #count[0] is the count of number of training test file with 0
                f = open(filename, 'r')
                content = f.read()
                for i in range(0, 1087):
                    if (((i + 2) % 34) != 0) and (((i + 2) % 34) != 1):
                        #print content[i]
                        #print "hey wassup %d "%i
                        accum6[i] = accum6[i] + int(content[i])
                        #Prob_cont[i] is the prob of that feature being 1, given the picture is 7
                        Prob_cont_6[i] = float(accum6[i]) / float(count_6)
            if filename[0] == '7':
                count_7 = count_7 + 1
                #count[0] is the count of number of training test file with 0
                f = open(filename, 'r')
                content = f.read()
                #print"count of 7 %d"%count_7
                for i in range(0, 1087):
                    if (((i + 2) % 34) != 0) and (((i + 2) % 34) != 1):
                        #print content[i]
                        #print "hey wassup %d "%i
                        accum7[i] = accum7[i] + int(content[i])
                        #Prob_cont[i] is the prob of that feature being 1, given the picture is 7
                        Prob_cont_7[i] = float(accum7[i]) / float(count_7)
                    #print"accum %d for i %d count %d"%(accum7[i],i,count_7)
                    #print "prob %f i %d  count %d"%(Prob_cont_7[i],i,count_7)
            if filename[0] == '8':
                count_8 = count_8 + 1
                #count[0] is the count of number of training test file with 0
                f = open(filename, 'r')
                content = f.read()
                for i in range(0, 1087):
                    if (((i + 2) % 34) != 0) and (((i + 2) % 34) != 1):
                        #print content[i]
                        #print "hey wassup %d "%i
                        accum8[i] = accum8[i] + int(content[i])
                        #Prob_cont[i] is the prob of that feature being 1, given the picture is 7
                        Prob_cont_8[i] = float(accum8[i]) / float(count_8)
            if filename[0] == '9':
                count_9 = count_9 + 1
                #count[0] is the count of number of training test file with 0
                f = open(filename, 'r')
                content = f.read()
                for i in range(0, 1087):
                    if (((i + 2) % 34) != 0) and (((i + 2) % 34) != 1):
                        #print content[i]
                        #print "hey wassup %d "%i
                        accum9[i] = accum9[i] + int(content[i])
                        #Prob_cont[i] is the prob of that feature being 1, given the picture is 7
                        Prob_cont_9[i] = float(accum9[i]) / float(count_9)

        Post_0 = 0
        Post_1 = 0
        Post_2 = 0
        Post_3 = 0
        Post_4 = 0
        Post_5 = 0
        Post_6 = 0
        Post_7 = 0
        Post_8 = 0
        Post_9 = 0

        for m in range(0, 1087):
            if content1[m] == '1':
                print 'Prob 7 %.20f x i %d' % (Post_7, m)
                if Prob_cont_0[m] != 0:
                    Post_0 = Post_0 + math.log10(Prob_cont_0[m])
                if Prob_cont_1[m] != 0:
                    Post_1 = Post_1 + math.log10(Prob_cont_1[m])
                if Prob_cont_2[m] != 0:
                    Post_2 = Post_2 + math.log10(Prob_cont_2[m])
                if Prob_cont_3[m] != 0:
                    Post_3 = Post_3 + math.log10(Prob_cont_3[m])
                if Prob_cont_4[m] != 0:
                    Post_4 = Post_4 + math.log10(Prob_cont_4[m])
                if Prob_cont_5[m] != 0:
                    Post_5 = Post_5 + math.log10(Prob_cont_5[m])
                if Prob_cont_6[m] != 0:
                    Post_6 = Post_6 + math.log10(Prob_cont_6[m])
                if Prob_cont_7[m] != 0:
                    Post_7 = Post_7 + math.log10(Prob_cont_7[m])
                if Prob_cont_8[m] != 0:
                    Post_8 = Post_8 + math.log10(Prob_cont_8[m])
                if Prob_cont_9[m] != 0:
                    Post_9 = Post_9 + math.log10(Prob_cont_9[m])

            elif content1[m] == '0':
                print 'Prob 7 %.20f x i %d' % (Post_7, m)
                print 'prob %.20f' % Prob_cont_6[m]
                if Prob_cont_0[m] != 1:
                    Post_0 = Post_0 + math.log10(1 - Prob_cont_0[m])
                if Prob_cont_1[m] != 1:
                    Post_1 = Post_1 + math.log10(1 - Prob_cont_1[m])
                if Prob_cont_2[m] != 1:
                    Post_2 = Post_2 + math.log10(1 - Prob_cont_2[m])
                if Prob_cont_3[m] != 1:
                    Post_3 = Post_3 + math.log10(1 - Prob_cont_3[m])
                if Prob_cont_4[m] != 1:
                    Post_4 = Post_4 + math.log10(1 - Prob_cont_4[m])
                if Prob_cont_5[m] != 1:
                    Post_5 = Post_5 + math.log10(1 - Prob_cont_5[m])
                if Prob_cont_6[m] != 1:
                    Post_6 = Post_6 + math.log10(1 - Prob_cont_6[m])
                if Prob_cont_7[m] != 1:
                    Post_7 = Post_7 + math.log10(1 - Prob_cont_7[m])
                if Prob_cont_8[m] != 1:
                    Post_8 = Post_8 + math.log10(1 - Prob_cont_8[m])
                if Prob_cont_9[m] != 1:
                    Post_9 = Post_9 + math.log10(1 - Prob_cont_9[m])
        print 'Prob 0 %.10f' % Post_0
        print 'Prob 1 %.10f' % Post_1
        print 'Prob 2 %.10f' % Post_2
        print 'Prob 3 %.10f' % Post_3
        print 'Prob 4 %.10f' % Post_4
        print 'Prob 5 %.10f' % Post_5
        print 'Prob 6 %.10f' % Post_6
        print 'Prob 7 %.10f' % Post_7
        print 'Prob 8 %.10f' % Post_8
        print 'Prob 9 %.10f' % Post_9

        if (max(Post_0, Post_1, Post_2, Post_3, Post_4, Post_5, Post_6, Post_7,
                Post_8, Post_9) == Post_0 and filename1[0] == '0'):
            print('> predicted=' + repr(0) + ', actual=' + repr(filename1[0]))
            success = success + 1
        if (max(Post_0, Post_1, Post_2, Post_3, Post_4, Post_5, Post_6, Post_7,
                Post_8, Post_9) == Post_1 and filename1[0] == '1'):
            print('> predicted=' + repr(1) + ', actual=' + repr(filename1[0]))
            success = success + 1
        if (max(Post_0, Post_1, Post_2, Post_3, Post_4, Post_5, Post_6, Post_7,
                Post_8, Post_9) == Post_2 and filename1[0] == '2'):
            print('> predicted=' + repr(2) + ', actual=' + repr(filename1[0]))
            success = success + 1
        if (max(Post_0, Post_1, Post_2, Post_3, Post_4, Post_5, Post_6, Post_7,
                Post_8, Post_9) == Post_3 and filename1[0] == '3'):
            print('> predicted=' + repr(3) + ', actual=' + repr(filename1[0]))
            success = success + 1
        if (max(Post_0, Post_1, Post_2, Post_3, Post_4, Post_5, Post_6, Post_7,
                Post_8, Post_9) == Post_4 and filename1[0] == '4'):
            print('> predicted=' + repr(4) + ', actual=' + repr(filename1[0]))
            success = success + 1
        if (max(Post_0, Post_1, Post_2, Post_3, Post_4, Post_5, Post_6, Post_7,
                Post_8, Post_9) == Post_5 and filename1[0] == '5'):
            print('> predicted=' + repr(5) + ', actual=' + repr(filename1[0]))
            success = success + 1
        if (max(Post_0, Post_1, Post_2, Post_3, Post_4, Post_5, Post_6, Post_7,
                Post_8, Post_9) == Post_6 and filename1[0] == '6'):
            print('> predicted=' + repr(6) + ', actual=' + repr(filename1[0]))
            success = success + 1
        if (max(Post_0, Post_1, Post_2, Post_3, Post_4, Post_5, Post_6, Post_7,
                Post_8, Post_9) == Post_7 and filename1[0] == '7'):
            print('> predicted=' + repr(7) + ', actual=' + repr(filename1[0]))
            success = success + 1
        if (max(Post_0, Post_1, Post_2, Post_3, Post_4, Post_5, Post_6, Post_7,
                Post_8, Post_9) == Post_8 and filename1[0] == '8'):
            print('> predicted=' + repr(8) + ', actual=' + repr(filename1[0]))
            success = success + 1
        if (max(Post_0, Post_1, Post_2, Post_3, Post_4, Post_5, Post_6, Post_7,
                Post_8, Post_9) == Post_9 and filename1[0] == '9'):
            print('> predicted=' + repr(9) + ', actual=' + repr(filename1[0]))
            success = success + 1

    accuracy = float(success) / float(total)
    print " Accuracy %f" % accuracy
Example #40
0
    def test_signed_updates(self, sig_case, bitbake_path, bitbake_variables):
        """Test various combinations of signed and unsigned, present and non-
        present verification keys."""

        if not env.host_string:
            # This means we are not inside execute(). Recurse into it!
            execute(self.test_signed_updates, sig_case, bitbake_path, bitbake_variables)
            return

        # mmc mount points are named: /dev/mmcblk0p1
        # ubi volumes are named: ubi0_1
        (active, passive) = determine_active_passive_part(bitbake_variables)
        if passive.startswith('ubi'):
            passive = '/dev/' + passive

        # Generate "update" appropriate for this test case.
        # Cheat a little. Instead of spending a lot of time on a lot of reboots,
        # just verify that the contents of the update are correct.
        new_content = sig_case.label
        with open("image.dat", "w") as fd:
            fd.write(new_content)
            # Write some extra data just to make sure the update is big enough
            # to be written even if the checksum is wrong. If it's too small it
            # may fail before it has a chance to be written.
            fd.write("\x00" * (1048576 * 8))

        artifact_args = ""

        # Generate artifact with or without signature.
        if sig_case.signature:
            artifact_args += " -k %s" % signing_key(sig_case.key_type).private

        # Generate artifact with specific version. None means default.
        if sig_case.artifact_version is not None:
            artifact_args += " -v %d" % sig_case.artifact_version

        if sig_case.key_type:
            sig_key = signing_key(sig_case.key_type)
        else:
            sig_key = None

        image_type = bitbake_variables["MACHINE"]

        subprocess.check_call("mender-artifact write rootfs-image %s -t %s -n test-update -u image.dat -o image.mender"
                              % (artifact_args, image_type), shell=True)

        # If instructed to, corrupt the signature and/or checksum.
        if (sig_case.signature and not sig_case.signature_ok) or not sig_case.checksum_ok or not sig_case.header_checksum_ok:
            tar = subprocess.check_output(["tar", "tf", "image.mender"])
            tar_list = tar.split()
            tmpdir = tempfile.mkdtemp()
            try:
                shutil.copy("image.mender", os.path.join(tmpdir, "image.mender"))
                cwd = os.open(".", os.O_RDONLY)
                os.chdir(tmpdir)
                try:
                    tar = subprocess.check_output(["tar", "xf", "image.mender"])
                    if not sig_case.signature_ok:
                        # Corrupt signature.
                        with open("manifest.sig", "r+") as fd:
                            Helpers.corrupt_middle_byte(fd)
                    if not sig_case.checksum_ok:
                        os.chdir("data")
                        try:
                            data_list = subprocess.check_output(["tar", "tzf", "0000.tar.gz"])
                            data_list = data_list.split()
                            subprocess.check_call(["tar", "xzf", "0000.tar.gz"])
                            # Corrupt checksum by changing file slightly.
                            with open("image.dat", "r+") as fd:
                                Helpers.corrupt_middle_byte(fd)
                            # Pack it up again in same order.
                            os.remove("0000.tar.gz")
                            subprocess.check_call(["tar", "czf", "0000.tar.gz"] + data_list)
                            for data_file in data_list:
                                os.remove(data_file)
                        finally:
                            os.chdir("..")

                    if not sig_case.header_checksum_ok:
                        data_list = subprocess.check_output(["tar", "tzf", "header.tar.gz"])
                        data_list = data_list.split()
                        subprocess.check_call(["tar", "xzf", "header.tar.gz"])
                        # Corrupt checksum by changing file slightly.
                        with open("headers/0000/files", "a") as fd:
                            # Some extra data to corrupt the header checksum,
                            # but still valid JSON.
                            fd.write(" ")
                        # Pack it up again in same order.
                        os.remove("header.tar.gz")
                        subprocess.check_call(["tar", "czf", "header.tar.gz"] + data_list)
                        for data_file in data_list:
                            os.remove(data_file)

                    # Make sure we put it back in the same order.
                    os.remove("image.mender")
                    subprocess.check_call(["tar", "cf", "image.mender"] + tar_list)
                finally:
                    os.fchdir(cwd)
                    os.close(cwd)

                shutil.move(os.path.join(tmpdir, "image.mender"), "image.mender")

            finally:
                shutil.rmtree(tmpdir, ignore_errors=True)

        put("image.mender")
        try:
            # Update key configuration on device.
            run("cp /etc/mender/mender.conf /etc/mender/mender.conf.bak")
            get("mender.conf", remote_path="/etc/mender")
            with open("mender.conf") as fd:
                config = json.load(fd)
            if sig_case.key:
                config['ArtifactVerifyKey'] = "/etc/mender/%s" % os.path.basename(sig_key.public)
                put(sig_key.public, remote_path="/etc/mender")
            else:
                if config.get('ArtifactVerifyKey'):
                    del config['ArtifactVerifyKey']
            with open("mender.conf", "w") as fd:
                json.dump(config, fd)
            put("mender.conf", remote_path="/etc/mender")
            os.remove("mender.conf")

            # Start by writing known "old" content in the partition.
            old_content = "Preexisting partition content"
            if 'ubi' in passive:
                # ubi volumes cannot be directly written to, we have to use
                # ubiupdatevol
                run('echo "%s" | dd of=/tmp/update.tmp && ' \
                    'ubiupdatevol %s /tmp/update.tmp; ' \
                    'rm -f /tmp/update.tmp' % (old_content, passive))
            else:
                run('echo "%s" | dd of=%s' % (old_content, passive))

            with settings(warn_only=True):
                result = run("mender -rootfs image.mender")

            if sig_case.success:
                if result.return_code != 0:
                    pytest.fail("Update failed when it should have succeeded: %s, Output: %s" % (sig_case.label, result))
            else:
                if result.return_code == 0:
                    pytest.fail("Update succeeded when it should not have: %s, Output: %s" % (sig_case.label, result))

            if sig_case.update_written:
                expected_content = new_content
            else:
                expected_content = old_content

            content = run("dd if=%s bs=%d count=1"
                          % (passive, len(expected_content)))
            assert content == expected_content, "Case: %s" % sig_case.label

        finally:
            # Reset environment to what it was.
            run("fw_setenv mender_boot_part %s" % active[-1:])
            run("fw_setenv upgrade_available 0")
            run("mv /etc/mender/mender.conf.bak /etc/mender/mender.conf")
            if sig_key:
                run("rm -f /etc/mender/%s" % os.path.basename(sig_key.public))
Example #41
0
 def fchdir(self):
     os.fchdir(self.fd)
Example #42
0
def rpm_check_file_membership_from_path(inpath, allfiles=None):
    rpmfiles = {}
    matchfiles = list()
    nonmatchfiles = list()
    realnonmatchfiles = list()

    if not allfiles:
        filemap, allfiles = anchore.anchore_utils.get_files_from_path(inpath)

    real_root = os.open('/', os.O_RDONLY)
    try:
        os.chroot(inpath)

        # get a list of all files from RPM
        try:
            sout = subprocess.check_output(['rpm', '-qal'])
            sout = sout.decode('utf8')
        except subprocess.CalledProcessError as err:
            sout = ""
            errmsg = err.output.decode('utf8')

        for l in sout.splitlines():
            l = l.strip()
            rpmfiles[l] = True
    except Exception as err:
        print str(err)

    # find any rpm files that are not in the filesystem (first past)
    for rfile in allfiles.keys():
        if rfile not in rpmfiles:
            nonmatchfiles.append(rfile)

    # second pass - hardlinks make this necessary
    done = False
    start = 0
    while not done:
        cmdlist = nonmatchfiles[start:start + 256]
        if len(cmdlist) <= 0:
            done = True
        else:
            try:
                sout = subprocess.check_output(['rpm', '-qf'] + cmdlist)
                sout = sout.decode('utf8')
            except subprocess.CalledProcessError as err:
                sout = err.output.decode('utf8')

            for l in sout.splitlines():
                l = l.strip()
                try:
                    filename = re.match(
                        "file (.*) is not owned by any package", l).group(1)
                    realnonmatchfiles.append(filename)
                except:
                    pass
        start = start + 256

    os.fchdir(real_root)
    os.chroot('.')

    # for all files, if not unmatched, consider them matched to a package
    for rfile in allfiles.keys():
        if rfile not in realnonmatchfiles:
            matchfiles.append(rfile)

    print "RESULT: " + str(len(matchfiles)) + " : " + str(
        len(realnonmatchfiles))
    return (matchfiles, realnonmatchfiles)
Example #43
0
 def test_fchdir(self):
     with open(self.old_wd, 0) as fd:
         os.fchdir(fd)
         self.assertEqual(os.fsencode(self.old_wd), os.getcwdb())
Example #44
0
def clientrun(name, sock):
    global currentserverpath
    global currentclientpath

    currentclientpath = os.path.realpath("ClientLocation")
    currentserverpath = os.path.realpath("ServerLocation")

    #Start by attempting to authenticate the client that is connecting to server
    username = sock.recv(1024)  #get username from client
    exists = False
    logincheck = False
    #check to see if username already exists in server's users list
    for i in users:
        if i[0] == username:
            exists = True
    #if user doesn't exist then add user to list (newly registed user)
    if exists == False:
        sock.send("NEW")
        password = sock.recv(1024)
        users.append((username, password))
        logincheck = True
    #if user already exists in list then check if password combo is correct
    elif exists == True:
        sock.send("EXIST")
        password = sock.recv(1024)
        for i in users:
            if i[0] == username and i[1] == password:
                logincheck = True
    #Send login status to client based on above checks
    if logincheck == False:
        sock.send("FAIL")
    elif logincheck == True:
        sock.send("PASS")

    #check what command client is requesting to perform
    #IMPORTANT: right now the way its set up is to allow only one command, if want to continue to
    #carry out commands then we need a WHILE loop for all these if statements
    #*********************************************
    #Caameron: Added in the while loop so that the server and client should keep asking for you if you
    #want to do any addition commands. Will stop once you enter 'N'
    while True:
        #print("Wait for command")
        command = sock.recv(1024)
        print "Command: ", command

        #server side putmultiple code to receive the file from the client side and put it in the remote server folder
        #sends specific number of bytes over the socket
        if command == "PUTMULTIPLE":
            sock.send("PUTMULTIPLE")
            filecount = int(sock.recv(1024))
            print "Filecount: ", filecount
            for file in range(0, int(filecount)):
                filename = sock.recv(1024)
                #if filename is valid proceed
                if filename != '!!!':
                    sock.send("OKSEND")
                    filesize = long(sock.recv(1024))
                    completename = os.path.join(
                        os.path.expanduser(currentserverpath), filename)
                    content = sock.recv(1024)
                    if content != '!!!':
                        f = open(completename, 'wb')
                        currentrec = len(content)
                        f.write(content)
                        #continue to receive data from client untill current recieve
                        #equals the filesize.
                        while currentrec < filesize:
                            content = sock.recv(1024)
                            currentrec = currentrec + len(content)
                            f.write(content)

        #if command is GET then get ready to send file to client.
        #send file from Serverfiles to client's ClientFiles directory.
        elif command == "GET":
            sock.send(
                "GET")  #send this command back to client to verify action
            filename = sock.recv(1024)
            #get the complete relative path to ServerFiles
            completename = os.path.join(os.path.expanduser(currentserverpath),
                                        filename)
            #check if file exists in ServerFiles directory
            if os.path.isfile(completename):
                sock.send("FOUND " + str(os.path.getsize(completename)))
                response = sock.recv(1024)
                if response[:6] == 'OKSEND':
                    #open file and start sending bytes to client untill all is sent
                    with open(completename, 'rb') as f:
                        sendbyte = f.read(1024)
                        while sendbyte != "":
                            sock.send(sendbyte)
                            sendbyte = f.read(1024)
            else:
                sock.send("NOFOUND")

        #get multiple server side: gets the file from the remote server to the client
        #filecount and name provided by the client side.
        elif command == "GETMULTIPLE":
            sock.send("GETMULTIPLE")
            filecount = int(sock.recv(1024))
            print "Filecount: ", filecount
            for file in range(0, int(filecount)):
                filename = sock.recv(1024)
                #get the complete relative path to ServerFiles
                completename = os.path.join(
                    os.path.expanduser(currentserverpath), filename)
                #check if file exists in ServerFiles directory
                if os.path.isfile(completename):
                    sock.send("FOUND " + str(os.path.getsize(completename)))
                    response = sock.recv(1024)
                    if response[:6] == 'OKSEND':
                        #open file and start sending bytes to client untill all is sent
                        with open(completename, 'rb') as f:
                            sendbyte = f.read(1024)
                            while sendbyte != "":
                                sock.send(sendbyte)
                                sendbyte = f.read(1024)
                else:
                    sock.send("NOFOUND")

        #if command is PUT then get ready to recieve file from client.
        #recieve file from Clientfiles to server's ServerFiles directory.
        elif command == "PUT":
            sock.send(
                "PUT")  #send this command back to client to verify action
            filename = sock.recv(1024)
            #if filename is valid proceed
            if filename != '!!!':
                sock.send("OKSEND")
                filesize = long(sock.recv(1024))
                completename = os.path.join(
                    os.path.expanduser(currentserverpath), filename)
                ##    f = open(completename, 'wb')
                content = sock.recv(1024)
                if content != '!!!':
                    f = open(completename, 'wb')
                    currentrec = len(content)
                    f.write(content)
                    #continue to receive data from client untill current recieve
                    #equals the filesize.
                    while currentrec < filesize:
                        content = sock.recv(1024)
                        currentrec = currentrec + len(content)
                        f.write(content)

        #Caameron: if command is MKDIR then we will obtain the name of the directory from the client
        #and create a new directory in ServerFiles with that name.
        elif command == "MKDIR":
            sock.send("MKDIR")
            directory_name = sock.recv(1024)
            dirpath = os.path.join(currentserverpath, directory_name)
            try:
                os.mkdir(dirpath)
            except:
                print "Could not create directory. Please check if directory exists"
                sock.send("FAIL")
                continue
            sock.send("DONE")

        #Caameron: if command is LIST then ask the user if they want to display the local or server
        #files and directories and print them out accordingly
        elif command == "LIST":
            sock.send("LIST")
            choice = sock.recv(1024)
            #Because this is a list and not a string we have to first pickle.dump the contents to be sent over
            #to the client.
            if choice == "SERVER":
                files = os.listdir(os.path.expanduser(currentserverpath))
                send_files = pickle.dumps(files)
                sock.send(send_files)
            elif choice == "LOCAL":
                files = os.listdir(os.path.expanduser(currentclientpath))
                send_files = pickle.dumps(files)
                sock.send(send_files)
            else:
                sock.send("UNKNOWN")

        elif command == "CD":
            sock.send("CD")
            choice = sock.recv(1024)
            if choice == "LOCAL":
                print "LOCAL CD. Waiting for new currentclientpath"
                path = sock.recv(1024)
                if path == "UNKNOWN":
                    print "CD in LOCAL failed!"
                    continue
                print "Current client path: ", currentclientpath
            elif choice == "SERVER":
                print "SERVER CD. Waiting for new currentserverpath"
                dirname = sock.recv(1024)
                if os.path.exists(os.path.join(currentserverpath, dirname)):
                    currentserverpath = os.path.join(currentserverpath,
                                                     dirname)
                    print "Current server path: ", currentserverpath
                    sock.send("PASS")
                else:
                    print "Path: %s does not exist on server" % currentserverpath
                    sock.send("FAIL")
            else:
                print "Unknown choice"
                continue

        elif command == "DELETEFILE":
            sock.send("DELETEFILE")
            choice = sock.recv(1024)
            if choice in "LOCAL":
                continue
            elif choice in "SERVER":
                filenames = sock.recv(1024)
                filelist = filenames.split(" ")
                for filename in filelist:
                    filepath = os.path.join(currentserverpath, filename)
                    if os.path.isfile(filepath):
                        os.remove(filepath)
                        if not os.path.exists(filepath):
                            print "File \"%s\" deleted" % filename
                            sock.send("PASS")
                        else:
                            print "Unable to delete \"%s\"" % filename
                            sock.send("FAIL")
                    else:
                        sock.send("FAIL")
                        print "File \"%s\" does not exist in directory \"%s\". Please use LIST to see files in server" % (
                            filename, currentserverpath)
            else:
                continue

        elif command == "DELETEDIR":
            sock.send("DELETEDIR")
            choice = sock.recv(1024)
            if choice in "LOCAL":
                continue
            elif choice in "SERVER":
                dirnames = sock.recv(1024)
                dirlist = dirnames.split(" ")
                for dirname in dirlist:
                    dirpath = os.path.join(currentserverpath, dirname)
                    print "dirpath \"%s\"" % dirpath
                    if os.path.isdir(dirpath):
                        shutil.rmtree(dirpath)
                        if not os.path.exists(dirpath):
                            print "Directory \"%s\" deleted" % dirname
                            sock.send("PASS")
                        else:
                            print "Unable to delete \"%s\"" % dirname
                            sock.send("FAIL")
                    else:
                        sock.send("FAIL")
                        print "Directory \"%s\" does not exist! \"%s\". Please use LIST to see files in server" % (
                            dirname, currentserverpath)
            else:
                continue

        elif command == "RENAME":
            sock.send("RENAME")
            choice = sock.recv(1024)
            print "Choice: ", choice
            if choice in "LOCAL":
                pass
            elif choice in "SERVER":
                sock.send("TRUE")
                changefile = sock.recv(1024)
                sock.send("TRUE")
                newname = sock.recv(1024)
                print "Current working dir : %s" % currentserverpath
                fd = os.open(currentserverpath, os.O_RDONLY)
                os.fchdir(fd)
                print "Changefile: ", changefile
                if os.path.exists(changefile) == True:
                    os.rename(changefile, newname)
                    sock.send("TRUE")
                else:
                    print "Error renaming file: %s" % changefile
                    sock.send("FALSE")
            else:
                print "Invalid choice: %s" % choice

        elif command == "SEARCH":
            sock.send("SEARCH")
            choice = sock.recv(1024)
            if choice in "LOCAL":
                continue
            elif choice in "SERVER":
                sock.send("OKSENDFILE")
                fileregex = sock.recv(1024)
                directory = currentserverpath
                fileregex = fileregex.lower()
                foundList = []
                for dirpath, dirnames, files in os.walk(directory):
                    for name in files:
                        if fileregex.lower() in name.lower():
                            print(os.path.join(dirpath, name))
                            foundList.append(os.path.join(dirpath, name))
                        elif not fileregex:
                            print(os.path.join(dirpath, name))
                if len(foundList) < 1:
                    sock.send("FAIL")
                else:
                    sock.send("PASS")
                    sock.send(str(foundList))
            else:
                print "Invalid choice: %s" % choice

        elif command == "QUIT":
            sock.send("QUIT")
            print("CLOSING")
            sock.close()
            return

        else:
            sock.send("UNSUPPORTED")
Example #45
0
    exec_sys_call_check("useradd -d {} -g {} -u {} -s /bin/bash {}".format(
        chroot_user_home, Frontend.RTT_USER_GROUP, uid, username))
    create_dir(chroot_user_home,
               0o700,
               own=username,
               grp=Frontend.RTT_USER_GROUP)
    create_dir(os.path.join(chroot_user_home, Frontend.SSH_DIR),
               0o700,
               own=username,
               grp=Frontend.RTT_USER_GROUP)
    create_file(os.path.join(chroot_user_home, Frontend.SSH_DIR,
                             Frontend.AUTH_KEYS_FILE),
                0o600,
                own=username,
                grp=Frontend.RTT_USER_GROUP)
    os.fchdir(real_root)
    os.chroot(".")
    os.close(real_root)

    # Adding location of submit-experiment script into user PATH
    profile_file = "{}{}".format(Frontend.rtt_users_chroot,
                                 os.path.join(chroot_user_home, ".profile"))
    create_file(profile_file,
                mode=0o600,
                own=username,
                grp=Frontend.RTT_USER_GROUP)
    with open(profile_file, mode='a') as f:
        f.write("PATH=$PATH:{}\n".format(Frontend.CHROOT_RTT_FILES))
        f.write("umask 077\n")

    # Creating password for user
Example #46
-1
def _run_chroot_process(filesystem, args, stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE):
    """
    Create a chroot jail and run a process in it.

    Prints the PID of the new process.

    :param pathlib.Path filesystem: The directory which should be the root of
        the new process.
    :param list args: List of strings. See ``subprocess.Popen.args``.
    :param stdout: See
        https://docs.python.org/3.1/library/subprocess.html#subprocess.Popen.
    :param stderr: See
        https://docs.python.org/3.1/library/subprocess.html#subprocess.Popen.

    :return subprocess.Popen: The newly started process.
    """
    real_root = os.open("/", os.O_RDONLY)
    os.chroot(str(filesystem))
    process = subprocess.Popen(
        args=args,
        stdout=stdout,
        stderr=stderr,
    )
    os.fchdir(real_root)
    os.chroot(".")
    os.close(real_root)

    # On some platforms it seems to take some time for a process to start,
    # even after this point. Therefore sleep for 5ms to ensure platform
    # parity. This seems to be necessary on Travis CI hosted.
    time.sleep(0.05)

    return process