def check_for_cmd(cmd):
    """Make sure that a program necessary for using this script is
    available.

    Arguments:
        cmd -- string or list of strings of commands. A single string may
               not contain spaces.

    Returns:
        Nothing
    """

    # Display input parameters
    start_time = get_clock()
    logging.info('Entered function')
    logging.debug('cmd: %s', cmd)

    # Ensure there are no embedded spaces in a string command
    if isinstance(cmd, str) and ' ' in cmd:
        end_time = get_clock()
        logging.error('Exiting - embedded space in command')
        logging.info('Elapsed time: %f', (end_time - start_time))
        exit(1)

    # Execute the command
    execute_cmd(cmd=cmd, cmd_source='check_for_cmd')

    end_time = get_clock()
    logging.info('Elapsed time: %f', (end_time - start_time))
def git_ls_files():
    """Find files that are relevant based on all files for the
       repository branch.

    Arguments:
        None

    Returns:
        A list of filenames.
    """

    # Display input parameters
    start_time = get_clock()
    logging.info('Entered function')

    cmd = ['git', 'ls-files']

    # Get a list of all files in the current repository branch
    cmd_stdout = execute_cmd(cmd=cmd, cmd_source='git_ls_files')

    end_time = get_clock()
    logging.info('Elapsed time: %f', (end_time - start_time))

    # Return from the function
    return cmd_stdout
def execute_cmd(cmd, cmd_source=None):
    """Execute the supplied program.

    Arguments:
        cmd -- string or list of strings of commands. A single string may
               not contain spaces.
        cmd_source -- The function requesting the program execution.
                      Default value of None.

    Returns:
        Process stdout file handle
    """

    # Display input parameters
    start_time = get_clock()
    logging.info('Entered function')
    logging.debug('cmd: %s', cmd)
    logging.debug('cmd_source: %s', cmd_source)

    # Ensure there are no embedded spaces in a string command
    if isinstance(cmd, str) and ' ' in cmd:
        end_time = get_clock()
        logging.error('Exiting - embedded space in command')
        logging.info('Elapsed time: %f', (end_time - start_time))
        exit(1)

    # Execute the command
    try:
        cmd_handle = subprocess.Popen(cmd,
                                      stdout=subprocess.PIPE,
                                      stderr=subprocess.PIPE)
        (cmd_stdout, cmd_stderr) = cmd_handle.communicate()
        logging.info('Command %s successfully executed', cmd)
        if cmd_stderr:
            for line in cmd_stderr.strip().decode("utf-8").splitlines():
                logging.info("stderr line: %s", line)
    # If the command fails, notify the user and exit immediately
    except subprocess.CalledProcessError as err:
        end_time = get_clock()
        logging.info("Program %s call failed! -- Exiting.", cmd, exc_info=True)
        logging.error("Program %s call failed! -- Exiting.", cmd)
        logging.info('Elapsed time: %f', (end_time - start_time))
        raise
    except OSError as err:
        end_time = get_clock()
        logging.info("Program %s caused on OS error! -- Exiting.",
                     cmd,
                     exc_info=True)
        logging.error("Program %s caused OS error %s! -- Exiting.", cmd,
                      err.errno)
        logging.info('Elapsed time: %f', (end_time - start_time))
        raise

    end_time = get_clock()
    logging.info('Elapsed time: %f', (end_time - start_time))

    # Return from the function
    return cmd_stdout
