[libvirt] [PATCH 07/16] Change all internal functions that use __METHOD to use _METHOD. __METHOD's.

Dan Walsh dwalsh at redhat.com
Tue Apr 2 22:11:23 UTC 2013


Python makes assumptions about __METHOD names that will break some of the other patches that I am adding, involving inheritance of classes.  The _METHODS are
treated the same as any methods, but still give maintainers an idea that they
should not be used.

Signed-off-by: Dan Walsh <dwalsh at redhat.com>
---
 bin/virt-sandbox-service | 108 +++++++++++++++++++++++------------------------
 1 file changed, 54 insertions(+), 54 deletions(-)

diff --git a/bin/virt-sandbox-service b/bin/virt-sandbox-service
index a064e9a..7f4dcc8 100755
--- a/bin/virt-sandbox-service
+++ b/bin/virt-sandbox-service
@@ -103,7 +103,7 @@ class Container:
             except GLib.GError, e:
                 raise OSError(config + ": " + str(e))
 
-    def __follow_units(self):
+    def _follow_units(self):
         unitst=""
         for i, src in self.unit_file_list:
             unitst += "ReloadPropagatedFrom=%s\n" % i
@@ -118,17 +118,17 @@ class Container:
     def set_unit_file_list(self, unit_file_list):
         self.unit_file_list = unit_file_list
 
-    def __get_sandboxed_service(self):
+    def _get_sandboxed_service(self):
         return self.unit_file_list[0][0].split(".")[0]
 
-    def __get_sandbox_target(self):
+    def _get_sandbox_target(self):
         if len(self.unit_file_list) > 1:
             raise ValueError([_("Only Valid for single units")])
-        return "%s_sandbox.target" %  self.__get_sandboxed_service()
+        return "%s_sandbox.target" %  self._get_sandboxed_service()
 
-    def __target(self):
+    def _target(self):
         try:
-            name = self.__get_sandbox_target()
+            name = self._get_sandbox_target()
             path = "/etc/systemd/system/" + name
             if not os.path.exists(path):
                 fd = open(path, "w")
@@ -147,12 +147,12 @@ Documentation=man:virt-sandbox-service(1)
 
 [Install]
 WantedBy=multi-user.target
-""" % { "NAME" : self.__get_sandboxed_service() })
+""" % { "NAME" : self._get_sandboxed_service() })
             return "%s" % name
         except OSError:
             return ""
 
