OSDN Git Service

lib: lxcf-erase-setup clean up and bug fixes
authorMUNEDA Takahiro <mux003@gmail.com>
Mon, 3 Mar 2014 03:16:19 +0000 (22:16 -0500)
committerHideyuki Niwa <niwa.hideyuki@jp.fujitsu.com>
Mon, 3 Mar 2014 06:44:06 +0000 (15:44 +0900)
This patch series clean up and fix bugs of lxcf-erase-setup.

Thanks,
Takahiro

MUNEDA Takahiro (3):
  lib: Cleanup lxcf-erase-setup python script
  lib: fix missing library import
  lib: fix undefined name

 lxcf/lib/lxcf-erase-setup | 213 ++++++++++++++++++++++------------------------
 1 file changed, 104 insertions(+), 109 deletions(-)

--
1.8.3.2

------------------------------------------------------------------------------
Subversion Kills Productivity. Get off Subversion & Make the Move to Perforce.
With Perforce, you get hassle-free workflows. Merge that actually works.
Faster operations. Version large binaries.  Built-in WAN optimization and the
freedom to use Git, Perforce or both. Make the move to Perforce.
http://pubads.g.doubleclick.net/gampad/clk?id=122218951&iu=/4140/ostg.clktrk
_______________________________________________
Lxcfacility-allmail1 mailing list
Lxcfacility-allmail1@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/lxcfacility-allmail1
Received: from G01JPEXCHKW15.g01.fujitsu.local (10.0.194.54) by
 G01JPEXCHYT13.g01.fujitsu.local (10.128.194.52) with Microsoft SMTP Server
 (TLS) id 14.3.146.2; Mon, 3 Mar 2014 12:16:46 +0900
Received: from m025.s.css.fujitsu.com (10.0.81.65) by
 G01JPEXCHKW15.g01.fujitsu.local (10.0.194.54) with Microsoft SMTP Server id
 14.3.146.2; Mon, 3 Mar 2014 12:16:46 +0900
Received: from m025.css.fujitsu.com (m025 [127.0.0.1]) by
 m025.s.css.fujitsu.com (Postfix) with ESMTP id 8364B180AB; Mon,  3 Mar 2014
 12:16:47 +0900 (JST)
Received: from mt1.gw.fujitsu.co.jp (mt1.gw.nic.fujitsu.com [10.75.179.171])
by m025.s.css.fujitsu.com (Postfix) with ESMTP id 64C2F18113; Mon,  3 Mar
 2014 12:16:47 +0900 (JST)
Received: from st1.gw.fujitsu.co.jp (st1.gw.nic.fujitsu.com [10.75.179.161])
by mt1.gw.fujitsu.co.jp (Postfix) with ESMTP id 5518E45DD73; Mon,  3 Mar 2014
 12:16:47 +0900 (JST)
Received: from st1.gw.fujitsu.co.jp (localhost.localdomain [127.0.0.1]) by
 st1.gw.fujitsu.co.jp (Postfix) with ESMTP id 47B321DB803B; Mon,  3 Mar 2014
 12:16:47 +0900 (JST)
Received: from fgwkm6.fujitsu.co.jp (fgwkm6.fujitsu.co.jp [164.71.1.187]) by
 st1.gw.fujitsu.co.jp (Postfix) with ESMTP id DE1F8E08007; Mon,  3 Mar 2014
 12:16:46 +0900 (JST)
X-IPAS-Result: AgwCAIXzE1PYIrVYm2dsb2JhbABZg0GuR45ohRAWDgEBAQEBCAkLCRQogiYBAQQBAQEPKAwKDBIOAwECBgEWKwQECAMBGwQEAQwBBQEcGQUdh0IBFAUInwoCNYxel3QKGYEYhjkXkxgEiUuCU4IpJIZ7glaBMo8LQYR4
Received: from lists.sourceforge.net ([216.34.181.88])  by
 fgwkm6.fujitsu.co.jp with ESMTP/TLS/DHE-RSA-AES256-SHA; 03 Mar 2014 12:16:34
 +0900