def get_checkout_files(first_hash, second_hash):
    """Find files that have been modified over the range of the supplied
       commit hashes.

    Arguments:
        first_hash - The starting hash of the range
        second_hash - The ending hash of the range

    Returns:
        A list of filenames.
    """

    # Display input parameters
    start_time = get_clock()
    logging.debug('Entered function')
    logging.debug('First hash: %s', first_hash)
    logging.debug('Second hash: %s', second_hash)

    file_list = []

    # Get the list of files impacted.  If argv[1] and argv[2] are the same
    # commit, then pass the value only once otherwise the file list is not
    # returned
    if first_hash == second_hash:
        cmd = [
            'git', 'diff-tree', '-r', '--name-only', '--no-commit-id',
            '--diff-filter=ACMRT', first_hash
        ]
    else:
        cmd = [
            'git', 'diff-tree', '-r', '--name-only', '--no-commit-id',
            '--diff-filter=ACMRT', first_hash, second_hash
        ]

    # Fetch the list of files modified by the last commit
    cmd_stdout = execute_cmd(cmd=cmd, cmd_source='get_checkout_files')

    # Convert the stdout stream to a list of files
    file_list = cmd_stdout.decode('utf8').splitlines()

    # Deal with unmodified repositories
    if file_list and file_list[0] == 'clean':
        end_time = get_clock()
        logging.info('No files to process')
        logging.info('Elapsed time: %f', (end_time - start_time))
        exit(0)

    # Only return regular files.
    file_list = [i for i in file_list if os.path.isfile(i)]

    end_time = get_clock()
    logging.debug('Returning file list to process %s', file_list)
    logging.info('Elapsed time: %f', (end_time - start_time))

    # Return from the function
    return file_list
def post_checkout():
    """Main program.

    Arguments:
        argv: command line arguments

    Returns:
        Nothing
    """

    # Display input parameters
    start_time = get_clock()
    logging.info('Entered function')
    logging.debug('sys.argv: %s', sys.argv)

    # If argv[3] is zero (file checkout rather than branch checkout),
    # then exit the hook as there is no need to re-smudge the file.
    # (The commit info was already available)  If the vallue is 1, then
    # this is a branch checkout and commit info was not available at the
    # time the file was checkted out.
    if sys.argv[3] == '0':
        end_time = get_clock()
        logging.debug('File checkout - no work required')
        logging.info('Elapsed time: %f', (end_time - start_time))
        exit(0)

    # Check if git is available.
    check_for_cmd(cmd=['git', '--version'])

    # Get the list of files impacted.
    files = get_checkout_files(first_hash=sys.argv[1], second_hash=sys.argv[2])
    logging.debug('Files to checkout: %s', files)

    # Filter the list of modified files to exclude those modified since
    # the commit
    files = remove_modified_files(files=files)
    logging.debug('Non-modified files: %s', files)

    # Force a checkout of the remaining file list
    files_processed = 0
    if files:
        files.sort()
        for file_name in files:
            logging.info('Checking out file %s', file_name)
            check_out_file(file_name=file_name)
            files_processed += 1
            sys.stderr.write('Smudged file %s\n' % file_name)
            logging.info('Checked out file %s', file_name)

    end_time = get_clock()
    logging.info('Elapsed time: %f', (end_time - start_time))
def remove_modified_files(files):
    """Filter the found files to eliminate any that have changes that have
       not been checked in.

    Arguments:
        files - list of files to checkout

    Returns:
        A list of files to checkout that do not have pending changes.
    """

    # Display input parameters
    start_time = get_clock()
    logging.info('Entered function')
    logging.debug('files: %s', files)

    cmd = ['git', 'status', '-s']

    # Get the list of files that are modified but not checked in
    cmd_stdout = execute_cmd(cmd=cmd, cmd_source='remove_modified_files')

    # Convert the stream output to a list of output lines
    modified_files_list = cmd_stdout.decode('utf8').splitlines()

    # Deal with unmodified repositories
    if not modified_files_list:
        end_time = get_clock()
        logging.info('No modified files found')
        logging.info('Elapsed time: %f', (end_time - start_time))
        return files

    # Pull the file name (second field) of the output line and
    # remove any double quotes
    modified_files_list = [
        l.split(None, 1)[-1].strip('"') for l in modified_files_list
    ]

    logging.debug('Modified files list: %s', modified_files_list)

    # Remove any modified files from the list of files to process
    if modified_files_list:
        files = [f for f in files if f not in modified_files_list]

    end_time = get_clock()
    logging.debug('files: %s', files)
    logging.info('Elapsed time: %f', (end_time - start_time))

    # Return from the function
    return files
