Skip to content

Commit

Permalink
Finished switching over to git module, subprocess calls appear to be …
Browse files Browse the repository at this point in the history
…more robust.
  • Loading branch information
cmdln committed Apr 7, 2009
1 parent 51de747 commit 4e71c42
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 36 deletions.
18 changes: 7 additions & 11 deletions flashbake/__init__.py
Expand Up @@ -213,28 +213,24 @@ def warnproblems(self):
for filename in self.not_exists:
logging.info('%s does not exist.' % filename)

def addorphans(self, control_config):
def addorphans(self, git_obj, control_config):
if len(self.to_add) == 0:
return

message_file = context.buildmessagefile(control_config)

add_template = 'git add "%s"'
git_commit = 'git commit -F %(msg_filename)s %(filenames)s'
file_template = ' "%s"'
to_commit = ''
to_commit = list()
for orphan in self.to_add:
logging.debug('Adding %s.' % orphan)
add_output = commands.getoutput(add_template % orphan)
to_commit += file_template % orphan
add_output = git_obj.add(orphan)
logging.debug('Add output, %s' % orphan)
to_commit.append(orphan)

logging.info('Adding new files, %s.' % to_commit)
# consolidate the commit to be friendly to how git normally works
git_commit = git_commit % {'msg_filename' : message_file, 'filenames' : to_commit}
logging.debug(git_commit)
if not control_config.dryrun:
commit_output = commands.getoutput(git_commit)
logging.debug(commit_output)
commit_output = git_obj.commit(message_file, to_commit)
logging.debug('Commit output, %s' % commit_output)

os.remove(message_file)

Expand Down
28 changes: 10 additions & 18 deletions flashbake/commit.py
Expand Up @@ -56,13 +56,14 @@ def commit(control_config, hot_files, quiet_mins, dryrun):
# to correctly refer to the project files by relative paths
os.chdir(hot_files.project_dir)

git_obj = git.Git(control_config.git_path)
git_obj = git.Git(hot_files.project_dir, control_config.git_path)

control_config.dryrun = dryrun

# get the git status for the project
# the wrapper object ensures git is on the path
git_status = git_obj.status()

# get the git status for the project
if git_status.startswith('fatal'):
logging.error('Fatal error from git.')
if 'fatal: Not a git repository' == git_status:
Expand All @@ -71,20 +72,14 @@ def commit(control_config, hot_files, quiet_mins, dryrun):
else:
logging.error(git_status)
sys.exit(1)
# especially under cron, git may not be on the path even if installed
if git_status.find('command not found') > 0:
logging.error('Could not find git command.')
sys.exit(1)

# in particular find the existing entries that need a commit
pending_re = re.compile('#\s*(renamed|copied|modified|new file):.*')

now = datetime.datetime.today()
quiet_period = datetime.timedelta(minutes=quiet_mins)

git_commit = 'git commit -F %(msg_filename)s %(filenames)s'
file_template = ' "%s"'
to_commit = ''
to_commit = list()
# first look in the files git already knows about
logging.debug("Examining git status.")
for line in git_status.splitlines():
Expand All @@ -107,7 +102,7 @@ def commit(control_config, hot_files, quiet_mins, dryrun):

# add the file to the list to include in the commit
if pending_mod < now:
to_commit += file_template % pending_file
to_commit.append(pending_file)
logging.debug('Flagging file, %s, for commit.' % pending_file)
else:
logging.debug('Change for file, %s, is too recent.' % pending_file)
Expand All @@ -117,7 +112,6 @@ def commit(control_config, hot_files, quiet_mins, dryrun):
hot_files.warnproblems()

# figure out what the status of the remaining files is
git_status = 'git status "%s"'
for control_file in hot_files.control_files:
# this shouldn't happen since HotFiles.addfile uses glob.iglob to expand
# the original file lines which does so based on what is in project_dir
Expand All @@ -126,7 +120,7 @@ def commit(control_config, hot_files, quiet_mins, dryrun):
hot_files.putabsent(control_file)
continue

status_output = commands.getoutput(git_status % control_file)
status_output = git_obj.status(control_file)

if status_output.startswith('error'):
if status_output.find('did not match') > 0:
Expand All @@ -147,16 +141,14 @@ def commit(control_config, hot_files, quiet_mins, dryrun):
logging.error('%s is in the status message but failed other tests.' % control_file)
logging.error('Try \'git status "%s"\' for more info.' % control_file)

hot_files.addorphans(control_config)
hot_files.addorphans(git_obj, control_config)

if len(to_commit.strip()) > 0:
if len(to_commit) > 0:
logging.info('Committing changes to known files, %s.' % to_commit)
message_file = context.buildmessagefile(control_config)
# consolidate the commit to be friendly to how git normally works
git_commit = git_commit % {'msg_filename' : message_file, 'filenames' : to_commit}
logging.debug(git_commit)
if not dryrun:
commit_output = commands.getoutput(git_commit)
# consolidate the commit to be friendly to how git normally works
commit_output = git_obj.commit(message_file, to_commit)
logging.debug(commit_output)
os.remove(message_file)
logging.info('Commit for known files complete.')
Expand Down
34 changes: 27 additions & 7 deletions flashbake/git.py
Expand Up @@ -16,7 +16,7 @@ def __str__(self):
return repr(self.value)

class Git():
def __init__(self, git_path=None):
def __init__(self, cwd, git_path=None):
# look for git in the environment's PATH var
path_env = os.getenv('PATH')
if path_env.find(';') > 0:
Expand Down Expand Up @@ -47,19 +47,39 @@ def __init__(self, git_path=None):
# set up an environment mapping suitable for use with the subprocess
# module
self.__init_env(path_sep, git_path)
self.__cwd = cwd

def status(self):
def status(self, filename=None):
""" Get the git status for the specified files, or the entire current
directory. """
return self.__run('status')
if filename != None:
files = list()
files.append(filename)
return self.__run('status', files=files)
else:
return self.__run('status')

def add(self, file):
""" Add an unknown but existing file. """
files = [ file ]
return self.__run('add', files=files)

def commit(self, messagefile, files):
""" Commit a list of files, the files should be strings and quoted. """
options = ['-F', messagefile]
return self.__run('commit', options, files)

def __run(self, cmd, files=None):
def __run(self, cmd, options=None, files=None):
cmds = list()
cmds.append('git')
cmds.append(cmd)
if options != None:
cmds += options
if files != None:
cmds += files
return subprocess.Popen(cmds, stdout=subprocess.PIPE, env=self.env).communicate()[0]
proc = subprocess.Popen(cmds, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, cwd=self.__cwd, env=self.env)
return proc.communicate()[0]

def __init_env(self, path_sep, git_path):
self.env = dict()
Expand All @@ -72,9 +92,9 @@ def __init_env(self, path_sep, git_path):
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG,
format='%(message)s')
git = Git('/opt/local/bin')
git = Git('../foo', '/opt/local/bin')
try:
git = Git()
git = Git('../foo')
except VCError, e:
logging.info(e)
os.chdir('../foo')
Expand Down

0 comments on commit 4e71c42

Please sign in to comment.