Пример #1
0
def get_trashdir(path):
    """
        Use path to determine the trash directory.  This method does not create
        the directory. If path is not contained within an image, return None.
        The directories for the images that have already been accessed are
        cached to improve the speed of this method.
        """
    global cached_image_info
    global cache_lock
    import pkg.client.image as image
    from pkg.client.api_errors import ImageNotFoundException
    try:
        cache_lock.acquire()
        for iroot, itrash in cached_image_info:
            if path.startswith(iroot):
                return itrash

        try:
            img = image.Image(os.path.dirname(path),
                              allow_ondisk_upgrade=False)
        except ImageNotFoundException:
            # path is not within an image, no trash dir
            return None
        trashdir = os.path.join(img.imgdir, trashname)
        # this is the first time putting something in the trash for
        # this image, so try to empty the trash first
        shutil.rmtree(trashdir, True)
        cached_image_info.append((img.get_root(), trashdir))
        return trashdir
    finally:
        cache_lock.release()
Пример #2
0
Файл: IPS.py Проект: rcuza/bcfg2
 def __init__(self, config):
     self.installed = {}
     self.pending_upgrades = set()
     self.image = image.Image()
     self.image.find_root('/', False)
     self.image.load_config()
     Bcfg2.Client.Tools.PkgTool.__init__(self, config)
Пример #3
0
    def testRenameOfRunningExecutable(self):
        if util.get_canonical_os_type() != 'windows':
            return
        import pkg.portable.os_windows as os_windows
        cwd = os.getcwdu()
        exefilesrc = 'C:\\Windows\\system32\\more.com'
        self.assertTrue(os.path.exists(exefilesrc))

        # create an image, copy an executable into it,
        # run the executable, replace the executable
        tdir1 = tempfile.mkdtemp()
        img1 = image.Image(tdir1,
                           imgtype=image.IMG_USER,
                           should_exist=False,
                           user_provided_dir=True)
        img1.history.client_name = "pkg-test"
        img1.set_attrs(False,
                       "test",
                       origins=["http://localhost:10000"],
                       refresh_allowed=False)
        exefile = os.path.join(tdir1, 'less.com')
        shutil.copyfile(exefilesrc, exefile)
        proc = subprocess.Popen([exefile], stdin=subprocess.PIPE)
        self.assertRaises(OSError, os.unlink, exefile)
        fd1, path1 = tempfile.mkstemp(dir=tdir1)
        os.write(fd1, b"foo")
        os.close(fd1)
        portable.rename(path1, exefile)
        fd2 = os.open(exefile, os.O_RDONLY)
        self.assertEqual(os.read(fd2, 3), "foo")
        os.close(fd2)
        proc.communicate()

        # Make sure that the moved executable gets deleted
        # This is a white-box test
        # To simulate running another process, we delete the cache
        # and call get_trashdir as if another file was being moved
        # to the trash.
        os_windows.cached_image_info = []
        os_windows.get_trashdir(exefile)
        self.assertTrue(not os.path.exists(
            os.path.join(img1.imgdir, os_windows.trashname)))

        # cleanup
        os.chdir(cwd)
        shutil.rmtree(tdir1)
Пример #4
0
    def __verify_pub_cfg(self, img_path, prefix, pub_cfg):
        """Private helper method to verify publisher configuration."""

        # pretend like the Image object is being allocated from
        # a pkg command run from within the target image.
        cmdpath = os.path.join(self.get_img_path(), "pkg")

        img = image.Image(img_path,
                          should_exist=True,
                          user_provided_dir=True,
                          cmdpath=cmdpath)
        pub = img.get_publisher(prefix=prefix)
        for section in pub_cfg:
            for prop, val in six.iteritems(pub_cfg[section]):
                if section == "publisher":
                    pub_val = getattr(pub, prop)
                else:
                    pub_val = getattr(pub.repository, prop)

                if prop in ("legal_uris", "mirrors", "origins",
                            "related_uris"):
                    # The publisher will have these as lists,
                    # so transform both sets of data first
                    # for reliable comparison.  Remove any
                    # trailing slashes so comparison can
                    # succeed.
                    if not val:
                        val = set()
                    else:
                        val = set(val.split(","))
                    new_pub_val = set()
                    for u in pub_val:
                        uri = u.uri
                        if uri.endswith("/"):
                            uri = uri[:-1]
                        new_pub_val.add(uri)
                    pub_val = new_pub_val
                self.assertEqual(val, pub_val)

        # Loading an image changed the cwd, so change it back.
        os.chdir(self.test_root)
Пример #5
0
            print "Since the port (", port, ") is in use, I am going to try the next port."
            port = port + 1
        dc = depotthread.DepotThread(repo=repo, port=port)
        print "args:", dc.get_args()
        print "starting depot..."
        try:
            dc.start()
            dc.waitforup()
        except depotthread.DepotStateException, e:
            print "unable to start depot."
            sys.exit(1)
        print "depot started."
        depoturl = dc.get_depot_url()

    pt = progress.CommandLineProgressTracker()
    img = image.Image()
    img.history.client_name = 'makeimage'
    img.set_attrs(image.IMG_USER, imagedir, False, authority, depoturl)

    # create a new Image object - it doesn't work to reuse the old one
    img = image.Image()
    img.history.client_name = 'makeimage'
    img.find_root(imagedir)
    img.load_config()
    img.set_property(u'title', title)
    img.set_property(u'description', desc)
    img.load_catalogs(pt)
    filters = []
    verbose = False
    try:
        img.make_install_plan(pkg_list,