devtools/pkginfo.py
changeset 0 b97547f5f1fa
child 1977 606923dff11b
equal deleted inserted replaced
-1:000000000000 0:b97547f5f1fa
       
     1 """distutils / __pkginfo__ helpers for cubicweb applications"""
       
     2 
       
     3 import os
       
     4 from os.path import isdir, join
       
     5 
       
     6 
       
     7 def get_distutils_datafiles(cube, i18n=True, recursive=False):
       
     8     """
       
     9     :param cube: application cube's name
       
    10     """
       
    11     data_files = []
       
    12     data_files += get_basepyfiles(cube)
       
    13     data_files += get_webdatafiles(cube)
       
    14     if i18n:
       
    15         data_files += get_i18nfiles(cube)
       
    16     data_files += get_viewsfiles(cube, recursive=recursive)
       
    17     data_files += get_migrationfiles(cube)
       
    18     data_files += get_schemafiles(cube)
       
    19     return data_files
       
    20 
       
    21 
       
    22 
       
    23 ## listdir filter funcs ################################################
       
    24 def nopyc_and_nodir(fname):
       
    25     if isdir(fname) or fname.endswith('.pyc') or fname.endswith('~'):
       
    26         return False
       
    27     return True
       
    28 
       
    29 def no_version_control(fname):
       
    30     if fname in ('CVS', '.svn', '.hg'):
       
    31         return False
       
    32     if fname.endswith('~'):
       
    33         return False
       
    34     return True
       
    35 
       
    36 def basepy_files(fname):
       
    37     if fname.endswith('.py') and fname != 'setup.py':
       
    38         return True
       
    39     return False
       
    40 
       
    41 def chain(*filters):
       
    42     def newfilter(fname):
       
    43         for filterfunc in filters:
       
    44             if not filterfunc(fname):
       
    45                 return False
       
    46         return True
       
    47     return newfilter
       
    48 
       
    49 def listdir_with_path(path='.', filterfunc=None):
       
    50     if filterfunc:
       
    51         return [join(path, fname) for fname in os.listdir(path) if filterfunc(join(path, fname))]
       
    52     else:
       
    53         return [join(path, fname) for fname in os.listdir(path)]
       
    54 
       
    55 
       
    56 ## data_files helpers ##################################################
       
    57 CUBES_DIR = join('share', 'cubicweb', 'cubes')
       
    58 
       
    59 def get_i18nfiles(cube):
       
    60     """returns i18n files in a suitable format for distutils's
       
    61     data_files parameter
       
    62     """
       
    63     i18ndir = join(CUBES_DIR, cube, 'i18n')
       
    64     potfiles = [(i18ndir, listdir_with_path('i18n', chain(no_version_control, nopyc_and_nodir)))]
       
    65     return potfiles
       
    66 
       
    67 
       
    68 def get_viewsfiles(cube, recursive=False):
       
    69     """returns views files in a suitable format for distutils's
       
    70     data_files parameter
       
    71 
       
    72     :param recursive: include views' subdirs recursively if True
       
    73     """
       
    74     if recursive:
       
    75         datafiles = []
       
    76         for dirpath, dirnames, filenames in os.walk('views'):
       
    77             filenames = [join(dirpath, fname) for fname in filenames
       
    78                          if nopyc_and_nodir(join(dirpath, fname))]
       
    79             dirpath = join(CUBES_DIR, cube, dirpath)
       
    80             datafiles.append((dirpath, filenames))
       
    81         return datafiles
       
    82     else:
       
    83         viewsdir = join(CUBES_DIR, cube, 'views')
       
    84         return [(viewsdir,
       
    85                  listdir_with_path('views', filterfunc=nopyc_and_nodir))]
       
    86 
       
    87 
       
    88 def get_basepyfiles(cube):
       
    89     """returns cube's base python scripts (tali18n.py, etc.)
       
    90     in a suitable format for distutils's data_files parameter
       
    91     """
       
    92     return [(join(CUBES_DIR, cube),
       
    93              [fname for fname in os.listdir('.')
       
    94               if fname.endswith('.py') and fname != 'setup.py'])]
       
    95 
       
    96 
       
    97 def get_webdatafiles(cube):
       
    98     """returns web's data files (css, png, js, etc.) in a suitable
       
    99     format for distutils's data_files parameter
       
   100     """
       
   101     return [(join(CUBES_DIR, cube, 'data'),
       
   102              listdir_with_path('data', filterfunc=no_version_control))]
       
   103 
       
   104 
       
   105 def get_migrationfiles(cube):
       
   106     """returns cube's migration scripts
       
   107     in a suitable format for distutils's data_files parameter
       
   108     """
       
   109     return [(join(CUBES_DIR, cube, 'migration'),
       
   110              listdir_with_path('migration', no_version_control))]
       
   111 
       
   112 
       
   113 def get_schemafiles(cube):
       
   114     """returns cube's schema files
       
   115     in a suitable format for distutils's data_files parameter
       
   116     """
       
   117     return [(join(CUBES_DIR, cube, 'schema'),
       
   118              listdir_with_path('schema', no_version_control))]
       
   119 
       
   120