예제 #7
0
 def fader_start(self, *args):
     """ Start fading process
     :param args:
     *args (OrderedDict or int x4): Arguments passed to fade_init """
     self.fRun, self.fFinished = True, False
     self.fade_init(*args)
     self._fTimeUpdate = get_clock()
     self.fHandler_start()
예제 #8
0
def process_hooks():
    """Main program.

    Returns:
        Nothing
    """
    # Display the parameters passed on the command line
    start_time = get_clock()
    logging.info('Entered function')
    logging.debug('sys.argv parameter count %d', len(sys.argv))
    logging.debug('sys.argv parameters %s', sys.argv)

    # Verify that the named hook directory is a directory
    list_dir = sys.argv[0] + '.d'
    if not os.path.isdir(list_dir):
        logging.info('The hook directory %s is not a directory', list_dir)
        exit(0)

    # Execute each of the hooks found in the relevant directory
    hook_count = 0
    hook_executed = 0
    for file_name in sorted(os.listdir(list_dir)):
        hook_count += 1
        hook_program = os.path.join(list_dir, file_name)
        if os.path.isfile(hook_program) and os.access(hook_program, os.X_OK):
            # If parameters were supplied, pass them through to the actual
            # hook program
            if len(sys.argv) > 1:
                hook_program = '"%s" %s' \
                               % (hook_program,
                                  ' '.join('"%s"' % param
                                           for param in sys.argv[1:]))
            hook_executed += 1
            logging.info('Executing hook %s', hook_program)
            hook_call = subprocess.call([hook_program], shell=True)
            if hook_call > 0:
                end_time = get_clock()
                logging.info('Exiting - Hook program failed with error %s',
                             hook_call)
                logging.info('Elapsed time: %f', (end_time - start_time))
                exit(hook_call)

    end_time = get_clock()
    logging.debug('Hooks examined: %d', hook_count)
    logging.debug('Hooks executed: %d', hook_executed)
    logging.info('Elapsed time: %f', (end_time - start_time))
def post_rewrite():
    """Main program.

    Arguments:
        argv: command line arguments

    Returns:
        Nothing
    """

    start_time = get_clock()
    logging.info('Entered function')

    # Check if git is available.
    check_for_cmd(cmd=['git', '--version'])

    # Read stdin and write it to stderr
    input_lines = sys.stdin.readlines()
    line_count = 1

    files = []
    for source_line in input_lines:
        line_count += 1
        words = source_line.split()
        # Get the list of modified files
        files = files + get_modified_files(dest_hash=words[1].strip())
    logging.debug('Files: %s', files)

    # Check if git is available.
    check_for_cmd(cmd=['git', '--version'])

    # Force a checkout of the remaining file list
    files_processed = 0
    if files:
        for file_name in sorted(files):
            check_out_file(file_name=file_name)
            files_processed += 1
            sys.stderr.write('Smudged file %s\n' % file_name)
            logging.info('Checked out file %s', file_name)
    logging.debug('Files processed: %s', files_processed)

    end_time = get_clock()
    logging.info('Elapsed time: %f', (end_time - start_time))
예제 #10
0
def get_modified_files():
    """Find files that were modified by the commit.

    Arguments:
        None

    Returns:
        A list of filenames.
    """

    # Display input parameters
    start_time = get_clock()
    logging.debug('Entered function')

    modified_file_list = []
    cmd = [
        'git', 'diff-tree', 'HEAD~1', 'HEAD', '--name-only', '-r',
        '--diff-filter=ACMRT'
    ]

    # Fetch the list of files modified by the last commit
    cmd_stdout = execute_cmd(cmd=cmd, cmd_source='get_modified_files')

    # Convert the stdout stream to a list of files
    modified_file_list = cmd_stdout.decode('utf8').splitlines()
    logging.debug('modified_file_list: %s', cmd)

    # Deal with unmodified repositories
    if modified_file_list and modified_file_list[0] == 'clean':
        end_time = get_clock()
        logging.info('No modified files found')
        logging.info('Elapsed time: %f', (end_time - start_time))
        exit(0)

    # Only return regular files.
    modified_file_list = [i for i in modified_file_list if os.path.isfile(i)]

    end_time = get_clock()
    logging.debug('modified_file_list: %s', cmd)
    logging.info('Elapsed time: %f', (end_time - start_time))

    # Return from the function
    return modified_file_list