Received: from localhost ([127.0.0.1] helo=sfs-ml-4.v29.ch3.sourceforge.com)
by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from
 <lxcfacility-allmail1-bounces@lists.sourceforge.net>) id 1WKJMm-0008Vo-UD;
 Mon, 03 Mar 2014 03:16:32 +0000
Received: from sog-mx-3.v43.ch3.sourceforge.com ([172.29.43.193]
helo=mx.sourceforge.net) by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim
 4.76) (envelope-from <mux003@gmail.com>) id 1WKJMm-0008Vj-54 for
 lxcfacility-allmail1@lists.sourceforge.net; Mon, 03 Mar 2014 03:16:32 +0000
Received-SPF: pass (sog-mx-3.v43.ch3.sourceforge.com: domain of gmail.com
designates 209.85.192.46 as permitted sender)
client-ip=209.85.192.46; envelope-from=mux003@gmail.com;
helo=mail-qg0-f46.google.com;
Received: from mail-qg0-f46.google.com ([209.85.192.46]) by
 sog-mx-3.v43.ch3.sourceforge.com with esmtps (TLSv1:RC4-SHA:128) (Exim 4.76)
 id 1WKJMl-0001NX-3W for lxcfacility-allmail1@lists.sourceforge.net; Mon, 03
 Mar 2014 03:16:32 +0000
Received: by mail-qg0-f46.google.com with SMTP id e89so9837092qgf.5 for
 <lxcfacility-allmail1@lists.sourceforge.net>; Sun, 02 Mar 2014 19:16:25 -0800
 (PST)
X-Received: by 10.140.19.212 with SMTP id 78mr19777374qgh.84.1393816585637;
Sun, 02 Mar 2014 19:16:25 -0800 (PST)
Received: from localhost (pool-173-76-203-247.bstnma.fios.verizon.net.
[173.76.203.247]) by mx.google.com with ESMTPSA id
u10sm34043496qar.21.2014.03.02.19.16.24 for
 <lxcfacility-allmail1@lists.sourceforge.net> (version=TLSv1.2 cipher=RC4-SHA
 bits=128/128); Sun, 02 Mar 2014 19:16:25 -0800 (PST)
From: MUNEDA Takahiro <mux003@gmail.com>
To: <lxcfacility-allmail1@lists.sourceforge.net>
Date: Sun, 2 Mar 2014 22:16:20 -0500
Message-ID: <1393816582-2609-2-git-send-email-mux003@gmail.com>
X-Mailer: git-send-email 1.8.3.2
In-Reply-To: <1393816582-2609-1-git-send-email-mux003@gmail.com>
References: <1393816582-2609-1-git-send-email-mux003@gmail.com>
X-Spam-Score: -1.4 (-)
X-Spam-Report: Spam Filtering performed by mx.sourceforge.net.
See http://spamassassin.org/tag/ for more details.
-0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/,
no trust [209.85.192.46 listed in list.dnswl.org]
-1.5 SPF_CHECK_PASS SPF reports sender host as permitted sender for
sender-domain
0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider
(mux003[at]gmail.com)
-0.0 SPF_PASS               SPF: sender matches SPF record
0.2 FREEMAIL_ENVFROM_END_DIGIT Envelope-from freemail username ends in
digit (mux003[at]gmail.com)
-0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from
author's domain
0.1 DKIM_SIGNED            Message has a DKIM or DK signature,
not necessarily valid
-0.1 DKIM_VALID Message has at least one valid DKIM or DK signature
X-Headers-End: 1WKJMl-0001NX-3W
Subject: [Lxcfacility-allmail1] [PATCH 1/3] lib: Cleanup lxcf-erase-setup
python script
X-BeenThere: lxcfacility-allmail1@lists.sourceforge.net
X-Mailman-Version: 2.1.9
Precedence: list
List-Id: <lxcfacility-allmail1.lists.sourceforge.net>
List-Unsubscribe: <https://lists.sourceforge.net/lists/listinfo/lxcfacility-allmail1>,
<mailto:lxcfacility-allmail1-request@lists.sourceforge.net?subject=unsubscribe>
List-Archive: <http://sourceforge.net/mailarchive/forum.php?forum_name=lxcfacility-allmail1>
List-Post: <mailto:lxcfacility-allmail1@lists.sourceforge.net>
List-Help: <mailto:lxcfacility-allmail1-request@lists.sourceforge.net?subject=help>
List-Subscribe: <https://lists.sourceforge.net/lists/listinfo/lxcfacility-allmail1>,
<mailto:lxcfacility-allmail1-request@lists.sourceforge.net?subject=subscribe>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: lxcfacility-allmail1-bounces@lists.sourceforge.net
Return-Path: lxcfacility-allmail1-bounces@lists.sourceforge.net
X-MS-Exchange-Organization-AVStamp-Mailbox: MSFTFF;1;0;0 0 0
X-MS-Exchange-Organization-AuthSource: G01JPEXCHKW15.g01.fujitsu.local
X-MS-Exchange-Organization-AuthAs: Anonymous
MIME-Version: 1.0