-    def __create_system_unit(self):
+    def _create_system_unit(self):
         self.unitfile = self.get_unit_path()
         name = self.config.get_name()
         unit = r"""
@@ -169,7 +169,7 @@ ExecStop=/usr/bin/virt-sandbox-service stop %(NAME)s
 
 [Install]
 WantedBy=%(TARGET)s
-""" % { 'NAME':name, 'FOLLOW':self.__follow_units(), 'TARGET':self.__target(), 'RELOAD': " -u ".join(map(lambda x: x[0], self.unit_file_list)) }
+""" % { 'NAME':name, 'FOLLOW':self._follow_units(), 'TARGET':self._target(), 'RELOAD': " -u ".join(map(lambda x: x[0], self.unit_file_list)) }
         fd = open(self.unitfile, "w")
         fd.write(unit)
         fd.close()
@@ -181,7 +181,7 @@ WantedBy=%(TARGET)s
             raise OSError(_("Failed to enable %s unit file") %  self.unitfile)
         sys.stdout.write(_("Created unit file %s\n") %  self.unitfile)
 
-    def __add_dir(self, newd):
+    def _add_dir(self, newd):
         if newd in self.all_dirs:
             return
         for ignd in self.IGNORE_DIRS:
@@ -200,7 +200,7 @@ WantedBy=%(TARGET)s
                 self.dirs.append(newd)
                 break;
 
-    def __add_file(self, newf):
+    def _add_file(self, newf):
         if newf in self.files:
             return
         for d in self.IGNORE_DIRS:
@@ -280,7 +280,7 @@ WantedBy=%(TARGET)s
         self.dest = "%s/%s" % (self.path, self.name)
         self.config = LibvirtSandbox.ConfigService.new(name)
 
-    def __extract_rpms(self):
+    def _extract_rpms(self):
         self.all_dirs = []
         self.dirs = []
         self.files = []
@@ -288,9 +288,9 @@ WantedBy=%(TARGET)s
         self.ts = rpm.ts()
 
         for u, src in self.unit_file_list:
-            self.__extract_rpm_for_unit(src)
+            self._extract_rpm_for_unit(src)
 
-    def __split_filename(self, filename):
+    def _split_filename(self, filename):
         if filename[-4:] == '.rpm':
             filename = filename[:-4]
 
@@ -312,7 +312,7 @@ WantedBy=%(TARGET)s
         name = filename[epochIndex + 1:verIndex]
         return name, ver, rel, epoch, arch
 
-    def __extract_rpm_for_unit(self, unitfile):
+    def _extract_rpm_for_unit(self, unitfile):
         mi = self.ts.dbMatch(rpm.RPMTAG_BASENAMES, unitfile)
         try:
             h = mi.next();
@@ -323,12 +323,12 @@ WantedBy=%(TARGET)s
             fname = fentry[0]
 
             if os.path.isdir(fname):
-                self.__add_dir(fname)
+                self._add_dir(fname)
             if os.path.isfile(fname):
-                self.__add_file(fname)
+                self._add_file(fname)
 
         srcrpm = h[rpm.RPMTAG_SOURCERPM]
-        srcrpmbits = self.__split_filename(srcrpm)
+        srcrpmbits = self._split_filename(srcrpm)
 
         if srcrpmbits[0] == h[rpm.RPMTAG_NAME]:
             return
@@ -343,11 +343,11 @@ WantedBy=%(TARGET)s
             fname = fentry[0]
 
             if os.path.isdir(fname):
-                self.__add_dir(fname)
+                self._add_dir(fname)
             if os.path.isfile(fname):
-                self.__add_file(fname)
+                self._add_file(fname)
 
-    def __add_mount(self, source, dest):
+    def _add_mount(self, source, dest):
         mount = LibvirtSandbox.ConfigMountHostBind.new(source, dest)
         self.config.add_mount(mount)
 
@@ -376,7 +376,7 @@ WantedBy=%(TARGET)s
 
         os.symlink(self.dest + jpath, jpath)
 
-    def __gen_filesystems(self):
+    def _gen_filesystems(self):
         if self.use_image:
             self.image = self.DEFAULT_IMAGE % self.get_name()
             mount = LibvirtSandbox.ConfigMountHostImage.new(self.image, self.dest)
@@ -397,12 +397,12 @@ WantedBy=%(TARGET)s
         for d in self.BIND_SYSTEM_DIRS:
             if os.path.exists(d):
                 source = "%s%s" % ( self.dest, d)
-                self.__add_mount(source, d)
+                self._add_mount(source, d)
 
         for f in self.BIND_SYSTEM_FILES:
             if os.path.exists(f):
                 source = "%s%s" % ( self.dest, f)
-                self.__add_mount(source, f)
+                self._add_mount(source, f)
 
         for d in self.dirs:
             found = False
@@ -413,12 +413,12 @@ WantedBy=%(TARGET)s
                     break
             if not found:
                 source = "%s%s" % ( self.dest, d)
-                self.__add_mount(source, d)
+                self._add_mount(source, d)
 
-    def __get_init_path(self):
+    def _get_init_path(self):
         return "%s/%s/init" % (self.path, self.get_name())
 
-    def __create_container_unit(self, src, dest, unit):
+    def _create_container_unit(self, src, dest, unit):
             fd = open(dest + "/" + unit, "w")
             fd.write(""".include %s
 [Service]