예제 #11
0
def check_out_file(file_name):
    """Checkout file that was been modified by the latest merge.

    Arguments:
        file_name -- the file name to be checked out for smudging

    Returns:
        Nothing.
    """

    start_time = get_clock()
    logging.info('Entered function')
    logging.debug('file_name: %s', file_name)

    # Remove the file if it currently exists
    try:
        os.remove(file_name)
    except OSError as err:
        # Ignore a file not found error, it was being removed anyway
        if err.errno != errno.ENOENT:
            end_time = get_clock()
            logging.info(
                "File removal of %s caused on OS error %d! -- Exiting.",
                file_name,
                err.errno,
                exc_info=True
            )
            logging.error(
                "File removal %s caused OS error %d! -- Exiting.",
                file_name,
                err.errno
            )
            logging.info('Elapsed time: %f', (end_time - start_time))
            exit(err.errno)

    cmd = ['git', 'checkout', '-f', '%s' % file_name]

    # Check out the file so that it is smudged
    execute_cmd(cmd=cmd, cmd_source='check_out_files')

    end_time = get_clock()
    logging.info('Elapsed time: %f', (end_time - start_time))
예제 #12
0
def post_commit():
    """Main program.

    Arguments:
        None

    Returns:
        Nothing
    """

    # Display input parameters
    start_time = get_clock()
    logging.info('Entered function')

    # Check if git is available.
    check_for_cmd(cmd=['git', '--version'])

    # Get the list of modified files
    committed_files = get_modified_files()
    logging.debug('committed_files: %s', committed_files)

    # Filter the list of modified files to exclude those modified since
    # the commit
    committed_files = remove_modified_files(files=committed_files)
    logging.debug('committed_files: %s', committed_files)

    # Force a checkout of the remaining file list
    # Process the remaining file list
    files_processed = 0
    if committed_files:
        committed_files.sort()
        for file_name in committed_files:
            check_out_file(file_name=file_name)
            files_processed += 1
            sys.stderr.write('Smudged file %s\n' % file_name)
            logging.info('Checked out file %s', file_name)

    end_time = get_clock()
    logging.debug('files processed: %s', files_processed)
    logging.info('Elapsed time: %f', (end_time - start_time))
예제 #13
0
    def fade_inc(self):
        """ Fading thread (handles increments) """
        while 1:
            if self._fHandler_en is True:
                now = get_clock()
                if (now - self._fTimeUpdate) >= self._fTimeInc:
                    self._fTimeUpdate = now

                    if not self.fFinished:
                        try:
                            next(self)
                        except StopIteration:
                            pass
def check_out_file(file_name):
    """Checkout file that was been modified by the latest branch checkout.

    Arguments:
        file_name -- the file name to be checked out for smudging

    Returns:
        Nothing.
    """

    # Display input parameters
    start_time = get_clock()
    logging.info('Entered function')
    logging.debug('File_name: %s', file_name)

    # Remove the file if it currently exists
    try:
        os.remove(file_name)
        logging.info('Removed file %s', file_name)
    except OSError as err:
        # Ignore a file not found error, it was being removed anyway
        if err.errno != errno.ENOENT:
            end_time = get_clock()
            logging.error('Unable to remove file %s for re-checkout',
                          file_name)
            logging.info('Elapsed time: %f', (end_time - start_time))
            exit(err.errno)
        else:
            logging.info('File %s not found to remove', file_name)

    cmd = ['git', 'checkout', '-f', '%s' % file_name]

    # Check out the file so that it is smudged
    execute_cmd(cmd=cmd, cmd_source='check_out_files')
    logging.info('Checked out file %s', file_name)

    end_time = get_clock()
    logging.info('Elapsed time: %f', (end_time - start_time))