Clean up warnings detected by flake8 (a python source code checker).
This patch just fixes warnings, does not fix bugs.

lxcf/lib/lxcf-erase-setup

index 772d6c7..e01625f 100755 (executable)
@@ -5,24 +5,27 @@
 # modify it under the terms of the GNU General Public License
 # as published by the Free Software Foundation; version 2
 # of the License.
-# 
+#
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
-# 
+#
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 # 02110-1301, USA.
 
 # -*- coding: utf-8 -*-
 
-import sys, os, shutil
+import sys
+import os
 import fcntl
 import IPy
-from optparse import OptionParser,OptionGroup
+import ConfigParser
+from optparse import OptionParser
 
+CONFIG_FILE = '/etc/lxcf/lxcf.conf'
 HOSTS_FILE = '/etc/hosts'
 
 parser = OptionParser()
@@ -30,9 +33,9 @@ parser = OptionParser()
 
 argc = len(args)
 
-if (argc != 2) :
-  print "usage : lxcf-erase-setup  LXCNAME"
-  quit()
+if (argc != 2):
+    print "usage : lxcf-erase-setup  LXCNAME"
+    quit()
 
 domain_name = args[1]
 
@@ -40,133 +43,125 @@ domain_name = args[1]
 # Function for IP address
 #
 
-#
-# erase IP addres from /etc/hosts
-#
 
-def erase_ipaddr(name, hosts_file) :
-  # read hosts file and check ip addr 
-  try : 
-    f = open(hosts_file,"r")
-  except :
-    return
+def erase_ipaddr(name, hosts_file):
+    """erase IP addres from /etc/hosts"""
+    # read hosts file and check ip addr
+    try:
+        f = open(hosts_file, "r")
+    except:
+        return
 
-  Lines = f.readlines()
+    Lines = f.readlines()
 
-  f = open(hosts_file, "w")
-  fcntl.flock(f.fileno(), fcntl.LOCK_EX)
+    f = open(hosts_file, "w")
+    fcntl.flock(f.fileno(), fcntl.LOCK_EX)
 
-  for l in Lines :
-    S = l.split()
-    if (len(S) >= 1) :
-      if ((S[0])[0] == '#') :
-        continue
-      if (name != S[1]) :
-        f.write(l)
+    for l in Lines:
+        S = l.split()
+        if (len(S) >= 1):
+            if ((S[0])[0] == '#'):
+                continue
+            if (name != S[1]):
+                f.write(l)
 
-  # unlock hosts file
-  fcntl.flock(f.fileno(), fcntl.LOCK_UN)
+    # unlock hosts file
+    fcntl.flock(f.fileno(), fcntl.LOCK_UN)
 
-  f.close()
+    f.close()
 
-#
-# find IP addres from /etc/hosts
-#
 
-def find_ipaddr(name, hosts_file) :
-  # read hosts file and check ip addr 
-  try :
-    f = open(hosts_file,"r")
-  except :
-    return IPy.IP(1)
+def find_ipaddr(name, hosts_file):
+    """find IP addres from /etc/hosts"""
+    # read hosts file and check ip addr
+    try:
+        f = open(hosts_file, "r")
+    except:
+        return IPy.IP(1)
+
+    while True:
+        Line = f.readline()
+        if (Line):
+            S = Line.split()
 
