toolsutils.py
branchtls-sprint
changeset 1802 d628defebc17
parent 1138 22f634977c95
child 1977 606923dff11b
equal deleted inserted replaced
1801:672acc730ce5 1802:d628defebc17
    37     except OSError, ex:
    37     except OSError, ex:
    38         import errno
    38         import errno
    39         if ex.errno != errno.EEXIST:
    39         if ex.errno != errno.EEXIST:
    40             raise
    40             raise
    41         print 'directory %s already exists' % directory
    41         print 'directory %s already exists' % directory
    42                 
    42 
    43 def create_symlink(source, target):
    43 def create_symlink(source, target):
    44     """create a symbolic link"""
    44     """create a symbolic link"""
    45     if exists(target):
    45     if exists(target):
    46         remove(target)
    46         remove(target)
    47     symlink(source, target)
    47     symlink(source, target)
    49 
    49 
    50 def create_copy(source, target):
    50 def create_copy(source, target):
    51     import shutil
    51     import shutil
    52     print '[copy] %s <-- %s' % (target, source)
    52     print '[copy] %s <-- %s' % (target, source)
    53     shutil.copy2(source, target)
    53     shutil.copy2(source, target)
    54     
    54 
    55 def rm(whatever):
    55 def rm(whatever):
    56     import shutil
    56     import shutil
    57     shutil.rmtree(whatever)
    57     shutil.rmtree(whatever)
    58     print 'removed %s' % whatever
    58     print 'removed %s' % whatever
    59 
    59 
    64     import shutil
    64     import shutil
    65     p_output = os.popen('diff -u %s %s' % (appl_file, ref_file), 'r')
    65     p_output = os.popen('diff -u %s %s' % (appl_file, ref_file), 'r')
    66     diffs = p_output.read()
    66     diffs = p_output.read()
    67     if diffs:
    67     if diffs:
    68         if askconfirm:
    68         if askconfirm:
    69             print 
    69             print
    70             print diffs
    70             print diffs
    71             action = raw_input('replace (N/y/q) ? ').lower()
    71             action = raw_input('replace (N/y/q) ? ').lower()
    72         else:
    72         else:
    73             action = 'y'
    73             action = 'y'
    74         if action == 'y':
    74         if action == 'y':
   118                     print '[generate] %s <-- %s' % (tfpath, fpath)
   118                     print '[generate] %s <-- %s' % (tfpath, fpath)
   119             elif exists(tfpath):
   119             elif exists(tfpath):
   120                 show_diffs(tfpath, fpath, askconfirm)
   120                 show_diffs(tfpath, fpath, askconfirm)
   121             else:
   121             else:
   122                 shutil.copyfile(fpath, tfpath)
   122                 shutil.copyfile(fpath, tfpath)
   123                 
   123 
   124 def fill_templated_file(fpath, tfpath, context):
   124 def fill_templated_file(fpath, tfpath, context):
   125     fobj = file(tfpath, 'w')
   125     fobj = file(tfpath, 'w')
   126     templated = file(fpath).read()
   126     templated = file(fpath).read()
   127     fobj.write(templated % context)
   127     fobj.write(templated % context)
   128     fobj.close()
   128     fobj.close()
   156 
   156 
   157     :type config_file: str
   157     :type config_file: str
   158     :param config_file: path to the configuration file
   158     :param config_file: path to the configuration file
   159 
   159 
   160     :rtype: dict
   160     :rtype: dict
   161     :return: a dictionary with specified values associated to option names 
   161     :return: a dictionary with specified values associated to option names
   162     """
   162     """
   163     from logilab.common.fileutils import lines
   163     from logilab.common.fileutils import lines
   164     config = current = {}
   164     config = current = {}
   165     try:
   165     try:
   166         for line in lines(config_file, comments='#'):
   166         for line in lines(config_file, comments='#'):
   192     :type env_var: str
   192     :type env_var: str
   193     :param env_var: name of an environment variable
   193     :param env_var: name of an environment variable
   194 
   194 
   195     :type default: str
   195     :type default: str
   196     :param default: default value if the environment variable is not defined
   196     :param default: default value if the environment variable is not defined
   197     
   197 
   198     :type name: str
   198     :type name: str
   199     :param name: the informal name of the path, used for error message
   199     :param name: the informal name of the path, used for error message
   200     
   200 
   201     :rtype: str
   201     :rtype: str
   202     :return: the value of the environment variable or the default value
   202     :return: the value of the environment variable or the default value
   203 
   203 
   204     :raise `ConfigurationError`: if the returned path does not exist
   204     :raise `ConfigurationError`: if the returned path does not exist
   205     """
   205     """
   241                     return helpercls(config)
   241                     return helpercls(config)
   242         if required:
   242         if required:
   243             msg = 'No helper for command %s using %s configuration' % (
   243             msg = 'No helper for command %s using %s configuration' % (
   244                 cmdname, config.name)
   244                 cmdname, config.name)
   245             raise ConfigurationError(msg)
   245             raise ConfigurationError(msg)
   246         
   246 
   247     def fail(self, reason):
   247     def fail(self, reason):
   248         print "command failed:", reason
   248         print "command failed:", reason
   249         sys.exit(1)
   249         sys.exit(1)
   250     
   250 
   251                     
   251 
   252 def main_run(args, doc):
   252 def main_run(args, doc):
   253     """command line tool"""
   253     """command line tool"""
   254     try:
   254     try:
   255         base_main_run(args, doc)
   255         base_main_run(args, doc)
   256     except ConfigurationError, err:
   256     except ConfigurationError, err:
   287         user = raw_input('login: ')
   287         user = raw_input('login: ')
   288     password = optconfig.password
   288     password = optconfig.password
   289     if not password:
   289     if not password:
   290         password = getpass('password: ')
   290         password = getpass('password: ')
   291     return connect(user=user, password=password, host=optconfig.host, database=appid)
   291     return connect(user=user, password=password, host=optconfig.host, database=appid)
   292     
   292