예제 #15
0
def smudge_input():
    """Main program.

    Arguments:
        argv: command line arguments

    Returns:
        Nothing
    """

    # Display the parameters passed on the command line
    if LOGGING_LEVEL and LOGGING_LEVEL <= logging.INFO:
        start_time = get_clock()
        logging.debug('')
        logging.debug('Function: %s' % sys._getframe().f_code.co_name)
        logging.debug('sys.argv parameter count %d' % len(sys.argv))
        logging.debug('sys.argv parameters %s' % sys.argv)

    # Calculate the source file being smudged
    if len(sys.argv) > 1:
        file_name = sys.argv[1]
    else:
        file_name = '<Unknown file>'

    # Log the results of the git log operation
    if LOGGING_LEVEL:
        logging.debug('Display the file name parameter %s' % file_name)

    # Define the fields to be extracted from the commit log
    git_field_name = [
        'hash', 'author_name', 'author_email', 'commit_date', 'short_hash'
    ]
    git_field_log = ['%H', '%an', '%ae', '%ci', '%h']

    # Define the various substitution regular expressions
    author_regex = re.compile(r"\$Author: +[.\w@<> ]+ +\$|\$Author\$",
                              re.IGNORECASE)
    id_regex = re.compile(r"\$Id: +.+ \| [-:\d ]+ \| .+ +\$|\$Id\$",
                          re.IGNORECASE)
    date_regex = re.compile(r"\$Date: +[-:\d ]+ +\$|\$Date\$", re.IGNORECASE)
    source_regex = re.compile(r"\$Source: .+[.].+ \$|\$Source\$",
                              re.IGNORECASE)
    file_regex = re.compile(r"\$File: .+[.].+ \$|\$File\$", re.IGNORECASE)
    revision_regex = re.compile(r"\$Revision: +[-:\d ]+ +\$|\$Revision\$",
                                re.IGNORECASE)
    rev_regex = re.compile(r"\$Rev: +[-:\d ]+ +\$|\$Rev\$", re.IGNORECASE)
    hash_regex = re.compile(r"\$Hash: +\w+ +\$|\$Hash\$", re.IGNORECASE)

    regex_dict = {}

    # Process each of the rows found on stdin
    line_count = 0
    try:
        for line in sys.stdin:
            line_count += 1
            if line.count('$') > 1:
                if len(regex_dict) == 0:
                    regex_dict = build_regex_dict(
                        git_field_log=git_field_log,
                        file_name=file_name,
                        git_field_name=git_field_name)

                line = author_regex.sub(regex_dict['git_author'], line)
                line = id_regex.sub(regex_dict['git_id'], line)
                line = date_regex.sub(regex_dict['git_date'], line)
                line = source_regex.sub(regex_dict['git_source'], line)
                line = file_regex.sub(regex_dict['git_file'], line)
                line = revision_regex.sub(regex_dict['git_revision'], line)
                line = rev_regex.sub(regex_dict['git_rev'], line)
                line = hash_regex.sub(regex_dict['git_hash'], line)
            sys.stdout.write(line)
    except Exception:
        logging.error('Exception smudging file %s' % file_name, exc_info=True)
        sys.stderr.write(
            'Exception smudging file %s - Key words were not replaced\n' %
            file_name)
        exit(2)

    if LOGGING_LEVEL and LOGGING_LEVEL <= logging.INFO:
        end_time = get_clock()
        logging.info('Line count in %s: %s' %
                     (sys._getframe().f_code.co_name, line_count))
        logging.info('Elapsed for %s: %s' %
                     (sys._getframe().f_code.co_name, end_time - start_time))