@@ -427,7 +427,7 @@ PrivateNetwork=false
 """ % src )
             fd.close()
 
-    def __fix_stat(self, f):
+    def _fix_stat(self, f):
         try:
             s = os.stat(f)
             path = "%s%s" % (self.dest, f)
@@ -437,15 +437,15 @@ PrivateNetwork=false
             if not e.errno == errno.ENOENT:
                 raise e
 
-    def __fix_protection(self):
+    def _fix_protection(self):
         l = len(self.dest)
         for root, dirs, files in os.walk(self.dest):
             for f in files:
                 dest = root + "/" + f
-                self.__fix_stat(dest[l:])
+                self._fix_stat(dest[l:])
             for d in dirs:
                 dest = root + "/" + d
-                self.__fix_stat(dest[l:])
+                self._fix_stat(dest[l:])
 
     def makedirs(self, d):
         try:
@@ -465,7 +465,7 @@ PrivateNetwork=false
             if not e.errno == errno.EEXIST:
                 raise e
 
-    def __gen_content(self):
+    def _gen_content(self):
         if self.copy:
             for d in self.dirs:
                 shutil.copytree(d, "%s%s" % (self.dest, d), symlinks=True)
@@ -505,7 +505,7 @@ PrivateNetwork=false
         os.symlink("/run", self.dest + "/var/run")
 
         for i, src in self.unit_file_list:
-            self.__create_container_unit(src, self.dest + unitdir, i)
+            self._create_container_unit(src, self.dest + unitdir, i)
             os.symlink("../" + i, self.dest + tgtdir + "/" + i)
 
         tgtfile = unitdir + "/multi-user.target"
@@ -522,15 +522,15 @@ PrivateNetwork=false
             profile = "/etc/skel/" + p
             shutil.copy(profile, self.dest + "/root/")
 
-        self.__fix_protection()
+        self._fix_protection()
 
-    def __umount(self):
+    def _umount(self):
         p = Popen(["/bin/umount", self.dest])
         p.communicate()
         if p.returncode and p.returncode != 0:
             raise OSError(_("Failed to unmount image %s from %s") %  (self.image, self.dest))
 
-    def __create_image(self):
+    def _create_image(self):
         fd = open(self.image, "w")
         fd.truncate(self.size)
         fd.close()
@@ -553,7 +553,7 @@ PrivateNetwork=false
             selinux.restorecon(config)
         sys.stdout.write(_("Created sandbox config %s\n") % config)
 
-    def __get_image_path(self):
+    def _get_image_path(self):
             mounts = self.config.get_mounts()
             for m in mounts:
                 if type(m) != LibvirtSandbox.ConfigMountHostImage:
@@ -577,7 +577,7 @@ PrivateNetwork=false
 
         if os.path.exists(self.dest):
             shutil.rmtree(self.dest)
-            mount_path = self.__get_image_path()
+            mount_path = self._get_image_path()
             if mount_path:
                 os.remove(mount_path)
 
@@ -618,23 +618,23 @@ PrivateNetwork=false
         self._delete()
 
     def _create(self):
-        self.__connect()
+        self._connect()
         self.config.set_shell(True)
         self.config.set_boot_target("multi-user.target")
-        self.__extract_rpms()
-        self.__gen_filesystems()
+        self._extract_rpms()
+        self._gen_filesystems()
         os.mkdir(self.dest)
         if self.image:
-            self.__create_image()
-            self.__gen_content()
-            self.__umount()
+            self._create_image()
+            self._gen_content()
+            self._umount()
             sys.stdout.write(_("Created sandbox container image %s\n") % self.image)
         else:
-            self.__gen_content()
+            self._gen_content()
             sys.stdout.write(_("Created sandbox container dir %s\n") % self.dest)
         self.set_security_label()
         self.save_config()
-        self.__create_system_unit()
+        self._create_system_unit()
 
     def create(self):
         if os.path.exists(self.dest):
@@ -661,12 +661,12 @@ PrivateNetwork=false
         args.command = [ "systemctl", "reload" ] + map(lambda x: x[0], unitfiles)
         execute(args)
 
-    def __connect(self):
+    def _connect(self):
         if not self.conn:
             self.conn=LibvirtGObject.Connection.new(self.uri)
             self.conn.open(None)
 
-    def __disconnect(self):
+    def _disconnect(self):
         if self.conn:
             self.conn.close()
             self.conn = None
@@ -675,10 +675,10 @@ PrivateNetwork=false
         def closed(obj, error):
             self.loop.quit()
         try:
-            self.__connect()
+            self._connect()
             context = LibvirtSandbox.ContextService.new(self.conn, self.config)
             context.attach()
-            self.__disconnect()
+            self._disconnect()
             return 1
         except GLib.GError, e:
             return 0
@@ -688,7 +688,7 @@ PrivateNetwork=false
             self.loop.quit()
 
         try:
-            self.__connect()
+            self._connect()
             context = LibvirtSandbox.ContextService.new(self.conn, self.config)
             context.start()
             console = context.get_log_console()
@@ -709,7 +709,7 @@ PrivateNetwork=false
 
     def stop(self):
         try:
-            self.__connect()
+            self._connect()
             context = LibvirtSandbox.ContextService.new(self.conn, self.config)
             context.attach()
             context.stop()
@@ -721,7 +721,7 @@ PrivateNetwork=false
             self.loop.quit()
 
         try:
-            self.__connect()
+            self._connect()
             context = LibvirtSandbox.ContextService.new(self.conn, self.config)
             context.attach()
             console = context.get_shell_console()
@@ -741,7 +741,7 @@ PrivateNetwork=false
             self.loop.quit()
 
         try:
-            self.__connect()
+            self._connect()
             context = LibvirtSandbox.ContextService.new(self.conn, self.config)
             context.attach()
             console = context.get_shell_console()
-- 
1.8.2




More information about the libvir-list mailing list