-  while True :
-      Line = f.readline()
-      if (Line) :
-        S = Line.split()
+            if (len(S) >= 1):
+                if ((S[0])[0] == '#'):
+                    continue
 
-        if (len(S) >= 1) :
-          if ((S[0])[0] == '#') :
-             continue
+                try:
+                    ip_s = IPy.IP(S[0])
+                except:
+                    continue
 
-          try:
-            ip_s = IPy.IP(S[0])
-          except:
-            continue 
+                if (name == S[1]):
+                    f.close()
+                    return ip_s
+        else:
+            break
 
-          if (name == S[1]) :
-            f.close()
-            return ip_s
-      else:
-        break
+    return IPy.IP(1)
 
-  return IPy.IP(1)
 
-#
-# check IP address in /etc/hosts
-#
+def check_ipaddr(ip, hosts_file):
+    """check IP address in /etc/hosts"""
+    # read hosts file and check ip addr
+    try:
+        f = open(hosts_file, "r")
+    except:
+        return False
+
+    while True:
+        Line = f.readline()
+        if (Line):
+            S = Line.split()
 
-def check_ipaddr(ip, hosts_file) :
-  # read hosts file and check ip addr 
-  try :
-    f = open(hosts_file,"r")
-  except :
-    return False
-
-  while True :
-      Line = f.readline()
-      if (Line) :
-        S = Line.split()
-
-        if (len(S) >= 1) :
-          if ((S[0])[0] == '#') :
-             continue
-
-          try:
-            ip_s = IPy.IP(S[0])
-          except:
-            continue 
-
-          if (ip_s == ip) :
-            f.close()
+            if (len(S) >= 1):
+                if ((S[0])[0] == '#'):
+                    continue
+
+                try:
+                    ip_s = IPy.IP(S[0])
+                except:
+                    continue
+
+            if (ip_s == ip):
+                f.close()
             return False
-      else:
-        break
+        else:
+            break
 
-  return True
+    return True
 
-#
-# get new IP address 
-#
 
 def get_ipaddr(hosts_file):
-  # read the config file
-  conf = ConfigParser.SafeConfigParser()
-  conf.read(CONFIG_FILE)
+    """get new IP address"""
+    # read the config file
+    conf = ConfigParser.SafeConfigParser()
+    conf.read(CONFIG_FILE)
 
-  ipaddr_start = IPy.IP(conf.get('ipaddr_range', 'ipaddr_start'))
-  ipaddr_end = IPy.IP(conf.get('ipaddr_range', 'ipaddr_end'))
+    ipaddr_start = IPy.IP(conf.get('ipaddr_range', 'ipaddr_start'))
+    ipaddr_end = IPy.IP(conf.get('ipaddr_range', 'ipaddr_end'))
 
-  ip1 = ipaddr_start.int()
-  ip2 = ipaddr_end.int()
+    ip1 = ipaddr_start.int()
+    ip2 = ipaddr_end.int()
 
-  # looks for empty IP.address  
-  for i in range(ip1, ip2+1) :
-    ipa = IPy.IP(i)
-    if (check_ipaddr(ipa, hosts_file)) :
-       break
+    # looks for empty IP.address
+    for i in range(ip1, ip2+1):
+        ipa = IPy.IP(i)
+        if (check_ipaddr(ipa, hosts_file)):
+            break
 
-  try:
-    with open(hosts_file, 'a') as f :
-      f.write(ipa.strNormal()+'\t'+domain_name+'\n')
-  except:
-      pass
+    try:
+        with open(hosts_file, 'a') as f:
+            f.write(ipa.strNormal()+'\t'+domain_name+'\n')
+    except:
+        pass
 
-  return ipa
+    return ipa
 
 erase_ipaddr(domain_name, HOSTS_FILE)
-for hfile in os.listdir('/opt/lxcf/') :
-  hf = '/opt/lxcf/'+hfile+'/etc/hosts'
-  erase_ipaddr(domain_name, hf)
+for hfile in os.listdir('/opt/lxcf/'):
+    hf = '/opt/lxcf/'+hfile+'/etc/hosts'
+    erase_ipaddr(domain_name, hf)
 
 print "erase ip address from /etc/hosts"