예제 #16
0
def build_regex_dict(git_field_log, file_name, git_field_name):
    """Function to converts a 1 row list of git log attributes into
    dictionary of regex expressions.

    Arguments:
        git_field_log -- a list of git log fields to capture
        file_name -- The full file name to be examined
        git_field_name -- Name of the attributes fields for the dictionary

    Returns:
        regex_dict -- Array of defined attribute dictionaries
    """

    # Display input parameters
    if LOGGING_LEVEL and LOGGING_LEVEL <= logging.INFO:
        start_time = get_clock()
        logging.debug('')
        logging.debug('Function: %s' % sys._getframe().f_code.co_name)
        logging.debug('git_field_log %s' % git_field_log)
        logging.debug('file_name: %s' % file_name)
        logging.debug('git_field_name: %s' % git_field_name)

    # Format the git log command
    git_log = git_log_attributes(git_field_log=git_field_log,
                                 file_name=file_name,
                                 git_field_name=git_field_name)

    if not git_log:
        logging.error('No git attributes returned: %s' %
                      sys._getframe().f_code.co_name)
        exit(4)

    if len(git_log) > 1:
        logging.error('More than one row of git attributes returned: %s' %
                      sys._getframe().f_code.co_name)
        exit(3)

    regex_dict = {}
    if git_log:
        # Calculate the replacement strings based on the git log results
        # Deal with values in author name that have a Windows domain name
        if '\\' in git_log[0]['author_name']:
            git_log[0]['author_name'] = git_log[0]['author_name'].split(
                '\\')[-1]

        regex_dict['git_hash'] = '$Hash:     %s $' % str(git_log[0]['hash'])
        regex_dict['git_short_hash'] = '$Short Hash:     %s $' % str(
            git_log[0]['short_hash'])
        regex_dict['git_author'] = '$Author:   %s <%s> $' % (str(
            git_log[0]['author_name']), str(git_log[0]['author_email']))
        regex_dict['git_date'] = '$Date:     %s $' % str(
            git_log[0]['commit_date'])
        regex_dict['git_rev'] = '$Rev:      %s $' % str(
            git_log[0]['commit_date'])
        regex_dict['git_revision'] = '$Revision: %s $' % str(
            git_log[0]['commit_date'])
        regex_dict['git_file'] = '$File:     %s $' % str(file_name)
        regex_dict['git_source'] = '$Source:   %s $' % str(file_name)
        regex_dict['git_id'] = '$Id:       %s | %s | %s $' % (
            str(file_name), str(
                git_log[0]['commit_date']), str(git_log[0]['author_name']))

    else:
        # Build a empty keyword list if no source data was found
        # Note: the unusual means of building the list is to keep
        #       the code from being modified while using keywords!
        regex_dict['git_hash'] = '$%s$' % 'Hash'
        regex_dict['git_author'] = '$%s$' % 'Author'
        regex_dict['git_date'] = '$%s$' % 'Date'
        regex_dict['git_rev'] = '$%s$' % 'Rev'
        regex_dict['git_revision'] = '$%s$' % 'Revision'
        regex_dict['git_file'] = '$%s$' % 'File'
        regex_dict['git_source'] = '$%s$' % 'Source'
        regex_dict['git_id'] = '$%s$' % 'Id'

    # Log the results of the git log operation
    if LOGGING_LEVEL and LOGGING_LEVEL <= logging.INFO:
        end_time = get_clock()
        logging.info('Elapsed for %s: %s' %
                     (sys._getframe().f_code.co_name, end_time - start_time))
        logging.debug('regex_dict: %s' % regex_dict)

    return regex_dict
예제 #17
0
            'Exception smudging file %s - Key words were not replaced\n' %
            file_name)
        exit(2)

    if LOGGING_LEVEL and LOGGING_LEVEL <= logging.INFO:
        end_time = get_clock()
        logging.info('Line count in %s: %s' %
                     (sys._getframe().f_code.co_name, line_count))
        logging.info('Elapsed for %s: %s' %
                     (sys._getframe().f_code.co_name, end_time - start_time))


# Execute the main function
if __name__ == '__main__':
    # Initialize logging
    if LOGGING_LEVEL:
        if LOGGING_LEVEL <= logging.INFO:
            start_time = get_clock()
        logging.basicConfig(level=LOGGING_LEVEL,
                            format='%(levelname)s: %(message)s',
                            filename='.git-hook.smudge.log')
        logging.debug('')
        logging.debug('')
        logging.debug('Executing: %s' % sys.argv[0])
    smudge_input()

    if LOGGING_LEVEL and LOGGING_LEVEL <= logging.INFO:
        end_time = get_clock()
        logging.info('Elapsed for %s: %s' %
                     (sys.argv[0], end_time - start_time))
