[hipl-dev] [Branch ~hipl-core/hipl/trunk] Rev 6374: Use subprocess without shell instead of os.system() or Popen(shell=True).

  • From: noreply@xxxxxxxxxxxxx
  • To: HIPL core team <hipl-dev@xxxxxxxxxxxxx>
  • Date: Tue, 24 Apr 2012 15:49:13 -0000

------------------------------------------------------------
revno: 6374
committer: Paul Tötterman <paul.totterman@xxxxxx>
branch nick: methods-to-functions
timestamp: Wed 2012-04-18 22:59:17 +0300
message:
  Use subprocess without shell instead of os.system() or Popen(shell=True).
modified:
  tools/hipdnsproxy/hipdnsproxy.in


--
lp:hipl
https://code.launchpad.net/~hipl-core/hipl/trunk

Your team HIPL core team is subscribed to branch lp:hipl.
To unsubscribe from this branch go to 
https://code.launchpad.net/~hipl-core/hipl/trunk/+edit-subscription
=== modified file 'tools/hipdnsproxy/hipdnsproxy.in'
--- tools/hipdnsproxy/hipdnsproxy.in    2012-04-18 19:42:50 +0000
+++ tools/hipdnsproxy/hipdnsproxy.in    2012-04-18 19:59:17 +0000
@@ -106,8 +106,8 @@
 def hit_to_lsi(hit):
     """Return LSI for HIT if found."""
     output = subprocess.Popen(['hipconf', 'daemon', 'hit-to-lsi', hit],
-                              stderr=subprocess.STDOUT,
-                              stdout=subprocess.PIPE).stdout
+                              stdout=subprocess.PIPE,
+                              stderr=subprocess.STDOUT).stdout
 
     for line in output:
         match = LSI_RE.search(line)
@@ -118,8 +118,8 @@
 def lsi_to_hit(lsi):
     """Return HIT for LSI if found."""
     output = subprocess.Popen(['hipconf', 'daemon', 'lsi-to-hit', lsi],
-                              stderr=subprocess.STDOUT,
-                              stdout=subprocess.PIPE).stdout
+                              stdout=subprocess.PIPE,
+                              stderr=subprocess.STDOUT).stdout
 
     for line in output:
         match = HIT_RE.search(line)
@@ -207,8 +207,7 @@
         else:
             self.resolvconf_towrite = '/etc/resolv.conf'
 
-        self.dnsmasq_restart = (self.dnsmasq_initd_script +
-                                ' restart > /dev/null')
+        self.dnsmasq_restart = [self.dnsmasq_initd_script, 'restart']
         if filetowatch is None:
             self.filetowatch = self.guess_resolvconf()
         self.resolvconf_orig = self.filetowatch
@@ -288,7 +287,9 @@
                     if line.find(self.rh_before) == 0:
                         print self.rh_inject
                     print line,
-            os.system(self.dnsmasq_restart)
+            subprocess.check_call(self.dnsmasq_restart,
+                                  stdout=open(os.devnull, 'wb'),
+                                  stderr=subprocess.STDOUT)
             self.fout.write('Hooked with dnsmasq\n')
             # Restarting of dnsproxy changes also resolv conf. Reset timer
             # to make sure that we don't load dnsproxy's IP address. Otherwise
@@ -312,7 +313,9 @@
                                             inplace=1):
                     if line.find(self.rh_inject) == -1:
                         print line,
-            os.system(self.dnsmasq_restart)
+            subprocess.check_call(self.dnsmasq_restart,
+                                  stdout=open(os.devnull, 'wb'),
+                                  stderr=subprocess.STDOUT)
         if (not (self.use_dnsmasq_hook and self.use_resolvconf) and
             self.overwrite_resolv_conf):
             os.rename(self.resolvconf_bkname, self.resolvconf_towrite)
@@ -368,10 +371,11 @@
     def stop(self):
         """Perform shutdown routines."""
         self.restore_resolvconf_dnsmasq()
-        os.system("ifconfig lo:53 down")
+        subprocess.check_call(['ifconfig', 'lo:53', 'down'])
         # Sometimes hipconf processes get stuck, particularly when
         # hipd is busy or unresponsive. This is a workaround.
-        os.system('killall --quiet hipconf 2> /dev/null')
+        subprocess.check_call(['killall', '--quiet', 'hipconf'],
+                              stderr=open(os.devnull, 'wb'))
 
 
 class DNSProxy:
@@ -604,12 +608,13 @@
         adding them to the cache?
         """
         localhit = []
-        cmd = "ifconfig dummy0 2>&1"
-        proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout
+        proc = subprocess.Popen(['ifconfig', 'dummy0'],
+                                stdout=subprocess.PIPE,
+                                stderr=subprocess.STDOUT).stdout
         result = proc.readline()
         while result:
-            start = result.find("2001:1")
-            end = result.find("/28")
+            start = result.find('2001:1')
+            end = result.find('/28')
             if start != -1 and end != -1:
                 hit = result[start:end]
                 if not self.getaddr(hit):
@@ -618,15 +623,15 @@
         proc.close()
         ofile = open(self.default_hiphosts, 'a')
         for i in range(len(localhit)):
-            ofile.write(localhit[i] + "\tlocalhit" + str(i + 1) + '\n')
+            ofile.write('%s\tlocalhit%s\n' % (localhit[i], i + 1))
         ofile.close()
 
     def add_hit_ip_map(self, hit, addr):
         """Add IP for HIT."""
-        cmd = "hipconf daemon add map " + hit + " " + addr + \
-            " > /dev/null 2>&1"
         self.fout.write('Associating HIT %s with IP %s\n' % (hit, addr))
-        os.system(cmd)
+        subprocess.check_call(['hipconf', 'daemon', 'add', 'map', hit, addr],
+                              stdout=open(os.devnull, 'wb'),
+                              stderr=subprocess.STDOUT)
 
     def hip_cache_lookup(self, packet):
         """Make a cache lookup."""

Other related posts:

  • » [hipl-dev] [Branch ~hipl-core/hipl/trunk] Rev 6374: Use subprocess without shell instead of os.system() or Popen(shell=True). - noreply