PYTHON 61
Gather src for windows .py Guest on 1st June 2020 10:11:19 AM
  1. #! /usr/bin/env python
  2. #
  3. #  BLIS    
  4. #  An object-based framework for developing high-performance BLAS-like
  5. #  libraries.
  6. #
  7. #  Copyright (C) 2014, The University of Texas at Austin
  8. #
  9. #  Redistribution and use in source and binary forms, with or without
  10. #  modification, are permitted provided that the following conditions are
  11. #  met:
  12. #   - Redistributions of source code must retain the above copyright
  13. #     notice, this list of conditions and the following disclaimer.
  14. #   - Redistributions in binary form must reproduce the above copyright
  15. #     notice, this list of conditions and the following disclaimer in the
  16. #     documentation and/or other materials provided with the distribution.
  17. #   - Neither the name of The University of Texas at Austin nor the names
  18. #     of its contributors may be used to endorse or promote products
  19. #     derived from this software without specific prior written permission.
  20. #
  21. #  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. #  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. #  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. #  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. #  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. #  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. #  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. #  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. #  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. #  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. #  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. #
  33. #
  34.  
  35. # ------------------------------------------------------------------------------
  36.  
  37. # Import modules
  38. import sys
  39. import os
  40. import os.path
  41. import getopt
  42. import shutil
  43. import string
  44.  
  45. # Global variables for command line options, with default settings.
  46. script_name  = ""
  47. dry_run_flag = False
  48. verbose_flag = False
  49.  
  50. # Global constants
  51. flat_config_dirname  = "config"
  52. flat_header_dirname  = "include"
  53. flat_source_dirname  = "frame"
  54. leaf_list_path       = "build/leaf_list"
  55. ignore_list_path     = "build/ignore_list"
  56. ignore_list_win_path = "build/ignore_list.windows"
  57.  
  58. # ------------------------------------------------------------------------------
  59.  
  60. def print_usage():
  61.        
  62.         # Print help information.
  63.         print " "
  64.         print " %s" % script_name
  65.         print " "
  66.         print " Field G. Van Zee"
  67.         print " "
  68.         print " Walk the BLIS source tree and copy all sources necessary for"
  69.         print " building BLIS under Windows into a single flat directory with"
  70.         print " no subdirectory hierarchy."
  71.         print " "
  72.         print " Usage:"
  73.         print "   %s [options] tree_dir flat_dir" % script_name
  74.         print " "
  75.         print " The following options are accepted:"
  76.         print " "
  77.         print "   -d          dry-run"
  78.         print "                 Go through all the motions, but don't actually copy any"
  79.         print "                 files."
  80.         print "   -v          verbose"
  81.         print "                 Be verbose about actions (one line of output her action)."
  82.         print " "
  83.  
  84.         # Exit the script.
  85.         sys.exit()
  86.  
  87. # ------------------------------------------------------------------------------
  88.  
  89. def main():
  90.  
  91.         # Extern our global veriables. 
  92.         global script_name
  93.         global dry_run_flag
  94.         global verbose_flag
  95.  
  96.         # Get the script name so we can use it in our output.
  97.         ( script_dir, script_name ) = os.path.split( sys.argv[0] )
  98.        
  99.         try:
  100.                
  101.                 # Get the command line options.
  102.                 options, args = getopt.getopt( sys.argv[1:], "dv")
  103.        
  104.         except getopt.GetoptError, err:
  105.        
  106.                 # print help information and exit:
  107.                 print str( err ) # will print something like "option -a not recognized"
  108.                 print_usage()
  109.        
  110.         # Parse our expected command line options.
  111.         print 'checking options'
  112.         for o, a in options:
  113.                
  114.                 if o == "-d":
  115.                         print 'found dry run'
  116.                         dry_run_flag = True
  117.                 elif o == "-v":
  118.                         verbose_flag = True
  119.                 else:
  120.                         assert False, "unhandled option"
  121.        
  122.         # Check the number of arguments after command line option processing.
  123.         n_args = len( args )
  124.         if n_args != 2:
  125.                 print_usage()
  126.  
  127.         # Acquire the non-optional arguments.
  128.         tree_dir = args[0]
  129.         flat_dir = args[1]
  130.  
  131.         # Acquire the list of directories we will ignore.
  132.         ignore_list = read_ignore_list()
  133.  
  134.         # Acquire the list of leaf-type directories we will descend into.
  135.         leaf_list = read_leaf_list()
  136.  
  137.         # Create strings for each of the base subdirectories in the flat
  138.         # destination directory.
  139.         flat_config_base_dirpath = os.path.join( flat_dir, flat_config_dirname )
  140.         flat_header_base_dirpath = os.path.join( flat_dir, flat_header_dirname )
  141.         flat_source_base_dirpath = os.path.join( flat_dir, flat_source_dirname )
  142.  
  143.         # Start a list of directories to create.
  144.         dirs_to_create = []
  145.  
  146.         # Append the config directory. We do this outside of the for loop because
  147.         # we don't need subdirectories for each leaf type.
  148.         dirs_to_create.append( flat_config_base_dirpath )
  149.  
  150.         # For each of the leaf specifications, make the full pathnames of the
  151.         # subdirectories that will reside within the root destination directory.
  152.         for leaf_spec in leaf_list:
  153.                
  154.                 # Unpack the leaf_spec tuple.
  155.                 src_exts, hdr_exts = leaf_spec
  156.                
  157.                 # Append the directory path name to our list.
  158.                 dirs_to_create.append( flat_header_base_dirpath )
  159.                 dirs_to_create.append( flat_source_base_dirpath )
  160.  
  161.         # Iterate over the directory list we just created.
  162.         for dirpath in dirs_to_create:
  163.  
  164.                 # Make the subdirectories within the root destination directory, but
  165.                 # only if they are not existing directories.
  166.                 if os.path.isdir( dirpath ) == False:
  167.        
  168.                         # Take action only if this is not a dry run.
  169.                         if dry_run_flag == False:
  170.        
  171.                                 # Be verbose if verbosity was requested.
  172.                                 if verbose_flag == True:
  173.                                         print "%s: creating directory %s" % ( script_name, dirpath )
  174.                        
  175.                                 # Make the directory, and parent directories, for dirpath.
  176.                                 os.makedirs( dirpath )
  177.        
  178.                         else:
  179.        
  180.                                 # Be verbose if verbosity was requested.
  181.                                 if verbose_flag == True:
  182.                                         print "%s: (dry-run) creating directory %s" % ( script_name, dirpath )
  183.  
  184.  
  185.         # Walk the directory structure top-down.
  186.         for dirpath, dirnames, filenames in os.walk( tree_dir ):
  187.                
  188.                 # Remove directories that appear in the ignore list.
  189.                 for item in ignore_list:
  190.                         if item in dirnames:
  191.                                 dirnames.remove( item )
  192.  
  193.                 # Consider each leaf specification. If we find the name in the directory
  194.                 # path, then copy the files with its designated extensions into the flat
  195.                 # source directory.
  196.                 for leaf_spec in leaf_list:
  197.  
  198.                         # Unpack the leaf_spec tuple.
  199.                         src_exts, hdr_exts = leaf_spec
  200.  
  201.                         # At this point following line can probably be removed
  202.                         type_dir_name = os.sep + ''
  203.  
  204.                         flat_source_leaf_dirpath = flat_source_base_dirpath
  205.                         flat_header_leaf_dirpath = flat_header_base_dirpath
  206.  
  207.                         if dirpath.find( type_dir_name ) != -1:
  208.                                 copy_files_to_flat_subdirs( dirpath, filenames, src_exts, hdr_exts,
  209.                                                             flat_source_leaf_dirpath,
  210.                                                             flat_header_leaf_dirpath )
  211.  
  212. # ------------------------------------------------------------------------------
  213.  
  214. def copy_files_to_flat_subdirs( dirpath, filenames, src_exts, hdr_exts, src_dirpath, hdr_dirpath ):
  215.  
  216.         # Consider all files in dirpath.
  217.         for filename in filenames:
  218.                
  219.                 # Construct the full file path for the current file.
  220.                 filepath = os.path.join( dirpath, filename )
  221.  
  222.                 # Iterate over the valid source extensions for the current directory
  223.                 # path.
  224.                 for src_ext in src_exts:
  225.  
  226.                         # If the filename/filepath ends with the source extension, copy it
  227.                         # to the source subdirectory within the flat destination directory.
  228.                         if filepath.endswith( src_ext ):
  229.                                
  230.                                 # Take action only if this is not a dry run.
  231.                                 if dry_run_flag == False:
  232.        
  233.                                         # Be verbose if verbosity was requested.
  234.                                         if verbose_flag == True:
  235.                                                 print "%s: copying to %s from %s" % ( script_name, src_dirpath, filepath )
  236.                                
  237.                                         # Copy the source file to the source subdirectory.
  238.                                         shutil.copy2( filepath, src_dirpath )
  239.        
  240.                                 else:
  241.        
  242.                                         # Be verbose if verbosity was requested.
  243.                                         if verbose_flag == True:
  244.                                                 print "%s: (dry-run) copying to %s from %s" % ( script_name, src_dirpath, filepath )
  245.        
  246.                 # Iterate over the valid header extensions for the current directory
  247.                 # path.
  248.                 for hdr_ext in hdr_exts:
  249.  
  250.                         # If the filename/filepath ends with the header extension, copy it
  251.                         # to the include subdirectory within the flat destination directory.
  252.                         if filepath.endswith( hdr_ext ):
  253.        
  254.                                 # Take action only if this is not a dry run.
  255.                                 if dry_run_flag == False:
  256.        
  257.                                         # Be verbose if verbosity was requested.
  258.                                         if verbose_flag == True:
  259.                                                 print "%s: copying to %s from %s" % ( script_name, hdr_dirpath, filepath )
  260.                                
  261.                                         # Copy the header file to the header subdirectory.
  262.                                         shutil.copy2( filepath, hdr_dirpath )
  263.        
  264.                                 else:
  265.  
  266.                                         # Be verbose if verbosity was requested.
  267.                                         if verbose_flag == True:
  268.                                                 print "%s: (dry-run) copying to %s from %s" % ( script_name, hdr_dirpath, filepath )
  269.  
  270. # ------------------------------------------------------------------------------
  271.  
  272. def read_ignore_list():
  273.  
  274.         # Open the ignore list files as read-only.
  275.         ignore_file     = open( ignore_list_path, 'r' )
  276.         ignore_file_win = open( ignore_list_win_path, 'r' )
  277.  
  278.         # Read all lines in the ignore list files. The items in these lists contain
  279.         # newlines, which we'll strip out shortly.
  280.         raw_list     = ignore_file.readlines()
  281.         raw_win_list = ignore_file_win.readlines()
  282.  
  283.         # Close the files.
  284.         ignore_file.close()
  285.         ignore_file_win.close()
  286.  
  287.         # Initialize an empty ignore list for the stripped version of the raw list.
  288.         ignore_list = []
  289.  
  290.         # Iterate over the first raw list.
  291.         for line in raw_list:
  292.                
  293.                 # Append the stripped line to a new list.
  294.                 ignore_list.append( line.strip() )
  295.  
  296.         # Iterate over the second raw list.
  297.         for line in raw_win_list:
  298.                
  299.                 # Append the stripped line to a new list.
  300.                 ignore_list.append( line.strip() )
  301.  
  302.         # Return the list of stripped lines.
  303.         return ignore_list
  304.  
  305. # ------------------------------------------------------------------------------
  306.  
  307. def read_leaf_list():
  308.  
  309.         # Open the leaf list file.
  310.         leaf_file = open( leaf_list_path, 'r' )
  311.  
  312.         # Read the lines in the file.
  313.         line_list = leaf_file.readlines()
  314.  
  315.         # Start with a blank list.
  316.         leaf_list = []
  317.  
  318.         # Iterate over the lines.
  319.         for line in line_list:
  320.  
  321.                 # Split the specification by colon to separate the fields.
  322.                 fields = string.split( string.strip( line ), ':' )
  323.  
  324.                 # Get the individual fields of the specification.
  325.                 src_exts = string.split( fields[0], ',' )
  326.                 hdr_exts = string.split( fields[1], ',' )
  327.  
  328.                 # If it's a singleton list of an empty string, make it an empty list.
  329.                 if len(src_exts) == 1:
  330.                         if src_exts[0] == '':
  331.                                 src_exts = []
  332.                
  333.                 # If it's a singleton list of an empty string, make it an empty list.
  334.                 if len(hdr_exts) == 1:
  335.                         if hdr_exts[0] == '':
  336.                                 hdr_exts = []
  337.                
  338.                 # Pack the fields into a tuple.
  339.                 leaf_spec = ( src_exts, hdr_exts )
  340.  
  341.                
  342.                 # Append the tuple to our list.
  343.                 leaf_list.append( leaf_spec )
  344.  
  345.         # Return the list.
  346.         return leaf_list
  347.  
  348. # ------------------------------------------------------------------------------
  349.  
  350. # Begin by executing main().
  351. main()

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.