예제 #18
0
 def fader_resume(self):
     """ Resume fading process """
     self.fRun = True
     self._fTimeUpdate = get_clock()
     self.fHandler_start()
예제 #19
0
def clean_input():
    """Main program.

    Arguments:
        argv: command line arguments

    Returns:
        Nothing
    """

    # Display the parameters passed on the command line
    if LOGGING_LEVEL and LOGGING_LEVEL <= logging.INFO:
        start_time = get_clock()
        logging.debug('')
        logging.debug('Function: %s' % sys._getframe().f_code.co_name)
        logging.debug('sys.argv parameter count %d' % len(sys.argv))
        logging.debug('sys.argv parameters %s' % sys.argv)

    # Calculate the source file being cleaned
    if len(sys.argv) > 1:
        file_name = sys.argv[1]
    else:
        file_name = '<Unknown file>'

    # Define the various substitution regular expressions
    author_regex = re.compile(r"\$Author:.*\$", re.IGNORECASE)
    id_regex = re.compile(r"\$Id: +.+ \| [-:\d ]+ \| .+ +\$|\$Id\$",
                          re.IGNORECASE)
    date_regex = re.compile(r"\$Date: +[-:\d ]+ +\$|\$Date\$", re.IGNORECASE)
    source_regex = re.compile(r"\$Source: .+[.].+ \$|\$Source\$",
                              re.IGNORECASE)
    file_regex = re.compile(r"\$File: .+[.].+ \$|\$File\$", re.IGNORECASE)
    revision_regex = re.compile(r"\$Revision: +[-:\d+ ]+ +\$|\$Revision\$",
                                re.IGNORECASE)
    rev_regex = re.compile(r"\$Rev: +[-:\d+ ]+ +\$|\$Rev\$", re.IGNORECASE)
    hash_regex = re.compile(r"\$Hash: +\w+ +\$|\$Hash\$", re.IGNORECASE)

    # Calculate empty strings based on the keyword
    git_hash = '$%s$' % 'Hash'
    git_author = '$%s$' % 'Author'
    git_date = '$%s$' % 'Date'
    git_rev = '$%s$' % 'Rev'
    git_revision = '$%s$' % 'Revision'
    git_file = '$%s$' % 'File'
    git_source = '$%s$' % 'Source'
    git_id = '$%s$' % 'Id'

    # Process each of the rows found on stdin
    line_count = 0
    try:
        for line in sys.stdin:
            line_count += 1
            if line.count('$') > 1:
                line = author_regex.sub(git_author, line)
                line = id_regex.sub(git_id, line)
                line = date_regex.sub(git_date, line)
                line = source_regex.sub(git_source, line)
                line = file_regex.sub(git_file, line)
                line = revision_regex.sub(git_revision, line)
                line = rev_regex.sub(git_rev, line)
                line = hash_regex.sub(git_hash, line)
            sys.stdout.write(line)
    except Exception:
        logging.error('Exception cleaning file %s' % file_name, exc_info=True)
        sys.stderr.write(
            'Exception smudging file %s - Key words were not replaced\n' %
            file_name)
        exit(2)

    if LOGGING_LEVEL and LOGGING_LEVEL <= logging.INFO:
        end_time = get_clock()
        logging.info('Line count in %s: %s' %
                     (sys._getframe().f_code.co_name, line_count))
        logging.info('Elapsed for %s: %s' %
                     (sys._getframe().f_code.co_name, end_time - start_time))
