def check_database(target): """This function checks whether the database is in a consisitent state, this is to say whether a transaction was not brutally aborted and left the working directory with changes not committed. This is meant to be used by scripts, like 'icms-start.py' """ cwd = '%s/database' % target # Check modifications to the working tree not yet in the index. command = ['git', 'ls-files', '-m', '-d', '-o'] data1 = get_pipe(command, cwd=cwd) # Check changes in the index not yet committed. command = ['git', 'diff-index', '--cached', '--name-only', 'HEAD'] data2 = get_pipe(command, cwd=cwd) # Everything looks fine if len(data1) == 0 and len(data2) == 0: return True # Something went wrong print 'The database is not in a consistent state. Fix it manually with' print 'the help of Git:' print print ' $ cd %s/database' % target print ' $ git clean -fxd' print ' $ git checkout -f' print return False
def forget(parser, target, options): # Find out the commit to start from commits = get_commits(target) if len(commits) == 0: print 'There is nothing to forget.' return if options.commits is not None: if len(commits) > options.commits: since = commits[options.commits][0] else: print 'There is nothing to forget' return else: if commits[len(commits) - 1][1] <= options.days: print 'There is nothing to forget.' return for commit in commits: since, delta = commit if delta > options.days: break # Check the server is not running pid = get_pid('%s/pid' % target) if pid is not None: print 'The server is running. Stop it before running this command.' return # Export to new database print '* Make new branch with shorter history (may take a while)' cwd = '%s/database' % target command = ( 'git fast-export --no-data --progress=1000 %s.. | ' 'sed "s|refs/heads/master|refs/heads/new|" | ' 'git fast-import --quiet') # FIXME This does not work if the error comes from git-fast-export, # because execution continues and sed returns 0. See the hack just below. returncode = call(command % since, shell=True, cwd=cwd) if returncode: exit() # Verify the step before was fine try: get_pipe(['git', 'log', '-n', '0', 'new'], cwd=cwd) except EnvironmentError: print_exc() exit() # Backup old branch and deploy new one print '* Deploy new branch and backup old branch' now = datetime.now().strftime('%Y%m%d%H%M') command = ['git', 'branch', '-m', 'master', now] get_pipe(command, cwd=cwd) command = ['git', 'branch', '-m', 'new', 'master'] get_pipe(command, cwd=cwd) command = ['git', 'checkout', 'master'] get_pipe(command, cwd=cwd) # Ok print 'Done. Backup branch is %s' % now
def forget(parser, target, options): # Find out the commit to start from commits = get_commits(target) if len(commits) == 0: print 'There is nothing to forget.' return if options.commits is not None: if len(commits) > options.commits: since = commits[options.commits][0] else: print 'There is nothing to forget' return else: if commits[len(commits) - 1][1] <= options.days: print 'There is nothing to forget.' return for commit in commits: since, delta = commit if delta > options.days: break # Check the server is not running pid = get_pid('%s/pid' % target) if pid is not None: print 'The server is running. Stop it before running this command.' return # Export to new database print '* Make new branch with shorter history (may take a while)' cwd = '%s/database' % target command = ('git fast-export --no-data --progress=1000 %s.. | ' 'sed "s|refs/heads/master|refs/heads/new|" | ' 'git fast-import --quiet') # FIXME This does not work if the error comes from git-fast-export, # because execution continues and sed returns 0. See the hack just below. returncode = call(command % since, shell=True, cwd=cwd) if returncode: exit() # Verify the step before was fine try: get_pipe(['git', 'log', '-n', '0', 'new'], cwd=cwd) except EnvironmentError: print_exc() exit() # Backup old branch and deploy new one print '* Deploy new branch and backup old branch' now = datetime.now().strftime('%Y%m%d%H%M') command = ['git', 'branch', '-m', 'master', now] get_pipe(command, cwd=cwd) command = ['git', 'branch', '-m', 'new', 'master'] get_pipe(command, cwd=cwd) command = ['git', 'checkout', 'master'] get_pipe(command, cwd=cwd) # Ok print 'Done. Backup branch is %s' % now
def get_compile_flags(command): include_dirs = [] extra_compile_args = [] library_dirs = [] libraries = [] if isinstance(command, str): command = command.split() data = get_pipe(command) for line in data.splitlines(): for token in line.split(): flag, value = token[:2], token[2:] if flag == '-I': include_dirs.append(value) elif flag == '-f': extra_compile_args.append(token) elif flag == '-L': library_dirs.append(value) elif flag == '-l': libraries.append(value) return {'include_dirs': include_dirs, 'extra_compile_args': extra_compile_args, 'library_dirs': library_dirs, 'libraries': libraries}
def is_available(): """Returns True if we are in a git working directory, False otherwise. """ try: data = get_pipe(['git', 'branch']) except EnvironmentError: return False return bool(data)
def get_branch_name(cwd=None): """Returns the name of the current branch. """ data = get_pipe(['git', 'branch'], cwd=cwd) for line in data.splitlines(): if line.startswith('*'): return line[2:] return None
def get_version_of_packages(self, context): versions = BaseRoot.get_version_of_packages(self, context) # check nodejs version try: nodejs = get_pipe(['node', '--version']) versions['nodejs'] = nodejs except OSError: versions['nodejs'] = None return versions
def describe(match=None, cwd=None): # The command command = ['git', 'describe', '--tags', '--long'] if match: command.extend(['--match', match]) # Call try: data = get_pipe(command, cwd=cwd) except EnvironmentError: return None tag, n, commit = data.rsplit('-', 2) return tag, int(n), commit
def run(self, command, cwd=None): # Change dir if cwd: self.chdir(cwd) # Format command if type(command) is str: command = expanduser(command) command_str = command command = command.split() else: command_str = ' '.join(command) # Print print '%s $ %s' % (self.cwd, command_str) # Call return get_pipe(command, cwd=self.cwd)
def get_metadata(reference='HEAD', cwd=None): """Returns some metadata about the given commit reference. For now only the commit id and the timestamp are returned. """ data = get_pipe(['git', 'cat-file', 'commit', reference], cwd=cwd) lines = data.splitlines() # Default values metadata = { 'tree': None, 'parent': None, 'author': (None, None), 'committer': (None, None), 'message': [] } # Parse the data (with a simple automaton) state = 0 for line in lines: if state == 0: # Heading line = line.strip() if not line: state = 1 continue key, value = line.split(' ', 1) if key == 'tree': metadata['tree'] = value elif key == 'parent': metadata['parent'] = value elif key == 'author': name, ts, tz = value.rsplit(' ', 2) ts = datetime.fromtimestamp(int(ts)) metadata['author'] = (name, ts) elif key == 'committer': name, ts, tz = value.rsplit(' ', 2) ts = datetime.fromtimestamp(int(ts)) metadata['committer'] = (name, ts) else: # Message metadata['message'].append(line) # Post-process message metadata['message'] = '\n'.join(metadata['message']) # Ok return metadata
def get_metadata(reference='HEAD', cwd=None): """Returns some metadata about the given commit reference. For now only the commit id and the timestamp are returned. """ data = get_pipe(['git', 'cat-file', 'commit', reference], cwd=cwd) lines = data.splitlines() # Default values metadata = { 'tree': None, 'parent': None, 'author': (None, None), 'committer': (None, None), 'message': []} # Parse the data (with a simple automaton) state = 0 for line in lines: if state == 0: # Heading line = line.strip() if not line: state = 1 continue key, value = line.split(' ', 1) if key == 'tree': metadata['tree'] = value elif key == 'parent': metadata['parent'] = value elif key == 'author': name, ts, tz = value.rsplit(' ', 2) ts = datetime.fromtimestamp(int(ts)) metadata['author'] = (name, ts) elif key == 'committer': name, ts, tz = value.rsplit(' ', 2) ts = datetime.fromtimestamp(int(ts)) metadata['committer'] = (name, ts) else: # Message metadata['message'].append(line) # Post-process message metadata['message'] = '\n'.join(metadata['message']) # Ok return metadata
def get_commits(target): """Returns a list with one tuple for every commit: [(<commit hash>, <days since today>), ...] """ cwd = '%s/database' % target command = ['git', 'log', '--pretty=format:%H %at'] data = get_pipe(command, cwd=cwd) today = date.today() commits = [] for line in data.splitlines(): commit, seconds = line.split() delta = today - date.fromtimestamp(float(seconds)) commits.append((commit, delta.days)) return commits
def get_revisions(files=freeze([]), cwd=None): command = ['git', 'rev-list', 'HEAD', '--'] + files data = get_pipe(command, cwd=cwd) return [ x.rstrip() for x in data.splitlines() ]
parser = OptionParser('%prog [OPTIONS] <POT file> <PO file>', version=version, description=description) parser.add_option('-o', '--output', help="The output will be written to the given file," " instead of printed to the standard output.") options, args = parser.parse_args() if len(args) != 2: parser.error('incorrect number of arguments') if options.output is None: output = sys.stdout else: output = open(options.output, 'w') try: pot, po = args if exists(po): # a .po file already exist, merge it with locale.pot command = ['msgmerge', '-s', po, pot] data = get_pipe(command) output.write(data) else: # po doesn't exist, just copy locale.pot output.write(open(pot).read()) finally: if options.output is not None: output.close()
def get_filenames(): """Returns the list of filenames tracked by git. """ data = get_pipe(['git', 'ls-files']) return [ x.strip() for x in data.splitlines() ]
if key == 'N': name = value elif key == 'E': emails.append(value) if len(emails) == 1: credits_names[value] = name else: credits_mails[value] = emails[0] else: emails = [] # Go! for filename in args: # Call "git blame" command = ['git', 'blame', '-p', filename] output = get_pipe(command) header = True authors = {} for line in output.splitlines(): if line.startswith('author '): name = line[7:] elif line.startswith('author-mail '): email = line[13:-1] if email in credits_mails: email = credits_mails[email] if email in credits_names: name = credits_names[email] elif line.startswith('author-time '): year = datetime.fromtimestamp(int(line[12:])).year elif line.startswith('\t'):
if __name__ == '__main__': # The command line parser usage = '%prog [html|pdf|release]' version = 'itools %s' % itools.__version__ description = ('Make the documentation, default mode is html.') parser = OptionParser(usage, version=version, description=description) options, args = parser.parse_args() if len(args) > 1: parser.error('incorrect number of arguments') mode = args[0] if args else 'html' # Find out the package name & version (for the release mode) try: pkgname = get_pipe(['python', 'setup.py', '--fullname']).rstrip() except EnvironmentError: pkgname = 'noname-noversion' # Go chdir('docs') if mode == 'html': make_html() elif mode == 'pdf': make_pdf() elif mode == 'release': make_release() else: parser.error('unkwnon "%s" mode' % mode)
def get_filenames(): """Returns the list of filenames tracked by git. """ data = get_pipe(['git', 'ls-files']) return [x.strip() for x in data.splitlines()]
if key == 'N': name = value elif key == 'E': emails.append(value) if len(emails) == 1: credits_names[value] = name else: credits_mails[value] = emails[0] else: emails = [] # Go! for filename in args: # Call "git blame" command = ['git', 'blame', '-p', filename] output = get_pipe(command) state = 0 header = True authors = {} cache = {} for line in output.splitlines(): if state == 0: commit = line[:40] state = 1 continue if line.startswith('author '): name = line[7:] elif line.startswith('author-mail '): email = line[13:-1]
def get_revisions(files=freeze([]), cwd=None): command = ['git', 'rev-list', 'HEAD', '--'] + files data = get_pipe(command, cwd=cwd) return [x.rstrip() for x in data.splitlines()]