예제 #20
0
def git_log_attributes(git_field_log, file_name, git_field_name):
    """Function to dump the git log associated with the provided
    file name.

    Arguments:
        git_field_log -- a list of git log fields to capture
        file_name -- The full file name to be examined
        git_field_name -- Name of the attributes fields for the dictionary

    Returns:
        git_log -- List of defined attribute dictionaries
    """

    # Display input parameters
    if LOGGING_LEVEL and LOGGING_LEVEL <= logging.INFO:
        start_time = get_clock()
        logging.debug('')
        logging.debug('Function: %s' % sys._getframe().f_code.co_name)
        logging.debug('git_field_log %s' % git_field_log)
        logging.debug('file_name: %s' % file_name)
        logging.debug('git_field_name: %s' % git_field_name)

    # Format the git log command
    git_field_format = '%x1f'.join(git_field_log) + '%x1e'
    cmd = [
        'git', 'log', '--date=iso8601', '--max-count=1',
        '--format=%s' % git_field_format, '--',
        str(file_name)
    ]

    # Process the git log command
    try:
        cmd_handle = subprocess.Popen(cmd,
                                      stdout=subprocess.PIPE,
                                      stderr=subprocess.PIPE)
        (cmd_stdout, cmd_stderr) = cmd_handle.communicate()
        if cmd_stderr:
            for line in cmd_stderr.strip().decode("utf-8").splitlines():
                sys.stderr.write("%s\n" % line)
    # If the command fails, notify the user and exit immediately
    except subprocess.CalledProcessError as err:
        sys.stderr.write(
            "{0} - Program {1} called by {2} not found! -- Exiting.".format(
                str(err), str(cmd[0]), str(' '.join(cmd))))
        # shutdown_message(return_code=err.returncode)
        exit(err.returncode)
    except OSError as err:
        sys.stderr.write(
            "{0} - Program {1} called by {2} not found! -- Exiting.".format(
                str(err), str(cmd[0]), str(' '.join(cmd))))
        # shutdown_message(return_code=err.errno)
        exit(err.errno)

    # If an error occurred, display the command output and exit
    # with the returned exit code
    if cmd_handle.returncode != 0:
        sys.stderr.write("Exiting -- git log return code: %s\n" %
                         str(cmd_handle.returncode))
        sys.stderr.write("Output text: %s\n" %
                         cmd_stdout.strip().decode("utf-8"))
        # shutdown_message(return_code=cmd_handle.returncode)
        exit(cmd_handle.returncode)

    # Calculate replacement strings based on the git log results
    if cmd_stdout:
        # Convert returned values to a list of dictionaries
        # git_log = cmd_stdout.strip().decode("utf-8")
        # git_log = git_log.strip().split("\x1e")
        git_output = cmd_stdout.strip().decode("utf-8")
        git_log = git_output.strip().split("\x1e")
        git_log = [row.strip().split("\x1f") for row in git_log]
        git_log = [dict(zip(git_field_name, row)) for row in git_log]
    else:
        git_log = []

    # Log the results of the git log operation
    if LOGGING_LEVEL and LOGGING_LEVEL <= logging.INFO:
        end_time = get_clock()
        logging.info('Elapsed for %s: %s' %
                     (sys._getframe().f_code.co_name, end_time - start_time))
        logging.debug('git_log: %s' % git_log)

    # Return from the function
    return git_log
예제 #21
0
# Write a small program to display information on the
# four clocks whose functions we have just looked at:
# i.e. time(), perf_counter, monotonic() and process_time().
#
# Use the documentation for the get_clock_info() function
# to work out how to call it for each of the clocks.
import time
from time import get_clock_info as get_clock

print("Time: {:.10}".format(get_clock('time').resolution))
print("Perf_counter: {:.20}".format(get_clock('perf_counter').resolution))
print("Monotonic: {:.10}".format(get_clock('monotonic').resolution))
print("Process_time: {:.20}".format(get_clock('process_time').resolution))
예제 #22
0
    committed_files = remove_modified_files(files=committed_files)
    logging.debug('committed_files: %s', committed_files)

    # Force a checkout of the remaining file list
    # Process the remaining file list
    files_processed = 0
    if committed_files:
        committed_files.sort()
        for file_name in committed_files:
            check_out_file(file_name=file_name)
            files_processed += 1
            sys.stderr.write('Smudged file %s\n' % file_name)
            logging.info('Checked out file %s', file_name)

    end_time = get_clock()
    logging.debug('files processed: %s', files_processed)
    logging.info('Elapsed time: %f', (end_time - start_time))


# Execute the main function
if __name__ == '__main__':
    configure_logging()

    START_TIME = get_clock()
    logging.debug('Entered module')

    post_commit()

    END_TIME = get_clock()
    logging.info('Elapsed time: %f', (END_TIME - START_TIME))